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" json5 = "0.3.0"
log = "0.4.14" log = "0.4.14"
miette = { version = "3.1.0", features = ["fancy"] } miette = { version = "3.1.0", features = ["fancy"] }
mlua = { version = "0.6.6", features = ["luajit", "serialize"] }
percent-encoding = "2.1.0" percent-encoding = "2.1.0"
reqwest = { version = "0.11.4", features = ["stream"] } reqwest = { version = "0.11.4", features = ["stream"] }
serde = { version = "1.0.129", features = ["derive"] } 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 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<()> { pub async fn run(config: Config, infile: PathBuf) -> miette::Result<()> {
if config.locations.src.join("modpack.json").exists() || 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) tokio::fs::create_dir_all(&config.locations.src)
.await .await
.into_diagnostic()?; .into_diagnostic()?;
tokio::fs::write(config.locations.src.join("modpack.json5"), json) tokio::fs::write(
.await config.locations.src.join("modpack.lua"),
.into_diagnostic()?; make_lua_manifest(&manif)?,
)
.await
.into_diagnostic()?;
println!("{}", "Imported manifest!".green()); 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::{ use addonscript::{
manifest::{ manifest::{
installer::Installer, installer::Installer,
@ -164,7 +164,7 @@ pub async fn run(
.await .await
.into_diagnostic()?; .into_diagnostic()?;
let data = serde_json::to_vec_pretty(&Manifest { let data = make_lua_manifest(&Manifest {
id: modpack_name.to_kebab_case(), id: modpack_name.to_kebab_case(),
manifest_type: ManifestType::Modpack, manifest_type: ManifestType::Modpack,
versions: vec![Version { versions: vec![Version {
@ -193,10 +193,9 @@ pub async fn run(
icon_url: None, icon_url: None,
website_url: None, website_url: None,
}, },
}) })?;
.into_diagnostic()?;
tokio::fs::write(path.join("modpack.json5"), data) tokio::fs::write(path.join("modpack.lua"), data)
.await .await
.into_diagnostic()?; .into_diagnostic()?;

View file

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