Skip to content

Commit

Permalink
Refactor and simplify the usage of wca in the wpublisher module.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Barsik-sus committed Mar 11, 2024
1 parent da92a2c commit 6c2bf97
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 77 deletions.
121 changes: 58 additions & 63 deletions module/move/wpublisher/src/publisher/commands/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

4 changes: 2 additions & 2 deletions module/move/wpublisher/src/publisher/commands/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
4 changes: 2 additions & 2 deletions module/move/wpublisher/src/publisher/commands/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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();

Expand Down
13 changes: 4 additions & 9 deletions module/move/wpublisher/src/publisher/wpublisher_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,17 @@ 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" )?;
std::process::exit( 1 )
}
else
{
ca.perform( program.as_str() )
ca.perform( args )
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(String, ',')> - List packages." ) );
}

//
Expand Down

0 comments on commit 6c2bf97

Please sign in to comment.