Skip to content

Commit

Permalink
Add support for configuring indentSize to all tasks (#12)
Browse files Browse the repository at this point in the history
* Add support for configuring `indentSize` to all tasks

Also update docs and version.

* DRY up userData and make magic numbers constants
  • Loading branch information
jeremymailen authored Jun 22, 2017
1 parent 79a9a28 commit a72d35f
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 13 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
```

Expand Down Expand Up @@ -57,6 +57,7 @@ Options are configured in the `kotlinter` extension. Defaults shown.
```groovy
kotlinter {
ignoreFailures = false
indentSize = 4
}
```

Expand Down
7 changes: 3 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ class KotlinterPlugin : Plugin<Project> {
lintTask.ignoreFailures = kotlinterExtention.ignoreFailures
lintTask.indentSize = kotlinterExtention.indentSize
}
project.tasks.withType(FormatTask::class.java) { formatTask ->
formatTask.indentSize = kotlinterExtention.indentSize
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package org.jmailen.gradle.kotlinter.support

fun userData(indentSize: Int) = mapOf("indent_size" to indentSize.toString())
10 changes: 8 additions & 2 deletions src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/FormatTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,23 @@ 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() {

@OutputFile
lateinit var report: File

@Input
var indentSize = KotlinterExtension.DEFAULT_INDENT_SIZE

@TaskAction
fun run() {
var fixes = ""
Expand Down Expand Up @@ -59,13 +65,13 @@ open class FormatTask : SourceTask() {
}

private fun formatKt(file: File, ruleSets: List<RuleSet>, 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<RuleSet>, 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)
}
}
Expand Down
10 changes: 6 additions & 4 deletions src/main/kotlin/org/jmailen/gradle/kotlinter/tasks/LintTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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() {
Expand Down Expand Up @@ -61,13 +63,13 @@ open class LintTask : SourceTask() {
}

private fun lintKt(file: File, ruleSets: List<RuleSet>, 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<RuleSet>, 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)
}
}
Expand Down

0 comments on commit a72d35f

Please sign in to comment.