Skip to content

Commit

Permalink
feat(core): allow disabling project inference from config file
Browse files Browse the repository at this point in the history
  • Loading branch information
AgentEnder committed Oct 24, 2022
1 parent 90b3aab commit 2c8eeeb
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 11 deletions.
26 changes: 19 additions & 7 deletions packages/core/src/graph/infer-project.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
import type { NxDotnetConfig } from '@nx-dotnet/utils';

import * as fs from 'fs';

import * as config from '@nx-dotnet/utils/src/lib/utility-functions/config';
let configValues: NxDotnetConfig = {
nugetPackages: {},
};

jest.mock(
'@nx-dotnet/utils/src/lib/utility-functions/config',
() =>
({
readConfig: () => configValues,
} as typeof import('@nx-dotnet/utils/src/lib/utility-functions/config')),
);

import { registerProjectTargets } from './infer-project';

Expand All @@ -10,19 +22,19 @@ describe('infer-project', () => {
});

it('should obey inferProjectTargets: false', () => {
jest.spyOn(config, 'readConfig').mockReturnValue({
configValues = {
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', () => {
jest.spyOn(config, 'readConfig').mockReturnValue({
configValues = {
nugetPackages: {},
});
};
jest.spyOn(fs, 'readFileSync').mockReturnValueOnce('<project></project>');

const targets = registerProjectTargets('libs/api/my.csproj');
Expand Down Expand Up @@ -65,9 +77,9 @@ describe('infer-project', () => {
});

it('should generate test target for test projects', () => {
jest.spyOn(config, 'readConfig').mockReturnValue({
configValues = {
nugetPackages: {},
});
};
jest
.spyOn(fs, 'readFileSync')
.mockReturnValueOnce('<project ref=Microsoft.NET.Test.Sdk></project>');
Expand Down
6 changes: 4 additions & 2 deletions packages/core/src/graph/infer-project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ import {
GetTestExecutorConfig,
} from '../models';

export const projectFilePatterns = ['*.csproj', '*.fsproj', '*.vbproj'];
export const projectFilePatterns = readConfig().inferProjects
? ['*.csproj', '*.fsproj', '*.vbproj']
: [];

export const registerProjectTargets = (projectFile: string) => {
const { inferProjectTargets } = readConfig();
const targets: Record<string, TargetConfiguration> = {};
const { inferProjectTargets } = readConfig();
if (inferProjectTargets ?? true) {
const projectFileContents = readFileSync(
resolve(workspaceRoot, projectFile),
Expand Down
6 changes: 6 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 @@ -23,4 +23,10 @@ export interface NxDotnetConfig {
* @default true
*/
inferProjectTargets?: boolean;

/**
* Set to false to skip project inference
* @default true
*/
inferProjects?: boolean;
}
14 changes: 12 additions & 2 deletions packages/utils/src/lib/utility-functions/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,26 @@ import {
import { CONFIG_FILE_PATH } from '../constants';
import { NxDotnetConfig } from '../models';

export const DefaultConfigValues: Partial<NxDotnetConfig> = {
export const DefaultConfigValues: NxDotnetConfig = {
solutionFile: '{npmScope}.nx-dotnet.sln',
inferProjects: true,
inferProjectTargets: true,
nugetPackages: {},
};

let cachedConfig: NxDotnetConfig;
export function readConfig(host?: Tree): NxDotnetConfig {
if (host) {
return readJson(host, CONFIG_FILE_PATH);
} else {
cachedConfig ??= readJsonFile(`${workspaceRoot}/${CONFIG_FILE_PATH}`);
try {
cachedConfig ??= {
...DefaultConfigValues,
...readJsonFile(`${workspaceRoot}/${CONFIG_FILE_PATH}`),
};
} catch {
return DefaultConfigValues;
}
return cachedConfig;
}
}
Expand Down

0 comments on commit 2c8eeeb

Please sign in to comment.