-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(core): dedupe project files in rust (#17618)
- Loading branch information
1 parent
a546d5f
commit 00d07b1
Showing
7 changed files
with
197 additions
and
54 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 |
---|---|---|
@@ -1 +1,2 @@ | ||
pub mod glob; | ||
pub mod path; |
This file contains 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,14 @@ | ||
pub trait Normalize { | ||
fn to_normalized_string(&self) -> String; | ||
} | ||
|
||
impl Normalize for std::path::Path { | ||
fn to_normalized_string(&self) -> String { | ||
// convert back-slashes in Windows paths, since the js expects only forward-slash path separators | ||
if cfg!(windows) { | ||
self.display().to_string().replace('\\', "/") | ||
} else { | ||
self.display().to_string() | ||
} | ||
} | ||
} |
This file contains 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 |
---|---|---|
@@ -1,17 +1,103 @@ | ||
use crate::native::parallel_walker::nx_walker; | ||
use crate::native::utils::glob::build_glob_set; | ||
use crate::native::utils::path::Normalize; | ||
use globset::GlobSet; | ||
use std::collections::hash_map::Entry; | ||
use std::collections::HashMap; | ||
use std::path::{Path, PathBuf}; | ||
|
||
#[napi] | ||
/// Get workspace config files based on provided globs | ||
pub fn get_config_files(workspace_root: String, globs: Vec<String>) -> anyhow::Result<Vec<String>> { | ||
let globs = build_glob_set(globs)?; | ||
Ok(nx_walker(workspace_root, move |rec| { | ||
let mut config_paths: Vec<String> = vec![]; | ||
for (path, _) in rec { | ||
if globs.is_match(&path) { | ||
config_paths.push(path.to_owned()); | ||
} | ||
let mut config_paths: HashMap<PathBuf, (PathBuf, Vec<u8>)> = HashMap::new(); | ||
for (path, content) in rec { | ||
insert_config_file_into_map((path, content), &mut config_paths, &globs); | ||
} | ||
config_paths | ||
.into_iter() | ||
.map(|(_, (val, _))| val.to_normalized_string()) | ||
.collect() | ||
})) | ||
} | ||
|
||
pub fn insert_config_file_into_map( | ||
(path, content): (PathBuf, Vec<u8>), | ||
config_paths: &mut HashMap<PathBuf, (PathBuf, Vec<u8>)>, | ||
globs: &GlobSet, | ||
) { | ||
if globs.is_match(&path) { | ||
let parent = path.parent().unwrap_or_else(|| Path::new("")).to_path_buf(); | ||
|
||
let file_name = path | ||
.file_name() | ||
.expect("Config paths always have file names"); | ||
if file_name == "project.json" { | ||
config_paths.insert(parent, (path, content)); | ||
} else if file_name == "package.json" { | ||
match config_paths.entry(parent) { | ||
Entry::Occupied(mut o) => { | ||
if o.get() | ||
.0 | ||
.file_name() | ||
.expect("Config paths always have file names") | ||
!= "project.json" | ||
{ | ||
o.insert((path, content)); | ||
} | ||
} | ||
Entry::Vacant(v) => { | ||
v.insert((path, content)); | ||
} | ||
} | ||
} else { | ||
config_paths.entry(parent).or_insert((path, content)); | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
use std::collections::HashMap; | ||
use std::path::PathBuf; | ||
|
||
#[test] | ||
fn should_insert_config_files_properly() { | ||
let mut config_paths: HashMap<PathBuf, (PathBuf, Vec<u8>)> = HashMap::new(); | ||
let globs = build_glob_set(vec!["**/*".into()]).unwrap(); | ||
|
||
insert_config_file_into_map( | ||
(PathBuf::from("project.json"), vec![]), | ||
&mut config_paths, | ||
&globs, | ||
); | ||
insert_config_file_into_map( | ||
(PathBuf::from("package.json"), vec![]), | ||
&mut config_paths, | ||
&globs, | ||
); | ||
insert_config_file_into_map( | ||
(PathBuf::from("lib1/project.json"), vec![]), | ||
&mut config_paths, | ||
&globs, | ||
); | ||
insert_config_file_into_map( | ||
(PathBuf::from("lib2/package.json"), vec![]), | ||
&mut config_paths, | ||
&globs, | ||
); | ||
|
||
let config_files: Vec<PathBuf> = config_paths | ||
.into_iter() | ||
.map(|(_, (path, _))| path) | ||
.collect(); | ||
|
||
assert!(config_files.contains(&PathBuf::from("project.json"))); | ||
assert!(config_files.contains(&PathBuf::from("lib1/project.json"))); | ||
assert!(config_files.contains(&PathBuf::from("lib2/package.json"))); | ||
|
||
assert!(!config_files.contains(&PathBuf::from("package.json"))); | ||
} | ||
} |
Oops, something went wrong.
00d07b1
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.
Successfully deployed to the following URLs:
nx-dev – ./
nx-dev-nrwl.vercel.app
nx-five.vercel.app
nx.dev
nx-dev-git-master-nrwl.vercel.app