package studio.tongtong.remotecompile import java.nio.file.* import java.nio.file.attribute.BasicFileAttributes /** Read common literal Gradle declarations; never execute Gradle merely to populate the selector. */ object BuildTasks { private val name = Regex("[A-Za-z0-9_]+") fun valid(task: String) = Regex(":?[A-Za-z0-9_]+(:[A-Za-z0-9_]+)*").matches(task) private data class Token(val value: String, val string: Boolean = false) private fun tokens(text: String): List { val result = mutableListOf(); var i = 0 while (i < text.length) { val c = text[i] when { c.isWhitespace() -> i++ text.startsWith("//", i) -> { i = text.indexOf('\n', i).let { if (it < 0) text.length else it } } text.startsWith("/*", i) -> { i = text.indexOf("*/", i + 2).let { if (it < 0) text.length else it + 2 } } c == '\'' || c == '"' -> { val triple = text.startsWith("$c$c$c", i); val quote = if (triple) "$c$c$c" else "$c" i += quote.length; val value = StringBuilder() while (i < text.length && !text.startsWith(quote, i)) { if (text[i] == '\\' && i + 1 < text.length) i++ value.append(text[i++]) } i = minOf(text.length, i + quote.length); result.add(Token(value.toString(), true)) } c.isLetterOrDigit() || c == '_' -> { val start = i++; while (i < text.length && (text[i].isLetterOrDigit() || text[i] == '_')) i++; result.add(Token(text.substring(start, i))) } else -> { result.add(Token(c.toString())); i++ } } } return result } fun fromScript(module: String, text: String): List { if (!module.split(':').filter { it.isNotEmpty() }.all { name.matches(it) }) return emptyList() val tokens = tokens(text) if (tokens.none { it.string && it.value == "com.android.application" }) return emptyList() val types = linkedSetOf("debug", "release") var i = 0 while (i < tokens.size - 1) { if (tokens[i].string || tokens[i].value != "buildTypes" || tokens[i + 1].value != "{") { i++; continue } var depth = 1; i += 2 while (i < tokens.size && depth > 0) { val t = tokens[i] if (depth == 1 && !t.string) { val next = tokens.getOrNull(i + 1)?.value if (name.matches(t.value) && (next == "{" || (next == "." && tokens.getOrNull(i + 2)?.value == "initWith"))) types.add(t.value) if (t.value in setOf("create", "register", "maybeCreate", "getByName", "named") && next == "(") { tokens.getOrNull(i + 2)?.takeIf { it.string && name.matches(it.value) }?.let { types.add(it.value) } } } if (!t.string) { if (t.value == "{") depth++; if (t.value == "}") depth-- } i++ } } return types.map { "$module:assemble${it.replaceFirstChar(Char::uppercaseChar)}" }.distinct() } fun discover(root: Path): List { val result = linkedSetOf() Files.walkFileTree(root, emptySet(), 5, object : SimpleFileVisitor() { override fun preVisitDirectory(dir: Path, attrs: BasicFileAttributes) = if (dir != root && SourceFiles.reserved(root.relativize(dir).joinToString("/"))) FileVisitResult.SKIP_SUBTREE else FileVisitResult.CONTINUE override fun visitFile(file: Path, attrs: BasicFileAttributes): FileVisitResult { if (file.fileName.toString() in setOf("build.gradle", "build.gradle.kts") && attrs.isRegularFile && attrs.size() <= 2 * 1024 * 1024) { val relative = root.relativize(file.parent).joinToString(":") result.addAll(fromScript(if (relative.isEmpty()) "" else ":$relative", Files.readString(file))) } return FileVisitResult.CONTINUE } }) return result.toList() } }