Compare commits

..

1 Commits

Author SHA1 Message Date
Sergey Vlasenko b183a4f5f4 feature add detektAnalysis task 2024-03-19 16:49:51 +03:00
2 changed files with 27 additions and 9 deletions

View File

@ -15,6 +15,7 @@ import java.io.File
class DetektLinter : Linter {
private companion object {
const val TAG = "DetektLinter"
const val ONLY_DIFFS_FLAG = "only-diffs"
const val GET_GIT_DIFFS_COMMAND = "git diff --name-only --ignore-submodules"
}
@ -58,20 +59,30 @@ class DetektLinter : Linter {
}
}
val isOnlyDiffs = properties["args"] == ONLY_DIFFS_FLAG // example: ./gradlew detektAnalysis -Pargs="only-diffs"
source = getSources(extension.excludes, isOnlyDiffs, project)
val diffsBranch = properties[ONLY_DIFFS_FLAG] as? String
source = getSources(extension.excludes, diffsBranch, project)
}
}
}
private fun getSources(excludes: String, isOnlyDiffs: Boolean, project: Project): FileTree = when {
isOnlyDiffs -> getGitDiffFiles(excludes, project)
else -> project.getSources(excludes)
private fun getSources(excludes: String, diffsBranch: String?, project: Project): FileTree = when (diffsBranch) {
null -> project.getSources(excludes)
else -> getGitDiffFiles(excludes, diffsBranch, project)
}
private fun getGitDiffFiles(excludes: String, project: Project): FileTree {
val gitDiffs = GET_GIT_DIFFS_COMMAND.runCommand() ?: return project.files().asFileTree
private fun getGitDiffFiles(excludes: String, diffsBranch: String, project: Project): FileTree {
val getGitDiffsCommand = if (diffsBranch.isEmpty()) {
GET_GIT_DIFFS_COMMAND
} else {
GET_GIT_DIFFS_COMMAND.plus(" --merge-base $diffsBranch")
}
val gitDiffs = getGitDiffsCommand.runCommand()
if (gitDiffs.isNullOrEmpty()) {
project.logger.error("$TAG: Diffs are empty or specified branch or commit does not exists")
return project.files().asFileTree
}
val diffFiles = gitDiffs.lines()
.map { File(it) }

View File

@ -27,7 +27,14 @@ class StaticAnalysisAndroidPlugin : StaticAnalysisPlugin() {
buildVariant = applicationVariants.first { it.name.contains("Debug") }.name
)
}
/**
* Task to run detekt checks.
*
* @param --only-diffs <branch or commit> if specified, only files modified
* relative to this branch or commit will be checked. If specified without value
* then current uncommited changes will be checked. If not specified all source files will be checked.
* @see DetektLinter.getGitDiffFiles, 'git diff' for more info.
* */
project.tasks.register("detektAnalysis") {
val detektLinter = linters.find { it is DetektLinter }
?: throw IllegalStateException("DetektLinter not found")