Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

project: better cli handling, table output format #833

Merged
merged 4 commits into from
Nov 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 88 additions & 16 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ serde_json = "1.0.132"
sha-1 = "0.10.1"
strsim = "0.11.1"
supports-hyperlinks = "3.0.0"
tabled = "0.16.0"
terminal-link = "0.1.0"
thiserror = "1.0.65"
toml = "0.8.19"
Expand Down
4 changes: 2 additions & 2 deletions bin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ hemtt-stringtable = { path = "../libs/stringtable", version = "1.0.0" }
hemtt-workspace = { path = "../libs/workspace", version = "1.0.0" }

arma3-wiki = { workspace = true }
clap = { workspace = true }
clap = { workspace = true, features = ["derive"] }
dialoguer = "0.11.0"
dirs = { workspace = true }
fs_extra = "1.3.0"
Expand All @@ -49,7 +49,7 @@ semver = "1.0.23"
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
supports-hyperlinks = { workspace = true }
term-table = "1.4.0"
tabled = { workspace = true }
terminal-link = { workspace = true }
time = { version = "0.3.36", features = ["formatting"] }
tracing = { workspace = true }
Expand Down
11 changes: 4 additions & 7 deletions bin/src/commands/book.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
use clap::{ArgMatches, Command};

use crate::{report::Report, Error};

#[must_use]
pub fn cli() -> Command {
Command::new("book").about("Open The HEMTT book")
}
#[derive(clap::Parser)]
/// Open The HEMTT book
pub struct Command {}

/// Execute the book command
///
/// # Errors
/// Will not return an error
pub fn execute(_: &ArgMatches) -> Result<Report, Error> {
pub fn execute(_: &Command) -> Result<Report, Error> {
if let Err(e) = webbrowser::open("https://hemtt.dev/") {
eprintln!("Failed to open the HEMTT book: {e}");
}
Expand Down
72 changes: 30 additions & 42 deletions bin/src/commands/build.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use clap::{ArgAction, ArgMatches, Command};

use crate::{
context::{self, Context},
error::Error,
Expand All @@ -10,51 +8,41 @@ use crate::{

use super::global_modules;

#[must_use]
pub fn cli() -> Command {
add_just(add_args(
Command::new("build")
.about("Build the project for final testing")
.long_about(
"Build your project in release mode for testing, without signing for full release.",
),
))
}
#[derive(clap::Parser)]
/// Build the project for final testing
///
/// Build your project in release mode for testing, without signing for full release.
pub struct Command {
#[clap(flatten)]
build: BuildArgs,

#[must_use]
pub fn add_args(cmd: Command) -> Command {
cmd.arg(
clap::Arg::new("no-bin")
.long("no-bin")
.help("Do not binarize the project")
.action(ArgAction::SetTrue),
)
.arg(
clap::Arg::new("no-rap")
.long("no-rap")
.help("Do not rapify (cpp, rvmat)")
.action(ArgAction::SetTrue),
)
#[clap(flatten)]
just: super::JustArgs,

#[clap(flatten)]
global: crate::GlobalArgs,
}

#[must_use]
pub fn add_just(cmd: Command) -> Command {
cmd.arg(
clap::Arg::new("just")
.long("just")
.help("Only build the given addon")
.action(ArgAction::Append),
)
#[derive(clap::Args)]
#[allow(clippy::module_name_repetitions)]
pub struct BuildArgs {
#[arg(long, action = clap::ArgAction::SetTrue)]
/// Do not binarize the project
no_bin: bool,
#[arg(long, action = clap::ArgAction::SetTrue)]
/// Do not rapify (cpp, rvmat)
no_rap: bool,
}

/// Execute the build command, build a new executor
///
/// # Errors
/// [`Error`] depending on the modules
pub fn execute(matches: &ArgMatches) -> Result<Report, Error> {
let just = matches
.get_many::<String>("just")
.unwrap_or_default()
pub fn execute(cmd: &Command) -> Result<Report, Error> {
let just = cmd
.just
.just
.iter()
.map(|s| s.to_lowercase())
.collect::<Vec<_>>();
let mut ctx = Context::new(
Expand All @@ -70,7 +58,7 @@ pub fn execute(matches: &ArgMatches) -> Result<Report, Error> {
if !just.is_empty() {
ctx = ctx.filter(|a, _| just.contains(&a.name().to_lowercase()));
}
let mut executor = executor(ctx, matches);
let mut executor = executor(ctx, &cmd.build);

if !just.is_empty() {
warn!("Use of `--just` is not recommended, only use it if you know what you're doing");
Expand All @@ -82,16 +70,16 @@ pub fn execute(matches: &ArgMatches) -> Result<Report, Error> {
}

#[must_use]
pub fn executor(ctx: Context, matches: &ArgMatches) -> Executor {
pub fn executor(ctx: Context, args: &BuildArgs) -> Executor {
let mut executor = Executor::new(ctx);
global_modules(&mut executor);

executor.collapse(Collapse::No);

if matches.get_one::<bool>("no-rap") != Some(&true) {
if !args.no_rap {
executor.add_module(Box::<Rapifier>::default());
}
if matches.get_one::<bool>("no-bin") != Some(&true) {
if !args.no_bin {
executor.add_module(Box::<Binarize>::default());
}
executor.add_module(Box::<Files>::default());
Expand Down
12 changes: 6 additions & 6 deletions bin/src/commands/check.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use clap::Command;

use crate::{
commands::global_modules,
context::Context,
Expand All @@ -9,16 +7,18 @@ use crate::{
report::Report,
};

#[must_use]
pub fn cli() -> Command {
Command::new("check").about("Check the project for errors")
#[derive(clap::Parser)]
/// Check the project for errors
pub struct Command {
#[clap(flatten)]
global: crate::GlobalArgs,
}

/// Execute the dev command
///
/// # Errors
/// [`Error`] depending on the modules
pub fn execute() -> Result<Report, Error> {
pub fn execute(_: &Command) -> Result<Report, Error> {
let ctx = Context::new(
Some("check"),
crate::context::PreservePrevious::Remove,
Expand Down
Loading
Loading