fix(gradle): expose create nodes v2 (#26282)

<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

## Current Behavior
<!-- This is the behavior we have today -->

`createNodesV2` was written but not exported.

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->

`createNodesV2` is exported.

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #
This commit is contained in:
Jason Jean 2024-05-31 09:18:19 -04:00 committed by GitHub
parent 2cb7ecb77b
commit fde4932ab9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
19 changed files with 268 additions and 160 deletions

View File

@ -11,7 +11,7 @@ import {
updateNxJson,
} from '@nx/devkit';
import {
addPlugin as _addPlugin,
addPluginV1 as _addPlugin,
generateCombinations,
} from '@nx/devkit/src/utils/add-plugin';
import { createNodes } from '../../plugins/plugin';

View File

@ -9,7 +9,7 @@ import {
Tree,
} from '@nx/devkit';
import {
addPlugin,
addPluginV1,
generateCombinations,
} from '@nx/devkit/src/utils/add-plugin';
import { createNodes, DetoxPluginOptions } from '../../plugins/plugin';
@ -36,7 +36,7 @@ export async function detoxInitGeneratorInternal(host: Tree, schema: Schema) {
}
if (schema.addPlugin) {
await addPlugin(
await addPluginV1(
host,
await createProjectGraphAsync(),
'@nx/detox/plugin',

View File

@ -2,7 +2,7 @@ import { createTreeWithEmptyWorkspace } from 'nx/src/generators/testing-utils/cr
import type { Tree } from 'nx/src/generators/tree';
import { readJson, writeJson } from 'nx/src/generators/utils/json';
import type { PackageJson } from 'nx/src/utils/package-json';
import { CreateNodes } from 'nx/src/project-graph/plugins';
import { CreateNodesV2 } from 'nx/src/project-graph/plugins';
import { ProjectGraph } from 'nx/src/devkit-exports';
import { TempFs } from 'nx/src/internal-testing-utils/temp-fs';
@ -10,7 +10,7 @@ import { addPlugin, generateCombinations } from './add-plugin';
describe('addPlugin', () => {
let tree: Tree;
let createNodes: CreateNodes<{ targetName: string }>;
let createNodes: CreateNodesV2<{ targetName: string }>;
let graph: ProjectGraph;
let fs: TempFs;
@ -55,7 +55,10 @@ describe('addPlugin', () => {
};
createNodes = [
'**/next.config.{js,cjs,mjs}',
(_, { targetName }) => ({
(_, { targetName }) => [
[
'app1/next.config.js',
{
projects: {
app1: {
name: 'app1',
@ -63,6 +66,13 @@ describe('addPlugin', () => {
[targetName]: { command: 'next build' },
},
},
},
},
],
[
'app2/next.config.js',
{
projects: {
app2: {
name: 'app2',
targets: {
@ -70,7 +80,9 @@ describe('addPlugin', () => {
},
},
},
}),
},
],
],
];
await fs.createFiles({
@ -217,7 +229,10 @@ describe('addPlugin', () => {
createNodes = [
'**/cypress.config.{js,ts,mjs,mts,cjs,cts}',
() => ({
() => [
[
'app1/cypress.config.ts',
{
projects: {
app1: {
name: 'app1',
@ -229,7 +244,9 @@ describe('addPlugin', () => {
},
},
},
}),
},
],
],
];
await addPlugin(
@ -284,7 +301,10 @@ describe('addPlugin', () => {
createNodes = [
'**/next.config.{js,cjs,mjs}',
() => ({
() => [
[
'app1/next.config.js',
{
projects: {
app1: {
name: 'app1',
@ -295,7 +315,9 @@ describe('addPlugin', () => {
},
},
},
}),
},
],
],
];
await addPlugin(
@ -328,7 +350,10 @@ describe('addPlugin', () => {
createNodes = [
'**/tsconfig.json',
() => ({
() => [
[
'app1/tsconfig.json',
{
projects: {
app1: {
name: 'app1',
@ -337,7 +362,9 @@ describe('addPlugin', () => {
},
},
},
}),
},
],
],
];
await addPlugin(
@ -370,7 +397,10 @@ describe('addPlugin', () => {
createNodes = [
'**/tsconfig.json',
() => ({
() => [
[
'app1/tsconfig.json',
{
projects: {
app1: {
name: 'app1',
@ -379,7 +409,9 @@ describe('addPlugin', () => {
},
},
},
}),
},
],
],
];
await addPlugin(

View File

@ -5,6 +5,7 @@ import * as yargs from 'yargs-parser';
import {
CreateNodes,
CreateNodesV2,
ProjectConfiguration,
ProjectGraph,
readJson,
@ -24,6 +25,41 @@ import {
*/
export async function addPlugin<PluginOptions>(
tree: Tree,
graph: ProjectGraph,
pluginName: string,
createNodesTuple: CreateNodesV2<PluginOptions>,
options: Partial<
Record<keyof PluginOptions, PluginOptions[keyof PluginOptions][]>
>,
shouldUpdatePackageJsonScripts: boolean
): Promise<void> {
return _addPluginInternal(
tree,
graph,
pluginName,
(pluginOptions) =>
new LoadedNxPlugin(
{
name: pluginName,
createNodesV2: createNodesTuple,
},
{
plugin: pluginName,
options: pluginOptions,
}
),
options,
shouldUpdatePackageJsonScripts
);
}
/**
* @deprecated Use `addPlugin` instead
* Iterates through various forms of plugin options to find the one which does not conflict with the current graph
*/
export async function addPluginV1<PluginOptions>(
tree: Tree,
graph: ProjectGraph,
pluginName: string,
@ -33,6 +69,36 @@ export async function addPlugin<PluginOptions>(
>,
shouldUpdatePackageJsonScripts: boolean
): Promise<void> {
return _addPluginInternal(
tree,
graph,
pluginName,
(pluginOptions) =>
new LoadedNxPlugin(
{
name: pluginName,
createNodes: createNodesTuple,
},
{
plugin: pluginName,
options: pluginOptions,
}
),
options,
shouldUpdatePackageJsonScripts
);
}
async function _addPluginInternal<PluginOptions>(
tree: Tree,
graph: ProjectGraph,
pluginName: string,
pluginFactory: (pluginOptions: PluginOptions) => LoadedNxPlugin,
options: Partial<
Record<keyof PluginOptions, PluginOptions[keyof PluginOptions][]>
>,
shouldUpdatePackageJsonScripts: boolean
) {
const graphNodes = Object.values(graph.nodes);
const nxJson = readNxJson(tree);
@ -56,18 +122,7 @@ export async function addPlugin<PluginOptions>(
global.NX_GRAPH_CREATION = true;
try {
projConfigs = await retrieveProjectConfigurations(
[
new LoadedNxPlugin(
{
name: pluginName,
createNodes: createNodesTuple,
},
{
plugin: pluginName,
options: pluginOptions,
}
),
],
[pluginFactory(pluginOptions)],
tree.root,
nxJson
);

View File

@ -8,7 +8,7 @@ import {
Tree,
updateNxJson,
} from '@nx/devkit';
import { addPlugin } from '@nx/devkit/src/utils/add-plugin';
import { addPluginV1 } from '@nx/devkit/src/utils/add-plugin';
import { eslintVersion, nxVersion } from '../../utils/versions';
import { findEslintFile } from '../utils/eslint-file';
import { createNodes } from '../../plugins/plugin';
@ -73,7 +73,7 @@ export async function initEsLint(
];
if (rootEslintFile && options.addPlugin && !hasPlugin) {
await addPlugin(
await addPluginV1(
tree,
graph,
'@nx/eslint/plugin',
@ -94,7 +94,7 @@ export async function initEsLint(
updateProductionFileset(tree);
if (options.addPlugin) {
await addPlugin(
await addPluginV1(
tree,
graph,
'@nx/eslint/plugin',

View File

@ -8,7 +8,7 @@ import {
runTasksInSerial,
Tree,
} from '@nx/devkit';
import { addPlugin } from '@nx/devkit/src/utils/add-plugin';
import { addPluginV1 } from '@nx/devkit/src/utils/add-plugin';
import { createNodes } from '../../../plugins/plugin';
import {
expoCliVersion,
@ -36,7 +36,7 @@ export async function expoInitGeneratorInternal(host: Tree, schema: Schema) {
addGitIgnoreEntry(host);
if (schema.addPlugin) {
await addPlugin(
await addPluginV1(
host,
await createProjectGraphAsync(),
'@nx/expo/plugin',

View File

@ -1,2 +1,2 @@
export { createDependencies } from './src/plugin/dependencies';
export { createNodes } from './src/plugin/nodes';
export { createNodes, createNodesV2 } from './src/plugin/nodes';

View File

@ -11,10 +11,10 @@ jest.mock('../utils/get-gradle-report.ts', () => {
};
});
import { createNodes } from './nodes';
import { createNodesV2 } from './nodes';
describe('@nx/gradle/plugin', () => {
let createNodesFunction = createNodes[1];
let createNodesFunction = createNodesV2[1];
let context: CreateNodesContext;
let tempFs: TempFs;
let cwd: string;
@ -59,16 +59,21 @@ describe('@nx/gradle/plugin', () => {
});
it('should create nodes based on gradle', async () => {
const nodes = await createNodesFunction(
'proj/gradle.build',
const results = await createNodesFunction(
['proj/gradle.build'],
{
buildTargetName: 'build',
},
context
);
expect(nodes.projects.proj).toMatchInlineSnapshot(`
expect(results).toMatchInlineSnapshot(`
[
[
"proj/gradle.build",
{
"projects": {
"proj": {
"metadata": {
"targetGroups": {
"Test": [
@ -96,10 +101,13 @@ describe('@nx/gradle/plugin', () => {
"gradle",
],
},
"outputs": undefined,
},
},
}
},
},
},
],
]
`);
});
@ -121,16 +129,21 @@ describe('@nx/gradle/plugin', () => {
'nested/nested/proj/gradle.build': ``,
});
const nodes = await createNodesFunction(
'nested/nested/proj/gradle.build',
const results = await createNodesFunction(
['nested/nested/proj/gradle.build'],
{
buildTargetName: 'build',
},
context
);
expect(nodes.projects['nested/nested/proj']).toMatchInlineSnapshot(`
expect(results).toMatchInlineSnapshot(`
[
[
"nested/nested/proj/gradle.build",
{
"projects": {
"nested/nested/proj": {
"metadata": {
"targetGroups": {
"Test": [
@ -158,10 +171,13 @@ describe('@nx/gradle/plugin', () => {
"gradle",
],
},
"outputs": undefined,
},
},
}
},
},
},
],
]
`);
});
});

View File

@ -218,18 +218,23 @@ function createGradleTargets(
const targetName = options?.[`${task.name}TargetName`] ?? task.name;
const outputs = outputDirs.get(task.name);
targets[targetName] = {
command: `${getGradleExecFile()} ${
gradleProject ? gradleProject + ':' : ''
}${task.name}`,
cache: cacheableTaskType.has(task.type),
inputs: inputsMap[task.name],
outputs: outputs ? [outputs] : undefined,
dependsOn: dependsOnMap[task.name],
metadata: {
technologies: ['gradle'],
},
};
if (outputs) {
targets[targetName].outputs = [outputs];
}
if (!targetGroups[task.type]) {
targetGroups[task.type] = [];
}

View File

@ -9,7 +9,7 @@ import {
type GeneratorCallback,
type Tree,
} from '@nx/devkit';
import { addPlugin } from '@nx/devkit/src/utils/add-plugin';
import { addPluginV1 } from '@nx/devkit/src/utils/add-plugin';
import { createNodes } from '../../plugins/plugin';
import {
getPresetExt,
@ -104,7 +104,7 @@ export async function jestInitGeneratorInternal(
if (!tree.exists(`jest.preset.${presetExt}`)) {
updateProductionFileSet(tree);
if (options.addPlugin) {
await addPlugin(
await addPluginV1(
tree,
await createProjectGraphAsync(),
'@nx/jest/plugin',

View File

@ -7,7 +7,7 @@ import {
readNxJson,
createProjectGraphAsync,
} from '@nx/devkit';
import { addPlugin } from '@nx/devkit/src/utils/add-plugin';
import { addPluginV1 } from '@nx/devkit/src/utils/add-plugin';
import { reactDomVersion, reactVersion } from '@nx/react/src/utils/versions';
import { addGitIgnoreEntry } from '../../utils/add-gitignore-entry';
import { nextVersion, nxVersion } from '../../utils/versions';
@ -53,7 +53,7 @@ export async function nextInitGeneratorInternal(
schema.addPlugin ??= addPluginDefault;
if (schema.addPlugin) {
const { createNodes } = await import('../../plugins/plugin');
await addPlugin(
await addPluginV1(
host,
await createProjectGraphAsync(),
'@nx/next/plugin',

View File

@ -1,12 +1,12 @@
import { createProjectGraphAsync, GeneratorCallback, Tree } from '@nx/devkit';
import { addPlugin } from '@nx/devkit/src/utils/add-plugin';
import { addPluginV1 } from '@nx/devkit/src/utils/add-plugin';
import { createNodes } from '../../plugins/plugin';
import { InitSchema } from './schema';
import { updateDependencies } from './lib/utils';
export async function nuxtInitGenerator(host: Tree, schema: InitSchema) {
await addPlugin(
await addPluginV1(
host,
await createProjectGraphAsync(),
'@nx/nuxt/plugin',

View File

@ -7,7 +7,7 @@ import {
runTasksInSerial,
Tree,
} from '@nx/devkit';
import { addPlugin } from '@nx/devkit/src/utils/add-plugin';
import { addPluginV1 } from '@nx/devkit/src/utils/add-plugin';
import { createNodes } from '../../plugins/plugin';
import { nxVersion, playwrightVersion } from '../../utils/versions';
import { InitGeneratorSchema } from './schema';
@ -45,7 +45,7 @@ export async function initGeneratorInternal(
}
if (options.addPlugin) {
await addPlugin(
await addPluginV1(
tree,
await createProjectGraphAsync(),
'@nx/playwright/plugin',

View File

@ -8,7 +8,7 @@ import {
runTasksInSerial,
Tree,
} from '@nx/devkit';
import { addPlugin } from '@nx/devkit/src/utils/add-plugin';
import { addPluginV1 } from '@nx/devkit/src/utils/add-plugin';
import { createNodes } from '../../../plugins/plugin';
import {
nxVersion,
@ -39,7 +39,7 @@ export async function reactNativeInitGeneratorInternal(
schema.addPlugin ??= addPluginDefault;
if (schema.addPlugin) {
await addPlugin(
await addPluginV1(
host,
await createProjectGraphAsync(),
'@nx/react-native/plugin',

View File

@ -8,7 +8,7 @@ import {
createProjectGraphAsync,
} from '@nx/devkit';
import {
addPlugin,
addPluginV1,
generateCombinations,
} from '@nx/devkit/src/utils/add-plugin';
import { createNodes } from '../../plugins/plugin';
@ -44,7 +44,7 @@ export async function remixInitGeneratorInternal(tree: Tree, options: Schema) {
nxJson.useInferencePlugins !== false;
options.addPlugin ??= addPluginDefault;
if (options.addPlugin) {
await addPlugin(
await addPluginV1(
tree,
await createProjectGraphAsync(),
'@nx/remix/plugin',

View File

@ -7,7 +7,7 @@ import {
} from '@nx/devkit';
import { nxVersion } from '../../utils/versions';
import { Schema } from './schema';
import { addPlugin } from '@nx/devkit/src/utils/add-plugin';
import { addPluginV1 } from '@nx/devkit/src/utils/add-plugin';
import { createNodes } from '../../plugins/plugin';
export async function rollupInitGenerator(tree: Tree, schema: Schema) {
@ -25,7 +25,7 @@ export async function rollupInitGenerator(tree: Tree, schema: Schema) {
schema.addPlugin ??= process.env.NX_ADD_PLUGINS !== 'false';
if (schema.addPlugin) {
await addPlugin(
await addPluginV1(
tree,
await createProjectGraphAsync(),
'@nx/rollup/plugin',

View File

@ -10,7 +10,7 @@ import {
updateJson,
updateNxJson,
} from '@nx/devkit';
import { addPlugin } from '@nx/devkit/src/utils/add-plugin';
import { addPluginV1 } from '@nx/devkit/src/utils/add-plugin';
import { gte } from 'semver';
import { createNodes } from '../../plugins/plugin';
import {
@ -102,7 +102,7 @@ export async function initGeneratorInternal(tree: Tree, schema: Schema) {
schema.addPlugin ??= addPluginDefault;
if (schema.addPlugin) {
await addPlugin(
await addPluginV1(
tree,
await createProjectGraphAsync(),
'@nx/storybook/plugin',

View File

@ -7,7 +7,7 @@ import {
Tree,
updateNxJson,
} from '@nx/devkit';
import { addPlugin } from '@nx/devkit/src/utils/add-plugin';
import { addPluginV1 } from '@nx/devkit/src/utils/add-plugin';
import { createNodes } from '../../plugins/plugin';
import { InitGeneratorSchema } from './schema';
@ -60,7 +60,7 @@ export async function initGeneratorInternal(
schema.addPlugin ??= addPluginDefault;
if (schema.addPlugin) {
await addPlugin(
await addPluginV1(
tree,
await createProjectGraphAsync(),
'@nx/vite/plugin',

View File

@ -6,7 +6,7 @@ import {
readNxJson,
Tree,
} from '@nx/devkit';
import { addPlugin } from '@nx/devkit/src/utils/add-plugin';
import { addPluginV1 } from '@nx/devkit/src/utils/add-plugin';
import { createNodes } from '../../plugins/plugin';
import { nxVersion, webpackCliVersion } from '../../utils/versions';
import { Schema } from './schema';
@ -23,7 +23,7 @@ export async function webpackInitGeneratorInternal(tree: Tree, schema: Schema) {
schema.addPlugin ??= addPluginDefault;
if (schema.addPlugin) {
await addPlugin(
await addPluginV1(
tree,
await createProjectGraphAsync(),
'@nx/webpack/plugin',