Skip to content

Commit 1589e72

Browse files
authored
Added coroutine module to gradle settings. (#58)
1 parent 342b08e commit 1589e72

File tree

3 files changed

+99
-36
lines changed

3 files changed

+99
-36
lines changed

core/src/main/kotlin/com/mitteloupe/cag/core/generation/SettingsFileUpdater.kt

Lines changed: 53 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -15,34 +15,45 @@ class SettingsFileUpdater(
1515
) {
1616
updateSettingsIfPresent(
1717
startDirectory = startDirectory,
18-
groupPrefix = ":features:$featureNameLowerCase",
18+
moduleNamesPrefix = ":features:$featureNameLowerCase",
1919
moduleNames = listOf("ui", "presentation", "domain", "data")
2020
)
2121
}
2222

2323
fun updateDataSourceSettingsIfPresent(startDirectory: File) {
2424
updateSettingsIfPresent(
2525
startDirectory = startDirectory,
26-
groupPrefix = ":datasource",
26+
moduleNamesPrefix = ":datasource",
2727
moduleNames = listOf("source", "implementation")
2828
)
2929
}
3030

3131
fun updateArchitectureSettingsIfPresent(projectRoot: File) {
3232
updateSettingsIfPresent(
3333
startDirectory = projectRoot,
34-
groupPrefix = ":architecture",
34+
moduleNamesPrefix = ":architecture",
3535
moduleNames = listOf("ui", "instrumentation-test", "presentation", "presentation-test", "domain")
3636
)
3737
}
3838

39+
fun updateModuleSettingsIfPresent(
40+
projectRoot: File,
41+
moduleName: String
42+
) {
43+
updateSettingsIfPresent(
44+
startDirectory = projectRoot,
45+
moduleNamesPrefix = moduleName,
46+
moduleNames = emptyList()
47+
)
48+
}
49+
3950
private fun updateSettingsIfPresent(
4051
startDirectory: File,
41-
groupPrefix: String,
52+
moduleNamesPrefix: String,
4253
moduleNames: List<String>
4354
) {
4455
val settingsFile = findSettingsFile(startDirectory) ?: return
45-
updateIncludes(settingsFile, groupPrefix, moduleNames)
56+
updateIncludes(settingsFile, moduleNamesPrefix, moduleNames)
4657
}
4758

4859
private fun findSettingsFile(startDirectory: File): File? {
@@ -66,7 +77,7 @@ class SettingsFileUpdater(
6677

6778
private fun updateIncludes(
6879
settingsFile: File,
69-
groupPrefix: String,
80+
moduleNamesPrefix: String,
7081
moduleNames: List<String>
7182
) {
7283
val originalFileContent =
@@ -75,9 +86,9 @@ class SettingsFileUpdater(
7586
throw GenerationException("Failed to read ${settingsFile.name}: ${it.message}")
7687
}
7788

78-
val groupedIncludeKts = $$"include(\"$$groupPrefix:$module\")"
79-
val groupedIncludeGroovyDouble = $$"include \"$$groupPrefix:$module\""
80-
val groupedIncludeGroovySingle = $$"include '$$groupPrefix:$module'"
89+
val groupedIncludeKts = $$"include(\"$$moduleNamesPrefix:$module\")"
90+
val groupedIncludeGroovyDouble = $$"include \"$$moduleNamesPrefix:$module\""
91+
val groupedIncludeGroovySingle = $$"include '$$moduleNamesPrefix:$module'"
8192

8293
val hasGroupedInclude =
8394
originalFileContent.contains(groupedIncludeKts) ||
@@ -88,7 +99,7 @@ class SettingsFileUpdater(
8899
return
89100
}
90101

91-
val modulePaths = moduleNames.map { moduleName -> "$groupPrefix:$moduleName" }
102+
val modulePaths = moduleNames.map { moduleName -> "$moduleNamesPrefix:$moduleName" }
92103

93104
fun includeRegexesFor(path: String): List<Regex> =
94105
listOf(
@@ -97,13 +108,14 @@ class SettingsFileUpdater(
97108
)
98109

99110
fun isModuleIncludedIndividually(moduleName: String): Boolean {
100-
val pathWithRoot = "$groupPrefix:$moduleName"
111+
val pathWithRoot = "$moduleNamesPrefix:$moduleName"
101112
val pathWithoutRoot = pathWithRoot.removePrefix(":")
102113
val regexes = includeRegexesFor(pathWithRoot) + includeRegexesFor(pathWithoutRoot)
103114
return regexes.any { regex -> originalFileContent.contains(regex) }
104115
}
105116

106-
val allIncludedIndividually = moduleNames.all { moduleName -> isModuleIncludedIndividually(moduleName) }
117+
val allIncludedIndividually =
118+
moduleNames.isNotEmpty() && moduleNames.all { moduleName -> isModuleIncludedIndividually(moduleName) }
107119

108120
if (allIncludedIndividually) {
109121
return
@@ -115,36 +127,42 @@ class SettingsFileUpdater(
115127
.filterNot { line ->
116128
modulePaths.any { pathWithRoot ->
117129
val pathWithoutRoot = pathWithRoot.removePrefix(":")
118-
line.contains("include(\"$pathWithRoot\")") ||
119-
line.contains("include '$pathWithRoot'") ||
120-
line.contains("include \"$pathWithRoot\"") ||
121-
line.contains("include(\"$pathWithoutRoot\")") ||
122-
line.contains("include '$pathWithoutRoot'") ||
123-
line.contains("include \"$pathWithoutRoot\"")
130+
line.contains("include\\s*\\(\\s*(['\"]):?$pathWithoutRoot\\1\\s*\\)".toRegex()) ||
131+
line.contains("include\\s+(['\"]):?$pathWithoutRoot\\1".toRegex())
124132
}
125133
}.joinToString(separator = "\n", postfix = if (originalFileContent.endsWith("\n")) "\n" else "")
126134

127135
val contentToAppend =
128136
buildString {
129-
if (!filteredContent.endsWith("\n")) append('\n')
130-
val modulesKtsBlock = moduleNames.joinToString(",\n") { " \"$it\"" }
131-
val modulesGroovyBlock = moduleNames.joinToString(",\n") { " '$it'" }
137+
if (!filteredContent.endsWith("\n")) {
138+
append('\n')
139+
}
132140
if (settingsFile.name.endsWith(".kts")) {
133-
append(
134-
"setOf(\n" +
135-
modulesKtsBlock +
136-
"\n).forEach { module ->\n" +
137-
$$" include(\"$$groupPrefix:$module\")\n" +
138-
"}"
139-
)
141+
if (moduleNames.isEmpty()) {
142+
append("include(\"$moduleNamesPrefix\")\n")
143+
} else {
144+
val modulesKtsBlock = moduleNames.joinToString(",\n") { " \"$it\"" }
145+
append(
146+
"setOf(\n" +
147+
modulesKtsBlock +
148+
"\n).forEach { module ->\n" +
149+
$$" include(\"$$moduleNamesPrefix:$module\")\n" +
150+
"}"
151+
)
152+
}
140153
} else {
141-
append(
142-
"[\n" +
143-
modulesGroovyBlock +
144-
"\n].each { module ->\n" +
145-
$$" include \"$$groupPrefix:$module\"\n" +
146-
"}"
147-
)
154+
if (moduleNames.isEmpty()) {
155+
append("include \"$moduleNamesPrefix\"\n")
156+
} else {
157+
val modulesGroovyBlock = moduleNames.joinToString(",\n") { " '$it'" }
158+
append(
159+
"[\n" +
160+
modulesGroovyBlock +
161+
"\n].each { module ->\n" +
162+
$$" include \"$$moduleNamesPrefix:$module\"\n" +
163+
"}"
164+
)
165+
}
148166
}
149167
}
150168

core/src/main/kotlin/com/mitteloupe/cag/core/generation/bulk/ArchitectureFilesGenerator.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import com.mitteloupe.cag.core.kotlinpackage.toSegments
1111
import com.mitteloupe.cag.core.option.DependencyInjection
1212
import java.io.File
1313

14+
private const val COROUTINE_MODULE_NAME = "coroutine"
15+
1416
class ArchitectureFilesGenerator(
1517
private val coroutineModuleContentGenerator: CoroutineModuleContentGenerator,
1618
private val architectureModulesContentGenerator: ArchitectureModulesContentGenerator,
@@ -58,7 +60,7 @@ class ArchitectureFilesGenerator(
5860
coroutineModuleContentGenerator
5961
.generate(
6062
projectRoot = destinationRootDirectory,
61-
coroutinePackageName = architecturePackageName.replaceAfterLast(".", "coroutine")
63+
coroutinePackageName = architecturePackageName.replaceAfterLast(".", COROUTINE_MODULE_NAME)
6264
)
6365

6466
architectureModulesContentGenerator
@@ -72,6 +74,7 @@ class ArchitectureFilesGenerator(
7274
)
7375

7476
settingsFileUpdater.updateArchitectureSettingsIfPresent(destinationRootDirectory)
77+
settingsFileUpdater.updateModuleSettingsIfPresent(destinationRootDirectory, COROUTINE_MODULE_NAME)
7578

7679
buildSrcContentCreator.writeGradleFile(destinationRootDirectory)
7780
buildSrcContentCreator.writeSettingsGradleFile(destinationRootDirectory)

core/src/test/kotlin/com/mitteloupe/cag/core/generation/SettingsFileUpdaterTest.kt

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,48 @@ class SettingsFileUpdaterTest {
582582
assertThat(content, endsWith(expectedTail))
583583
}
584584

585+
@Test
586+
fun `Given kotlin settings file when updateModuleSettingsIfPresent then appends one include`() {
587+
// Given
588+
val (projectRoot, _) =
589+
createProjectWithKotlinSettings(
590+
initialContent = "rootProject.name = \"app\"\n"
591+
)
592+
val expectedTail =
593+
"""
594+
include(":coroutines")
595+
596+
""".trimIndent()
597+
598+
// When
599+
classUnderTest.updateModuleSettingsIfPresent(projectRoot, ":coroutines")
600+
601+
// Then
602+
val content = File(projectRoot, "settings.gradle.kts").readText()
603+
assertThat(content, endsWith(expectedTail))
604+
}
605+
606+
@Test
607+
fun `Given groovy settings file when updateModuleSettingsIfPresent then appends one include`() {
608+
// Given
609+
val (projectRoot, _) =
610+
createProjectWithGroovySettings(
611+
initialContent = "rootProject.name = 'app'\n"
612+
)
613+
val expectedTail =
614+
"""
615+
include ":coroutines"
616+
617+
""".trimIndent()
618+
619+
// When
620+
classUnderTest.updateModuleSettingsIfPresent(projectRoot, ":coroutines")
621+
622+
// Then
623+
val content = File(projectRoot, "settings.gradle").readText()
624+
assertThat(content, endsWith(expectedTail))
625+
}
626+
585627
@Test
586628
fun `Given no settings files when updateArchitectureSettingsIfPresent then does nothing`() {
587629
// Given

0 commit comments

Comments
 (0)