add lua configs

This commit is contained in:
LordMZTE 2021-11-25 19:34:10 +01:00
parent 4b2c701574
commit 2741536e69
5 changed files with 101 additions and 18 deletions

View file

@ -19,6 +19,7 @@ indicatif = "0.16.2"
json5 = "0.3.0"
log = "0.4.14"
miette = { version = "3.1.0", features = ["fancy"] }
mlua = { version = "0.6.6", features = ["luajit", "serialize"] }
percent-encoding = "2.1.0"
reqwest = { version = "0.11.4", features = ["stream"] }
serde = { version = "1.0.129", features = ["derive"] }

View file

@ -0,0 +1,45 @@
manifest = {
id = "{{ id }}",
type = "{{ type }}",
meta = {
name = "{{ meta.name }}",
contributors = {
{% for cont in meta.contributors %} {
name = "{{ cont.name }}",
roles = {% for role in cont.roles %}{
"{{ role }}",
{% endfor %}},
{% endfor %}},
{% if meta.description %}description = "{{ meta.description }}",
{% endif %}{% if meta.icon_url %}icon_url = "{{ meta.icon_url }}",
{% endif %}{% if meta.website_url %}website_url = "{{ meta.website_url }}",
{% endif %}},
},
versions = {
{% for ver in versions %}{
version = "{{ ver.version }}",
mcversion = {
{% for mcver in ver.mcversion %} "{{ mcver }}",
{% endfor %}},
files = {
{% for file in ver.files %} {
{% if file.artifact %}installer = "{{ file.installer }}",
artifact = "{{ file.artifact }}",
repository = "{{ file.repository }}",{% else %}{% if file.id %}id = "{{ file.id }}",{% endif %}
installer = "{{ file.installer }}",
link = "{{ file.link }}",{% if file.options %}
options = {
{% for opt in file.options %} "{{ opt }}",
{% endfor %}},{% endif %}{% endif %}
},{% endfor %}
},
},{% endfor %}
},
repositories = {
{% for repo in repositories %} {
id = "{{ repo.id }}",
type = "{{ repo.type }}",
url = "{{ repo.url }}",
},{% endfor %}
},
}

View file

@ -24,7 +24,7 @@ use url::Url;
use std::path::PathBuf;
use crate::config::Config;
use crate::{config::Config, util::make_lua_manifest};
pub async fn run(config: Config, infile: PathBuf) -> miette::Result<()> {
if config.locations.src.join("modpack.json").exists() ||
@ -118,13 +118,15 @@ pub async fn run(config: Config, infile: PathBuf) -> miette::Result<()> {
},
};
let json = serde_json::to_vec_pretty(&manif).into_diagnostic()?;
tokio::fs::create_dir_all(&config.locations.src)
.await
.into_diagnostic()?;
tokio::fs::write(config.locations.src.join("modpack.json5"), json)
.await
.into_diagnostic()?;
tokio::fs::write(
config.locations.src.join("modpack.lua"),
make_lua_manifest(&manif)?,
)
.await
.into_diagnostic()?;
println!("{}", "Imported manifest!".green());

View file

@ -1,4 +1,4 @@
use crate::{config::Locations, forge};
use crate::{config::Locations, forge, util::make_lua_manifest};
use addonscript::{
manifest::{
installer::Installer,
@ -164,7 +164,7 @@ pub async fn run(
.await
.into_diagnostic()?;
let data = serde_json::to_vec_pretty(&Manifest {
let data = make_lua_manifest(&Manifest {
id: modpack_name.to_kebab_case(),
manifest_type: ManifestType::Modpack,
versions: vec![Version {
@ -193,10 +193,9 @@ pub async fn run(
icon_url: None,
website_url: None,
},
})
.into_diagnostic()?;
})?;
tokio::fs::write(path.join("modpack.json5"), data)
tokio::fs::write(path.join("modpack.lua"), data)
.await
.into_diagnostic()?;

View file

@ -3,6 +3,7 @@ use crossterm::style::{Attribute, Attributes, Color, ContentStyle, Stylize};
use indicatif::ProgressStyle;
use log::info;
use miette::{Diagnostic, IntoDiagnostic, WrapErr};
use mlua::{Lua, LuaSerdeExt};
use percent_encoding::percent_decode;
use std::{
collections::HashMap,
@ -10,6 +11,7 @@ use std::{
path::{Path, PathBuf},
string::FromUtf8Error,
};
use tera::{Context, Tera};
use thiserror::Error;
use url::Url;
use walkdir::WalkDir;
@ -35,10 +37,14 @@ pub async fn parse_config_and_manifest() -> miette::Result<(Config, Manifest)> {
let config = parse_config().await?;
let src = Path::new(&config.locations.src);
let mut is_lua = false;
let path = if src.join("modpack.json5").exists() {
src.join("modpack.json5")
} else {
} else if src.join("modpack.json").exists() {
src.join("modpack.json")
} else {
is_lua = true;
src.join("modpack.lua")
};
info!("reading manifest");
@ -46,13 +52,31 @@ pub async fn parse_config_and_manifest() -> miette::Result<(Config, Manifest)> {
let data = tokio::fs::read(path)
.await
.into_diagnostic()
.context("Failed to read manifest")?;
let data = std::str::from_utf8(&data)
.into_diagnostic()
.wrap_err("Manifest is invalid UTF-8")?;
let manifest = json5::from_str::<Manifest>(data)
.into_diagnostic()
.wrap_err("Failed to parse manifest")?;
.wrap_err("Failed to read manifest")?;
let manifest = if is_lua {
let lua = Lua::new();
lua.load(&data)
.exec()
.into_diagnostic()
.wrap_err("Failed to execute lua manifest")?;
let lua_manifest = lua
.globals()
.get("manifest")
.into_diagnostic()
.wrap_err("Failed to get manifest value")?;
lua.from_value(lua_manifest).into_diagnostic().wrap_err(
"Failed to deserialize lua manifest. Did you set the global `manifest` to the correct \
data?",
)?
} else {
let data = std::str::from_utf8(&data)
.into_diagnostic()
.wrap_err("Manifest is invalid UTF-8")?;
json5::from_str::<Manifest>(data)
.into_diagnostic()
.wrap_err("Failed to parse manifest")?
};
Ok((config, manifest))
}
@ -222,6 +246,18 @@ pub async fn copy_dir(from: PathBuf, to: PathBuf) -> io::Result<()> {
Ok(())
}
pub fn make_lua_manifest(manif: &Manifest) -> miette::Result<String> {
Tera::one_off(
include_str!("../assets/modpack.lua.tera"),
&Context::from_serialize(manif)
.into_diagnostic()
.wrap_err("Failed to create context for tera")?,
false,
)
.into_diagnostic()
.wrap_err("Failed to create lua manifest.")
}
#[cfg(test)]
mod tests {
use addonscript::manifest::RepositoryType;