Skip to content

Commit 84bde4c

Browse files
authored
fix(core): allow opt-out of project inference (#402)
1 parent 2781ebb commit 84bde4c

File tree

6 files changed

+116
-15
lines changed

6 files changed

+116
-15
lines changed

packages/core/.babelrc

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import { registerProjectTargets } from './infer-project';
2+
import * as config from '@nx-dotnet/utils/src/lib/utility-functions/config';
3+
import * as fs from 'fs';
4+
5+
describe('infer-project', () => {
6+
it('should obey inferProjectTargets: false', () => {
7+
jest.spyOn(config, 'readConfig').mockReturnValue({
8+
nugetPackages: {},
9+
inferProjectTargets: false,
10+
});
11+
jest.spyOn(fs, 'readFileSync').mockReturnValueOnce('<project></project>');
12+
13+
expect(registerProjectTargets('libs/api/my.csproj')).toEqual({});
14+
});
15+
16+
it('should generate build, lint, serve targets for projects', async () => {
17+
jest.spyOn(config, 'readConfig').mockReturnValue({
18+
nugetPackages: {},
19+
});
20+
jest.spyOn(fs, 'readFileSync').mockReturnValueOnce('<project></project>');
21+
22+
const targets = registerProjectTargets('libs/api/my.csproj');
23+
console.log(targets);
24+
expect(targets.build).toMatchInlineSnapshot(`
25+
Object {
26+
"configurations": Object {
27+
"production": Object {
28+
"configuration": "Release",
29+
},
30+
},
31+
"executor": "@nx-dotnet/core:build",
32+
"options": Object {
33+
"configuration": "Debug",
34+
"noDependencies": true,
35+
},
36+
"outputs": Array [
37+
"dist/libs/api",
38+
],
39+
}
40+
`);
41+
expect(targets.lint).toMatchInlineSnapshot(`
42+
Object {
43+
"executor": "@nx-dotnet/core:format",
44+
}
45+
`);
46+
expect(targets.serve).toMatchInlineSnapshot(`
47+
Object {
48+
"configurations": Object {
49+
"production": Object {
50+
"configuration": "Release",
51+
},
52+
},
53+
"executor": "@nx-dotnet/core:serve",
54+
"options": Object {
55+
"configuration": "Debug",
56+
},
57+
}
58+
`);
59+
expect(targets.test).not.toBeDefined();
60+
});
61+
62+
it('should generate test target for test projects', async () => {
63+
jest.spyOn(config, 'readConfig').mockReturnValue({
64+
nugetPackages: {},
65+
});
66+
jest
67+
.spyOn(fs, 'readFileSync')
68+
.mockReturnValueOnce('<project ref=Microsoft.NET.Test.Sdk></project>');
69+
70+
const targets = registerProjectTargets('libs/api/my.csproj');
71+
console.log(targets);
72+
expect(targets.test).toMatchInlineSnapshot(`
73+
Object {
74+
"executor": "@nx-dotnet/core:test",
75+
"options": Object {
76+
"testProject": undefined,
77+
},
78+
}
79+
`);
80+
});
81+
});
Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { TargetConfiguration } from '@nrwl/devkit';
22
import { appRootPath } from '@nrwl/tao/src/utils/app-root';
3+
import { NxDotnetConfig, readConfig } from '@nx-dotnet/utils';
34

45
import { readFileSync } from 'fs';
56
import { dirname, resolve } from 'path';
@@ -14,16 +15,19 @@ import {
1415
export const projectFilePatterns = ['*.csproj', '*.fsproj', '*.vbproj'];
1516

1617
export const registerProjectTargets = (projectFile: string) => {
18+
const { inferProjectTargets } = readConfig();
1719
const targets: Record<string, TargetConfiguration> = {};
18-
const projectFileContents = readFileSync(
19-
resolve(appRootPath, projectFile),
20-
'utf8',
21-
);
22-
if (projectFileContents.includes('Microsoft.NET.Test.Sdk')) {
23-
targets['test'] = GetTestExecutorConfig();
20+
if (inferProjectTargets ?? true) {
21+
const projectFileContents = readFileSync(
22+
resolve(appRootPath, projectFile),
23+
'utf8',
24+
);
25+
if (projectFileContents.includes('Microsoft.NET.Test.Sdk')) {
26+
targets['test'] = GetTestExecutorConfig();
27+
}
28+
targets['build'] = GetBuildExecutorConfiguration(dirname(projectFile));
29+
targets['lint'] = GetLintExecutorConfiguration();
30+
targets['serve'] = GetServeExecutorConfig();
2431
}
25-
targets['build'] = GetBuildExecutorConfiguration(dirname(projectFile));
26-
targets['lint'] = GetLintExecutorConfiguration();
27-
targets['serve'] = GetServeExecutorConfig();
2832
return targets;
2933
};

packages/utils/src/lib/models/nx-dotnet-config.interface.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,20 @@ export interface NxDotnetConfig {
77
nugetPackages: {
88
[key: string]: string | undefined;
99
};
10+
11+
/**
12+
* Setup module boundary definitions here if not using eslint
13+
*/
1014
moduleBoundaries?: ModuleBoundaries;
15+
16+
/**
17+
* Default solution file
18+
*/
1119
solutionFile?: string;
20+
21+
/**
22+
* Set to false to skip target inference
23+
* @default true
24+
*/
25+
inferProjectTargets?: boolean;
1226
}

packages/utils/src/lib/utility-functions/config.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,14 @@ export const DefaultConfigValues: Partial<NxDotnetConfig> = {
99
solutionFile: '{npmScope}.nx-dotnet.sln',
1010
};
1111

12+
let cachedConfig: NxDotnetConfig;
1213
export function readConfig(host?: Tree): NxDotnetConfig {
13-
return host
14-
? readJson(host, CONFIG_FILE_PATH)
15-
: readJsonSync(`${appRootPath}/${CONFIG_FILE_PATH}`);
14+
if (host) {
15+
return readJson(host, CONFIG_FILE_PATH);
16+
} else {
17+
cachedConfig ??= readJsonSync(`${appRootPath}/${CONFIG_FILE_PATH}`);
18+
return cachedConfig;
19+
}
1620
}
1721

1822
export function updateConfig(host: Tree, value: NxDotnetConfig) {

tsconfig.base.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"@nx-dotnet/typescript": ["packages/typescript/src/index.ts"],
2525
"@nx-dotnet/utils": ["packages/utils/src/index.ts"],
2626
"@nx-dotnet/utils/e2e": ["packages/utils/src/e2e.ts"],
27+
"@nx-dotnet/utils/src/*": ["packages/utils/src/*"],
2728
"@nx-dotnet/utils/testing": ["packages/utils/src/testing.ts"]
2829
}
2930
},

0 commit comments

Comments
 (0)