Skip to content

Commit

Permalink
cleanup(core): create root map in js and send to rust instead of full… (
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenPandaz authored Nov 9, 2023
1 parent 6289373 commit 7a62353
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 194 deletions.
10 changes: 2 additions & 8 deletions packages/nx/src/native/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,9 @@ export const enum WorkspaceErrors {
ParseError = 'ParseError',
Generic = 'Generic'
}
export interface ConfigurationParserResult {
projectNodes: Record<string, object>
externalNodes: Record<string, object>
}
export interface NxWorkspaceFiles {
projectFileMap: Record<string, Array<FileData>>
globalFiles: Array<FileData>
projectConfigurations: Record<string, object>
externalNodes: Record<string, object>
}
export class ImportResult {
file: string
Expand Down Expand Up @@ -140,9 +134,9 @@ export class Watcher {
export class WorkspaceContext {
workspaceRoot: string
constructor(workspaceRoot: string)
getWorkspaceFiles(globs: Array<string>, parseConfigurations: (arg0: Array<string>) => ConfigurationParserResult): NxWorkspaceFiles
getWorkspaceFiles(globs: Array<string>, parseConfigurations: (arg0: Array<string>) => Record<string, string>): NxWorkspaceFiles
glob(globs: Array<string>): Array<string>
getProjectConfigurations(globs: Array<string>, parseConfigurations: (arg0: Array<string>) => ConfigurationParserResult): ConfigurationParserResult
getProjectConfigurations(globs: Array<string>, parseConfigurations: (arg0: Array<string>) => Record<string, string>): Record<string, string>
incrementalUpdate(updatedFiles: Array<string>, deletedFiles: Array<string>): Record<string, string>
allFileData(): Array<FileData>
}
96 changes: 5 additions & 91 deletions packages/nx/src/native/tests/workspace_files.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,9 @@ describe('workspace files', () => {
const res = {};
for (const filename of filenames) {
const json = readJsonFile(join(tempDir, filename));
res[json.name] = {
...json,
root: dirname(filename),
};
res[dirname(filename)] = json.name;
}
return {
projectNodes: res,
externalNodes: {},
};
return res;
};
}

Expand Down Expand Up @@ -57,14 +51,9 @@ describe('workspace files', () => {
let globs = ['project.json', '**/project.json', 'libs/*/package.json'];

const context = new WorkspaceContext(fs.tempDir);
let { projectFileMap, projectConfigurations, globalFiles } =
context.getWorkspaceFiles(
globs,
createParseConfigurationsFunction(fs.tempDir)
);

let sortedConfigs = Object.values(projectConfigurations).sort((a, b) =>
a['name'].localeCompare(b['name'])
let { projectFileMap, globalFiles } = context.getWorkspaceFiles(
globs,
createParseConfigurationsFunction(fs.tempDir)
);

expect(projectFileMap).toMatchInlineSnapshot(`
Expand Down Expand Up @@ -121,30 +110,6 @@ describe('workspace files', () => {
],
}
`);
expect(sortedConfigs).toMatchInlineSnapshot(`
[
{
"name": "nested-project",
"root": "libs/nested/project",
},
{
"name": "package-project",
"root": "libs/package-project",
},
{
"name": "project1",
"root": "libs/project1",
},
{
"name": "project2",
"root": "libs/project2",
},
{
"name": "project3",
"root": "libs/project3",
},
]
`);
expect(globalFiles).toMatchInlineSnapshot(`
[
{
Expand Down Expand Up @@ -215,57 +180,6 @@ describe('workspace files', () => {
`);
});

it('should dedupe configuration files', async () => {
const fs = new TempFs('workspace-files');
const nxJson: NxJsonConfiguration = {};
await fs.createFiles({
'./nx.json': JSON.stringify(nxJson),
'./package.json': JSON.stringify({
name: 'repo-name',
version: '0.0.0',
dependencies: {},
}),
'./project.json': JSON.stringify({
name: 'repo-name',
}),
'./libs/project1/project.json': JSON.stringify({
name: 'project1',
}),
'./libs/project1/package.json': JSON.stringify({
name: 'project1',
}),
'./libs/project1/index.js': '',
});

const context = new WorkspaceContext(fs.tempDir);
let globs = ['project.json', '**/project.json', '**/package.json'];

let nodes = context.getProjectConfigurations(globs, (filenames) => {
const res = {};
for (const filename of filenames) {
const json = readJsonFile(join(fs.tempDir, filename));
res[json.name] = {
...json,
root: dirname(filename),
};
}
return {
externalNodes: {},
projectNodes: res,
};
});
expect(nodes.projectNodes).toEqual({
project1: {
name: 'project1',
root: 'libs/project1',
},
'repo-name': expect.objectContaining({
name: 'repo-name',
root: '.',
}),
});
});

// describe('errors', () => {
// it('it should infer names of configuration files without a name', async () => {
// const fs = new TempFs('workspace-files');
Expand Down
11 changes: 5 additions & 6 deletions packages/nx/src/native/workspace/config_files.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::native::utils::glob::build_glob_set;
use crate::native::utils::path::Normalize;
use crate::native::workspace::types::ConfigurationParserResult;
use std::collections::HashMap;

use crate::native::workspace::errors::{InternalWorkspaceErrors, WorkspaceErrors};
use rayon::prelude::*;
Expand All @@ -12,7 +12,7 @@ pub(super) fn glob_files(
files: Option<&[(PathBuf, String)]>,
) -> napi::Result<Vec<String>, WorkspaceErrors> {
let Some(files) = files else {
return Ok(Default::default())
return Ok(Default::default());
};

let globs =
Expand All @@ -29,12 +29,11 @@ pub(super) fn get_project_configurations<ConfigurationParser>(
globs: Vec<String>,
files: Option<&[(PathBuf, String)]>,
parse_configurations: ConfigurationParser,
) -> napi::Result<ConfigurationParserResult>
) -> napi::Result<HashMap<String, String>>
where
ConfigurationParser: Fn(Vec<String>) -> napi::Result<ConfigurationParserResult>,
ConfigurationParser: Fn(Vec<String>) -> napi::Result<HashMap<String, String>>,
{
let config_paths =
glob_files(globs, files).map_err(anyhow::Error::from)?;
let config_paths = glob_files(globs, files).map_err(anyhow::Error::from)?;

parse_configurations(config_paths)
}
21 changes: 8 additions & 13 deletions packages/nx/src/native/workspace/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ use crate::native::workspace::errors::WorkspaceErrors;
use crate::native::workspace::workspace_files::NxWorkspaceFiles;
use crate::native::workspace::{config_files, workspace_files};

use crate::native::workspace::types::ConfigurationParserResult;

#[napi]
pub struct WorkspaceContext {
pub workspace_root: String,
Expand Down Expand Up @@ -70,7 +68,7 @@ impl FilesWorker {
pub fn get_files(&self) -> Option<MutexGuard<'_, RawMutex, Files>> {
let Some(files_sync) = &self.0 else {
trace!("there were no files because the workspace root did not exist");
return None
return None;
};

let (files_lock, cvar) = &files_sync.deref();
Expand All @@ -93,7 +91,7 @@ impl FilesWorker {
) -> HashMap<String, String> {
let Some(files_sync) = &self.0 else {
trace!("there were no files because the workspace root did not exist");
return HashMap::new();
return HashMap::new();
};

let (files_lock, _) = &files_sync.deref();
Expand All @@ -108,8 +106,8 @@ impl FilesWorker {
.par_iter()
.filter_map(|path| {
let full_path = workspace_root_path.join(path);
let Ok( content ) = std::fs::read(full_path) else {
trace!( "could not read file: ?full_path");
let Ok(content) = std::fs::read(full_path) else {
trace!("could not read file: ?full_path");
return None;
};
Some((path.to_string(), xxh3::xxh3_64(&content).to_string()))
Expand Down Expand Up @@ -153,7 +151,7 @@ impl WorkspaceContext {
parse_configurations: ConfigurationParser,
) -> napi::Result<NxWorkspaceFiles, WorkspaceErrors>
where
ConfigurationParser: Fn(Vec<String>) -> napi::Result<ConfigurationParserResult>,
ConfigurationParser: Fn(Vec<String>) -> napi::Result<HashMap<String, String>>,
{
workspace_files::get_files(
globs,
Expand All @@ -166,10 +164,7 @@ impl WorkspaceContext {
}

#[napi]
pub fn glob(
&self,
globs: Vec<String>,
) -> napi::Result<Vec<String>, WorkspaceErrors> {
pub fn glob(&self, globs: Vec<String>) -> napi::Result<Vec<String>, WorkspaceErrors> {
config_files::glob_files(
globs,
self.files_worker
Expand All @@ -184,9 +179,9 @@ impl WorkspaceContext {
&self,
globs: Vec<String>,
parse_configurations: ConfigurationParser,
) -> napi::Result<ConfigurationParserResult>
) -> napi::Result<HashMap<String, String>>
where
ConfigurationParser: Fn(Vec<String>) -> napi::Result<ConfigurationParserResult>,
ConfigurationParser: Fn(Vec<String>) -> napi::Result<HashMap<String, String>>,
{
config_files::get_project_configurations(
globs,
Expand Down
10 changes: 0 additions & 10 deletions packages/nx/src/native/workspace/types.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
use std::collections::HashMap;

use napi::JsObject;

#[derive(Debug, Eq, PartialEq)]
pub enum FileLocation {
Global,
Project(String),
}

#[napi(object)]
pub struct ConfigurationParserResult {
pub project_nodes: HashMap<String, JsObject>,
pub external_nodes: HashMap<String, JsObject>,
}
31 changes: 10 additions & 21 deletions packages/nx/src/native/workspace/workspace_files.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use napi::JsObject;
use std::collections::HashMap;
use std::path::{Path, PathBuf};

Expand All @@ -9,15 +8,13 @@ use crate::native::types::FileData;
use crate::native::utils::path::Normalize;
use crate::native::workspace::config_files;
use crate::native::workspace::errors::{InternalWorkspaceErrors, WorkspaceErrors};
use crate::native::workspace::types::{ConfigurationParserResult, FileLocation};
use crate::native::workspace::types::FileLocation;

#[napi(object)]
#[derive(Default)]
pub struct NxWorkspaceFiles {
pub project_file_map: HashMap<String, Vec<FileData>>,
pub global_files: Vec<FileData>,
pub project_configurations: HashMap<String, JsObject>,
pub external_nodes: HashMap<String, JsObject>,
}

pub(super) fn get_files<ConfigurationParser>(
Expand All @@ -26,18 +23,17 @@ pub(super) fn get_files<ConfigurationParser>(
file_data: Option<&[(PathBuf, String)]>,
) -> napi::Result<NxWorkspaceFiles, WorkspaceErrors>
where
ConfigurationParser: Fn(Vec<String>) -> napi::Result<ConfigurationParserResult>,
ConfigurationParser: Fn(Vec<String>) -> napi::Result<HashMap<String, String>>,
{
let Some(file_data) = file_data else {
return Ok(Default::default())
return Ok(Default::default());
};

trace!("{globs:?}");
let parsed_graph_nodes =
let root_map = transform_root_map(
config_files::get_project_configurations(globs, Some(file_data), parse_configurations)
.map_err(|e| InternalWorkspaceErrors::ParseError(e.to_string()))?;

let root_map = create_root_map(&parsed_graph_nodes.project_nodes);
.map_err(|e| InternalWorkspaceErrors::ParseError(e.to_string()))?,
);

trace!(?root_map);

Expand Down Expand Up @@ -89,19 +85,12 @@ where
Ok(NxWorkspaceFiles {
project_file_map,
global_files,
external_nodes: parsed_graph_nodes.external_nodes,
project_configurations: parsed_graph_nodes.project_nodes,
})
}

fn create_root_map(
project_configurations: &HashMap<String, JsObject>,
) -> hashbrown::HashMap<PathBuf, String> {
project_configurations
.iter()
.map(|(project_name, project_configuration)| {
let root: String = project_configuration.get("root").unwrap().unwrap();
(PathBuf::from(root), project_name.clone())
})
fn transform_root_map(root_map: HashMap<String, String>) -> hashbrown::HashMap<PathBuf, String> {
root_map
.into_iter()
.map(|(project_root, project_name)| (PathBuf::from(project_root), project_name))
.collect()
}
11 changes: 11 additions & 0 deletions packages/nx/src/project-graph/utils/project-configuration-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export function buildProjectsConfigurationsFromProjectPathsAndPlugins(
): {
projects: Record<string, ProjectConfiguration>;
externalNodes: Record<string, ProjectGraphExternalNode>;
rootMap: Record<string, string>;
} {
const projectRootMap: Map<string, ProjectConfiguration> = new Map();
const externalNodes: Record<string, ProjectGraphExternalNode> = {};
Expand Down Expand Up @@ -133,9 +134,12 @@ export function buildProjectsConfigurationsFromProjectPathsAndPlugins(
}
}

const rootMap = createRootMap(projectRootMap);

return {
projects: readProjectConfigurationsFromRootMap(projectRootMap),
externalNodes,
rootMap,
};
}

Expand Down Expand Up @@ -290,3 +294,10 @@ export function readTargetDefaultsForTarget(
return targetDefaults?.[targetName];
}
}
function createRootMap(projectRootMap: Map<string, ProjectConfiguration>) {
const map: Record<string, string> = {};
for (const [projectRoot, { name: projectName }] of projectRootMap) {
map[projectRoot] = projectName;
}
return map;
}
Loading

0 comments on commit 7a62353

Please sign in to comment.