Skip to content

Commit

Permalink
fix(core): allow opt-out of project inference (#402)
Browse files Browse the repository at this point in the history
  • Loading branch information
AgentEnder authored Mar 25, 2022
1 parent 2781ebb commit 84bde4c
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 15 deletions.
3 changes: 0 additions & 3 deletions packages/core/.babelrc

This file was deleted.

81 changes: 81 additions & 0 deletions packages/core/src/graph/infer-project.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { registerProjectTargets } from './infer-project';
import * as config from '@nx-dotnet/utils/src/lib/utility-functions/config';
import * as fs from 'fs';

describe('infer-project', () => {
it('should obey inferProjectTargets: false', () => {
jest.spyOn(config, 'readConfig').mockReturnValue({
nugetPackages: {},
inferProjectTargets: false,
});
jest.spyOn(fs, 'readFileSync').mockReturnValueOnce('<project></project>');

expect(registerProjectTargets('libs/api/my.csproj')).toEqual({});
});

it('should generate build, lint, serve targets for projects', async () => {
jest.spyOn(config, 'readConfig').mockReturnValue({
nugetPackages: {},
});
jest.spyOn(fs, 'readFileSync').mockReturnValueOnce('<project></project>');

const targets = registerProjectTargets('libs/api/my.csproj');
console.log(targets);
expect(targets.build).toMatchInlineSnapshot(`
Object {
"configurations": Object {
"production": Object {
"configuration": "Release",
},
},
"executor": "@nx-dotnet/core:build",
"options": Object {
"configuration": "Debug",
"noDependencies": true,
},
"outputs": Array [
"dist/libs/api",
],
}
`);
expect(targets.lint).toMatchInlineSnapshot(`
Object {
"executor": "@nx-dotnet/core:format",
}
`);
expect(targets.serve).toMatchInlineSnapshot(`
Object {
"configurations": Object {
"production": Object {
"configuration": "Release",
},
},
"executor": "@nx-dotnet/core:serve",
"options": Object {
"configuration": "Debug",
},
}
`);
expect(targets.test).not.toBeDefined();
});

it('should generate test target for test projects', async () => {
jest.spyOn(config, 'readConfig').mockReturnValue({
nugetPackages: {},
});
jest
.spyOn(fs, 'readFileSync')
.mockReturnValueOnce('<project ref=Microsoft.NET.Test.Sdk></project>');

const targets = registerProjectTargets('libs/api/my.csproj');
console.log(targets);
expect(targets.test).toMatchInlineSnapshot(`
Object {
"executor": "@nx-dotnet/core:test",
"options": Object {
"testProject": undefined,
},
}
`);
});
});
22 changes: 13 additions & 9 deletions packages/core/src/graph/infer-project.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { TargetConfiguration } from '@nrwl/devkit';
import { appRootPath } from '@nrwl/tao/src/utils/app-root';
import { NxDotnetConfig, readConfig } from '@nx-dotnet/utils';

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

export const registerProjectTargets = (projectFile: string) => {
const { inferProjectTargets } = readConfig();
const targets: Record<string, TargetConfiguration> = {};
const projectFileContents = readFileSync(
resolve(appRootPath, projectFile),
'utf8',
);
if (projectFileContents.includes('Microsoft.NET.Test.Sdk')) {
targets['test'] = GetTestExecutorConfig();
if (inferProjectTargets ?? true) {
const projectFileContents = readFileSync(
resolve(appRootPath, projectFile),
'utf8',
);
if (projectFileContents.includes('Microsoft.NET.Test.Sdk')) {
targets['test'] = GetTestExecutorConfig();
}
targets['build'] = GetBuildExecutorConfiguration(dirname(projectFile));
targets['lint'] = GetLintExecutorConfiguration();
targets['serve'] = GetServeExecutorConfig();
}
targets['build'] = GetBuildExecutorConfiguration(dirname(projectFile));
targets['lint'] = GetLintExecutorConfiguration();
targets['serve'] = GetServeExecutorConfig();
return targets;
};
14 changes: 14 additions & 0 deletions packages/utils/src/lib/models/nx-dotnet-config.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@ export interface NxDotnetConfig {
nugetPackages: {
[key: string]: string | undefined;
};

/**
* Setup module boundary definitions here if not using eslint
*/
moduleBoundaries?: ModuleBoundaries;

/**
* Default solution file
*/
solutionFile?: string;

/**
* Set to false to skip target inference
* @default true
*/
inferProjectTargets?: boolean;
}
10 changes: 7 additions & 3 deletions packages/utils/src/lib/utility-functions/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ export const DefaultConfigValues: Partial<NxDotnetConfig> = {
solutionFile: '{npmScope}.nx-dotnet.sln',
};

let cachedConfig: NxDotnetConfig;
export function readConfig(host?: Tree): NxDotnetConfig {
return host
? readJson(host, CONFIG_FILE_PATH)
: readJsonSync(`${appRootPath}/${CONFIG_FILE_PATH}`);
if (host) {
return readJson(host, CONFIG_FILE_PATH);
} else {
cachedConfig ??= readJsonSync(`${appRootPath}/${CONFIG_FILE_PATH}`);
return cachedConfig;
}
}

export function updateConfig(host: Tree, value: NxDotnetConfig) {
Expand Down
1 change: 1 addition & 0 deletions tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"@nx-dotnet/typescript": ["packages/typescript/src/index.ts"],
"@nx-dotnet/utils": ["packages/utils/src/index.ts"],
"@nx-dotnet/utils/e2e": ["packages/utils/src/e2e.ts"],
"@nx-dotnet/utils/src/*": ["packages/utils/src/*"],
"@nx-dotnet/utils/testing": ["packages/utils/src/testing.ts"]
}
},
Expand Down

0 comments on commit 84bde4c

Please sign in to comment.