Skip to content

Commit 05b9c3f

Browse files
committed
Let gradle handle resolving included builds!
This should handle included builds that are deeper than root-level and also handle included builds that are inserted by scripts (expo!) Signed-off-by: Roland Asmann <[email protected]>
1 parent 4b03f70 commit 05b9c3f

File tree

7 files changed

+58
-168
lines changed

7 files changed

+58
-168
lines changed

.github/workflows/repotests.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,6 @@ jobs:
631631
COCOA_PODSPEC_REPLACEMENTS: "/([\\s\\S]+)/=require File.join(File.dirname(`node --print \"require.resolve('expo/package.json')\"`), \"scripts/autolinking\")<NEWLINE>require File.join(File.dirname(`node --print \"require.resolve('react-native/package.json')\"`), \"scripts/react_native_pods\")<NEWLINE>$1"
632632
COCOA_RESOLVE_FROM_NODE_EXCLUSION_DIRS: sdks,third-party-podspecs
633633
GRADLE_ARGS_DEPENDENCIES: "--configuration releaseRuntimeClasspath"
634-
GRADLE_INCLUDED_BUILDS: :gradle-plugin
635634
GRADLE_SKIP_MODULES: root
636635
shell: bash
637636
- name: repotests expo not mac
@@ -650,7 +649,6 @@ jobs:
650649
COCOA_FULL_SCAN: false
651650
COCOA_RESOLVE_FROM_NODE_EXCLUSION_DIRS: sdks,third-party-podspecs
652651
GRADLE_ARGS_DEPENDENCIES: "--configuration releaseRuntimeClasspath"
653-
GRADLE_INCLUDED_BUILDS: :gradle-plugin
654652
GRADLE_SKIP_MODULES: root
655653
shell: bash
656654
- name: repotests cocoapods

data/helpers/init.gradle

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
gradle.taskGraph.whenReady {
2+
gradle.includedBuilds.each { includedBuild ->
3+
println "<CDXGEN:includedBuild>:${includedBuild.name}"
4+
}
5+
}

lib/cli/index.js

Lines changed: 39 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ import {
3333
listFiles,
3434
} from "../helpers/envcontext.js";
3535
import { thoughtLog } from "../helpers/logger.js";
36-
37-
import { analyzeBuildSettings } from "../helpers/package_specific/gradleutils.js";
3836
import {
3937
CARGO_CMD,
4038
CLJ_CMD,
@@ -226,6 +224,14 @@ if (process.env.GRADLE_USER_HOME) {
226224
);
227225
}
228226

227+
// Construct path to gradle init script
228+
const GRADLE_INIT_SCRIPT = resolve(
229+
dirNameStr,
230+
"data",
231+
"helpers",
232+
"init.gradle",
233+
);
234+
229235
// Construct sbt cache directory
230236
const SBT_CACHE_DIR =
231237
process.env.SBT_CACHE_DIR || join(homedir(), ".ivy2", "cache");
@@ -1959,59 +1965,50 @@ export async function createJavaBom(path, options) {
19591965
options,
19601966
)
19611967
) {
1962-
let rootProjects = [null];
19631968
let allProjectsStr = [];
1964-
let rootGradleModule = {};
1965-
let includedProjectsFound = false;
19661969
if (process.env.GRADLE_INCLUDED_BUILDS) {
19671970
// Automatically add the colon prefix
1968-
const includedBuilds = process.env.GRADLE_INCLUDED_BUILDS.split(",").map(
1969-
(b) => (!b.startsWith(":") ? `:${b}` : b),
1971+
allProjectsStr = process.env.GRADLE_INCLUDED_BUILDS.split(",").map((b) =>
1972+
!b.startsWith(":") ? `:${b}` : b,
19701973
);
1971-
rootProjects = rootProjects.concat(includedBuilds);
1972-
includedProjectsFound = true;
1973-
} else {
1974-
// Automatically detect included builds
1975-
// Only from the root path for now
1976-
for (const abuildFile of [
1977-
join(gradleRootPath, "build.gradle"),
1978-
join(gradleRootPath, "build.gradle.kts"),
1979-
join(gradleRootPath, "settings.gradle"),
1980-
join(gradleRootPath, "settings.gradle.kts"),
1981-
]) {
1982-
if (!safeExistsSync(abuildFile)) {
1983-
continue;
1974+
}
1975+
let parallelPropTaskOut = executeParallelGradleProperties(
1976+
gradleRootPath,
1977+
[null],
1978+
process.env.GRADLE_INCLUDED_BUILDS
1979+
? []
1980+
: ["--init-script", GRADLE_INIT_SCRIPT],
1981+
);
1982+
if (!process.env.GRADLE_INCLUDED_BUILDS) {
1983+
const outputLines = parallelPropTaskOut.split("\n");
1984+
for (const [i, line] of outputLines.entries()) {
1985+
if (line.startsWith("Root project '")) {
1986+
break;
19841987
}
1985-
const buildSettings = analyzeBuildSettings(abuildFile);
1986-
if (buildSettings?.includedBuilds?.length) {
1987-
for (const aib of buildSettings.includedBuilds) {
1988-
if (!rootProjects.includes(aib)) {
1989-
rootProjects.push(aib);
1990-
includedProjectsFound = true;
1991-
}
1988+
if (line.startsWith("<CDXGEN:includedBuild>")) {
1989+
const includedBuild = line.split(">");
1990+
if (!allProjectsStr.includes(includedBuild[1].trim())) {
1991+
allProjectsStr.push(includedBuild[1].trim());
19921992
}
1993-
break;
19941993
}
19951994
}
19961995
}
1997-
if (includedProjectsFound) {
1996+
if (allProjectsStr.length > 0) {
19981997
thoughtLog(
1999-
`Wait, this gradle project uses composite builds. I must carefully process these ${rootProjects.length} projects including the root.`,
1998+
`Wait, this gradle project uses composite builds. I must carefully process these ${allProjectsStr.length} projects, in addition to the root.`,
20001999
);
20012000
if (DEBUG_MODE) {
2002-
console.log(
2003-
`Additional root projects: ${rootProjects.join(" ").trim()}.`,
2004-
);
2001+
console.log(`Composite builds: ${allProjectsStr.join(" ").trim()}.`);
20052002
}
2003+
parallelPropTaskOut = parallelPropTaskOut.concat(
2004+
"\n",
2005+
executeParallelGradleProperties(gradleRootPath, allProjectsStr),
2006+
);
2007+
allProjectsStr = [];
20062008
}
2007-
const parallelPropTaskOut = executeParallelGradleProperties(
2008-
gradleRootPath,
2009-
rootProjects,
2010-
);
20112009
const splitPropTaskOut = splitOutputByGradleProjects(parallelPropTaskOut, [
20122010
"properties",
20132011
]);
2014-
20152012
for (const [key, propTaskOut] of splitPropTaskOut.entries()) {
20162013
const retMap = parseGradleProperties(propTaskOut);
20172014
const rootProject = retMap.rootProject;
@@ -2020,26 +2017,18 @@ export async function createJavaBom(path, options) {
20202017
rootProject,
20212018
retMap.metadata,
20222019
);
2020+
if (!key.startsWith(":")) {
2021+
parentComponent = rootComponent;
2022+
}
20232023
gradleModules.set(key, rootComponent);
2024-
if (!rootProjects.includes(key)) {
2025-
if (rootGradleModule.name) {
2026-
if (DEBUG_MODE) {
2027-
console.log(
2028-
`Received new root component: ${rootComponent.name} with key ${key}. Please verify the value used for included builds. Using the name ${rootGradleModule.name}.`,
2029-
);
2030-
}
2031-
} else {
2032-
rootGradleModule = rootComponent;
2033-
}
2034-
} else if (!allProjectsAddedPurls.includes(rootComponent["purl"])) {
2024+
if (!allProjectsAddedPurls.includes(rootComponent["purl"])) {
20352025
allProjects.push(rootComponent);
20362026
rootDependsOn.add(rootComponent["bom-ref"]);
20372027
allProjectsAddedPurls.push(rootComponent["purl"]);
20382028
}
20392029
allProjectsStr = allProjectsStr.concat(retMap.projects);
20402030
}
20412031
}
2042-
parentComponent = rootGradleModule;
20432032
// Get the sub-project properties and set the root dependencies
20442033
if (allProjectsStr?.length) {
20452034
const modulesToSkip = process.env.GRADLE_SKIP_MODULES

lib/helpers/package_specific/gradleutils.js

Lines changed: 0 additions & 48 deletions
This file was deleted.

lib/helpers/package_specific/gradleutils.test.js

Lines changed: 0 additions & 65 deletions
This file was deleted.

lib/helpers/utils.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3998,13 +3998,20 @@ export function parseGradleProperties(rawOutput, gradleModuleName = null) {
39983998
*
39993999
* @param {string} dir Directory to execute the command
40004000
* @param {array} allProjectsStr List of all sub-projects (including the preceding `:`)
4001+
* @param {array} extraArgs List of extra arguments to use when calling gradle
40014002
*
40024003
* @returns {string} The combined output for all subprojects of the Gradle properties task
40034004
*/
4004-
export function executeParallelGradleProperties(dir, allProjectsStr) {
4005+
export function executeParallelGradleProperties(
4006+
dir,
4007+
allProjectsStr,
4008+
extraArgs = [],
4009+
) {
40054010
const gradleCmd = getGradleCommand(dir, null);
40064011
const gradleArgs = buildGradleCommandArguments(
4007-
process.env.GRADLE_ARGS ? process.env.GRADLE_ARGS.split(" ") : [],
4012+
extraArgs.concat(
4013+
process.env.GRADLE_ARGS ? process.env.GRADLE_ARGS.split(" ") : [],
4014+
),
40084015
allProjectsStr.map((project) =>
40094016
project ? `${project}:properties` : "properties",
40104017
),

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@
3434
{
3535
"name": "Adam Setch",
3636
"url": "https://github.com/setchy"
37+
},
38+
{
39+
"name": "Roland Asmann",
40+
"url": "https://github.com/malice00"
3741
}
3842
],
3943
"type": "module",
@@ -54,7 +58,7 @@
5458
"cdx-verify": "bin/verify.js"
5559
},
5660
"scripts": {
57-
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js --inject-globals false lib/managers/docker.test.js lib/helpers/package_specific/gradleutils.test.js lib/helpers/utils.test.js lib/helpers/display.test.js lib/stages/postgen/postgen.test.js lib/evinser/swiftsem.test.js",
61+
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js --inject-globals false lib/managers/docker.test.js lib/helpers/utils.test.js lib/helpers/display.test.js lib/stages/postgen/postgen.test.js lib/evinser/swiftsem.test.js",
5862
"watch": "node --experimental-vm-modules node_modules/jest/bin/jest.js --watch --inject-globals false",
5963
"lint:check": "biome check",
6064
"lint": "biome check --fix",

0 commit comments

Comments
 (0)