legacympt-rs/mpt/src/commands/import.rs
LordMZTE 5ead609763
Some checks failed
continuous-integration/drone/push Build is failing
0.1.3
2022-03-05 01:49:37 +01:00

137 lines
3.9 KiB
Rust

use addonscript::{
manifest::{
installer::Installer,
link::Link,
Contributor,
File,
Manifest,
ManifestType,
Meta,
Relation,
RelationType,
Repository,
RepositoryType,
Version,
},
util::default_file_opts,
};
use crossterm::style::Stylize;
use heck::ToKebabCase;
use log::info;
use miette::{bail, IntoDiagnostic, WrapErr};
use twitch::manifest::Manifest as TwManifest;
use url::Url;
use std::path::PathBuf;
use crate::config::Config;
pub async fn run(config: Config, infile: PathBuf) -> miette::Result<()> {
if config.locations.src.join("modpack.json").exists() ||
config.locations.src.join("modpack.json5").exists()
{
bail!("Manifest already exists!");
}
let mut data = serde_json::from_slice::<TwManifest>(
&tokio::fs::read(infile)
.await
.into_diagnostic()
.wrap_err("Failed to read twitch manifest")?,
)
.into_diagnostic()
.wrap_err("Failed to parse twitch manifest")?;
info!("converting twitch mods to AS relations");
let mut relations = data
.files
.into_iter()
.map(|f| Relation {
id: f.project_id.to_string(),
file: Some(File::Maven {
installer: Installer::Dir("mods".into()),
artifact: format!("curse:{}:{}", f.project_id, f.file_id),
repository: "curseforge".into(),
}),
versions: None,
meta: None,
relation_type: RelationType::Mod,
options: default_file_opts(),
})
.collect::<Vec<_>>();
if let Some(ml) = data.minecraft.mod_loaders.pop() {
info!("fount modloader {:?}", &ml);
let mut splits = ml.id.split('-');
if !matches!(splits.next(), Some("forge")) {
bail!("Twitch manifest contains invalid or unknown modloader!");
}
let forgever = splits.next();
if forgever.is_none() {
bail!("Modloader in twitch manifest missing version!");
}
let forgever = forgever.unwrap();
relations.push(Relation {
id: "forge".into(),
file: None,
relation_type: RelationType::Modloader,
options: default_file_opts(),
versions: Some(format!(
"[{mcver}-{forgever}]",
mcver = &data.minecraft.version,
forgever = forgever,
)),
meta: None,
});
}
let manif = Manifest {
id: data.name.to_kebab_case(),
manifest_type: ManifestType::Modpack,
versions: vec![Version {
mcversion: vec![data.minecraft.version],
version: data.version,
files: vec![File::Link {
id: Some("overrides".into()),
link: Link::File("overrides".into()),
installer: Installer::Override,
options: Some(default_file_opts()),
}],
relations,
}],
repositories: vec![Repository {
id: "curseforge".into(),
repo_type: RepositoryType::Curseforge,
url: Url::parse("https://cursemaven.com").unwrap(), // unwrap is ok on fixed value
}],
meta: Meta {
name: data.name,
contributors: vec![Contributor {
roles: vec!["owner".into()],
name: data.author,
}],
description: None,
icon_url: None,
website_url: None,
},
};
tokio::fs::create_dir_all(&config.locations.src)
.await
.into_diagnostic()?;
tokio::fs::write(
config.locations.src.join("modpack.json5"),
serde_json::to_vec_pretty(&manif)
.into_diagnostic()
.wrap_err("Failed to generate json data")?,
)
.await
.into_diagnostic()?;
println!("{}", "Imported manifest!".green());
Ok(())
}