From a72d35f502a90d9b2c3d2f5adbbaf4270eca7844 Mon Sep 17 00:00:00 2001 From: Jeremy Mailen Date: Thu, 22 Jun 2017 08:48:51 -0700 Subject: [PATCH] Add support for configuring `indentSize` to all tasks (#12) * Add support for configuring `indentSize` to all tasks Also update docs and version. * DRY up userData and make magic numbers constants --- README.md | 3 ++- build.gradle | 7 +++---- .../org/jmailen/gradle/kotlinter/KotlinterExtension.kt | 8 ++++++-- .../org/jmailen/gradle/kotlinter/KotlinterPlugin.kt | 3 +++ .../jmailen/gradle/kotlinter/support/ktLintConfig.kt | 3 +++ .../org/jmailen/gradle/kotlinter/tasks/FormatTask.kt | 10 ++++++++-- .../org/jmailen/gradle/kotlinter/tasks/LintTask.kt | 10 ++++++---- 7 files changed, 31 insertions(+), 13 deletions(-) create mode 100644 src/main/kotlin/org/jmailen/gradle/kotlinter/support/ktLintConfig.kt diff --git a/README.md b/README.md index ee282605..84962e90 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ Available on the Gradle Plugins Portal: https://plugins.gradle.org/plugin/org.jm ```groovy plugins { - id 'org.jmailen.kotlinter' version '1.0.0' + id 'org.jmailen.kotlinter' version '1.1.0' } ``` @@ -57,6 +57,7 @@ Options are configured in the `kotlinter` extension. Defaults shown. ```groovy kotlinter { ignoreFailures = false + indentSize = 4 } ``` diff --git a/build.gradle b/build.gradle index 8dc7bab0..1b56bf98 100644 --- a/build.gradle +++ b/build.gradle @@ -14,14 +14,13 @@ repositories { dependencies { compile 'com.github.shyiko:ktlint:0.8.3' compileOnly 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.1.2-5' - compileOnly 'com.android.tools.build:gradle:2.3.2' + compileOnly 'com.android.tools.build:gradle:2.3.3' testCompile 'junit:junit:4.12' testRuntime 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.1.2-5' - testRuntime 'com.android.tools.build:gradle:2.3.2' + testRuntime 'com.android.tools.build:gradle:2.3.3' } - -version = '1.0.0' +version = '1.1.0' group = 'org.jmailen.gradle' def pluginId = 'org.jmailen.kotlinter' diff --git a/src/main/kotlin/org/jmailen/gradle/kotlinter/KotlinterExtension.kt b/src/main/kotlin/org/jmailen/gradle/kotlinter/KotlinterExtension.kt index 267e2d04..9f8d7657 100644 --- a/src/main/kotlin/org/jmailen/gradle/kotlinter/KotlinterExtension.kt +++ b/src/main/kotlin/org/jmailen/gradle/kotlinter/KotlinterExtension.kt @@ -1,9 +1,13 @@ package org.jmailen.gradle.kotlinter open class KotlinterExtension { + companion object { + const val DEFAULT_IGNORE_FAILURES = false + const val DEFAULT_INDENT_SIZE = 4 + } /** Don't fail build on lint issues */ - var ignoreFailures = false + var ignoreFailures = DEFAULT_IGNORE_FAILURES - var indentSize = 4 + var indentSize = DEFAULT_INDENT_SIZE } diff --git a/src/main/kotlin/org/jmailen/gradle/kotlinter/KotlinterPlugin.kt b/src/main/kotlin/org/jmailen/gradle/kotlinter/KotlinterPlugin.kt index 43544aaf..ce7a57e8 100644 --- a/src/main/kotlin/org/jmailen/gradle/kotlinter/KotlinterPlugin.kt +++ b/src/main/kotlin/org/jmailen/gradle/kotlinter/KotlinterPlugin.kt @@ -36,6 +36,9 @@ class KotlinterPlugin : Plugin { lintTask.ignoreFailures = kotlinterExtention.ignoreFailures lintTask.indentSize = kotlinterExtention.indentSize } + project.tasks.withType(FormatTask::class.java) { formatTask -> + formatTask.indentSize = kotlinterExtention.indentSize + } } } diff --git a/src/main/kotlin/org/jmailen/gradle/kotlinter/support/ktLintConfig.kt b/src/main/kotlin/org/jmailen/gradle/kotlinter/support/ktLintConfig.kt new file mode 100644 index 00000000..bc7e2c54 --- /dev/null +++ b/src/main/kotlin/org/jmailen/gradle/kotlinter/support/ktLintConfig.kt @@ -0,0 +1,3 @@ +package org.jmailen.gradle.kotlinter.support + +fun userData(indentSize: Int) = mapOf("indent_size" to indentSize.toString()) diff --git a/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/FormatTask.kt b/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/FormatTask.kt index 65d705c4..3203ad1f 100644 --- a/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/FormatTask.kt +++ b/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/FormatTask.kt @@ -3,10 +3,13 @@ package org.jmailen.gradle.kotlinter.tasks import com.github.shyiko.ktlint.core.KtLint import com.github.shyiko.ktlint.core.RuleSet import org.gradle.api.logging.LogLevel +import org.gradle.api.tasks.Input import org.gradle.api.tasks.OutputFile import org.gradle.api.tasks.SourceTask import org.gradle.api.tasks.TaskAction +import org.jmailen.gradle.kotlinter.KotlinterExtension import org.jmailen.gradle.kotlinter.support.resolveRuleSets +import org.jmailen.gradle.kotlinter.support.userData import java.io.File open class FormatTask : SourceTask() { @@ -14,6 +17,9 @@ open class FormatTask : SourceTask() { @OutputFile lateinit var report: File + @Input + var indentSize = KotlinterExtension.DEFAULT_INDENT_SIZE + @TaskAction fun run() { var fixes = "" @@ -59,13 +65,13 @@ open class FormatTask : SourceTask() { } private fun formatKt(file: File, ruleSets: List, onError: (line: Int, col: Int, detail: String, corrected: Boolean) -> Unit): String { - return KtLint.format(file.readText(), ruleSets) { error, corrected -> + return KtLint.format(file.readText(), ruleSets, userData(indentSize = indentSize)) { error, corrected -> onError(error.line, error.col, error.detail, corrected) } } private fun formatKts(file: File, ruleSets: List, onError: (line: Int, col: Int, detail: String, corrected: Boolean) -> Unit): String { - return KtLint.formatScript(file.readText(), ruleSets) { error, corrected -> + return KtLint.formatScript(file.readText(), ruleSets, userData(indentSize = indentSize)) { error, corrected -> onError(error.line, error.col, error.detail, corrected) } } diff --git a/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/LintTask.kt b/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/LintTask.kt index 439bd7f0..ce7509fd 100644 --- a/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/LintTask.kt +++ b/src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/LintTask.kt @@ -9,7 +9,9 @@ import org.gradle.api.tasks.OutputFile import org.gradle.api.tasks.ParallelizableTask import org.gradle.api.tasks.SourceTask import org.gradle.api.tasks.TaskAction +import org.jmailen.gradle.kotlinter.KotlinterExtension import org.jmailen.gradle.kotlinter.support.resolveRuleSets +import org.jmailen.gradle.kotlinter.support.userData import java.io.File @ParallelizableTask @@ -19,10 +21,10 @@ open class LintTask : SourceTask() { lateinit var report: File @Input - var ignoreFailures = false + var ignoreFailures = KotlinterExtension.DEFAULT_IGNORE_FAILURES @Input - var indentSize = 4 + var indentSize = KotlinterExtension.DEFAULT_INDENT_SIZE @TaskAction fun run() { @@ -61,13 +63,13 @@ open class LintTask : SourceTask() { } private fun lintKt(file: File, ruleSets: List, onError: (line: Int, col: Int, detail: String) -> Unit) { - KtLint.lint(file.readText(), ruleSets, mapOf("indent_size" to indentSize.toString())) { error -> + KtLint.lint(file.readText(), ruleSets, userData(indentSize = indentSize)) { error -> onError(error.line, error.col, error.detail) } } private fun lintKts(file: File, ruleSets: List, onError: (line: Int, col: Int, detail: String) -> Unit) { - KtLint.lintScript(file.readText(), ruleSets) { error -> + KtLint.lintScript(file.readText(), ruleSets, userData(indentSize = indentSize)) { error -> onError(error.line, error.col, error.detail) } }