@@ -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
0 commit comments