This repository was archived by the owner on Sep 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
feat: add subcommand to lint depenendency versions for consistent values #152
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
use std::collections::HashMap; | ||
use std::error::Error; | ||
|
||
use crate::opts; | ||
|
||
use crate::configuration_file::ConfigurationFile; | ||
use crate::monorepo_manifest::MonorepoManifest; | ||
|
||
pub fn handle_subcommand(opts: opts::Lint) -> Result<(), Box<dyn Error>> { | ||
match opts.subcommand { | ||
opts::ClapLintSubCommand::DependencyVersion(args) => lint_dependency_version(&args), | ||
} | ||
} | ||
|
||
fn most_common_dependency_version( | ||
package_manifests_by_dependency_version: &HashMap<String, Vec<String>>, | ||
) -> Option<String> { | ||
package_manifests_by_dependency_version | ||
.iter() | ||
// Map each dependecy version to its number of occurrences | ||
.map(|(dependency_version, package_manifests)| { | ||
(dependency_version, package_manifests.len()) | ||
}) | ||
// Take the max by value | ||
.max_by(|a, b| a.1.cmp(&b.1)) | ||
.map(|(k, _v)| k.to_owned()) | ||
} | ||
|
||
fn lint_dependency_version(opts: &opts::DependencyVersion) -> Result<(), Box<dyn Error>> { | ||
let opts::DependencyVersion { root, dependencies } = opts; | ||
|
||
let lerna_manifest = MonorepoManifest::from_directory(&root)?; | ||
let package_manifest_by_package_name = lerna_manifest.package_manifests_by_package_name()?; | ||
|
||
let mut is_exit_success = true; | ||
|
||
for dependency in dependencies { | ||
let package_manifests_by_dependency_version: HashMap<String, Vec<String>> = | ||
package_manifest_by_package_name | ||
.values() | ||
.filter_map(|package_manifest| { | ||
package_manifest | ||
.get_dependency_version(&dependency) | ||
.map(|dependency_version| (package_manifest, dependency_version)) | ||
}) | ||
.fold( | ||
HashMap::new(), | ||
|mut accumulator, (package_manifest, dependency_version)| { | ||
let packages_using_this_dependency_version = | ||
accumulator.entry(dependency_version).or_default(); | ||
packages_using_this_dependency_version.push( | ||
package_manifest | ||
.path() | ||
.into_os_string() | ||
.into_string() | ||
.expect("Path not UTF-8 encoded"), | ||
); | ||
accumulator | ||
}, | ||
); | ||
|
||
if package_manifests_by_dependency_version.keys().len() <= 1 { | ||
return Ok(()); | ||
} | ||
|
||
let expected_version_number = | ||
most_common_dependency_version(&package_manifests_by_dependency_version) | ||
.expect("Expected dependency to be used in at least one package"); | ||
|
||
println!("Linting versions of dependency \"{}\"", &dependency); | ||
|
||
package_manifests_by_dependency_version | ||
.into_iter() | ||
// filter out the packages using the expected dependency version | ||
.filter(|(dependency_version, _package_manifests)| { | ||
!dependency_version.eq(&expected_version_number) | ||
}) | ||
.for_each(|(dependency_version, package_manifests)| { | ||
package_manifests.into_iter().for_each(|package_manifest| { | ||
println!( | ||
"\tIn {}, expected version {} but found version {}", | ||
&package_manifest, &expected_version_number, dependency_version | ||
); | ||
}); | ||
}); | ||
|
||
is_exit_success = false; | ||
} | ||
|
||
if is_exit_success { | ||
return Ok(()); | ||
} else { | ||
return Err("Found unexpected dependency versions".into()); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,8 @@ pub enum ClapSubCommand { | |
MakeDepend(MakeDepend), | ||
#[clap(about = "Query properties of the current monorepo state")] | ||
Query(Query), | ||
#[clap(about = "Lint internal packages for consistent use of external dependency versions")] | ||
Lint(Lint), | ||
} | ||
|
||
#[derive(Parser)] | ||
|
@@ -59,7 +61,6 @@ pub struct MakeDepend { | |
|
||
#[derive(Parser)] | ||
pub struct Query { | ||
/// internal-dependencies | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pretty sure this doesn't appear anywhere in the docstring |
||
#[clap(subcommand)] | ||
pub subcommand: ClapQuerySubCommand, | ||
} | ||
|
@@ -72,6 +73,12 @@ pub enum ClapQuerySubCommand { | |
InternalDependencies(InternalDependencies), | ||
} | ||
|
||
#[derive(ArgEnum, Clone)] | ||
pub enum InternalDependenciesFormat { | ||
Name, | ||
Path, | ||
} | ||
|
||
#[derive(Parser)] | ||
pub struct InternalDependencies { | ||
/// Path to monorepo root | ||
|
@@ -82,8 +89,24 @@ pub struct InternalDependencies { | |
pub format: InternalDependenciesFormat, | ||
} | ||
|
||
#[derive(ArgEnum, Clone)] | ||
pub enum InternalDependenciesFormat { | ||
Name, | ||
Path, | ||
#[derive(Parser)] | ||
pub struct Lint { | ||
#[clap(subcommand)] | ||
pub subcommand: ClapLintSubCommand, | ||
} | ||
|
||
#[derive(Parser)] | ||
pub enum ClapLintSubCommand { | ||
#[clap(about = "Lint the used versions of an external dependency for consistency")] | ||
DependencyVersion(DependencyVersion), | ||
} | ||
|
||
#[derive(Parser)] | ||
pub struct DependencyVersion { | ||
/// Path to monorepo root | ||
#[clap(short, long, default_value = ".")] | ||
pub root: PathBuf, | ||
/// External dependency to lint for consistency of version used | ||
#[clap(short, long = "dependency")] | ||
pub dependencies: Vec<String>, | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -88,6 +88,39 @@ impl AsRef<PackageManifest> for PackageManifest { | |
} | ||
|
||
impl PackageManifest { | ||
// Get the dependency | ||
pub fn get_dependency_version<S>(&self, dependency: S) -> Option<String> | ||
where | ||
S: AsRef<str>, | ||
{ | ||
static DEPENDENCY_GROUPS: &[&str] = &[ | ||
"dependencies", | ||
"devDependencies", | ||
"optionalDependencies", | ||
"peerDependencies", | ||
]; | ||
|
||
DEPENDENCY_GROUPS | ||
.iter() | ||
// only iterate over the objects corresponding to each dependency group | ||
.filter_map(|dependency_group| { | ||
self.contents | ||
.extra_fields | ||
.get(dependency_group)? | ||
.as_object() | ||
}) | ||
// get the target dependency version, if exists | ||
.filter_map(|dependency_group_value| { | ||
dependency_group_value | ||
.get(dependency.as_ref()) | ||
// DISCUSS(Grayson): neither clone nor cloned work here, only to_owned | ||
// How do I resolve this with https://github.com/typescript-tools/rust-implementation/pull/141#discussion_r845294514 ? | ||
.and_then(|version_value| version_value.as_str().map(|a| a.to_owned())) | ||
}) | ||
.take(1) | ||
.next() | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Got a q for @ocornoc Does clone not work here because we need to change types, and clone can only preserve type? |
||
|
||
pub fn internal_dependencies_iter<'a, T>( | ||
&'a self, | ||
package_manifests_by_package_name: &'a HashMap<String, &'a T>, | ||
|
@@ -106,7 +139,10 @@ impl PackageManifest { | |
.iter() | ||
// only iterate over the objects corresponding to each dependency group | ||
.filter_map(|dependency_group| { | ||
self.contents.extra_fields.get(dependency_group)?.as_object() | ||
self.contents | ||
.extra_fields | ||
.get(dependency_group)? | ||
.as_object() | ||
}) | ||
// get all dependency names from all groups | ||
.flat_map(|dependency_group_value| dependency_group_value.keys()) | ||
|
@@ -132,8 +168,8 @@ impl PackageManifest { | |
while let Some(current_manifest) = to_visit_package_manifests.pop_front() { | ||
seen_package_names.insert(¤t_manifest.contents.name); | ||
|
||
for dependency in current_manifest | ||
.internal_dependencies_iter(package_manifest_by_package_name) | ||
for dependency in | ||
current_manifest.internal_dependencies_iter(package_manifest_by_package_name) | ||
{ | ||
internal_dependencies.insert(dependency.contents.name.to_owned()); | ||
if !seen_package_names.contains(&dependency.contents.name) { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ocornoc if I wanted to semantically type String to something narrower like PackageManifestPath or DependencyVersion, what would that look like?