Skip to content

Instantly share code, notes, and snippets.

@rr-codes
Created January 25, 2022 23:30
Show Gist options
  • Save rr-codes/bed48a4731971f620d20f443f187f9db to your computer and use it in GitHub Desktop.
Save rr-codes/bed48a4731971f620d20f443f187f9db to your computer and use it in GitHub Desktop.
data class Task(val name: String, val files: List<String>, val deps: List<String>)
fun globMatches(pattern: String, path: String): Boolean {
val start = "^"
val end = "$"
val regex = buildString {
append(start)
append(pattern.replace("([\\^\\[\\]\\.\\+\\?])".toRegex(), "\\\\$1").replace("*", "[^/]*"))
append(end)
}.toRegex()
val res = regex matches path
return res
}
fun tasksToRun(taskDefinitionsInput: Array<String>, changedFiles: Array<String>): Array<String> {
val chunked = taskDefinitionsInput.toList().chunked(4)
val tasks = chunked.filter { it.size >= 3 }.map {
Task(it[0].removePrefix("task: "), it[1].removePrefix("files: ").split(" "), it[2].removePrefix("depts: ").split(" "))
}
val res = mutableListOf<String>()
for (task in tasks) {
helper(tasks, changedFiles, res, task)
}
return res.toTypedArray()
}
fun helper(tasks: List<Task>, changedFiles: Array<String>, output: MutableList<String>, task: Task) {
for (dep in task.deps) {
val task = tasks.firstOrNull { it.name == dep }
if (task != null) {
helper(tasks, changedFiles, output, task)
}
}
for (file in task.files) {
for (c in changedFiles) {
if (task.name in output) {
continue
}
if (globMatches(file, c)) {
output += task.name
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment