-
Notifications
You must be signed in to change notification settings - Fork 1
/
settings.gradle.kts
71 lines (60 loc) · 1.82 KB
/
settings.gradle.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
val appName = "app"
val buildSrc = "dependencies"
val modulesName = "modules"
val gradlePathEnd = "/build.gradle.kts"
val generatedCode = "import com.project.picpicker.plugins.config.module\n" +
"\n" +
"module()"
autoInclude()
fun Settings.autoInclude() {
includeBuild("$rootDir/$buildSrc")
includeCore()
includeModules()
}
fun Settings.includeCore() {
rootDir.walkBottomUp()
.maxDepth(1)
.filter { it.absolutePath != rootDir.path && it.name != buildSrc }
.mapToModules()
.findNameAndInclude()
}
fun Settings.includeModules() {
File(rootDir, modulesName)
.walkTopDown()
.maxDepth(3)
.findNameAndInclude()
}
fun Sequence<File>.findNameAndInclude() {
filter { it.isDirectory && existDirectory(it) }
.forEach {
val moduleName = ":${it.name}"
include(moduleName)
project(moduleName).projectDir = file(it.path)
}
}
fun Sequence<File>.mapToModules(): Sequence<File> {
filter(::isModule)
.forEach {
it.moveTo()
}
return this
}
fun File.moveTo() {
replaceDefaultGradle(File("$rootDir/$name$gradlePathEnd"))
renameTo(File("$rootDir/$modulesName/$name"))
val settings = File("$rootDir/settings.gradle.kts")
var text = settings.inputStream().bufferedReader().use { it.readText() }
val findText = "include(\":$name\")"
text = text.replace(findText, "").trimEnd()
settings.bufferedWriter().use { it.write(text) }
}
fun replaceDefaultGradle(file: File) {
val text = generatedCode
file.bufferedWriter().use { it.write(text) }
}
fun isModule(file: File): Boolean {
return file.name != appName && file.isDirectory
&& existDirectory(file)
}
fun existDirectory(file: File): Boolean =
File("${file.absolutePath}$gradlePathEnd").exists()