Skip to content

Commit fda7a28

Browse files
authored
settings.gradle.kts: add subprojects.local.cfg` support (#8074)
1 parent de76cec commit fda7a28

2 files changed

Lines changed: 47 additions & 7 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ smoke-tests/build-debug-headGit-smoke-test
1414
smoke-tests/firehorn.log
1515
macrobenchmark-output.json
1616
vertexai-sdk-test-data/
17+
/subprojects.local.cfg
1718

1819
# generated Terraform docs
1920
.terraform/*

settings.gradle.kts

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,37 @@ dependencyResolutionManagement {
3939
}
4040
}
4141

42+
fun readSubprojectsFile(
43+
subprojectsFileNames: Iterable<String>,
44+
subprojectsDirectory: Directory
45+
): String {
46+
val subprojectsFileText =
47+
subprojectsFileNames.firstNotNullOfOrNull {
48+
val subprojectsRegularFile = subprojectsDirectory.file(it)
49+
val subprojectsFile = subprojectsRegularFile.asFile
50+
val fileTextProvider = providers.fileContents(subprojectsRegularFile).asText
51+
if (!fileTextProvider.isPresent) {
52+
logger.info("Skipping non-existent subprojects file: {}", subprojectsFile.absolutePath)
53+
null
54+
} else {
55+
logger.info("Loading subprojects file: {}", subprojectsFile.absolutePath)
56+
fileTextProvider.get()
57+
}
58+
}
59+
60+
return subprojectsFileText
61+
?: error(
62+
"No subprojects files found in ${subprojectsDirectory.asFile.absolutePath} " +
63+
"(looked for: ${subprojectsFileNames.joinToString()})"
64+
)
65+
}
66+
67+
fun readSubprojectsFile(): String {
68+
val subprojectsFileNames = listOf("subprojects.local.cfg", "subprojects.cfg")
69+
val subprojectsDirectory = @Suppress("UnstableApiUsage") layout.settingsDirectory
70+
return readSubprojectsFile(subprojectsFileNames, subprojectsDirectory)
71+
}
72+
4273
/**
4374
* Parses the input file and returns a list of subprojects.
4475
*
@@ -47,11 +78,15 @@ dependencyResolutionManagement {
4778
* - Text following a '#' character on the same line is treated as a comment.
4879
* - Other lines are treated as project paths.
4980
*/
50-
fun discoverSubprojects(subprojectsFile: File): List<String> {
51-
return subprojectsFile
52-
.readLines()
53-
.map { it.split("#").firstOrNull()?.trim() ?: "" }
54-
.filter { it.isNotEmpty() }
81+
fun discoverSubprojects(subprojectsFileContents: String): List<String> {
82+
return subprojectsFileContents.lines().mapNotNull { line ->
83+
line.substringBefore('#').trim().takeIf { it.isNotEmpty() }
84+
}
85+
}
86+
87+
fun discoverSubprojects(): List<String> {
88+
val subprojectsFileText = readSubprojectsFile()
89+
return discoverSubprojects(subprojectsFileText)
5590
}
5691

5792
/**
@@ -93,8 +128,12 @@ fun setBuildScripts(project: ProjectDescriptor) {
93128
}
94129
}
95130

96-
/** Note: Do not add subprojects to this file. Instead, add them to `subprojects.cfg`. */
97-
discoverSubprojects(file("subprojects.cfg")).forEach { include(":$it") }
131+
// Add subprojects to `subprojects.cfg` instead of modifying `settings.gradle.kts` directly.
132+
//
133+
// If `subprojects.local.cfg` exists, it overrides `subprojects.cfg`. This is useful for local
134+
// development to load only a subset of submodules, which reduces build times and improves
135+
// IDE performance by reducing the number of files to index.
136+
discoverSubprojects().forEach { include(":$it") }
98137

99138
setBuildScripts(rootProject)
100139

0 commit comments

Comments
 (0)