Skip to content

Instantly share code, notes, and snippets.

@cmargonis
Created January 24, 2019 15:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmargonis/69615548ea85254c5190f52372a23520 to your computer and use it in GitHub Desktop.
Save cmargonis/69615548ea85254c5190f52372a23520 to your computer and use it in GitHub Desktop.
This script gets fed two files, one containing all the available lint-issues (as listed here http://tools.android.com/tips/lint-checks copy-paste content with the exact formatting) and a lint.xml file which contains lint issues declared at an android project. The output is the lint issues that haven't yet been added to lint.xml
/**
* Usage:
* Download kotlin as per instructions:
* https://kotlinlang.org/docs/tutorials/command-line.html#using-the-command-line-to-run-scripts
*
* $> kotlinc -script diff-lint.kts <file with all issues> <lint.xml with your configuration> <optional: outpuf filename, defaults to "output">
*/
import java.io.File
data class Issue(val title: String, val lines: MutableList<String>? = null)
if (args.size < 2) throw IllegalArgumentException("Not enough arguments provided, Provided ${args.size}, wanted at least 2")
val srcFile = args[0]
val lintFile = args[1]
val outputFile = if (args.size >= 3) args[2] else null
val androidIssues = parseAndroidIssues(srcFile)
val projectIssues = parseLintChecks(lintFile)
val remainingIssues: List<Issue> =
androidIssues.filter { issue -> projectIssues.none { it.title == issue.title } }
println("Calculated remaining issues: ${remainingIssues.size}")
remainingIssues.writeOutputToFile(outputFile)
/**
* @return Issues as represented in Official [documentation](http://tools.android.com/tips/lint-checks)
*/
fun parseAndroidIssues(fileName: String): List<Issue> {
val file = File(fileName)
val reader = file.bufferedReader()
val issues = mutableListOf<Issue>()
val lineSeq: Sequence<String> = reader.lineSequence()
var previousLine = ""
var currentIssue: Issue? = null
lineSeq.filter { it.isNotBlank() }
.forEach {
if (it.contains("---")) {
// Finish previous issue parsing
currentIssue?.let { issue -> issues.add(issue) }
// start parsing issue
currentIssue = Issue(previousLine.trim(), mutableListOf())
}
currentIssue?.lines?.add(it)
previousLine = it
}
currentIssue?.let { issues.add(it) }
return issues
}
fun parseLintChecks(fName: String): List<Issue> {
val file = File(fName)
val reader = file.bufferedReader()
val lineSeq: Sequence<String> = reader.lineSequence()
var lintChecks = mutableListOf<Issue>()
lineSeq
.filter { it.contains("<issue id=(.)+/>".toRegex()) }
.forEach {
Regex("\"\\w+\"").find(it)?.let { match ->
lintChecks.add(Issue(match.value.substring(1, match.value.length - 1)))
}
}
return lintChecks
}
fun List<Issue>.writeOutputToFile(fileName: String?) {
val file = if (fileName.isNullOrEmpty()) "output" else fileName
File(file).printWriter().use { out ->
out.println("=== Issues to be implemented ===")
this.forEach { issue ->
out.println(issue.title)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment