Skip to content

Commit

Permalink
Merge pull request #1281 from YBoy-git/will_features
Browse files Browse the repository at this point in the history
READY : (willbe) : willbe .features
  • Loading branch information
Wandalen committed May 14, 2024
2 parents f198c3e + 3199e0b commit 7c05536
Show file tree
Hide file tree
Showing 20 changed files with 458 additions and 4 deletions.
97 changes: 97 additions & 0 deletions module/move/willbe/src/action/features.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
mod private
{
use crate::*;

use std::
{
collections::{ BTreeMap, HashMap },
fmt
};

use _path::AbsolutePath;
use former::Former;
use error_tools::{ for_app::Context, Result };
use workspace::Workspace;

/// Options available for the .features command
#[ derive( Debug, Former ) ]
pub struct FeaturesOptions
{
manifest_dir : AbsolutePath,
with_features_deps : bool,
}

/// Represents a report about features available in the package
#[ derive( Debug, Default ) ]
pub struct FeaturesReport
{
/// Flag to turn off/on displaying feature dependencies - "feature: [deps...]"
pub with_features_deps : bool,

/// A key-value pair structure representing available features.
///
/// Key: name of the package (useful for workspaces, where multiple packages can be found).
///
/// Value: Another key-value pair representing a feature and its dependencies
pub inner : HashMap< String, BTreeMap< String, Vec< String > > >,
}

impl fmt::Display for FeaturesReport
{
fn fmt( &self, f : &mut fmt::Formatter< '_ >) -> Result< (), fmt::Error >
{
self.inner.iter().try_for_each
( | ( package, features ) |
{
writeln!(f, "Package {}:", package)?;
features.iter().try_for_each
( | ( feature, dependencies ) |
{
let feature = match self.with_features_deps
{
false => format!( "\t{feature}" ),
true
=>
{
let deps = dependencies.join( ", " );
format!( "\t{feature}: [{deps}]" )
}
};
writeln!( f, "{feature}" )
}
)
}
)
}
}

/// List features
pub fn features( FeaturesOptions { manifest_dir, with_features_deps } : FeaturesOptions ) -> Result< FeaturesReport >
{
let workspace = Workspace::with_crate_dir( CrateDir::try_from( manifest_dir.clone() )? ).context( "Failed to find workspace" )?;
let packages = workspace.packages()?.into_iter().filter
( | package |
package.manifest_path().as_str().starts_with( manifest_dir.as_ref().as_os_str().to_str().unwrap() )
).collect::< Vec< _ > >();
let mut report = FeaturesReport
{
with_features_deps,
..Default::default()
};
packages.iter().for_each
( | package |
{
let features = package.features();
report.inner.insert(package.name().to_owned(), features.to_owned());
}
);
Ok( report )
}
}

crate::mod_interface!
{
orphan use features;
orphan use FeaturesOptions;
orphan use FeaturesReport;
}
2 changes: 2 additions & 0 deletions module/move/willbe/src/action/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ crate::mod_interface!
layer cicd_renew;
/// Workspace new.
layer workspace_renew;
/// List features.
layer features;
}
2 changes: 2 additions & 0 deletions module/move/willbe/src/action/readme_modules_headers_renew.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ mod private
let repo_url = url::extract_repo_url( &self.repository_url ).and_then( | r | url::git_info_extract( &r ).ok() ).ok_or_else::< Error, _ >( || err!( "Fail to parse repository url" ) )?;
let example = if let Some( name ) = find_example_file( self.module_path.as_path(), &self.module_name )
{
// qqq : for Bohdan : Hardcoded Strings, would be better to use `PathBuf` to avoid separator mismatch on Windows and Unix
let p = name.strip_prefix( workspace_path ).unwrap().get( 1.. ).unwrap().replace( "\\","%2F" );
let name = name.replace( "/", "\\" );
let name = name.split( "\\" ).last().unwrap().split( "." ).next().unwrap();
format!( " [![Open in Gitpod](https://raster.shields.io/static/v1?label=try&message=online&color=eee&logo=gitpod&logoColor=eee)](https://gitpod.io/#RUN_PATH=.,SAMPLE_FILE={p},RUN_POSTFIX=--example%20{}/https://github.com/{})", name, repo_url )
}
Expand Down
40 changes: 40 additions & 0 deletions module/move/willbe/src/command/features.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
mod private
{
use crate::*;

use action::features::FeaturesOptions;
use std::path::PathBuf;
use _path::AbsolutePath;
use wca::VerifiedCommand;
use wtools::error::Result;

///
/// List features of a package.
///

pub fn features( o : VerifiedCommand ) -> Result< () >
{
let path : PathBuf = o.args.get_owned( 0 ).unwrap_or_else( || "./".into() );
let path = AbsolutePath::try_from( path )?;
let with_features_deps = o.props.get_owned( "with_features_deps" ).unwrap_or( false );
let options = FeaturesOptions::former()
.manifest_dir( path )
.with_features_deps( with_features_deps )
.form();
let report = action::features( options );
match report
{
Ok(success) => println!("{success}"),
Err(failure) => eprintln!("{failure}"),
}
Ok( () )
}

}

crate::mod_interface!
{
/// List features.
orphan use features;
}

19 changes: 18 additions & 1 deletion module/move/willbe/src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,22 @@ with_gitpod: If set to 1, a column with a link to Gitpod will be added. Clicking
.long_hint( "For use this command you need to specify:\n\n[package]\nname = \"test_module\"\nrepository = \"https://github.com/Username/ProjectName/tree/master/module/test_module\"\n...\n[package.metadata]\nstability = \"stable\" (Optional)\ndiscord_url = \"https://discord.gg/1234567890\" (Optional)\n\nin module's Cargo.toml." )
.routine( command::readme_modules_headers_renew )
.end()

.command( "features" )
.hint( "Lists features of the package" )
.long_hint( "Lists features of the package located in a folder.\nWill list either separate package features or features for every package of a workspace")
.subject()
.hint( "Provide path to the package that you want to check.\n\t The path should point to a directory that contains a `Cargo.toml` file." )
.kind( Type::Path )
.optional( true )
.end()
.property("with_features_deps")
.hint( "Display dependencies of features of the package" )
.kind( Type::Bool )
.optional( true )
.end()
.routine( command::features )
.end()
}
}

Expand Down Expand Up @@ -286,5 +302,6 @@ crate::mod_interface!
layer main_header;
/// Generate headers
layer readme_modules_headers_renew;

/// List features
layer features;
}
2 changes: 1 addition & 1 deletion module/move/willbe/src/entity/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ mod private

estimate
}

}

crate::mod_interface!
Expand Down
1 change: 0 additions & 1 deletion module/move/willbe/src/entity/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ mod private
{
&self.inner.features
}

}

/// A dependency of the main crate
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[workspace]
resolver = "2"
members = [
"*",
]

[workspace.metadata]
discord_url = "https://discord.gg/123456789"
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "_chain_of_packages_b"
version = "0.1.0"
edition = "2021"
repository = "https://github.com/Username/test/b"

[package.metadata]
stability = "stable"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
_chain_of_packages_c = { path = "../c", optional = true }

[features]
enabled = []
default = ["boo"]
boo = ["_chain_of_packages_c"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<!--{ generate.module_header.start() }-->
<!--{ generate.module_header.end }-->
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
pub fn add( left : usize, right : usize ) -> usize
{
left + right
}

#[ cfg( test ) ]
mod tests
{
use super::*;

#[ test ]
fn it_works()
{
let result = add( 2, 2 );
assert_eq!( result, 4 );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "_chain_of_packages_c"
version = "0.1.0"
edition = "2021"
repository = "https://github.com/Username/test/c"

[package.metadata]
discord_url = "https://discord.gg/m3YfbXpUUY"
stability = "stable"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

[features]
enabled = []
default = ["foo"]
foo = []
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<!--{ generate.module_header.start() }-->
<!--{ generate.module_header.end }-->
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
pub fn add( left : usize, right : usize ) -> usize
{
left + right
}

#[ cfg( test ) ]
mod tests
{
use super::*;

#[ test ]
fn it_works()
{
let result = add( 2, 2 );
assert_eq!( result, 4 );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "_chain_of_packages_d"
version = "0.1.0"
edition = "2021"
repository = "https://github.com/Username/test/c"

[package.metadata]
stability = "stable"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

[features]
enabled = []
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<!--{ generate.module_header.start() }-->
<!--{ generate.module_header.end }-->
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
pub fn add( left : usize, right : usize ) -> usize
{
left + right
}

#[ cfg( test ) ]
mod tests
{
use super::*;

#[ test ]
fn it_works()
{
let result = add( 2, 2 );
assert_eq!( result, 4 );
}
}

0 comments on commit 7c05536

Please sign in to comment.