Skip to content

Commit

Permalink
feat(core): #14 swap to nx project graph generation
Browse files Browse the repository at this point in the history
  • Loading branch information
AgentEnder committed Apr 16, 2021
1 parent cd6a6e1 commit 17cb3ff
Show file tree
Hide file tree
Showing 14 changed files with 2,397 additions and 43 deletions.
2,253 changes: 2,248 additions & 5 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "nx-dotnet",
"license": "MIT",
"version": "0.1.0-dev.5",
"scripts": {
"nx": "nx",
"start": "nx serve",
Expand Down
7 changes: 5 additions & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
"builders": "./executors.json",
"private": false,
"dependencies": {
"chokidar": "^3.5.1"
}
"chokidar": "^3.5.1",
"@nx-dotnet/dotnet": "0.1.0-dev.5",
"@nx-dotnet/utils": "0.1.0-dev.5"
},
"version": "0.1.0-dev.5"
}
5 changes: 0 additions & 5 deletions packages/core/src/generators/project-reference/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {
formatFiles,
readProjectConfiguration,
Tree,
updateProjectConfiguration,
} from '@nrwl/devkit';

import { DotNetClient, dotnetFactory } from '@nx-dotnet/dotnet';
Expand All @@ -29,9 +28,5 @@ export default async function (

client.addProjectReference(hostProjectFile, sourceProjectFile);

hostProject.implicitDependencies = hostProject.implicitDependencies || [];
hostProject.implicitDependencies.push(options.reference);
updateProjectConfiguration(host, options.project, hostProject);

await formatFiles(host);
}
68 changes: 68 additions & 0 deletions packages/core/src/graph/process-project-graph.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import {
ProjectGraph,
ProjectGraphBuilder,
ProjectGraphProcessorContext,
DependencyType,
ProjectConfiguration,
} from '@nrwl/devkit';
import { getProjectFileForNxProjectSync } from '@nx-dotnet/utils';
import { readFileSync } from 'fs';
import { XmlDocument, XmlElement } from 'xmldoc';
import { isAbsolute, resolve } from 'path';

const projectRoots: { [key: string]: string } = {};

export function processProjectGraph(
graph: ProjectGraph,
context: ProjectGraphProcessorContext
) {
const builder = new ProjectGraphBuilder(graph);

Object.entries(context.workspace.projects).forEach(([name, project]) => {
projectRoots[name] = resolve(project.root);
});

Object.entries(context.workspace.projects).forEach(([name, project]) => {
try {
visitProject(builder, context, project, name);
} catch {
console.warn(`Failed to generate .NET dependencies for ${name}`);
}
});

return builder.getProjectGraph();
}

function visitProject(
builder: ProjectGraphBuilder,
context: ProjectGraphProcessorContext,
project: ProjectConfiguration,
projectName: string
) {
const netProjectFilePath = getProjectFileForNxProjectSync(project);

const xml: XmlDocument = new XmlDocument(
readFileSync(netProjectFilePath).toString()
);

xml.childrenNamed('ItemGroup').forEach((itemGroup) =>
itemGroup.childrenNamed('ProjectReference').forEach((x: XmlElement) => {
const includeFilePath = x.attr['Include'];
let absoluteFilePath: string;
if (isAbsolute(includeFilePath)) {
absoluteFilePath = includeFilePath;
} else {
absoluteFilePath = resolve(
netProjectFilePath.split('/').slice(0, -1).join('/'),
includeFilePath
);
}

Object.entries(projectRoots).forEach(([dependency, path]) => {
if (absoluteFilePath.startsWith(path)) {
builder.addDependency(DependencyType.static, projectName, dependency);
}
});
})
);
}
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './graph/process-project-graph'
5 changes: 4 additions & 1 deletion packages/dotnet/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@
"name": "@nx-dotnet/dotnet",
"private": false,
"main": "src/index.js",
"dependencies": {}
"dependencies": {
"@nx-dotnet/utils": "0.1.0-dev.5"
},
"version": "0.1.0-dev.5"
}
3 changes: 2 additions & 1 deletion packages/typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"name": "@nx-dotnet/typescript",
"main": "src/index.js",
"generators": "./generators.json",
"executors": "./executors.json"
"executors": "./executors.json",
"version": "0.1.0-dev.5"
}
3 changes: 2 additions & 1 deletion packages/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
"dependencies": {
"glob": "^7.1.6",
"rimraf": "^3.0.2"
}
},
"version": "0.1.0-dev.5"
}
37 changes: 28 additions & 9 deletions packages/utils/src/lib/glob.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as _glob from 'glob';
import { sync as globSync } from 'glob';

/**
* Wraps the glob package in a promise api.
Expand All @@ -11,24 +12,42 @@ export function glob(path): Promise<string[]> {
}

export function findProjectFileInPath(path: string): Promise<string> {
console.log(
`Looking for project files at '${path}/**/*.*proj'`
);
console.log(`Looking for project files at '${path}/**/*.*proj'`);

return glob(
`${path}/**/*.*proj`
).then(results => {
return glob(`${path}/**/*.*proj`).then((results) => {
if (!results || results.length === 0) {
throw new Error(
"Unable to find a build-able project within project's source directory!"
);
}

if (results.length > 1) {
throw new Error(
`More than one build-able projects are contained within the project's source directory! \r\n ${results.join(', \r\n')}`
`More than one build-able projects are contained within the project's source directory! \r\n ${results.join(
', \r\n'
)}`
);
}
return results[0];
});
}
}

export function findProjectFileInPathSync(path: string): string {
console.log(`Looking for project files at '${path}/**/*.*proj'`);

const results = globSync(`${path}/**/*.*proj`);
if (!results || results.length === 0) {
throw new Error(
"Unable to find a build-able project within project's source directory!"
);
}

if (results.length > 1) {
throw new Error(
`More than one build-able projects are contained within the project's source directory! \r\n ${results.join(
', \r\n'
)}`
);
}
return results[0];
}
9 changes: 8 additions & 1 deletion packages/utils/src/lib/workspace.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import { ProjectConfiguration } from '@nrwl/devkit';
import { findProjectFileInPath } from './glob';
import { findProjectFileInPath, findProjectFileInPathSync } from './glob';

export async function getProjectFileForNxProject(
project: ProjectConfiguration
) {
const srcDirectory = project.root;
return findProjectFileInPath(srcDirectory);
}

export function getProjectFileForNxProjectSync(
project: ProjectConfiguration
) {
const srcDirectory = project.root;
return findProjectFileInPathSync(srcDirectory);
}
2 changes: 1 addition & 1 deletion tools/scripts/patch-package-versions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@ export function PatchPackageVersions(newVersion: string) {
});
}

if (require.main) {
if (require.main === module) {
PatchPackageVersions(process.argv[2])
}
19 changes: 9 additions & 10 deletions tools/scripts/publish-dev/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import { WorkspaceJsonConfiguration } from '@nrwl/devkit';
import { execSync } from 'child_process';
import { readFileSync, writeFileSync, existsSync } from 'fs';
import { readJson, writeJson } from '../../utils';
import { existsSync, readJson } from '../../utils';
import { PatchPackageVersions } from '../patch-package-versions';

const MAX_ATTEMPTS = 5;

export async function main(all = false, specific?: string) {
export function main(all = false, specific?: string) {
const workspace: WorkspaceJsonConfiguration = readJson('workspace.json');
const rootPkg = readJson('package.json');

Expand All @@ -15,17 +12,19 @@ export async function main(all = false, specific?: string) {
rev = (parseInt(rev) + 1).toString();
rev = rev === 'NaN' ? '0' : rev;
const newVersion = `${prev}-${branch}.${rev}`;

console.log('New Version: ', { newVersion, prev, tag, branch, rev });
PatchPackageVersions(newVersion);

const projects = Object.values(workspace.projects);

projects.forEach((projectConfiguration, idx) => {
const outputPath = projectConfiguration.targets?.build?.options?.outputPath;
execSync(
`npm publish ${outputPath} --tag=dev --new-version=${newVersion} --access=public`,
{ stdio: 'inherit' }
);
if (existsSync(`${outputPath}/package.json`)) {
execSync(
`npm publish ${outputPath} --tag=dev --new-version=${newVersion} --access=public`,
{ stdio: 'inherit' }
);
}
});
}

Expand Down
27 changes: 20 additions & 7 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,11 @@
"@types/source-list-map" "*"
"source-map" "^0.6.1"

"@types/xmldoc@^1.1.5":
"integrity" "sha512-31y8EoleGz/8+cA1ofgy1h8/yAXUjr6pfR2DSohWGa477UC7T2SQ2A+aafljRI2Pe64oE7mrJX8E3euWztC3sQ=="
"resolved" "https://registry.npmjs.org/@types/xmldoc/-/xmldoc-1.1.5.tgz"
"version" "1.1.5"

"@types/yargs-parser@*":
"integrity" "sha512-37RSHht+gzzgYeobbG+KWryeAW8J33Nhr69cjTqSYymXVZEN9NbRYWoYlRtDhHKPVT1FyNKwaTPC1NynKZpzRA=="
"resolved" "https://registry.npmjs.org/@types/yargs-parser/-/yargs-parser-20.2.0.tgz"
Expand Down Expand Up @@ -944,7 +949,6 @@
"resolved" "https://registry.npmjs.org/@typescript-eslint/parser/-/parser-4.19.0.tgz"
"version" "4.19.0"
dependencies:
"@types/json-schema" "^7.0.3"
"@typescript-eslint/scope-manager" "4.19.0"
"@typescript-eslint/types" "4.19.0"
"@typescript-eslint/typescript-estree" "4.19.0"
Expand Down Expand Up @@ -5564,12 +5568,7 @@
dependencies:
"object-visit" "^1.0.0"

"marked@^2.0.1":
"integrity" "sha512-5otztIIcJfPc2qGTN8cVtOJEjNJZ0jwa46INMagrYfk0EvqtRuEHLsEe0LrFS0/q+ZRKT0+kXK7P2T1AN5lWRA=="
"resolved" "https://registry.npmjs.org/marked/-/marked-2.0.3.tgz"
"version" "2.0.3"

"marked@2.0.1":
"marked@^2.0.1", "marked@2.0.1":
"integrity" "sha512-5+/fKgMv2hARmMW7DOpykr2iLhl0NgjyELk5yn92iE7z8Se1IS9n3UsFm86hFXIkvMBmVxki8+ckcpjBeyo/hw=="
"resolved" "https://registry.npmjs.org/marked/-/marked-2.0.1.tgz"
"version" "2.0.1"
Expand Down Expand Up @@ -7143,6 +7142,11 @@
"minimist" "^1.1.1"
"walker" "~1.0.5"

"sax@^1.2.1":
"integrity" "sha512-NqVDv9TpANUjFm0N8uM5GxL36UgKi9/atZw+x7YFnQ8ckwFGKrl4xX4yWtrey3UJm5nP1kUbnYgLopqWNSRhWw=="
"resolved" "https://registry.npmjs.org/sax/-/sax-1.2.4.tgz"
"version" "1.2.4"

"saxes@^3.1.9":
"integrity" "sha512-Ydydq3zC+WYDJK1+gRxRapLIED9PWeSuuS41wqyoRmzvhhh9nc+QQrVMKJYzJFULazeGhzSV0QleN2wD3boh2g=="
"resolved" "https://registry.npmjs.org/saxes/-/saxes-3.1.11.tgz"
Expand Down Expand Up @@ -8167,6 +8171,8 @@
"version" "0.14.5"

"typanion@*":
"integrity" "sha512-e+54C4+ozFsTorFe50JNQlXlt4HVGQUvqul7VS0GbDfKlxh3aXbJY87Yu9IdtzvJWCyTgDX7q1PeMd3FH9zZqA=="
"resolved" "https://registry.npmjs.org/typanion/-/typanion-3.3.0.tgz"
"version" "3.3.0"

"type-check@^0.4.0":
Expand Down Expand Up @@ -8839,6 +8845,13 @@
"resolved" "https://registry.npmjs.org/xmlchars/-/xmlchars-2.2.0.tgz"
"version" "2.2.0"

"xmldoc@^1.1.2":
"integrity" "sha512-ruPC/fyPNck2BD1dpz0AZZyrEwMOrWTO5lDdIXS91rs3wtm4j+T8Rp2o+zoOYkkAxJTZRPOSnOGei1egoRmKMQ=="
"resolved" "https://registry.npmjs.org/xmldoc/-/xmldoc-1.1.2.tgz"
"version" "1.1.2"
dependencies:
"sax" "^1.2.1"

"xtend@^4.0.0", "xtend@~4.0.1":
"integrity" "sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ=="
"resolved" "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz"
Expand Down

0 comments on commit 17cb3ff

Please sign in to comment.