@nx/gradle 22.2.0-canary.20251208-300493e → 22.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +3 -3
- package/src/generators/init/gradle-project-graph-plugin-utils.d.ts.map +1 -1
- package/src/generators/init/gradle-project-graph-plugin-utils.js +102 -30
- package/src/utils/version-catalog-ast-utils.d.ts +5 -0
- package/src/utils/version-catalog-ast-utils.d.ts.map +1 -1
- package/src/utils/version-catalog-ast-utils.js +26 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nx/gradle",
|
|
3
|
-
"version": "22.2.0
|
|
3
|
+
"version": "22.2.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "The Nx Plugin for Gradle allows Gradle tasks to be run through Nx",
|
|
6
6
|
"repository": {
|
|
@@ -39,11 +39,11 @@
|
|
|
39
39
|
"migrations": "./migrations.json"
|
|
40
40
|
},
|
|
41
41
|
"dependencies": {
|
|
42
|
-
"@nx/devkit": "22.2.0
|
|
42
|
+
"@nx/devkit": "22.2.0",
|
|
43
43
|
"toml-eslint-parser": "^0.10.0"
|
|
44
44
|
},
|
|
45
45
|
"devDependencies": {
|
|
46
|
-
"nx": "22.2.0
|
|
46
|
+
"nx": "22.2.0"
|
|
47
47
|
},
|
|
48
48
|
"publishConfig": {
|
|
49
49
|
"access": "public"
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"gradle-project-graph-plugin-utils.d.ts","sourceRoot":"","sources":["../../../../../../packages/gradle/src/generators/init/gradle-project-graph-plugin-utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAqB,IAAI,EAAiB,MAAM,YAAY,CAAC;
|
|
1
|
+
{"version":3,"file":"gradle-project-graph-plugin-utils.d.ts","sourceRoot":"","sources":["../../../../../../packages/gradle/src/generators/init/gradle-project-graph-plugin-utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAqB,IAAI,EAAiB,MAAM,YAAY,CAAC;AASpE;;;GAGG;AACH,wBAAsB,sCAAsC,CAC1D,IAAI,EAAE,IAAI,GACT,OAAO,CAAC;IAAE,QAAQ,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,EAAE,CAAC,CASlD;AA8BD;;GAEG;AACH,wBAAsB,sBAAsB,CAC1C,cAAc,EAAE,MAAM,EACtB,aAAa,EAAE,MAAM,GACpB,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAoBxB;AAkBD;;GAEG;AACH,wBAAgB,qBAAqB,CACnC,OAAO,EAAE,MAAM,EACf,UAAU,EAAE,MAAM,GACjB,MAAM,CASR;AAED;;GAEG;AACH,wBAAsB,uBAAuB,CAC3C,IAAI,EAAE,IAAI,EACV,eAAe,GAAE,MAAkC,iBAYpD"}
|
|
@@ -8,6 +8,7 @@ const devkit_1 = require("@nx/devkit");
|
|
|
8
8
|
const versions_1 = require("../../utils/versions");
|
|
9
9
|
const path_1 = require("path");
|
|
10
10
|
const exec_gradle_1 = require("../../utils/exec-gradle");
|
|
11
|
+
const version_catalog_ast_utils_1 = require("../../utils/version-catalog-ast-utils");
|
|
11
12
|
/**
|
|
12
13
|
* Adds a `build.gradle(.kts)` file next to each `settings.gradle(.kts)` file found in the workspace.
|
|
13
14
|
* If the build.gradle file already exists, it reads its contents.
|
|
@@ -86,9 +87,60 @@ function updateNxPluginVersion(content, newVersion) {
|
|
|
86
87
|
*/
|
|
87
88
|
async function addNxProjectGraphPlugin(tree, expectedVersion = versions_1.gradleProjectGraphVersion) {
|
|
88
89
|
const files = await addBuildGradleFileNextToSettingsGradle(tree);
|
|
89
|
-
|
|
90
|
-
addNxProjectGraphPluginToBuildGradle(filePath, content, expectedVersion, tree);
|
|
91
|
-
}
|
|
90
|
+
for (const { filePath, content } of files) {
|
|
91
|
+
await addNxProjectGraphPluginToBuildGradle(filePath, content, expectedVersion, tree);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Converts a version catalog alias to a Gradle accessor path.
|
|
96
|
+
* In Gradle, dashes in aliases become dots in the accessor.
|
|
97
|
+
* e.g., "nx-project-graph" -> "nx.project.graph"
|
|
98
|
+
*/
|
|
99
|
+
function aliasToAccessorPath(alias) {
|
|
100
|
+
return alias.replace(/-/g, '.');
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Finds a version catalog file near the given build.gradle file and returns
|
|
104
|
+
* the plugin alias if the Nx project graph plugin is defined in it.
|
|
105
|
+
* Searches in the following order:
|
|
106
|
+
* 1. Standard Gradle location: gradle/libs.versions.toml relative to the build.gradle file
|
|
107
|
+
* 2. Workspace root: gradle/libs.versions.toml at the workspace root
|
|
108
|
+
* 3. Any libs.versions.toml in subdirectories of the build.gradle file
|
|
109
|
+
*/
|
|
110
|
+
async function findVersionCatalogPluginAlias(gradleFilePath, tree) {
|
|
111
|
+
const gradleDir = (0, path_1.dirname)(gradleFilePath);
|
|
112
|
+
// Check standard Gradle version catalog locations
|
|
113
|
+
const possibleCatalogPaths = [
|
|
114
|
+
// Standard location relative to the build.gradle file
|
|
115
|
+
(0, path_1.join)(gradleDir, 'gradle', 'libs.versions.toml'),
|
|
116
|
+
// Workspace root location (common in monorepos)
|
|
117
|
+
'gradle/libs.versions.toml',
|
|
118
|
+
];
|
|
119
|
+
for (const catalogPath of possibleCatalogPaths) {
|
|
120
|
+
if (tree.exists(catalogPath)) {
|
|
121
|
+
const catalogContent = tree.read(catalogPath, 'utf-8');
|
|
122
|
+
if (catalogContent) {
|
|
123
|
+
const alias = (0, version_catalog_ast_utils_1.getPluginAliasFromCatalogAst)(catalogContent, versions_1.gradleProjectGraphPluginName);
|
|
124
|
+
if (alias) {
|
|
125
|
+
return alias;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
// Fallback: search for any version catalog in subdirectories
|
|
131
|
+
const versionCatalogFiles = await (0, devkit_1.globAsync)(tree, [
|
|
132
|
+
(0, path_1.join)(gradleDir, '**/libs.versions.toml'),
|
|
133
|
+
]);
|
|
134
|
+
for (const versionCatalogPath of versionCatalogFiles) {
|
|
135
|
+
const catalogContent = tree.read(versionCatalogPath, 'utf-8');
|
|
136
|
+
if (catalogContent) {
|
|
137
|
+
const alias = (0, version_catalog_ast_utils_1.getPluginAliasFromCatalogAst)(catalogContent, versions_1.gradleProjectGraphPluginName);
|
|
138
|
+
if (alias) {
|
|
139
|
+
return alias;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
return null;
|
|
92
144
|
}
|
|
93
145
|
/**
|
|
94
146
|
* Adds or updates the Nx Project Graph plugin in the build.gradle(.kts) file.
|
|
@@ -97,9 +149,22 @@ async function addNxProjectGraphPlugin(tree, expectedVersion = versions_1.gradle
|
|
|
97
149
|
*/
|
|
98
150
|
async function addNxProjectGraphPluginToBuildGradle(gradleFilePath, buildGradleContent, expectedVersion = versions_1.gradleProjectGraphVersion, tree) {
|
|
99
151
|
const isKotlinDsl = gradleFilePath.endsWith('.kts');
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
152
|
+
// Check if a version catalog exists with the plugin alias
|
|
153
|
+
const pluginAlias = await findVersionCatalogPluginAlias(gradleFilePath, tree);
|
|
154
|
+
const versionCatalogPluginAccessor = pluginAlias
|
|
155
|
+
? `libs.plugins.${aliasToAccessorPath(pluginAlias)}`
|
|
156
|
+
: null;
|
|
157
|
+
// Determine the plugin declaration syntax based on whether we have a version catalog
|
|
158
|
+
const nxProjectGraphReportPlugin = versionCatalogPluginAccessor
|
|
159
|
+
? `alias(${versionCatalogPluginAccessor})`
|
|
160
|
+
: isKotlinDsl
|
|
161
|
+
? `id("${versions_1.gradleProjectGraphPluginName}") version("${expectedVersion}")`
|
|
162
|
+
: `id "${versions_1.gradleProjectGraphPluginName}" version "${expectedVersion}"`;
|
|
163
|
+
// Check if the plugin is already included (directly or via version catalog alias)
|
|
164
|
+
const hasPluginDirectly = buildGradleContent.includes(versions_1.gradleProjectGraphPluginName);
|
|
165
|
+
const hasPluginViaAlias = versionCatalogPluginAccessor &&
|
|
166
|
+
buildGradleContent.includes(versionCatalogPluginAccessor);
|
|
167
|
+
const hasPlugin = hasPluginDirectly || hasPluginViaAlias;
|
|
103
168
|
// Helper to add plugin to plugins block
|
|
104
169
|
function addPluginToPluginsBlock(content) {
|
|
105
170
|
return content.replace(/plugins\s*\{/, `plugins {\n ${nxProjectGraphReportPlugin}`);
|
|
@@ -110,44 +175,51 @@ async function addNxProjectGraphPluginToBuildGradle(gradleFilePath, buildGradleC
|
|
|
110
175
|
}
|
|
111
176
|
// Helper to add plugin application to allprojects
|
|
112
177
|
function addPluginToAllProjects(content) {
|
|
113
|
-
const applyPlugin =
|
|
114
|
-
? `plugin(
|
|
115
|
-
:
|
|
178
|
+
const applyPlugin = versionCatalogPluginAccessor
|
|
179
|
+
? `plugin(${versionCatalogPluginAccessor})`
|
|
180
|
+
: isKotlinDsl
|
|
181
|
+
? `plugin("${versions_1.gradleProjectGraphPluginName}")`
|
|
182
|
+
: `plugin("${versions_1.gradleProjectGraphPluginName}")`;
|
|
116
183
|
return `${content}\nallprojects {\n apply {\n ${applyPlugin}\n }\n}`;
|
|
117
184
|
}
|
|
118
|
-
//
|
|
185
|
+
// Add to plugins block if plugin not already present
|
|
119
186
|
if (buildGradleContent.includes('plugins {')) {
|
|
120
|
-
if (
|
|
121
|
-
//
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
187
|
+
if (hasPlugin) {
|
|
188
|
+
// Plugin already exists - update version if needed (only for direct declarations)
|
|
189
|
+
if (hasPluginDirectly) {
|
|
190
|
+
const currentVersion = await extractNxPluginVersion(gradleFilePath, buildGradleContent);
|
|
191
|
+
if (currentVersion && currentVersion !== expectedVersion) {
|
|
192
|
+
buildGradleContent = updateNxPluginVersion(buildGradleContent, expectedVersion);
|
|
193
|
+
}
|
|
125
194
|
}
|
|
126
195
|
}
|
|
127
196
|
else {
|
|
128
|
-
// Add plugin to plugins block
|
|
129
197
|
buildGradleContent = addPluginToPluginsBlock(buildGradleContent);
|
|
130
198
|
}
|
|
131
199
|
}
|
|
132
200
|
else {
|
|
133
|
-
// Add plugins block if missing
|
|
134
201
|
buildGradleContent = addPluginsBlock(buildGradleContent);
|
|
135
202
|
}
|
|
136
|
-
//
|
|
137
|
-
|
|
138
|
-
if (
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
203
|
+
// Skip allprojects handling if plugin was already correctly declared via version catalog alias
|
|
204
|
+
// In that case, the plugin is already applied and doesn't need to be propagated via allprojects
|
|
205
|
+
if (!hasPluginViaAlias) {
|
|
206
|
+
const applyPluginPattern = versionCatalogPluginAccessor
|
|
207
|
+
? new RegExp(`\\s*plugin\\(${versionCatalogPluginAccessor.replace(/\./g, '\\.')}\\)`)
|
|
208
|
+
: new RegExp(`\\s*plugin\\(["']${versions_1.gradleProjectGraphPluginName}["']\\)`);
|
|
209
|
+
if (buildGradleContent.includes('allprojects {')) {
|
|
210
|
+
if (!applyPluginPattern.test(buildGradleContent)) {
|
|
211
|
+
const applyPlugin = versionCatalogPluginAccessor
|
|
212
|
+
? `plugin(${versionCatalogPluginAccessor})`
|
|
213
|
+
: isKotlinDsl
|
|
214
|
+
? `plugin("${versions_1.gradleProjectGraphPluginName}")`
|
|
215
|
+
: `plugin "${versions_1.gradleProjectGraphPluginName}"`;
|
|
216
|
+
buildGradleContent = buildGradleContent.replace(/allprojects\s*\{/, `allprojects {\n apply ${applyPlugin}`);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
else {
|
|
220
|
+
buildGradleContent = addPluginToAllProjects(buildGradleContent);
|
|
145
221
|
}
|
|
146
222
|
}
|
|
147
|
-
else {
|
|
148
|
-
buildGradleContent = addPluginToAllProjects(buildGradleContent);
|
|
149
|
-
}
|
|
150
|
-
// 3. Write and return updated content
|
|
151
223
|
tree.write(gradleFilePath, buildGradleContent);
|
|
152
224
|
return buildGradleContent;
|
|
153
225
|
}
|
|
@@ -4,6 +4,11 @@ export declare function findVersionCatalogFiles(tree: Tree): Promise<string[]>;
|
|
|
4
4
|
* Updates a plugin version in a TOML catalog while preserving formatting
|
|
5
5
|
*/
|
|
6
6
|
export declare function updatePluginVersionInCatalogAst(sourceText: string, pluginName: string, newVersion: string): string | null;
|
|
7
|
+
/**
|
|
8
|
+
* Gets the plugin alias from a version catalog for a given plugin ID
|
|
9
|
+
* Returns the alias name (e.g., "nxProjectGraph") if found, null otherwise
|
|
10
|
+
*/
|
|
11
|
+
export declare function getPluginAliasFromCatalogAst(sourceText: string, pluginName: string): string | null;
|
|
7
12
|
/**
|
|
8
13
|
* Extracts plugin version from catalog using AST parsing
|
|
9
14
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"version-catalog-ast-utils.d.ts","sourceRoot":"","sources":["../../../../../packages/gradle/src/utils/version-catalog-ast-utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAa,IAAI,EAAE,MAAM,YAAY,CAAC;AAI7C,wBAAsB,uBAAuB,CAAC,IAAI,EAAE,IAAI,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CAW3E;
|
|
1
|
+
{"version":3,"file":"version-catalog-ast-utils.d.ts","sourceRoot":"","sources":["../../../../../packages/gradle/src/utils/version-catalog-ast-utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAa,IAAI,EAAE,MAAM,YAAY,CAAC;AAI7C,wBAAsB,uBAAuB,CAAC,IAAI,EAAE,IAAI,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CAW3E;AAkKD;;GAEG;AACH,wBAAgB,+BAA+B,CAC7C,UAAU,EAAE,MAAM,EAClB,UAAU,EAAE,MAAM,EAClB,UAAU,EAAE,MAAM,GACjB,MAAM,GAAG,IAAI,CA6Gf;AAED;;;GAGG;AACH,wBAAgB,4BAA4B,CAC1C,UAAU,EAAE,MAAM,EAClB,UAAU,EAAE,MAAM,GACjB,MAAM,GAAG,IAAI,CAmBf;AAED;;GAEG;AACH,wBAAgB,kCAAkC,CAChD,UAAU,EAAE,MAAM,EAClB,UAAU,EAAE,MAAM,GACjB,MAAM,GAAG,IAAI,CA4Df;AAED;;GAEG;AACH,wBAAsB,kCAAkC,CACtD,IAAI,EAAE,IAAI,EACV,eAAe,EAAE,MAAM,GACtB,OAAO,CAAC,OAAO,CAAC,CAkClB"}
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.findVersionCatalogFiles = findVersionCatalogFiles;
|
|
4
4
|
exports.updatePluginVersionInCatalogAst = updatePluginVersionInCatalogAst;
|
|
5
|
+
exports.getPluginAliasFromCatalogAst = getPluginAliasFromCatalogAst;
|
|
5
6
|
exports.extractPluginVersionFromCatalogAst = extractPluginVersionFromCatalogAst;
|
|
6
7
|
exports.updateNxPluginVersionInCatalogsAst = updateNxPluginVersionInCatalogsAst;
|
|
7
8
|
const devkit_1 = require("@nx/devkit");
|
|
@@ -92,11 +93,12 @@ function findPluginConfig(pluginsTable, pluginName) {
|
|
|
92
93
|
for (const item of pluginsTable.body) {
|
|
93
94
|
if (item.type === 'TOMLKeyValue') {
|
|
94
95
|
const value = item.value;
|
|
96
|
+
const alias = getKeyName(item.key);
|
|
95
97
|
if (value.type === 'TOMLValue' && value.kind === 'string') {
|
|
96
98
|
// Simple format: plugin = "id:version"
|
|
97
99
|
const stringValue = value.value;
|
|
98
100
|
if (stringValue.startsWith(`${pluginName}:`)) {
|
|
99
|
-
return { keyValue: item, format: 'simple' };
|
|
101
|
+
return { keyValue: item, format: 'simple', alias };
|
|
100
102
|
}
|
|
101
103
|
}
|
|
102
104
|
else if (value.type === 'TOMLInlineTable') {
|
|
@@ -105,7 +107,7 @@ function findPluginConfig(pluginsTable, pluginName) {
|
|
|
105
107
|
if (idKeyValue) {
|
|
106
108
|
const idValue = getStringValue(idKeyValue.value);
|
|
107
109
|
if (idValue === pluginName) {
|
|
108
|
-
return { keyValue: item, format: 'object' };
|
|
110
|
+
return { keyValue: item, format: 'object', alias };
|
|
109
111
|
}
|
|
110
112
|
}
|
|
111
113
|
}
|
|
@@ -252,6 +254,28 @@ function updatePluginVersionInCatalogAst(sourceText, pluginName, newVersion) {
|
|
|
252
254
|
return null;
|
|
253
255
|
}
|
|
254
256
|
}
|
|
257
|
+
/**
|
|
258
|
+
* Gets the plugin alias from a version catalog for a given plugin ID
|
|
259
|
+
* Returns the alias name (e.g., "nxProjectGraph") if found, null otherwise
|
|
260
|
+
*/
|
|
261
|
+
function getPluginAliasFromCatalogAst(sourceText, pluginName) {
|
|
262
|
+
try {
|
|
263
|
+
const ast = (0, toml_eslint_parser_1.parseTOML)(sourceText);
|
|
264
|
+
const pluginsTable = findPluginsTable(ast);
|
|
265
|
+
if (!pluginsTable) {
|
|
266
|
+
return null;
|
|
267
|
+
}
|
|
268
|
+
const pluginConfig = findPluginConfig(pluginsTable, pluginName);
|
|
269
|
+
if (!pluginConfig) {
|
|
270
|
+
return null;
|
|
271
|
+
}
|
|
272
|
+
return pluginConfig.alias;
|
|
273
|
+
}
|
|
274
|
+
catch (error) {
|
|
275
|
+
console.error('Error parsing TOML with AST:', error);
|
|
276
|
+
return null;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
255
279
|
/**
|
|
256
280
|
* Extracts plugin version from catalog using AST parsing
|
|
257
281
|
*/
|