diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/bin/jvn.rs | 5 | ||||
| -rw-r--r-- | src/cmds/arg/version.rs | 8 | ||||
| -rw-r--r-- | src/cmds/cmd/version.rs | 55 | ||||
| -rw-r--r-- | src/cmds/collect/compile_info.rs | 7 | ||||
| -rw-r--r-- | src/cmds/converter/version_in_out.rs | 20 | ||||
| -rw-r--r-- | src/cmds/in/version.rs | 4 | ||||
| -rw-r--r-- | src/cmds/out/version.rs | 10 | ||||
| -rw-r--r-- | src/cmds/renderer/version.rs | 92 |
8 files changed, 201 insertions, 0 deletions
diff --git a/src/bin/jvn.rs b/src/bin/jvn.rs index c2ae5ec..b051c51 100644 --- a/src/bin/jvn.rs +++ b/src/bin/jvn.rs @@ -89,8 +89,13 @@ async fn main() { // Other flags let no_error_logs = special_flag!(args, "--no-error-logs"); let help = special_flag!(args, "--help") || special_flag!(args, "-h"); + let version = special_flag!(args, "--version") || special_flag!(args, "-v"); let confirmed = special_flag!(args, "--confirm") || special_flag!(args, "-C"); + if version { + args.insert(0, "version".to_string()); + trace!("{}", t!("verbose.redirect_to_version_command")); + } if no_error_logs { trace!("{}", t!("verbose.no_error_logs")); } diff --git a/src/cmds/arg/version.rs b/src/cmds/arg/version.rs new file mode 100644 index 0000000..5e7edbe --- /dev/null +++ b/src/cmds/arg/version.rs @@ -0,0 +1,8 @@ +#[derive(clap::Parser)] +pub struct JVVersionArgument { + #[arg(short = 'c', long = "with-compile-info")] + pub with_compile_info: bool, + + #[arg(long)] + pub no_banner: bool, +} diff --git a/src/cmds/cmd/version.rs b/src/cmds/cmd/version.rs new file mode 100644 index 0000000..7a2e45b --- /dev/null +++ b/src/cmds/cmd/version.rs @@ -0,0 +1,55 @@ +use crate::{ + cmd_output, + cmds::{ + arg::version::JVVersionArgument, collect::compile_info::JVCompileInfoCollect, + converter::version_in_out::JVVersionInputOutputConverter, r#in::version::JVVersionInput, + out::version::JVVersionOutput, + }, + data::compile_info::CompileInfo, + systems::{ + cmd::{ + cmd_system::JVCommandContext, + errors::{CmdExecuteError, CmdPrepareError}, + }, + helpdoc::helpdoc_viewer, + }, +}; +use cmd_system_macros::exec; +use just_enough_vcs::data::compile_info::CoreCompileInfo; +use std::any::TypeId; + +pub struct JVVersionCommand; +type Cmd = JVVersionCommand; +type Arg = JVVersionArgument; +type In = JVVersionInput; +type Collect = JVCompileInfoCollect; + +async fn help_str() -> String { + helpdoc_viewer::display("commands/version").await; + String::new() +} + +async fn prepare(args: &Arg, _ctx: &JVCommandContext) -> Result<In, CmdPrepareError> { + Ok(JVVersionInput { + show_compile_info: args.with_compile_info, + show_banner: !args.no_banner, + }) +} + +async fn collect(_args: &Arg, _ctx: &JVCommandContext) -> Result<Collect, CmdPrepareError> { + Ok(JVCompileInfoCollect { + compile_info: CompileInfo::default(), + compile_info_core: CoreCompileInfo::default(), + }) +} + +#[exec] +async fn exec( + input: In, + collect: Collect, +) -> Result<(Box<dyn std::any::Any + Send + 'static>, TypeId), CmdExecuteError> { + let output = JVVersionInputOutputConverter::merge_to_output(input, collect); + cmd_output!(JVVersionOutput => output) +} + +crate::command_template!(); diff --git a/src/cmds/collect/compile_info.rs b/src/cmds/collect/compile_info.rs new file mode 100644 index 0000000..1f33660 --- /dev/null +++ b/src/cmds/collect/compile_info.rs @@ -0,0 +1,7 @@ +use crate::data::compile_info::CompileInfo; +use just_enough_vcs::data::compile_info::CoreCompileInfo; + +pub struct JVCompileInfoCollect { + pub compile_info: CompileInfo, + pub compile_info_core: CoreCompileInfo, +} diff --git a/src/cmds/converter/version_in_out.rs b/src/cmds/converter/version_in_out.rs new file mode 100644 index 0000000..34d243b --- /dev/null +++ b/src/cmds/converter/version_in_out.rs @@ -0,0 +1,20 @@ +use crate::cmds::{ + collect::compile_info::JVCompileInfoCollect, r#in::version::JVVersionInput, + out::version::JVVersionOutput, +}; + +pub struct JVVersionInputOutputConverter; + +impl JVVersionInputOutputConverter { + pub fn merge_to_output( + input: JVVersionInput, + collect: JVCompileInfoCollect, + ) -> JVVersionOutput { + JVVersionOutput { + show_compile_info: input.show_compile_info, + show_banner: input.show_banner, + compile_info: collect.compile_info, + compile_info_core: collect.compile_info_core, + } + } +} diff --git a/src/cmds/in/version.rs b/src/cmds/in/version.rs new file mode 100644 index 0000000..d167268 --- /dev/null +++ b/src/cmds/in/version.rs @@ -0,0 +1,4 @@ +pub struct JVVersionInput { + pub show_compile_info: bool, + pub show_banner: bool, +} diff --git a/src/cmds/out/version.rs b/src/cmds/out/version.rs new file mode 100644 index 0000000..fcbd90c --- /dev/null +++ b/src/cmds/out/version.rs @@ -0,0 +1,10 @@ +use crate::data::compile_info::CompileInfo; +use just_enough_vcs::data::compile_info::CoreCompileInfo; + +#[derive(serde::Serialize)] +pub struct JVVersionOutput { + pub show_compile_info: bool, + pub show_banner: bool, + pub compile_info: CompileInfo, + pub compile_info_core: CoreCompileInfo, +} diff --git a/src/cmds/renderer/version.rs b/src/cmds/renderer/version.rs new file mode 100644 index 0000000..cc76636 --- /dev/null +++ b/src/cmds/renderer/version.rs @@ -0,0 +1,92 @@ +use cli_utils::display::markdown::Markdown; +use render_system_macros::result_renderer; +use rust_i18n::t; + +use crate::{ + cmds::out::version::JVVersionOutput, + r_println, + systems::{cmd::errors::CmdRenderError, render::renderer::JVRenderResult}, +}; + +#[result_renderer(JVVersionRenderer)] +pub async fn render(data: &JVVersionOutput) -> Result<JVRenderResult, CmdRenderError> { + let mut r = JVRenderResult::default(); + + if data.show_banner { + draw_banner(&mut r, data) + } else { + draw_version(&mut r, data); + } + + if data.show_compile_info { + draw_compile_infos(&mut r, data); + } + + Ok(r) +} + +fn draw_banner(r: &mut JVRenderResult, data: &JVVersionOutput) { + let banner_str = t!( + "banner", + banner_line_1 = t!("version.banner_title_line").trim(), + banner_line_2 = t!( + "version.banner_cmd_version", + cli_version = data.compile_info.cli_version, + build_time = data.compile_info.date + ) + .trim(), + banner_line_3 = t!( + "version.banner_core_version", + core_version = data.compile_info_core.vcs_version + ) + .trim() + ); + let trimmed_banner_str = banner_str + .trim_start_matches("_banner_begin") + .trim_matches('\n'); + r_println!(r, "{}", trimmed_banner_str.to_string().markdown()) +} + +fn draw_version(r: &mut JVRenderResult, data: &JVVersionOutput) { + if data.show_compile_info { + r_println!( + r, + "{}", + t!( + "version.no_banner_output_with_compile_info", + version = data.compile_info.cli_version + ) + .trim() + ) + } else { + r_println!( + r, + "{}", + t!( + "version.no_banner_output", + version = data.compile_info.cli_version + ) + .trim() + ) + } +} + +fn draw_compile_infos(r: &mut JVRenderResult, data: &JVVersionOutput) { + r_println!( + r, + "\n{}", + t!( + "version.compile_info.info", + build_time = data.compile_info.date, + target = data.compile_info.target, + platform = data.compile_info.platform, + toolchain = data.compile_info.toolchain, + core_branch = data.compile_info_core.build_branch, + cli_branch = data.compile_info.build_branch, + core_commit = &data.compile_info_core.build_commit[..7], + cli_commit = &data.compile_info.build_commit[..7] + ) + .to_string() + .markdown() + ); +} |
