-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
deep-clean.kts
executable file
·466 lines (390 loc) · 16.6 KB
/
deep-clean.kts
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
#!/usr/bin/env kscript
@file:DependsOn("com.offbytwo:docopt:0.6.0.20150202")
import org.docopt.Docopt
import java.io.File
import java.nio.file.Files
import java.nio.file.Paths
import java.util.concurrent.TimeUnit
import kotlin.system.exitProcess
typealias CommandLineArguments = Map<String, Any>
val usage = """
This script nukes all build caches from Gradle/Android projects.
Run this in a Gradle/Android project folder.
Usage: deep-clean [options]
Options:
-b --backup Renames files and folders instead of deleting them. Implies
--verbose.
-d --dry-run Don't delete anything. Useful for testing. Implies --verbose.
-i --ide-files This also deletes IDEA/Android Studio project files (*.iml).
If used in conjunction with --nuke it will also delete the
.idea folder in the current directory.
-p --ide-preferences ⚠️ THIS IS DANGEROUS SHIT ⚠️ Will wipe your IDE settings!
This deletes the GLOBAL IDEA/Android Studio preferences.
This option requires the --nuke option to be active too, since
it touches global system state.
--not-recursive Don't recursively search sub-folders of this folder for matches.
The default behaviour is to look for matches in sub-directories,
since things like 'build' folders and '.iml' files are not all
found at the top level of a project directory structure. This
flag is useful if you know you have matches you want to keep,
e.g., if your code contains a package with a name like 'build'.
This option severely limits the effectiveness of the deep clean.
-n --nuke ⚠️ THIS IS DANGEROUS SHIT ⚠️ Super-deep clean
This includes clearing out global folders, including:
* the global Gradle cache
* the global Maven artefacts
* the wrapper-downloaded Gradle distros
* the Gradle daemon data (logs, locks, etc.)
* the Android build cache
Nukes the entire thing from orbit — it's the only way to be sure.
-v --verbose Print detailed information about all commands.
"""
val userHome = File(System.getProperty("user.home"))
val gradleHome = locateGradleHome()
val mavenLocalRepository = locateMavenLocalRepository()
val workingDir = File(Paths.get("").toAbsolutePath().toString())
assert(userHome.exists()) { "Unable to determine the user home folder, aborting..." }
val parsedArgs: CommandLineArguments = Docopt(usage)
.withVersion("deep-clean 1.5.0")
.parse(args.toList())
val nukeItFromOrbit = parsedArgs.isFlagSet("--nuke", "-n")
val ideFiles = parsedArgs.isFlagSet("--ide-files", "-i")
val shouldAlsoClearIdePreferences = parsedArgs.isFlagSet("--ide-preferences", "-p")
val idePreferences = shouldAlsoClearIdePreferences && nukeItFromOrbit
val recursively = parsedArgs.isFlagSet("--not-recursive").not()
val dryRun = parsedArgs.isFlagSet("--dry-run", "-d")
val backup = parsedArgs.isFlagSet("--backup", "-b")
val verbose = backup || dryRun || parsedArgs.isFlagSet("--verbose", "-v")
if (shouldAlsoClearIdePreferences != idePreferences) {
println("\n⚠️ To clear the IDE preferences you must also enable nuke mode.")
}
if (dryRun) println("\nℹ️ This is a dry-run. No files will be moved/deleted.\n")
val wetRun = dryRun.not()
val gradlew = "./gradlew" + if (isOsWindows()) ".bat" else ""
if (!File(gradlew.removePrefix("./")).exists()) {
printInBold("❌ Could not find Gradle wrapper in the work directory: $gradlew")
exitProcess(-1)
}
Runtime.getRuntime().apply {
printInBold("⏳ Executing Gradle clean...")
doWithGradleWrapper {
execOnWetRun("$gradlew clean -q")
?.printOutput(onlyErrors = !verbose)
}
println()
printInBold("🔫 Killing Gradle daemon...")
doWithGradleWrapper {
execOnWetRun("$gradlew --stop")
?.printOutput()
}
println()
printInBold("🔫 Killing ADB server...")
killAdb()
println()
printInBold("🔥 Removing every 'build' folder...")
workingDir.removeSubfoldersMatching { it.name.equals("build", ignoreCase = true) }
println()
printInBold("🔥 Removing every '.gradle' folder...")
workingDir.removeSubfoldersMatching { it.name.equals(".gradle", ignoreCase = true) }
println()
if (ideFiles) deleteIdeaProjectFiles()
if (idePreferences) {
printIdePreferencesWarning(timeoutSeconds = 3)
clearIdePreferences()
}
if (nukeItFromOrbit) {
printNukeModeWarning(timeoutSeconds = 3)
if (ideFiles) nukeIdeaProjectSettingsFolder()
nukeGlobalCaches()
}
printInBold("🔫 Killing Kotlin compile daemon...")
println(" ℹ️ Note: this kills any CLI Java instance running (including this script)")
execOnWetRun("killall java")
println()
}
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
fun locateGradleHome(): File? {
val envGradleHome = System.getenv("GRADLE_HOME")
?.let { File(it) }
val userGradleHome = File(userHome, ".gradle")
return when {
envGradleHome?.exists() == true -> envGradleHome
userGradleHome.exists() -> userGradleHome
else -> null
}
}
fun locateMavenLocalRepository(): File? {
return File(userHome, ".m2").takeIf { it.exists() }
}
fun CommandLineArguments.isFlagSet(vararg flagAliases: String): Boolean =
flagAliases.map { this[it] as Boolean? }
.first { it != null }!!
fun Runtime.execOnWetRun(command: String) = if (wetRun) exec(command) else null
fun Process.printOutput(onlyErrors: Boolean = true) {
if (onlyErrors.not()) {
inputStream.bufferedReader().lines().forEach { println(" $it") }
}
errorStream.bufferedReader().lines().forEach { println(" $it") }
}
fun Process.printIfNoError(message: String) {
if (errorStream.bufferedReader().lineSequence().none()) {
println(" $message")
}
}
fun Runtime.doWithGradleWrapper(action: () -> Unit) {
if (!Files.exists(Paths.get("gradlew")) && !isExecutableOnPath("adb")) {
println("⚠️ Gradle wrapper not found. Nothing to do here.")
return
}
action()
}
fun Runtime.killAdb() {
if (!isExecutableOnPath("adb")) {
println("⚠️ ADB not found. Nothing to do here.")
return
}
execOnWetRun("adb kill-server")
?.printIfNoError("Adb server killed.")
execOnWetRun("killall adb")
}
fun Runtime.isExecutableOnPath(executableName: String) =
System.getenv("PATH").split(File.pathSeparator)
.map(Paths::get)
.any { pathEntry -> Files.exists(pathEntry.resolve(executableName)) }
fun deleteIdeaProjectFiles() {
printInBold("🔥 Removing IntelliJ IDEA/Android Studio '.iml' project files...")
workingDir.removeFilesWithExtension("iml")
println()
}
fun File.removeFilesWithExtension(extension: String) {
val matchingFiles = this
.listContents(recursively = recursively) {
!it.isDirectory && it.extension.equals(extension, ignoreCase = true)
}
when {
backup -> matchingFiles.backupAndDeleteByRenaming()
else -> matchingFiles.deleteRecursively()
}
}
fun printIdePreferencesWarning(timeoutSeconds: Long) {
printInBold("⚠️ ⚠️ ⚠️ ⚠️ WARNING: deleting IDE settings ⚠️ ⚠️ ⚠️ ⚠️ ")
println()
println(" ( . )")
println(" ) ( )")
println(" . ' . ' . ' .")
println(" ( , ) (. ) ( ', )")
println(" .' ) ( . ) , ( , ) ( .")
println(" ). , ( . ( ) ( , ') .' ( , )")
println(" (_,) . ), ) _) _,') (, ) '. ) ,. (' )")
println(" jgs^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^")
println()
println("⚠️ This will reset all your IDE preferences! ⚠️")
println()
printInBold(" ⏲️ You have $timeoutSeconds seconds to cancel! ⏲️")
println(" Press Ctrl-C to stop now.")
println()
println()
Thread.sleep(TimeUnit.SECONDS.toMillis(timeoutSeconds))
}
fun clearIdePreferences() {
printInBold("🔥 Clearing ${Ide.IntelliJIdea} preferences...")
clearIdePreferences(Ide.IntelliJIdea)
println()
printInBold("🔥 Clearing ${Ide.AndroidStudio} preferences...")
clearIdePreferences(Ide.AndroidStudio)
println()
}
fun clearIdePreferences(ide: Ide) {
val preferencesDirectories = locatePreferencesFolderFor(ide)
when {
backup -> preferencesDirectories
.onEach {
println(" ℹ️ Clearing preferences for $ide ${extractVersion(it, ide)}...")
}
.backupAndDeleteByRenaming()
else -> preferencesDirectories
.onEach {
println(" ℹ️ Clearing preferences for $ide ${extractVersion(it, ide)}...")
}
.deleteRecursively()
}
}
fun locatePreferencesFolderFor(ide: Ide): Sequence<File> =
when {
isOsWindows() || isOsLinux() -> {
userHome.listContents(recursively = false) {
it.isDirectory && it.name.startsWith(".${ide.folderPrefix}")
}
}
isOsMacOs() -> {
File(userHome, "Library/Preferences")
.listContents(recursively = false) {
it.isDirectory && it.name.startsWith(ide.folderPrefix, ignoreCase = true)
}
}
else -> {
println(" ⚠️ Unsupported OS, skipping.")
emptySequence()
}
}
.filter { it.exists() }
fun nukeIdeaProjectSettingsFolder() {
printInBold("🔥 Removing IntelliJ IDEA/Android Studio '.idea' folders...")
workingDir.removeSubfoldersMatching {
it.isDirectory && it.name.equals(".idea", ignoreCase = true)
}
println()
}
fun printNukeModeWarning(timeoutSeconds: Long) {
printInBold("☢️ ☢️ ☢️ ☢️ WARNING: nuke mode activated ☢️ ☢️ ☢️ ☢️ ")
println()
println(" __,-~~/~ `---.")
println(" _/_,---( , )")
println(" __ / < / ) \\___")
println("- ------===;;;'====------------------===;;;===----- - -")
println(" \\/ ~\"~\"~\"~\"~\"~\\~\"~)~\"/")
println(" (_ ( \\ ( > \\)")
println(" \\_( _ < >_>'")
println(" ~ `-i' ::>|--\"")
println(" I;|.|.|")
println(" <|i::|i|`.")
println(" (` ^'\"`-' \")")
println("------------------------------------------------------------------")
println("")
println("⚠️ This will affect system-wide caches for Gradle and IDEs! ⚠️")
println("⚠️ You will lose local version history and other IDE data! ⚠️")
println()
printInBold(" ⏲️ You have $timeoutSeconds seconds to cancel! ⏲️")
println(" Press Ctrl-C to stop now.")
println()
println()
Thread.sleep(TimeUnit.SECONDS.toMillis(timeoutSeconds))
}
fun Runtime.nukeGlobalCaches() {
printInBold("⏳ Clearing Android Gradle build cache...")
exec("$gradlew cleanBuildCache")
println()
printInBold("🔥 Clearing ${Ide.IntelliJIdea} caches...")
clearIdeCache(Ide.IntelliJIdea)
println()
printInBold("🔥 Clearing ${Ide.AndroidStudio} caches...")
clearIdeCache(Ide.AndroidStudio)
println()
printInBold("🔥 Clearing Maven local repository artefacts...")
if (mavenLocalRepository != null) {
if (verbose) println(" ℹ️ Maven local repository found at: ${mavenLocalRepository.absolutePath}")
mavenLocalRepository.removeSubfoldersMatching { it.name.toLowerCase() == "repository" }
} else {
println(" ⚠️ Unable to locate Maven local repository. Checked ~/.m2")
}
printInBold("🔥 Clearing Gradle global cache directories: build-scan-data, caches, daemon, wrapper...")
if (gradleHome != null) {
if (verbose) println(" ℹ️ Gradle home found at: ${gradleHome.absolutePath}")
gradleHome.removeSubfoldersMatching {
it.name.toLowerCase() == "build-scan-data" ||
it.name.toLowerCase() == "caches" ||
it.name.toLowerCase() == "daemon" ||
it.name.toLowerCase() == "wrapper"
}
} else {
println(" ⚠️ Unable to locate Gradle home directory. Checked \$GRADLE_HOME and ~/.gradle")
}
println()
}
fun printInBold(message: String) {
when {
isOsWindows() -> println(message)
else -> println("\u001B[1;37m$message\u001B[0;37m")
}
}
fun clearIdeCache(ide: Ide) {
val cacheDirectories = locateCacheFolderFor(ide)
when {
backup -> cacheDirectories
.onEach {
println(" ℹ️ Clearing cache for $ide ${extractVersion(it, ide)}...")
}
.backupAndDeleteByRenaming()
else -> cacheDirectories
.onEach {
println(" ℹ️ Clearing cache for $ide ${extractVersion(it, ide)}...")
}
.deleteRecursively()
}
}
fun locateCacheFolderFor(ide: Ide): Sequence<File> =
when {
isOsWindows() || isOsLinux() -> {
userHome.listContents(recursively = false) {
it.isDirectory && it.name.startsWith(".${ide.folderPrefix}")
}
}
isOsMacOs() -> {
File(userHome, "Library/Caches")
.listContents(recursively = false) {
it.isDirectory && it.name.startsWith(ide.folderPrefix, ignoreCase = true)
}
}
else -> {
println(" ⚠️ Unsupported OS, skipping.")
emptySequence()
}
}
.filter { it.exists() }
fun isOsLinux() = System.getProperty("os.name").startsWith("Linux", ignoreCase = true)
fun isOsMacOs() = System.getProperty("os.name").startsWith("Mac", ignoreCase = true)
fun extractVersion(it: File, ide: Ide): String {
val versionName = it.name.substringAfter(ide.folderPrefix)
return if (versionName.startsWith("Preview")) {
"${versionName.substring("Preview".length)} Preview"
} else {
versionName
}
}
fun File.removeSubfoldersMatching(matcher: (file: File) -> Boolean) {
val matchingDirectories = this
.listContents(recursively = recursively) {
it.isDirectory && matcher(it)
}
when {
backup -> matchingDirectories.backupAndDeleteByRenaming()
else -> matchingDirectories.deleteRecursively()
}
}
fun File.listContents(recursively: Boolean, matcher: (File) -> Boolean): Sequence<File> =
listFiles()!!
.asSequence()
.flatMap {
when {
matcher(it) -> sequenceOf(it)
recursively && it.isDirectory -> {
it.listContents(recursively = true, matcher = matcher)
}
else -> sequenceOf()
}
}
fun Sequence<File>.backupAndDeleteByRenaming() =
this.onEach { if (verbose) println(" Deleting: ${it.absolutePath}") }
.map { Pair(it, generateBackupNameFor(it)) }
.onEach { (_, backup) -> if (verbose) println(" ⤷ Backing up to: ${backup.name}") }
.forEach { (original, backup) -> if (wetRun) original.renameTo(backup) }
fun generateBackupNameFor(file: File): File {
var backupFile: File
var index = 0
do {
backupFile = File(file.parentFile, "${file.name}-backup%02d".format(index))
index++
} while (backupFile.exists())
return backupFile
}
fun Sequence<File>.deleteRecursively() =
this.onEach { if (verbose) println(" Deleting: ${it.absolutePath}") }
.forEach { if (wetRun) it.deleteRecursively() }
fun isOsWindows() = System.getProperty("os.name").startsWith("Windows", ignoreCase = true)
sealed class Ide(private val name: String, val folderPrefix: String) {
object IntelliJIdea : Ide(name = "IntelliJ IDEA", folderPrefix = "IntelliJIdea")
object AndroidStudio : Ide(name = "Android Studio", folderPrefix = "AndroidStudio")
override fun toString() = name
}