-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from SARDONYX-sard/feature/implement-cli-subcmd
feat(cli): implement cli sub commands
- Loading branch information
Showing
8 changed files
with
291 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
use crate::convert::Convert; | ||
use clap::Args; | ||
|
||
#[derive(Debug, clap::Parser)] | ||
#[clap(version, about)] | ||
pub(crate) enum Commands { | ||
/// Convert DAR to OAR | ||
#[clap(arg_required_else_help = true)] | ||
Convert(Convert), | ||
|
||
#[clap(arg_required_else_help = true)] | ||
/// Unhide all files in the `DynamicAnimationReplacer` directory | ||
/// by removing the `mohidden` extension | ||
UnhideDar(UnhideDarOption), | ||
|
||
#[clap(arg_required_else_help = true)] | ||
/// Find and delete `OpenAnimationReplacer` directory | ||
RemoveOar(RemoveOarOption), | ||
} | ||
|
||
#[derive(Debug, Args)] | ||
pub(super) struct UnhideDarOption { | ||
#[clap(value_parser)] | ||
/// DAR directory containing files with ".mohidden" extension | ||
pub dar_dir: String, | ||
|
||
// ---logger | ||
#[clap(long)] | ||
/// Log output to stdout as well | ||
pub stdout: bool, | ||
#[clap(long, default_value = "error")] | ||
/// Log level | ||
/// | ||
/// trace | debug | info | warn | error | ||
pub log_level: String, | ||
#[clap(long, default_value = "./convert.log")] | ||
/// Output path of log file | ||
pub log_path: String, | ||
} | ||
|
||
#[derive(Debug, Args)] | ||
pub(super) struct RemoveOarOption { | ||
#[clap(value_parser)] | ||
/// Path containing the "OpenAnimationReplacer" directory | ||
pub target_path: String, | ||
|
||
// ---logger | ||
#[clap(long)] | ||
/// Log output to stdout as well | ||
pub stdout: bool, | ||
#[clap(long, default_value = "error")] | ||
/// Log level | ||
/// | ||
/// trace | debug | info | warn | error | ||
pub log_level: String, | ||
#[clap(long, default_value = "./convert.log")] | ||
/// Output path of log file | ||
pub log_path: String, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
mod commands; | ||
|
||
use crate::cli::commands::Commands; | ||
use crate::convert::dar2oar; | ||
use crate::init_tracing; | ||
use dar2oar_core::{remove_oar, unhide_dar, Closure}; | ||
use std::str::FromStr; | ||
use tracing::Level; | ||
|
||
/// Converter CLI version | ||
#[derive(Debug, clap::Parser)] | ||
#[clap(name = "dar2oar", about)] | ||
pub(crate) struct Cli { | ||
#[clap(subcommand)] | ||
command: Commands, | ||
} | ||
|
||
macro_rules! init_logger { | ||
($args:ident) => { | ||
init_tracing( | ||
&$args.log_path, | ||
Level::from_str(&$args.log_level).unwrap_or(Level::ERROR), | ||
$args.stdout, | ||
)?; | ||
}; | ||
} | ||
|
||
pub(crate) async fn run_cli(args: Cli) -> anyhow::Result<()> { | ||
match args.command { | ||
Commands::Convert(args) => { | ||
init_logger!(args); | ||
dar2oar(args).await?; | ||
} | ||
Commands::UnhideDar(args) => { | ||
init_logger!(args); | ||
unhide_dar(args.dar_dir, Closure::default).await?; | ||
} | ||
Commands::RemoveOar(args) => { | ||
init_logger!(args); | ||
remove_oar(args.target_path, Closure::default).await?; | ||
} | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
use dar2oar_core::{convert_dar_to_oar, get_mapping_table, Closure, ConvertOptions}; | ||
|
||
pub(crate) async fn dar2oar(args: Convert) -> anyhow::Result<()> { | ||
let config = ConvertOptions { | ||
dar_dir: args.src, | ||
oar_dir: args.dist, | ||
mod_name: args.name, | ||
author: args.author, | ||
section_table: get_mapping_table(args.mapping_file).await, | ||
section_1person_table: get_mapping_table(args.mapping_1person_file).await, | ||
run_parallel: args.run_parallel, | ||
hide_dar: args.hide_dar, | ||
}; | ||
match convert_dar_to_oar(config, Closure::default).await { | ||
Ok(()) => Ok(()), | ||
Err(err) => { | ||
tracing::error!("{}", err); | ||
anyhow::bail!("{}", err) | ||
} | ||
} | ||
} | ||
|
||
#[derive(Debug, clap::Args)] | ||
pub(crate) struct Convert { | ||
#[clap(value_parser)] | ||
/// DAR source dir path | ||
src: String, | ||
#[clap(long)] | ||
/// OAR destination dir path(If not, it is inferred from DAR path) | ||
dist: Option<String>, | ||
#[clap(long)] | ||
/// Mod name in config.json & directory name(If not, it is inferred from DAR path) | ||
name: Option<String>, | ||
#[clap(long)] | ||
/// Mod author in config.json | ||
author: Option<String>, | ||
#[clap(long)] | ||
/// Path to section name table | ||
/// | ||
/// - See more details | ||
/// https://github.com/SARDONYX-sard/dar-to-oar/wiki#what-is-the-mapping-file | ||
mapping_file: Option<String>, | ||
#[clap(long)] | ||
/// Path to section name table(For _1st_person) | ||
mapping_1person_file: Option<String>, | ||
#[clap(long)] | ||
/// Use multi thread | ||
/// | ||
/// [Note] | ||
/// More than twice the processing speed can be expected, | ||
/// but the concurrent processing results in thread termination timings being out of order, | ||
/// so log writes will be out of order as well, greatly reducing readability of the logs. | ||
run_parallel: bool, | ||
#[clap(long)] | ||
/// After conversion, add ".mohidden" to all DAR files to hide them(For MO2 user) | ||
hide_dar: bool, | ||
|
||
// ---logger | ||
#[clap(long)] | ||
/// Log output to stdout as well | ||
pub stdout: bool, | ||
#[clap(long, default_value = "error")] | ||
/// Log level | ||
/// | ||
/// trace | debug | info | warn | error | ||
pub log_level: String, | ||
#[clap(long, default_value = "./convert.log")] | ||
/// Output path of log file | ||
pub log_path: String, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
use std::fs::File; | ||
use std::path::Path; | ||
use tracing::level_filters::LevelFilter; | ||
|
||
/// Init logger. | ||
pub(crate) fn init_tracing( | ||
log_path: impl AsRef<Path>, | ||
filter: impl Into<LevelFilter>, | ||
with_stdout: bool, | ||
) -> anyhow::Result<()> { | ||
use tracing_subscriber::{fmt, layer::SubscriberExt}; | ||
let log_path = log_path.as_ref(); | ||
if let Some(log_path) = log_path.parent() { | ||
std::fs::create_dir_all(log_path)?; | ||
}; | ||
|
||
match with_stdout { | ||
true => tracing::subscriber::set_global_default( | ||
fmt::Subscriber::builder() | ||
.with_max_level(filter) | ||
.finish() | ||
.with( | ||
fmt::Layer::default() | ||
.with_writer(File::create(log_path)?) | ||
.with_line_number(true) | ||
.with_ansi(false), | ||
), | ||
)?, | ||
false => tracing_subscriber::fmt() | ||
.with_ansi(false) | ||
.with_writer(File::create(log_path)?) | ||
.with_max_level(filter) | ||
.init(), | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.