-
Notifications
You must be signed in to change notification settings - Fork 19
/
setup.gradle
233 lines (200 loc) · 7.45 KB
/
setup.gradle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
def renameConfig = [
templateName : "template",
templateAppId : "template.app.id",
templateMaterialThemeName : "TemplateTheme",
templateApplicationClassName: "TemplateApp",
newPackage : "aaa.yourname.app",
newProjectName : "Your Project",
newMaterialThemeName : "MyMaterialTheme",
newApplicationClassName : "MyApp",
useHiltDependencies : true,
useRoomDependencies : true,
useRetrofitDependencies : true,
usePaparazziDependencies : true,
useRenovateDependencies : true,
]
task deleteSetupCode() {
def workflowsFolder = "${rootDir}/.github/workflows"
def buildscriptsFolder = "${rootDir}/buildscripts"
def templateChangeWorkflowFile = "$workflowsFolder/template_change_test.yml"
def macosHooksWorkflowFile = "$workflowsFolder/install_git_hooks_macos.yml"
def windowsHooksWorkflowFile = "$workflowsFolder/install_git_hooks_windows.yml"
def setupGradle = "$buildscriptsFolder/setup.gradle"
def renovateFile = "${rootDir}/renovate.json"
doLast {
removeTextFromFile("${rootDir}/build.gradle.kts", "setup.gradle")
delete(templateChangeWorkflowFile)
delete(macosHooksWorkflowFile)
delete(windowsHooksWorkflowFile)
delete(setupGradle)
if (renameConfig.useRenovateDependencies != true) {
println("Removing renovate dependencies")
delete(renovateFile)
}
}
}
task renameAppPackage(type: Copy) {
description "Renames the template package in the app module."
group null
def newPackageAsDirectory = renameConfig.newPackage.replaceAll("\\.", "/")
def startingDirectory = "${rootDir}/app/src/main/java/${renameConfig.templateName}"
def endingDirectory = "${rootDir}/app/src/main/java/${newPackageAsDirectory}"
from(startingDirectory)
into(endingDirectory)
// Replace package statements
filter { line ->
line.replaceAll(
"package ${renameConfig.templateName}",
"package ${renameConfig.newPackage}"
)
}
// Replace import statements
filter { line ->
line.replaceAll(
"import ${renameConfig.templateName}",
"import ${renameConfig.newPackage}"
)
}
// Replace Theme references. We can just replace on name,
// which covers both imports and function calls.
filter { line ->
line.replaceAll(
"${renameConfig.templateMaterialThemeName}",
"${renameConfig.newMaterialThemeName}"
)
}
// Replace application class references
filter { line ->
line.replaceAll(
"${renameConfig.templateApplicationClassName}",
"${renameConfig.newApplicationClassName}",
)
}
rename { fileName ->
if (fileName.contains("${renameConfig.templateApplicationClassName}")) {
fileName.replace(
"${renameConfig.templateApplicationClassName}",
"${renameConfig.newApplicationClassName}",
)
} else {
fileName
}
}
doLast {
delete(startingDirectory)
}
}
task replaceTemplateReferences {
description "Replaces references to template in various files."
group null
doLast {
replaceTextInFile(
"${rootDir}/app/src/main/AndroidManifest.xml",
"${renameConfig.templateName}.MainActivity",
"${renameConfig.newPackage}.MainActivity",
)
replaceTextInFile(
"${rootDir}/app/src/main/AndroidManifest.xml",
".${renameConfig.templateApplicationClassName}",
".${renameConfig.newApplicationClassName}",
)
replaceTextInFile(
"${rootDir}/app/build.gradle.kts",
"namespace = \"${renameConfig.templateName}\"",
"namespace = \"${renameConfig.newPackage}\"",
)
replaceTextInFile(
"${rootDir}/app/build.gradle.kts",
"applicationId = \"${renameConfig.templateAppId}\"",
"applicationId = \"${renameConfig.newPackage}\"",
)
replaceTextInFile(
"${rootDir}/settings.gradle.kts",
"rootProject.name = \"${renameConfig.templateName}\"",
"rootProject.name = \"${renameConfig.newProjectName}\"",
)
replaceTextInFile(
"${rootDir}/app/src/main/res/values/strings.xml",
"<string name=\"app_name\">${renameConfig.templateName}</string>",
"<string name=\"app_name\">${renameConfig.newProjectName}</string>",
)
}
}
task keepOrRemoveDependencies {
description "Keeps or removes certain dependencies defined in renameConfig."
group null
doLast {
def filesWithDependencies = [
"${rootDir}/build.gradle.kts",
"${rootDir}/app/build.gradle.kts",
"${rootDir}/gradle/libs.versions.toml",
"${rootDir}/app/src/main/java/template/TemplateApp.kt",
]
filesWithDependencies.each { fileName ->
if (renameConfig.useHiltDependencies != true) {
println("Removing hilt dependencies")
removeTextFromFile(fileName, "hilt")
removeTextFromFile(fileName, "Hilt")
}
if (renameConfig.useRoomDependencies != true) {
println("Removing room dependencies")
removeTextFromFile(fileName, "room")
}
if (renameConfig.useRetrofitDependencies != true) {
println("Removing retrofit dependencies")
removeTextFromFile(fileName, "retrofit")
removeTextFromFile(fileName, "moshi")
}
if (renameConfig.usePaparazziDependencies != true) {
println("Removing paparazzi dependencies")
removeTextFromFile(fileName, "paparazzi")
}
}
}
}
project('app').tasks.named {
// startsWith is used because this applies to multiple tasks,
// like formatKotlinMain, formatKotlinTest, etc.
it.startsWith("formatKotlin")
}.configureEach {
mustRunAfter(rootProject.tasks.named("renameTemplate"))
}
task renameTemplate {
description "Runs all of the necessary template setup tasks based on the renameConfig."
group "Template Setup"
dependsOn(
keepOrRemoveDependencies,
renameAppPackage,
replaceTemplateReferences,
deleteSetupCode,
)
finalizedBy("app:formatKotlin")
doLast {
exec {
// After all setup changes happen, run a `git add` so
// folks can just immediately commit and push if they wish.
commandLine "git", "add", "${rootDir}/."
}
}
}
/**
* Replaces all instances of [text] in a given [fileName].
*/
static def replaceTextInFile(fileName, originalText, newText) {
def file = new File(fileName)
file.text = file.text.replaceAll(originalText, newText)
}
/**
* Removes all lines from the given fileName that contain some supplied text.
*/
static def removeTextFromFile(fileName, text) {
def file = new File(fileName)
List fileLines = file.readLines()
file.text = ""
fileLines.each { line ->
if (!line.contains(text)) {
file.append(line)
file.append("\n")
}
}
}