-
Notifications
You must be signed in to change notification settings - Fork 27
Ktlint integration #110
Ktlint integration #110
Conversation
Can one of the admins review this PR? |
|
||
private def createCollectViolationsTask(Violations violations, def sourceSetName) { | ||
project.tasks.create("collectKtlint${sourceSetName.capitalize()}Violations", CollectCheckstyleViolationsTask) { task -> | ||
task.xmlReportFile = new File(project.buildDir, "reports/ktlint/ktlint-${sourceSetName}.xml") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No way to change the output in the plugin. Hardcoded for now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this mean we get ktlint-main.xml
? Never used the underlying Ktlint plugin, so not sure :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think Gradle plugin provides the file name and path internally to ktlint tool.
It has report per sourceSet in pure Kotlin, and report per variant in Android projects.
The path and file names are hardcoded and not configurable by the user.
import org.gradle.api.tasks.TaskAction | ||
|
||
abstract class CollectViolationsTask extends DefaultTask { | ||
|
||
@InputFile |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added this to make the task better cacheable. Don't think it will hurt in any way. WDYT?
config.delegate = ktlint | ||
config() | ||
|
||
project.afterEvaluate { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a notice: Had to add project.afterEvaluate
since the plugin itself uses afterEvaluate
too. Don't think it is a problem.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be ok, at least for now, since as you say that's what the plugin we're wrapping does
@@ -14,7 +14,8 @@ buildscript { | |||
} | |||
} | |||
plugins { | |||
${formatPlugins(project)} | |||
${formatPlugins(project)} | |||
id 'com.novoda.static-analysis' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is better if the static-analysis
plugin is added later. I needed to do this earlier. It may not be needed anymore. But I think it is a good change anyways since it makes it explicit. Before it was added by the method withPlugin
in the constructor.
@@ -58,7 +59,7 @@ ${formatExtension(project)} | |||
.collect { Map.Entry<String, List<String>> entry -> | |||
"""$entry.key { | |||
manifest.srcFile '${Fixtures.ANDROID_MANIFEST}' | |||
kotlin { | |||
java { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Android projects uses java
sourceSet for mixed Kotlin and Java sources.
} | ||
|
||
apply plugin: 'kotlin' | ||
|
||
repositories { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was needed for ktlint
jar to be resolved in pure kotlin project.
Trying to close and reopening to see if the CI picks it up |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great job! Have a couple of questions — doubts really — then good for me
config.delegate = ktlint | ||
config() | ||
|
||
project.afterEvaluate { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be ok, at least for now, since as you say that's what the plugin we're wrapping does
|
||
private def createCollectViolationsTask(Violations violations, def sourceSetName) { | ||
project.tasks.create("collectKtlint${sourceSetName.capitalize()}Violations", CollectCheckstyleViolationsTask) { task -> | ||
task.xmlReportFile = new File(project.buildDir, "reports/ktlint/ktlint-${sourceSetName}.xml") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this mean we get ktlint-main.xml
? Never used the underlying Ktlint plugin, so not sure :)
class KtLintViolator { | ||
|
||
override fun equals(other: Any?): Boolean { | ||
// this is not allowed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is not allowed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Haha I think always returning true is not allowed. This is just a fixture and a copy paste from detekt fixtures.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here, I need any Kotlin code with style problem. Would be happy to change. 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, maybe @mr-archano can put his laser 👀 on this too :)
@mr-archano bump 👊 |
@@ -5,7 +5,8 @@ final class TestJavaProject extends TestProject<TestJavaProject> { | |||
private static final Closure<String> TEMPLATE = { TestProject project -> | |||
""" | |||
plugins { | |||
${formatPlugins(project)} | |||
${formatPlugins(project)} | |||
id 'com.novoda.static-analysis' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why this change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Explained here already. Can check if reverting is fine or not. Users are expected to apply static analysis at the last position. Also makes it more obvious here.
#110 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sorry I actually read it, but somehow missed the connection 🙈
@@ -15,9 +15,15 @@ buildscript { | |||
|
|||
plugins { | |||
${formatPlugins(project)} | |||
id 'com.novoda.static-analysis' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same as above.
Thanks @tasomaniac for this! A new snapshot should be available once merged to |
Opened an issue: JLLeitschuh/ktlint-gradle#125 |
Thanks @Tapchicoma I intended to do that but then I forgot. |
This PR adds Ktlint integration. Ktlint does not have a gradle plugin. There are 2 available gradle plugins and I chose
org.jlleitschuh.gradle.ktlint
since it handles Android flavors in a nice way.Considerations
ignoreFailures = true
because our plugin will fail if thresholds are passedreporters
are hardcoded to checkstyle and plain. Html is not supported 😞CollectCheckstyleViolationsTask
to parse thexml
. The tool explicitly says that it usescheckstyle
xml report so I think it is better than duplicating the class. If you want, I can duplicate.Fixes #50