Skip to content
This repository has been archived by the owner on Feb 11, 2022. It is now read-only.

Fix ktlint for all versions #153

Merged
merged 5 commits into from Dec 17, 2018
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -84,22 +84,35 @@ class KtlintConfigurator implements Configurator {
}

private void configureAndroidWithVariants(def mainVariants) {
mainVariants.all { configureKtlint(it.name) }
variantFilter.filteredTestVariants.all { configureKtlint(it.name) }
variantFilter.filteredUnitTestVariants.all { configureKtlint(it.name) }
mainVariants.all { configureAndroidVariant(it) }
variantFilter.filteredTestVariants.all { configureAndroidVariant(it) }
variantFilter.filteredUnitTestVariants.all { configureAndroidVariant(it) }
}

private void configureKtlint(def sourceSetName) {
private void configureAndroidVariant(def variant) {
variant.sourceSets.each { sourceSet ->
configureKtlint(sourceSet.name)
}
}

private void configureKtlint(String sourceSetName) {
project.tasks.matching {
it.name == "ktlint${sourceSetName.capitalize()}Check"
isKtlintTask(it, sourceSetName.capitalize())
}.all { Task ktlintTask ->
def collectViolations = configureKtlintWithOutputFiles(sourceSetName, ktlintTask.reportOutputFiles)
collectViolations.dependsOn ktlintTask
evaluateViolations.dependsOn collectViolations
}
}

private def configureKtlintWithOutputFiles(def sourceSetName, Map<?, RegularFileProperty> reportOutputFiles) {
/**
* KtLint task has the following naming convention and the needed property to resolve the reportOutputFiles
*/
private boolean isKtlintTask(Task task, String sourceSetName) {
task.name ==~ /^ktlint$sourceSetName(SourceSet)?Check$/ && task.hasProperty('reportOutputFiles')
}

private def configureKtlintWithOutputFiles(String sourceSetName, Map<?, RegularFileProperty> reportOutputFiles) {
File xmlReportFile = null
File txtReportFile = null
reportOutputFiles.each { key, fileProp ->
Expand All @@ -120,10 +133,11 @@ class KtlintConfigurator implements Configurator {
}

private def createCollectViolationsTask(Violations violations, def sourceSetName, File xmlReportFile, File txtReportFile) {
project.tasks.create("collectKtlint${sourceSetName.capitalize()}Violations", CollectCheckstyleViolationsTask) { task ->
task.xmlReportFile = xmlReportFile
task.htmlReportFile = txtReportFile
task.violations = violations
}
CollectCheckstyleViolationsTask task =
project.tasks.maybeCreate("collectKtlint${sourceSetName.capitalize()}Violations", CollectCheckstyleViolationsTask)
task.xmlReportFile = xmlReportFile
task.htmlReportFile = txtReportFile
task.violations = violations
return task
}
}
Expand Up @@ -35,12 +35,11 @@ class KtlintIntegrationTest {
[TestProjectRule.forKotlinProject(), '5.1.0', 'ktlint-main.txt'],
[TestProjectRule.forAndroidKotlinProject(), '5.1.0', 'ktlint-debug.txt'],
[TestProjectRule.forKotlinProject(), '6.1.0', 'ktlintMainCheck.txt'],
[TestProjectRule.forAndroidKotlinProject(), '6.1.0', 'ktlintMainCheck.txt'],
[TestProjectRule.forKotlinProject(), '6.2.1', 'ktlintMainCheck.txt'],
/**
* Tracked in https://github.com/novoda/gradle-static-analysis-plugin/issues/146
*/
//[TestProjectRule.forAndroidKotlinProject(), '6.1.0', 'ktlintDebugCheck.txt'],
//[TestProjectRule.forAndroidKotlinProject(), '6.2.1', 'ktlintDebugCheck.txt'],
[TestProjectRule.forAndroidKotlinProject(), '6.2.1', 'ktlintMainCheck.txt'],
[TestProjectRule.forKotlinProject(), '6.3.1', 'ktlintMainCheck.txt'],
[TestProjectRule.forAndroidKotlinProject(), '6.3.1', 'ktlintMainCheck.txt'],
]*.toArray()
}

Expand Down Expand Up @@ -116,7 +115,7 @@ class KtlintIntegrationTest {
private TestProject createProjectWith(File sources, int maxErrors = 0) {
projectRule.newProject()
.withPlugin('org.jlleitschuh.gradle.ktlint', ktlintVersion)
.withSourceSet('main', sources)
.copyIntoSourceSet('main', sources)
.withPenalty("""{
maxWarnings = 0
maxErrors = ${maxErrors}
Expand Down
7 changes: 7 additions & 0 deletions plugin/src/test/groovy/com/novoda/test/TestProject.groovy
Expand Up @@ -63,6 +63,13 @@ ${project.additionalConfiguration}
file.text = text
}

public T copyIntoSourceSet(String sourceSet, File srcDir) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯

srcDir.listFiles().each {
withFile(it, "src/${sourceSet}/java/${it.name}")
}
return this
}

public T withSourceSet(String sourceSet, File... srcDirs) {
sourceSets[sourceSet] = srcDirs
return this
Expand Down