Skip to content

Commit

Permalink
feat(core): create function to transform project graph between js and…
Browse files Browse the repository at this point in the history
… rust
  • Loading branch information
FrozenPandaz committed Aug 11, 2023
1 parent a7cf272 commit aed6757
Show file tree
Hide file tree
Showing 5 changed files with 207 additions and 0 deletions.
46 changes: 46 additions & 0 deletions packages/nx/src/native/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,52 @@ export function hashArray(input: Array<string>): string
export function hashFile(file: string): FileData | null
export function hashFiles(workspaceRoot: string): Record<string, string>
export function findImports(projectFileMap: Record<string, Array<string>>): Array<ImportResult>
export interface ExternalNodeData {
version: string
hash: string
}
export interface ExternalNode {
version: string
hash: string
}
export interface InputsInput {
input: string
dependencies?: boolean
}
export interface FileSetInput {
fileset: string
}
export interface RuntimeInput {
runtime: string
}
export interface EnvironmentInput {
env: string
}
export interface ExternalDependenciesInput {
externalDependencies: Array<string>
}
export interface DepsOutputsInput {
dependentTasksOutputFiles: string
transitive?: boolean
}
export interface ProjectsInput {
projects: string | Array<string>
}
export interface Target {
executor: string
inputs?: Array<InputsInput | string | FileSetInput | RuntimeInput | EnvironmentInput | ExternalDependenciesInput | DepsOutputsInput | ProjectsInput>
outputs?: Array<string>
}
export interface Project {
root: string
namedInputs?: Record<string, Array<InputsInput | string | FileSetInput | RuntimeInput | EnvironmentInput | ExternalDependenciesInput | DepsOutputsInput | ProjectsInput>>
targets: Record<string, Target>
}
export interface ProjectGraph {
nodes: Record<string, Project>
dependencies: Record<string, Array<string>>
externalNodes: Record<string, ExternalNode>
}
export interface FileData {
file: string
hash: string
Expand Down
1 change: 1 addition & 0 deletions packages/nx/src/native/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pub mod cache;
pub mod hasher;
mod logger;
pub mod plugins;
pub mod project_graph;
mod types;
mod utils;
mod walker;
Expand Down
1 change: 1 addition & 0 deletions packages/nx/src/native/project_graph/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod types;
102 changes: 102 additions & 0 deletions packages/nx/src/native/project_graph/types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
use napi::bindgen_prelude::Either8;
use napi::Either;
use std::collections::HashMap;

#[napi(object)]
pub struct ExternalNodeData {
pub version: String,
pub hash: String,
}

#[napi(object)]
pub struct ExternalNode {
pub version: String,
pub hash: String,
}

#[napi(object)]
pub struct InputsInput {
pub input: String,
pub dependencies: Option<bool>,
}

#[napi(object)]
pub struct FileSetInput {
pub fileset: String,
}

#[napi(object)]
pub struct RuntimeInput {
pub runtime: String,
}

#[napi(object)]
pub struct EnvironmentInput {
pub env: String,
}

#[napi(object)]
pub struct ExternalDependenciesInput {
pub external_dependencies: Vec<String>,
}

#[napi(object)]
pub struct DepsOutputsInput {
pub dependent_tasks_output_files: String,
pub transitive: Option<bool>,
}

#[napi(object)]
pub struct ProjectsInput {
pub projects: Either<String, Vec<String>>,
}

#[napi(object)]
pub struct Target {
pub executor: String,
pub inputs: Option<
Vec<
Either8<
InputsInput,
String,
FileSetInput,
RuntimeInput,
EnvironmentInput,
ExternalDependenciesInput,
DepsOutputsInput,
ProjectsInput,
>,
>,
>,
pub outputs: Option<Vec<String>>,
}

#[napi(object)]
pub struct Project {
pub root: String,
pub named_inputs: Option<
HashMap<
String,
Vec<
Either8<
InputsInput,
String,
FileSetInput,
RuntimeInput,
EnvironmentInput,
ExternalDependenciesInput,
DepsOutputsInput,
ProjectsInput,
>,
>,
>,
>,
pub targets: HashMap<String, Target>,
}

#[napi(object)]
pub struct ProjectGraph {
pub nodes: HashMap<String, Project>,
pub dependencies: HashMap<String, Vec<String>>,
pub external_nodes: HashMap<String, ExternalNode>,
}
57 changes: 57 additions & 0 deletions packages/nx/src/native/transform-project-graph.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { ProjectGraph } from '../config/project-graph';
import {
ExternalNode,
Project,
Target,
ProjectGraph as RustProjectGraph,
} from './index';

export function transformProjectGraphForRust(
graph: ProjectGraph
): RustProjectGraph {
const dependencies: Record<string, string[]> = {};
const nodes: Record<string, Project> = {};
const externalNodes: Record<string, ExternalNode> = {};
for (const [projectName, projectNode] of Object.entries(graph.nodes)) {
const targets: Record<string, Target> = {};
for (const [targetName, targetConfig] of Object.entries(
projectNode.data.targets
)) {
targets[targetName] = {
executor: targetConfig.executor,
inputs: targetConfig.inputs,
outputs: targetConfig.outputs,
};
}
nodes[projectName] = {
root: projectNode.data.root,
targets,
};
if (graph.dependencies[projectName]) {
dependencies[projectName] = [];
for (const dep of graph.dependencies[projectName]) {
dependencies[projectName].push(dep.target);
}
}
}
for (const [projectName, externalNode] of Object.entries(
graph.externalNodes
)) {
externalNodes[projectName] = {
hash: externalNode.data.hash,
version: externalNode.data.version,
};
if (graph.dependencies[projectName]) {
dependencies[projectName] = [];
for (const dep of graph.dependencies[projectName]) {
dependencies[projectName].push(dep.target);
}
}
}

return {
nodes,
externalNodes,
dependencies,
};
}

0 comments on commit aed6757

Please sign in to comment.