legacympt-rs/mpt/src/main.rs

138 lines
3.6 KiB
Rust
Raw Normal View History

2022-03-05 01:49:37 +01:00
use clap::{Parser, Subcommand};
2021-09-08 17:31:27 +02:00
use log::{info, LevelFilter};
2021-10-04 21:30:13 +02:00
use miette::{IntoDiagnostic, WrapErr};
2021-09-08 17:31:27 +02:00
use simplelog::{ColorChoice, TermLogger, TerminalMode};
2021-08-29 15:14:10 +02:00
use std::path::PathBuf;
mod commands;
mod config;
mod downloader;
2021-08-29 15:14:10 +02:00
mod forge;
mod util;
2022-03-05 01:49:37 +01:00
#[derive(Parser)]
2021-08-29 15:14:10 +02:00
struct Opt {
2022-03-05 01:49:37 +01:00
/// enable verbose logging
#[clap(short, long, parse(from_occurrences))]
2021-09-08 17:31:27 +02:00
verbose: u8,
2022-03-05 01:49:37 +01:00
/// add a parameter to be used by the build script
/// (only useful with lua manifests)
#[clap(short = 'D')]
defines: Vec<String>,
#[clap(subcommand)]
2021-08-29 15:14:10 +02:00
cmd: Command,
}
2022-03-05 01:49:37 +01:00
#[derive(Subcommand)]
2021-08-29 15:14:10 +02:00
enum Command {
2022-03-05 01:49:37 +01:00
/// Initializes a new modpack
2021-08-29 15:14:10 +02:00
Init {
2022-03-05 01:49:37 +01:00
/// The name of the modpack
2021-08-29 15:14:10 +02:00
modpack_name: String,
2022-03-05 01:49:37 +01:00
/// Name of the modpack author
2021-09-01 15:06:49 +02:00
name: String,
2022-03-05 01:49:37 +01:00
/// The minecraft version of the modpack
2021-08-29 15:14:10 +02:00
mcversion: String,
},
2022-03-05 01:49:37 +01:00
/// Downloads mods of the pack
#[clap(name = "downloadmods")]
2021-08-29 15:14:10 +02:00
DownloadMods {
2022-03-05 01:49:37 +01:00
/// Directory to download mods to
2021-08-29 15:14:10 +02:00
dir: PathBuf,
2022-03-05 01:49:37 +01:00
/// Download all relations and not just mods
#[clap(short, long)]
2021-08-29 15:14:10 +02:00
all: bool,
},
2021-09-01 15:06:49 +02:00
2022-03-05 01:49:37 +01:00
/// Builds a twitch export of the pack
#[clap(name = "buildtwitch")]
2021-09-01 15:06:49 +02:00
BuildTwitch {
2022-03-05 01:49:37 +01:00
/// Downloads all relations instead of just required ones
#[clap(short, long)]
2021-09-01 15:06:49 +02:00
all: bool,
},
2021-09-01 16:25:18 +02:00
2022-03-05 01:49:37 +01:00
/// Deletes artifacts and temporary files
2021-09-01 16:25:18 +02:00
Clean,
2021-09-01 19:24:26 +02:00
2022-03-05 01:49:37 +01:00
/// Creates a HTML list of the pack's mods
#[clap(name = "createmodlist")]
2021-09-01 19:24:26 +02:00
CreateModList {
2022-03-05 01:49:37 +01:00
/// File to write the mod list to
#[clap(default_value = "build/modlist.html")]
2021-09-01 19:24:26 +02:00
outfile: PathBuf,
},
2021-09-02 16:35:48 +02:00
2022-03-05 01:49:37 +01:00
/// Imports a twitch manifest file and converts it to an addonscript
/// modpack.json
2021-09-02 16:35:48 +02:00
Import {
2022-03-05 01:49:37 +01:00
/// Twitch manifest to convert
2021-09-02 16:35:48 +02:00
infile: PathBuf,
},
2021-08-29 15:14:10 +02:00
}
#[tokio::main]
2021-10-04 21:30:13 +02:00
async fn main() -> miette::Result<()> {
2022-03-05 01:49:37 +01:00
let Opt {
cmd,
defines,
verbose,
} = Opt::parse();
2021-09-08 17:31:27 +02:00
let log_level = match verbose {
0 => LevelFilter::Off,
1 => LevelFilter::Info,
2 => LevelFilter::Debug,
_ => LevelFilter::Trace,
};
TermLogger::init(
log_level,
simplelog::ConfigBuilder::new()
.set_time_to_local(true)
.set_target_level(LevelFilter::Error)
.build(),
TerminalMode::Stderr,
ColorChoice::Auto,
)
2021-10-04 21:30:13 +02:00
.into_diagnostic()
.wrap_err("Failed to init logger")?;
2021-09-08 17:31:27 +02:00
/// runs a given command, if the first arg is config or manifest, the
/// manifest or config file is passed to the comand respectively.
macro_rules! run_cmd {
(config: $cmd:ident $($args:expr),* $(,)?) => {
run_cmd!($cmd util::parse_config().await?, $($args),*)
};
(manifest: $cmd:ident $($args:expr),* $(,)?) => {
2022-03-05 01:49:37 +01:00
run_cmd!($cmd util::parse_config_and_manifest(defines, stringify!($cmd)).await?, $($args),*)
2021-09-08 17:31:27 +02:00
};
($cmd:ident $($args:expr),* $(,)?) => {{
info!("Running command {}", stringify!($cmd));
commands::$cmd::run($($args),*).await?;
}}
}
2021-08-29 15:14:10 +02:00
match cmd {
Command::Init {
modpack_name,
2021-09-01 15:06:49 +02:00
name,
2021-08-29 15:14:10 +02:00
mcversion,
2021-09-08 17:31:27 +02:00
} => run_cmd!(init modpack_name, name, mcversion),
Command::DownloadMods { dir, all } => run_cmd!(manifest: downloadmods dir, all),
Command::BuildTwitch { all } => run_cmd!(manifest: buildtwitch all),
Command::Clean => run_cmd!(config: clean),
Command::CreateModList { outfile } => run_cmd!(manifest: createmodlist outfile),
Command::Import { infile } => run_cmd!(config: import infile),
2021-08-29 15:14:10 +02:00
}
Ok(())
}