Skip to content

Commit

Permalink
fix(core): options should be read correctly for project inference (#785)
Browse files Browse the repository at this point in the history
  • Loading branch information
AgentEnder authored Oct 24, 2023
1 parent eab2d48 commit 0e5b73d
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 40 deletions.
1 change: 1 addition & 0 deletions packages/core/src/generators/import-projects/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ async function checkIfTestProject(host: Tree, path: string): Promise<boolean> {
});
return isTestProject;
}

function getDirectoriesWithProjectJson(host: Tree) {
const nxProjects = getProjects(host);
const collected: string[] = [];
Expand Down
41 changes: 33 additions & 8 deletions packages/core/src/generators/init/generator.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import * as devkit from '@nx/devkit';
import { readJson, Tree, writeJson } from '@nx/devkit';
import { readJson, readNxJson, Tree, writeJson } from '@nx/devkit';
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';

import { DotNetClient, mockDotnetFactory } from '@nx-dotnet/dotnet';
import { CONFIG_FILE_PATH, NxDotnetConfig } from '@nx-dotnet/utils';

import generator from './generator';

Expand All @@ -24,16 +23,42 @@ describe('init generator', () => {
writeJson(tree, 'package.json', packageJson);
});

it('should create config', async () => {
it('should add @nx-dotnet/core to plugins array', async () => {
writeJson(tree, 'nx.json', {});
await generator(tree, null, dotnetClient);
const config = tree.isFile(CONFIG_FILE_PATH);
expect(config).toBeTruthy();
expect(readNxJson(tree)?.plugins).toMatchInlineSnapshot(`
[
"@nx-dotnet/core",
]
`);
});

it('should put dependency array inside config', async () => {
it('should add duplicate @nx-dotnet/core entries to plugins array', async () => {
writeJson(tree, 'nx.json', { plugins: ['@nx-dotnet/core'] });
await generator(tree, null, dotnetClient);
const config: NxDotnetConfig = readJson(tree, CONFIG_FILE_PATH);
expect(config.nugetPackages).toBeDefined();
expect(readNxJson(tree)?.plugins).toMatchInlineSnapshot(`
[
"@nx-dotnet/core",
]
`);
});

it('should add duplicate @nx-dotnet/core entries to plugins array (object)', async () => {
writeJson(tree, 'nx.json', {
plugins: [
{
plugin: '@nx-dotnet/core',
},
],
});
await generator(tree, null, dotnetClient);
expect(readNxJson(tree)?.plugins).toMatchInlineSnapshot(`
[
{
"plugin": "@nx-dotnet/core",
},
]
`);
});

it('should create tool manifest', async () => {
Expand Down
54 changes: 35 additions & 19 deletions packages/core/src/generators/init/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
Tree,
writeJson,
NX_VERSION,
readNxJson,
} from '@nx/devkit';

import { DotNetClient, dotnetFactory } from '@nx-dotnet/dotnet';
Expand All @@ -19,38 +20,47 @@ import {
resolve,
} from '@nx-dotnet/utils';
import * as path from 'path';
import { major } from 'semver';

const noop = () => void 0;

export async function initGenerator(
host: Tree,
_: null, // Nx will populate this with options, which are currently unused.
_: unknown, // Nx will populate this with options, which are currently unused.
dotnetClient = new DotNetClient(dotnetFactory()),
) {
const tasks: GeneratorCallback[] = [];
const initialized = host.isFile(CONFIG_FILE_PATH);

const configObject: NxDotnetConfig = initialized
? readJson(host, CONFIG_FILE_PATH)
: {
nugetPackages: {},
};
// Prior to Nx 17, nx-dotnet had a custom config file.
if (major(NX_VERSION) < 17) {
const configObject: NxDotnetConfig = host.isFile(CONFIG_FILE_PATH)
? readJson(host, CONFIG_FILE_PATH)
: {
nugetPackages: {},
};

configObject.nugetPackages = configObject.nugetPackages || {};
configObject.nugetPackages = configObject.nugetPackages || {};

host.write(CONFIG_FILE_PATH, JSON.stringify(configObject, null, 2));
host.write(CONFIG_FILE_PATH, JSON.stringify(configObject, null, 2));
}

updateNxJson(host);
const nxJson = readNxJson(host);

if (!initialized) {
addPrepareScript(host);
tasks.push(installNpmPackages(host));
}
// Adds a `dotnet restore` operation to the prepare script.
addPrepareScript(host);

// Adds @nx-dotnet/core to nxJson
updateNxJson(host, nxJson);

// Setups up the .config/dotnet-tools.json for managing local .NET tools.
initToolManifest(host, dotnetClient);

// Creates Directory.Build.* to customize default C# builds.
initBuildCustomization(host);

// Adds @nx/js to package.json
tasks.push(installNpmPackages(host));

return async () => {
for (const task of tasks) {
await task();
Expand All @@ -74,13 +84,19 @@ function installNpmPackages(host: Tree): GeneratorCallback {
}
}

function updateNxJson(host: Tree) {
const nxJson: NxJsonConfiguration = readJson(host, 'nx.json');
nxJson.plugins = nxJson.plugins || [];
if (!nxJson.plugins.some((x) => x === '@nx-dotnet/core')) {
function hasPluginInNxJson(nxJson: NxJsonConfiguration | null): boolean {
return !!nxJson?.plugins?.some((x) => {
const plugin = typeof x === 'string' ? x : x.plugin;
return plugin === '@nx-dotnet/core';
});
}

function updateNxJson(host: Tree, nxJson: NxJsonConfiguration | null) {
if (nxJson && !hasPluginInNxJson(nxJson)) {
nxJson.plugins = nxJson.plugins || [];
nxJson.plugins.push('@nx-dotnet/core');
writeJson(host, 'nx.json', nxJson);
}
writeJson(host, 'nx.json', nxJson);
}

function initToolManifest(host: Tree, dotnetClient: DotNetClient) {
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/generators/nuget-reference/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export default async function (
const projectFilePath = await getProjectFileForNxProject(project);

const config = readConfig(host);
config.nugetPackages ??= {};
const configuredPkgVersion = config.nugetPackages[packageName];
const resolvedVersion = await resolveVersionMismatch(
options.version,
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/generators/sync/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { updateDependencyVersions } from '../utils/update-dependency-version';

export default async function (host: Tree) {
const config = readConfig(host);
config.nugetPackages ??= {};
const projects = await getNxDotnetProjects(host);

for (const [projectName, configuration] of projects.entries()) {
Expand Down
10 changes: 2 additions & 8 deletions packages/core/src/graph/create-nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@ import {
import { readFileSync } from 'fs';
import { dirname, resolve } from 'path';

import {
DefaultConfigValues,
NxDotnetConfig,
readConfig,
} from '@nx-dotnet/utils';
import { NxDotnetConfig, readConfig } from '@nx-dotnet/utils';

import {
GetBuildExecutorConfiguration,
Expand Down Expand Up @@ -76,9 +72,7 @@ export const createNodes: CreateNodesCompat<NxDotnetConfig> = [
ctxOrOpts: CreateNodesContext | NxDotnetConfig | undefined,
maybeCtx: CreateNodesContext | undefined,
) => {
const options: NxDotnetConfig =
((maybeCtx ? ctxOrOpts : readConfig()) as NxDotnetConfig) ??
DefaultConfigValues;
const options: NxDotnetConfig = readConfig();

if (!options.inferProjects) {
return {};
Expand Down
22 changes: 17 additions & 5 deletions packages/utils/src/lib/utility-functions/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ export const DefaultConfigValues: NxDotnetConfig = {
export function readConfig(host?: Tree): NxDotnetConfig {
const configFromFile = readConfigFromRCFile(host);
const configFromNxJson = readConfigFromNxJson(host);
return {
...DefaultConfigValues,
...configFromFile,
...configFromNxJson,
};
return mergeConfigValues(
DefaultConfigValues,
configFromFile,
configFromNxJson,
);
}

export function updateConfig(host: Tree, value: NxDotnetConfig) {
Expand Down Expand Up @@ -112,3 +112,15 @@ export function readConfigFromNxJson(host?: Tree): NxDotnetConfig | null {
return null;
}
}

export function mergeConfigValues(
...configs: (Partial<NxDotnetConfig> | null)[]
): NxDotnetConfig {
return configs.reduce(
(acc, config) => ({
...acc,
...config,
}),
DefaultConfigValues,
) as NxDotnetConfig;
}

0 comments on commit 0e5b73d

Please sign in to comment.