-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
build.gradle.kts
157 lines (143 loc) · 4.57 KB
/
build.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
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
data class SnippetData(
val file: File,
val lineNumber: Int,
val content: String,
var tempDir: File? = null
)
fun findSnippets(dirs: List<String>): List<SnippetData> {
val snippets = mutableListOf<SnippetData>()
dirs.forEach { dirName ->
val dir = rootProject
.projectDir
.parentFile
.resolve(dirName)
.listFiles()?.let {
it.filter { it.name.endsWith(".md") }
.forEach { file ->
val currentSnippet = kotlin.text.StringBuilder()
var snippetStart = 0
var lineNumber = 0
file.forEachLine { line ->
lineNumber++
if (line == "```kotlin")
snippetStart = lineNumber + 1
else if (line == "```" && snippetStart != 0) {
snippets.add(SnippetData(file, snippetStart, currentSnippet.toString()))
snippetStart = 0
currentSnippet.clear()
} else {
if (snippetStart != 0) {
currentSnippet.appendLine(line)
}
}
}
}
}
}
return snippets
}
fun cloneTemplate(template: String, index: Int, content: String): File {
val tempDir = file("${project.buildDir.absolutePath}/temp/cloned-$index")
tempDir.deleteRecursively()
tempDir.mkdirs()
file("${projectDir.parentFile.parentFile.absolutePath}/templates/$template").copyRecursively(tempDir)
// tempDir.deleteOnExit()
File("$tempDir/src/main/kotlin/main.kt").printWriter().use { out ->
out.println(content)
}
return tempDir
}
val ignoreTill = java.time.LocalDate.parse("2022-02-20")
fun isIgnored(tutorial: String): Boolean {
if (java.time.LocalDate.now() > ignoreTill) return false
return when (tutorial) {
"Mouse_Events" -> true
"Tab_Navigation" -> true
else -> false
}
}
fun maybeFail(tutorial: String, message: String) {
if (!isIgnored(tutorial)) {
throw GradleException(message)
} else {
println("IGNORED ERROR: $message")
}
}
@OptIn(ExperimentalStdlibApi::class)
fun checkDirs(dirs: List<String>, template: String, buildCmd: String = "build") {
val snippets = findSnippets(dirs)
snippets.forEachIndexed { index, snippet ->
println("process snippet $index at ${snippet.file}:${snippet.lineNumber} with $template")
snippet.tempDir = cloneTemplate(template, index, snippet.content)
val isWin = System.getProperty("os.name").startsWith("Win")
val args = buildList {
if (isWin) {
add("gradlew.bat")
} else {
add("bash")
add("./gradlew")
}
add(buildCmd)
project.findProperty("kotlin.version")?.also {
add("-Pkotlin.version=$it")
}
project.findProperty("compose.version")?.also {
add("-Pcompose.version=$it")
}
}
val proc = ProcessBuilder(*args.toTypedArray())
.directory(snippet.tempDir)
.redirectOutput(ProcessBuilder.Redirect.PIPE)
.redirectError(ProcessBuilder.Redirect.PIPE)
.start()
proc.waitFor(5, TimeUnit.MINUTES)
if (proc.exitValue() != 0) {
println(proc.inputStream.bufferedReader().readText())
println(proc.errorStream.bufferedReader().readText())
maybeFail(snippet.file.parentFile.name, "Error in snippet at ${snippet.file}:${snippet.lineNumber}")
}
}
}
// NOTICE: currently we use a bit hacky approach, when "```kotlin" marks code that shall be checked, while "``` kotlin"
// with whitespace marks code that shall not be checked.
tasks.register("check") {
val checks = CheckSpec.createCheckSpecs(
checkTargets = (project.property("CHECK_TARGET")?.toString() ?: "all").toLowerCase()
)
doLast {
for (check in checks) {
val subdirs = project
.projectDir
.parentFile
.resolve(check.dir)
.listFiles()
.filter {
it.isDirectory && it.name[0].isUpperCase()
}
.map { it.name }
checkDirs(
dirs = subdirs.map { "${check.dir}/$it" },
template = check.template,
buildCmd = check.gradleCmd
)
}
}
}
data class CheckSpec(
val gradleCmd: String,
val dir: String,
val template: String
) {
companion object {
fun desktop() = CheckSpec(gradleCmd = "build", dir = ".", template = "desktop-template")
fun web() = CheckSpec(gradleCmd = "compileKotlinJs", dir = "Web", template = "web-template")
fun all() = listOf(desktop(), web())
fun createCheckSpecs(checkTargets: String = "all"): List<CheckSpec> {
return when (checkTargets) {
"web" -> listOf(web())
"desktop" -> listOf(desktop())
else -> all()
}
}
}
}