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

Ktlint integration #110

Merged
merged 4 commits into from Jul 31, 2018
Merged

Conversation

tasomaniac
Copy link
Contributor

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 passed
  • reporters are hardcoded to checkstyle and plain. Html is not supported 馃槥
  • Used the default CollectCheckstyleViolationsTask to parse the xml. The tool explicitly says that it uses checkstyle xml report so I think it is better than duplicating the class. If you want, I can duplicate.

Fixes #50

@hal9002
Copy link
Contributor

hal9002 commented Jul 7, 2018

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")
Copy link
Contributor Author

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.

Copy link
Contributor

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 :)

Copy link
Contributor Author

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
Copy link
Contributor Author

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 {
Copy link
Contributor Author

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.

Copy link
Contributor

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'
Copy link
Contributor Author

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 {
Copy link
Contributor Author

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 {
Copy link
Contributor Author

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.

@rock3r
Copy link
Contributor

rock3r commented Jul 9, 2018

Trying to close and reopening to see if the CI picks it up

@rock3r rock3r closed this Jul 9, 2018
@rock3r rock3r reopened this Jul 9, 2018
@rock3r rock3r self-requested a review July 9, 2018 08:50
Copy link
Contributor

@rock3r rock3r left a 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 鈥斅燿oubts really 鈥斅爐hen good for me

config.delegate = ktlint
config()

project.afterEvaluate {
Copy link
Contributor

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")
Copy link
Contributor

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
Copy link
Contributor

Choose a reason for hiding this comment

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

What is not allowed?

Copy link
Contributor Author

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.

Copy link
Contributor Author

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. 馃憤

Copy link
Contributor

@rock3r rock3r left a 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 :)

@tasomaniac
Copy link
Contributor Author

@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'
Copy link
Contributor

Choose a reason for hiding this comment

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

why this change?

Copy link
Contributor Author

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)

Copy link
Contributor

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'
Copy link
Contributor

Choose a reason for hiding this comment

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

same as above.

@mr-archano
Copy link
Contributor

Thanks @tasomaniac for this! A new snapshot should be available once merged to develop

@mr-archano mr-archano merged commit 67894b2 into novoda:develop Jul 31, 2018
@tasomaniac tasomaniac deleted the taso/ktlint-integration branch August 1, 2018 18:51
@Tapchicoma
Copy link

reporters are hardcoded to checkstyle and plain. Html is not supported

Opened an issue: JLLeitschuh/ktlint-gradle#125

@tasomaniac
Copy link
Contributor Author

Thanks @Tapchicoma

I intended to do that but then I forgot.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants