From 6c2bf97d6a89d56742821a088595c7cc8a56e8a8 Mon Sep 17 00:00:00 2001 From: Barsik Date: Mon, 11 Mar 2024 20:28:13 +0200 Subject: [PATCH] Refactor and simplify the usage of wca in the wpublisher module. The process of forming a CommandsAggregator was restructured to use a more succinct builder pattern. This includes simplifying the initialization of a CommandsAggregator, along with updating the argument structure in the `publish` and `list` methods. Furthermore, a few unnecessary command definitions were eliminated from `init.rs`, leading to a leaner implementation. Test assertions have been updated accordingly. --- .../wpublisher/src/publisher/commands/init.rs | 121 +++++++++--------- .../wpublisher/src/publisher/commands/list.rs | 4 +- .../src/publisher/commands/publish.rs | 4 +- .../src/publisher/wpublisher_entry.rs | 13 +- .../tests/publisher/inc/publisher_test.rs | 2 +- 5 files changed, 67 insertions(+), 77 deletions(-) diff --git a/module/move/wpublisher/src/publisher/commands/init.rs b/module/move/wpublisher/src/publisher/commands/init.rs index f65be4ad83..ea5745ec57 100644 --- a/module/move/wpublisher/src/publisher/commands/init.rs +++ b/module/move/wpublisher/src/publisher/commands/init.rs @@ -2,83 +2,78 @@ /// Internal namespace. pub( crate ) mod private { - use wca::{ Type, Routine }; + use crate::*; + use { commands }; + use wca::{ Type, CommandsAggregator, CommandsAggregatorFormer }; /// /// Form CA commands grammar. /// - pub fn grammar_form() -> Vec< wca::Command > + pub fn ca() -> CommandsAggregatorFormer { - let publish_command = wca::Command::former() - .hint( "Publish package on `crates.io`." ) - .long_hint( "Publish package on `crates.io`." ) - .phrase( "publish" ) - .subject( "A path to package. Should be a directory with file `Cargo.toml`.", Type::List( Type::String.into(), ',' ), true ) - .property( "dry", "Run command dry. Default is false.", Type::String, true ) - .property( "verbosity", "Setup level of verbosity.", Type::String, true ) - .property_alias( "verbosity", "v" ) - .form(); + CommandsAggregator::former() - let workspace_publish_without_subject_command = wca::Command::former() - .hint( "Publish packages from workspace on `crates.io`." ) - .long_hint( "Publish packages from workspace on `crates.io`." ) - .phrase( "workspace.publish" ) - .property( "dry", "Run command dry. Default is false.", Type::String, true ) - .property( "verbosity", "Setup level of verbosity.", Type::String, true ) - .property_alias( "verbosity", "v" ) - .form(); + .command( "publish" ) + .hint( "Publish package on `crates.io`." ) + .long_hint( "Publish package on `crates.io`." ) + .subject() + .hint( "A path to package. Should be a directory with file `Cargo.toml`." ) + .kind( Type::List( Type::String.into(), ',' ) ) + .optional( true ) + .end() + .property( "dry" ) + .hint( "Run command dry. Default is false." ) + .kind( Type::String ) + .optional( true ) + .end() + .property( "verbosity" ) + .hint( "Setup level of verbosity." ) + .kind( Type::String ) + .optional( true ) + .alias( "v" ) + .end() + .routine( commands::publish::publish ) + .end() - let workspace_publish_command = wca::Command::former() - .hint( "Publish packages from workspace on `crates.io`." ) - .long_hint( "Publish packages from workspace on `crates.io`." ) - .phrase( "workspace.publish" ) - .subject( "A path to manifest path with workspace. Should be a directory with file `Cargo.toml`.", Type::String, true ) - .property( "dry", "Run command dry. Default is false.", Type::String, true ) - .property( "verbosity", "Setup level of verbosity.", Type::String, true ) - .property_alias( "verbosity", "v" ) - .form(); + .command( "workspace.publish" ) + .hint( "Publish packages from workspace on `crates.io`." ) + .long_hint( "Publish packages from workspace on `crates.io`." ) + .subject() + .hint( "A path to manifest path with workspace. Should be a directory with file `Cargo.toml`." ) + .kind( Type::String ) + .optional( true ) + .end() + .property( "dry" ) + .hint( "Run command dry. Default is false." ) + .kind( Type::String ) + .optional( true ) + .end() + .property( "verbosity" ) + .hint( "Setup level of verbosity." ) + .kind( Type::String ) + .optional( true ) + .alias( "v" ) + .end() + .routine( commands::publish::workspace_publish ) + .end() - let list_without_subject_command = wca::Command::former() - .hint( "List packages." ) - .long_hint( "List packages" ) - .phrase( "list" ) - .form(); - - let list_command = wca::Command::former() - .hint( "List packages." ) - .long_hint( "List packages" ) - .phrase( "list" ) - .subject( "A path to directory with packages. Should be a glob.", Type::List( Type::String.into(), ',' ), true ) - .form(); - - vec! - [ - publish_command, - workspace_publish_without_subject_command, workspace_publish_command, - list_without_subject_command, list_command - ] - } - - /// - /// Form CA commands executor. - /// - - pub fn executor_form() -> std::collections::HashMap< String, Routine > - { - std::collections::HashMap::from - ([ - ( "publish".to_owned(), Routine::new( crate::commands::publish::publish ) ), - ( "workspace.publish".to_owned(), Routine::new( crate::commands::publish::workspace_publish ) ), - ( "list".to_owned(), Routine::new( crate::commands::list::list ) ), - ]) + .command( "list" ) + .hint( "List packages." ) + .long_hint( "List packages" ) + .subject() + .hint( "A path to directory with packages. Should be a glob." ) + .kind( Type::List( Type::String.into(), ',' ) ) + .optional( true ) + .end() + .routine( commands::list::list ) + .end() } } // crate::mod_interface! { - prelude use grammar_form; - prelude use executor_form; + prelude use ca; } diff --git a/module/move/wpublisher/src/publisher/commands/list.rs b/module/move/wpublisher/src/publisher/commands/list.rs index 3533695b77..138d454aa3 100644 --- a/module/move/wpublisher/src/publisher/commands/list.rs +++ b/module/move/wpublisher/src/publisher/commands/list.rs @@ -3,14 +3,14 @@ pub( crate ) mod private { use crate::protected::*; use std::env; - use wca::{ Args, Props }; + use wca::Args; use wca::wtools::error::Result; /// /// List packages. /// - pub fn list( ( args, _ ) : ( Args, Props ) ) -> Result< () > + pub fn list( args : Args ) -> Result< () > { let current_path = env::current_dir().unwrap(); diff --git a/module/move/wpublisher/src/publisher/commands/publish.rs b/module/move/wpublisher/src/publisher/commands/publish.rs index 83c78e3c44..1c9829d822 100644 --- a/module/move/wpublisher/src/publisher/commands/publish.rs +++ b/module/move/wpublisher/src/publisher/commands/publish.rs @@ -39,7 +39,7 @@ pub( crate ) mod private /// Publish package. /// - pub fn publish( ( args, properties ) : ( Args, Props ) ) -> Result< () > + pub fn publish( args : Args, properties : Props ) -> Result< () > { let current_path = env::current_dir().unwrap(); @@ -175,7 +175,7 @@ pub( crate ) mod private /// /// Publish packages from workspace. /// - pub fn workspace_publish( ( args, properties ) : ( Args, Props ) ) -> Result< () > + pub fn workspace_publish( args : Args, properties : Props ) -> Result< () > { let current_path = env::current_dir().unwrap(); diff --git a/module/move/wpublisher/src/publisher/wpublisher_entry.rs b/module/move/wpublisher/src/publisher/wpublisher_entry.rs index dfd020baa5..83db66e6c3 100644 --- a/module/move/wpublisher/src/publisher/wpublisher_entry.rs +++ b/module/move/wpublisher/src/publisher/wpublisher_entry.rs @@ -22,14 +22,9 @@ fn main() -> Result< (), wca::Error > { let args = env::args().skip( 1 ).collect::< Vec< String > >(); - let ca = wca::CommandsAggregator::former() - // .exit_code_on_error( 1 ) - .grammar( commands::grammar_form() ) - .executor( commands::executor_form() ) - .perform(); - - let program = args.join( " " ); - if program.is_empty() + let ca = init::ca().perform(); + + if args.is_empty() { eprintln!( "Ambiguity. Did you mean?" ); ca.perform( ".help" )?; @@ -37,7 +32,7 @@ fn main() -> Result< (), wca::Error > } else { - ca.perform( program.as_str() ) + ca.perform( args ) } } diff --git a/module/move/wpublisher/tests/publisher/inc/publisher_test.rs b/module/move/wpublisher/tests/publisher/inc/publisher_test.rs index 6a2f62c202..50f626a20f 100644 --- a/module/move/wpublisher/tests/publisher/inc/publisher_test.rs +++ b/module/move/wpublisher/tests/publisher/inc/publisher_test.rs @@ -76,7 +76,7 @@ tests_impls! let stderr = std::str::from_utf8( proc.stderr.as_slice() ).unwrap(); assert_eq!( stderr, "Ambiguity. Did you mean?\n" ); let stdout = std::str::from_utf8( proc.stdout.as_slice() ).unwrap(); - assert!( stdout.contains( "list - List packages." ) ); + assert!( stdout.contains( "list - List packages." ) ); } //