Skip to content

Commit

Permalink
Merge pull request #76 from Grinkers/master
Browse files Browse the repository at this point in the history
Add `--avoid-proc-macros`.
  • Loading branch information
dalance authored Dec 5, 2024
2 parents 74da4d9 + db12055 commit 5e6d9de
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
34 changes: 28 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,24 @@ fn normalize(license_string: &str) -> String {
list.join(" OR ")
}

fn get_node_name_filter(metadata: &Metadata, opt: &GetDependenciesOpt) -> Result<HashSet<String>> {
fn get_proc_macro_node_names(metadata: &Metadata, opt: &GetDependenciesOpt) -> HashSet<String> {
let mut proc_macros = HashSet::new();
if opt.avoid_proc_macros {
for packages in &metadata.packages {
for target in &packages.targets {
if target.crate_types.contains(&String::from("proc-macro")) {
proc_macros.insert(target.name.clone());
for package in &packages.dependencies {
proc_macros.insert(package.name.clone());
}
}
}
}
}
proc_macros
}

fn get_node_name_filter(metadata: &Metadata, opt: &GetDependenciesOpt) -> HashSet<String> {
let mut filter = HashSet::new();

let roots = if let Some(root) = metadata.root_package() {
Expand All @@ -32,7 +49,7 @@ fn get_node_name_filter(metadata: &Metadata, opt: &GetDependenciesOpt) -> Result
for root in roots {
filter.insert(root.name.clone());
}
return Ok(filter);
return filter;
}

if opt.direct_deps_only {
Expand All @@ -43,7 +60,7 @@ fn get_node_name_filter(metadata: &Metadata, opt: &GetDependenciesOpt) -> Result
}
}
}
Ok(filter)
filter
}

#[derive(Debug, Serialize, Clone, Hash, Ord, PartialOrd, Eq, PartialEq)]
Expand Down Expand Up @@ -98,7 +115,9 @@ struct GitlabLicense {

impl GitlabLicense {
fn parse_licenses(dependency: &DependencyDetails) -> Result<HashSet<Self>> {
let Some(license) = &dependency.license else {return Ok(HashSet::new())};
let Some(license) = &dependency.license else {
return Ok(HashSet::new());
};
let expression = spdx::Expression::parse_mode(license, spdx::ParseMode::LAX)?;
Ok(expression
.requirements()
Expand Down Expand Up @@ -153,6 +172,7 @@ impl TryFrom<&[DependencyDetails]> for GitlabLicenseScanningReport {
pub struct GetDependenciesOpt {
pub avoid_dev_deps: bool,
pub avoid_build_deps: bool,
pub avoid_proc_macros: bool,
pub direct_deps_only: bool,
pub root_only: bool,
}
Expand All @@ -163,7 +183,8 @@ pub fn get_dependencies_from_cargo_lock(
) -> Result<Vec<DependencyDetails>> {
let metadata = metadata_command.exec()?;

let filter = get_node_name_filter(&metadata, &opt)?;
let node_name_filter = get_node_name_filter(&metadata, &opt);
let proc_macro_exclusions = get_proc_macro_node_names(&metadata, &opt);

let connected = {
let resolve = metadata.resolve.as_ref().expect("missing `resolve`");
Expand Down Expand Up @@ -218,7 +239,8 @@ pub fn get_dependencies_from_cargo_lock(
.packages
.iter()
.filter(|p| connected.contains(&p.id))
.filter(|p| filter.is_empty() || filter.contains(&p.name))
.filter(|p| node_name_filter.is_empty() || node_name_filter.contains(&p.name))
.filter(|p| !proc_macro_exclusions.contains(&p.name))
.map(DependencyDetails::new)
.collect::<Vec<_>>();
detailed_dependencies.sort_unstable();
Expand Down
5 changes: 5 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ struct Opt {
/// Exclude build dependencies
avoid_build_deps: bool,

#[clap(long, display_order(0))]
/// Exclude `proc_macros` dependencies
avoid_proc_macros: bool,

#[clap(long = "features", value_name = "FEATURE", display_order(0))]
/// Space-separated list of features to activate.
features: Option<Vec<String>>,
Expand Down Expand Up @@ -246,6 +250,7 @@ fn run() -> Result<()> {
let get_opts = GetDependenciesOpt {
avoid_dev_deps: opt.avoid_dev_deps,
avoid_build_deps: opt.avoid_build_deps,
avoid_proc_macros: opt.avoid_proc_macros,
direct_deps_only: opt.direct_deps_only,
root_only: opt.root_only,
};
Expand Down

0 comments on commit 5e6d9de

Please sign in to comment.