diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-05-17 02:53:53 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-05-17 02:53:53 +0900 |
| commit | a1c7e6908a26e10f6e1f23a51721664b5e2d838d (patch) | |
| tree | c575c76f1b43359ed74913da4c6a2636643f1ba0 /crates/shirabe/src/command/licenses_command.rs | |
| parent | 7f606f36fef0c0467c3c0db3d0da33af486dae8a (diff) | |
| download | php-shirabe-a1c7e6908a26e10f6e1f23a51721664b5e2d838d.tar.gz php-shirabe-a1c7e6908a26e10f6e1f23a51721664b5e2d838d.tar.zst php-shirabe-a1c7e6908a26e10f6e1f23a51721664b5e2d838d.zip | |
chore(style): cargo fmt
Diffstat (limited to 'crates/shirabe/src/command/licenses_command.rs')
| -rw-r--r-- | crates/shirabe/src/command/licenses_command.rs | 156 |
1 files changed, 127 insertions, 29 deletions
diff --git a/crates/shirabe/src/command/licenses_command.rs b/crates/shirabe/src/command/licenses_command.rs index 84cbff6..ddb7b32 100644 --- a/crates/shirabe/src/command/licenses_command.rs +++ b/crates/shirabe/src/command/licenses_command.rs @@ -33,16 +33,41 @@ impl LicensesCommand { .set_name("licenses") .set_description("Shows information about licenses of dependencies") .set_definition(vec![ - InputOption::new("format", Some(PhpMixed::String("f".to_string())), Some(InputOption::VALUE_REQUIRED), "Format of the output: text, json or summary", Some(PhpMixed::String("text".to_string())), vec!["text".to_string(), "json".to_string(), "summary".to_string()]), - InputOption::new("no-dev", None, Some(InputOption::VALUE_NONE), "Disables search in require-dev packages.", None, vec![]), - InputOption::new("locked", None, Some(InputOption::VALUE_NONE), "Shows licenses from the lock file instead of installed packages.", None, vec![]), + InputOption::new( + "format", + Some(PhpMixed::String("f".to_string())), + Some(InputOption::VALUE_REQUIRED), + "Format of the output: text, json or summary", + Some(PhpMixed::String("text".to_string())), + vec![ + "text".to_string(), + "json".to_string(), + "summary".to_string(), + ], + ), + InputOption::new( + "no-dev", + None, + Some(InputOption::VALUE_NONE), + "Disables search in require-dev packages.", + None, + vec![], + ), + InputOption::new( + "locked", + None, + Some(InputOption::VALUE_NONE), + "Shows licenses from the lock file instead of installed packages.", + None, + vec![], + ), ]) .set_help( "The license command displays detailed information about the licenses of\n\ the installed dependencies.\n\n\ Use --locked to show licenses from composer.lock instead of what's currently\n\ installed in the vendor directory.\n\n\ - Read more at https://getcomposer.org/doc/03-cli.md#licenses" + Read more at https://getcomposer.org/doc/03-cli.md#licenses", ); } @@ -50,8 +75,11 @@ impl LicensesCommand { let composer = self.inner.require_composer()?; // TODO(plugin): dispatch COMMAND event for plugin hooks - let command_event = CommandEvent::new(PluginEvents::COMMAND, "licenses".to_string(), input, output); - composer.get_event_dispatcher().dispatch(command_event.get_name(), &command_event); + let command_event = + CommandEvent::new(PluginEvents::COMMAND, "licenses".to_string(), input, output); + composer + .get_event_dispatcher() + .dispatch(command_event.get_name(), &command_event); let root = composer.get_package(); @@ -78,7 +106,11 @@ impl LicensesCommand { let packages = PackageSorter::sort_packages_alphabetically(packages); let io = self.inner.get_io(); - let format = input.get_option("format").as_string().unwrap_or("text").to_string(); + let format = input + .get_option("format") + .as_string() + .unwrap_or("text") + .to_string(); match format.as_str() { "text" => { let root_licenses = root.get_license(); @@ -87,23 +119,39 @@ impl LicensesCommand { } else { root_licenses.join(", ") }; - io.write(&format!("Name: <comment>{}</comment>", root.get_pretty_name())); - io.write(&format!("Version: <comment>{}</comment>", root.get_full_pretty_version())); + io.write(&format!( + "Name: <comment>{}</comment>", + root.get_pretty_name() + )); + io.write(&format!( + "Version: <comment>{}</comment>", + root.get_full_pretty_version() + )); io.write(&format!("Licenses: <comment>{}</comment>", licenses_str)); io.write("Dependencies:"); io.write(""); let mut table = Table::new(output); table.set_style("compact"); - table.set_headers(vec!["Name".to_string(), "Version".to_string(), "Licenses".to_string()]); + table.set_headers(vec![ + "Name".to_string(), + "Version".to_string(), + "Licenses".to_string(), + ]); for package in &packages { let link = PackageInfo::get_view_source_or_homepage_url(package.as_ref()); let name = if let Some(link) = link { - format!("<href={}>{}</>", OutputFormatter::escape(&link), package.get_pretty_name()) + format!( + "<href={}>{}</>", + OutputFormatter::escape(&link), + package.get_pretty_name() + ) } else { package.get_pretty_name().to_string() }; - let pkg_licenses = if let Some(complete_pkg) = (package.as_any() as &dyn Any).downcast_ref::<CompletePackage>() { + let pkg_licenses = if let Some(complete_pkg) = + (package.as_any() as &dyn Any).downcast_ref::<CompletePackage>() + { complete_pkg.get_license() } else { vec![] @@ -113,40 +161,85 @@ impl LicensesCommand { } else { pkg_licenses.join(", ") }; - table.add_row(vec![name, package.get_full_pretty_version().to_string(), licenses_str]); + table.add_row(vec![ + name, + package.get_full_pretty_version().to_string(), + licenses_str, + ]); } table.render(); } "json" => { - let mut dependencies: IndexMap<String, IndexMap<String, PhpMixed>> = IndexMap::new(); + let mut dependencies: IndexMap<String, IndexMap<String, PhpMixed>> = + IndexMap::new(); for package in &packages { - let pkg_licenses = if let Some(complete_pkg) = (package.as_any() as &dyn Any).downcast_ref::<CompletePackage>() { + let pkg_licenses = if let Some(complete_pkg) = + (package.as_any() as &dyn Any).downcast_ref::<CompletePackage>() + { complete_pkg.get_license() } else { vec![] }; let mut dep_info: IndexMap<String, PhpMixed> = IndexMap::new(); - dep_info.insert("version".to_string(), PhpMixed::String(package.get_full_pretty_version().to_string())); - dep_info.insert("license".to_string(), PhpMixed::List(pkg_licenses.into_iter().map(|l| Box::new(PhpMixed::String(l))).collect())); + dep_info.insert( + "version".to_string(), + PhpMixed::String(package.get_full_pretty_version().to_string()), + ); + dep_info.insert( + "license".to_string(), + PhpMixed::List( + pkg_licenses + .into_iter() + .map(|l| Box::new(PhpMixed::String(l))) + .collect(), + ), + ); dependencies.insert(package.get_pretty_name().to_string(), dep_info); } let mut output_map: IndexMap<String, PhpMixed> = IndexMap::new(); - output_map.insert("name".to_string(), PhpMixed::String(root.get_pretty_name().to_string())); - output_map.insert("version".to_string(), PhpMixed::String(root.get_full_pretty_version().to_string())); + output_map.insert( + "name".to_string(), + PhpMixed::String(root.get_pretty_name().to_string()), + ); + output_map.insert( + "version".to_string(), + PhpMixed::String(root.get_full_pretty_version().to_string()), + ); let root_licenses = root.get_license(); - output_map.insert("license".to_string(), PhpMixed::List(root_licenses.into_iter().map(|l| Box::new(PhpMixed::String(l))).collect())); - output_map.insert("dependencies".to_string(), PhpMixed::Array( - dependencies.into_iter().map(|(k, v)| (k, Box::new(PhpMixed::Array( - v.into_iter().map(|(k2, v2)| (k2, Box::new(v2))).collect() - )))).collect() - )); + output_map.insert( + "license".to_string(), + PhpMixed::List( + root_licenses + .into_iter() + .map(|l| Box::new(PhpMixed::String(l))) + .collect(), + ), + ); + output_map.insert( + "dependencies".to_string(), + PhpMixed::Array( + dependencies + .into_iter() + .map(|(k, v)| { + ( + k, + Box::new(PhpMixed::Array( + v.into_iter().map(|(k2, v2)| (k2, Box::new(v2))).collect(), + )), + ) + }) + .collect(), + ), + ); io.write(&JsonFile::encode(&output_map)); } "summary" => { let mut used_licenses: IndexMap<String, i64> = IndexMap::new(); for package in &packages { - let mut licenses = if let Some(complete_pkg) = (package.as_any() as &dyn Any).downcast_ref::<CompletePackage>() { + let mut licenses = if let Some(complete_pkg) = + (package.as_any() as &dyn Any).downcast_ref::<CompletePackage>() + { complete_pkg.get_license() } else { vec![] @@ -162,7 +255,8 @@ impl LicensesCommand { let mut entries: Vec<(String, i64)> = used_licenses.into_iter().collect(); entries.sort_by(|a, b| b.1.cmp(&a.1)); - let rows: Vec<Vec<String>> = entries.iter() + let rows: Vec<Vec<String>> = entries + .iter() .map(|(license, count)| vec![license.clone(), count.to_string()]) .collect(); @@ -174,9 +268,13 @@ impl LicensesCommand { } _ => { return Err(RuntimeException { - message: format!("Unsupported format \"{}\". See help for supported formats.", format), + message: format!( + "Unsupported format \"{}\". See help for supported formats.", + format + ), code: 0, - }.into()); + } + .into()); } } |
