BuildScripts/gradle/staticAnalysis.gradle

151 lines
4.8 KiB
Groovy

buildscript {
repositories {
maven { url "https://plugins.gradle.org/m2" }
}
dependencies {
classpath "io.gitlab.arturbosch.detekt:detekt-gradle-plugin:1.5.1"
}
}
def getServerProjectSources
def getAndroidProjectSources
apply from: "$buildScriptsDir/gradle/commonStaticAnalysis.gradle"
gradle.projectsEvaluated {
tasks.withType(JavaCompile) {
options.compilerArgs <<
"-Xlint:cast" <<
"-Xlint:divzero" <<
"-Xlint:empty" <<
"-Xlint:deprecation" <<
"-Xlint:finally" <<
"-Xlint:overrides" <<
"-Xlint:path" <<
"-Werror"
}
def excludes = rootProject.extensions.findByName("staticAnalysisExcludes")
def androidSources = getAndroidProjectSources(excludes)
def androidStaticAnalysisTasks = getStaticAnalysisTaskNames(true, androidSources, null)
def androidIdeaFormatTask = getIdeaFormatTask(true, androidSources)
task staticAnalysisWithFormatting {
androidStaticAnalysisTasks.each { task ->
tasks.findByName(task)?.mustRunAfter(androidIdeaFormatTask)
}
dependsOn androidIdeaFormatTask
dependsOn androidStaticAnalysisTasks
doFirst {
generateReport(true)
}
}
task staticAnalysis {
dependsOn androidStaticAnalysisTasks
doFirst {
generateReport(true)
}
}
def serverStaticAnalysisTasks = getStaticAnalysisTaskNames(false, getServerProjectSources(excludes), null)
def serverIdeaFormatTask = getIdeaFormatTask(false, getServerProjectSources(excludes))
task serverStaticAnalysisWithFormatting {
serverStaticAnalysisTasks.each { task ->
tasks.findByName(task)?.mustRunAfter(serverIdeaFormatTask)
}
dependsOn serverIdeaFormatTask
dependsOn serverStaticAnalysisTasks
doFirst {
generateReport(false)
}
}
task serverStaticAnalysis {
dependsOn serverStaticAnalysisTasks
doFirst {
generateReport(false)
}
}
subprojects { subproject ->
pluginManager.withPlugin('com.android.application') {
android.applicationVariants.all { variant ->
task("staticAnalysis${variant.name.capitalize()}") {
dependsOn getStaticAnalysisTaskNames(true, androidSources, variant)
doFirst { generateReport(true) }
}
}
}
tasks.withType(io.gitlab.arturbosch.detekt.Detekt) {
exclude '**/test/'
exclude 'resources/'
exclude 'build/'
exclude 'tmp/'
jvmTarget = "1.8"
}
detekt {
config = files("$buildScriptsDir/kotlin/detekt-config.yml")
reports {
txt.enabled = false
html.enabled = false
xml {
enabled = true
destination = file("${rootProject.buildDir}/reports/kotlin-detekt-${subproject.name}.xml")
}
}
}
}
}
getServerProjectSources = { excludes ->
def sources = new ArrayList<String>()
def sourcesDirectory = new File(project.projectDir.path, 'src')
for (def sourceFlavorDirectory : sourcesDirectory.listFiles()) {
def javaSourceDirectory = new File(sourceFlavorDirectory.path, 'java')
def kotlinSourceDirectory = new File(sourceFlavorDirectory.path, 'kotlin')
if (javaSourceDirectory.exists() && javaSourceDirectory.isDirectory()) {
sources.add(javaSourceDirectory.absolutePath)
}
if (kotlinSourceDirectory.exists() && kotlinSourceDirectory.isDirectory()) {
sources.add(kotlinSourceDirectory.absolutePath)
}
}
return sources
}
getAndroidProjectSources = { excludes ->
def sources = new ArrayList<String>()
for (def project : rootProject.subprojects) {
if (!project.subprojects.isEmpty() || (excludes != null && excludes.contains(project.path))) {
continue
}
def sourcesDirectory = new File(project.projectDir.path, 'src')
if (!sourcesDirectory.exists() || !sourcesDirectory.isDirectory()) {
continue
}
for (def sourceFlavorDirectory : sourcesDirectory.listFiles()) {
def javaSourceDirectory = new File(sourceFlavorDirectory.path, 'java')
def kotlinSourceDirectory = new File(sourceFlavorDirectory.path, 'kotlin')
if (javaSourceDirectory.exists() && javaSourceDirectory.isDirectory()) {
sources.add(javaSourceDirectory.absolutePath)
}
if (kotlinSourceDirectory.exists() && kotlinSourceDirectory.isDirectory()) {
sources.add(kotlinSourceDirectory.absolutePath)
}
}
}
return sources
}