Skip to content

Commit

Permalink
Testing new ktlint apis pinterest/ktlint#1659
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuszkwiecinski committed Oct 9, 2022
1 parent a539a4d commit 5274ac0
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 34 deletions.
5 changes: 3 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,22 @@ plugins {
repositories {
mavenCentral()
google()
maven { setUrl("https://oss.sonatype.org/content/repositories/snapshots") }
}

val pluginId = "org.jmailen.kotlinter"
val githubUrl = "https://github.com/jeremymailen/kotlinter-gradle"
val webUrl = "https://github.com/jeremymailen/kotlinter-gradle"
val projectDescription = "Lint and formatting for Kotlin using ktlint with configuration-free setup on JVM and Android projects"

version = "3.12.0"
version = "3.13.0"
group = "org.jmailen.gradle"
description = projectDescription

object Versions {
const val androidTools = "7.3.0"
const val junit = "5.9.1"
const val ktlint = "0.47.1"
const val ktlint = "0.48.0-SNAPSHOT"
const val mockitoKotlin = "4.0.0"
}

Expand Down
14 changes: 14 additions & 0 deletions src/main/kotlin/org/jmailen/gradle/kotlinter/KotlinterPlugin.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.jmailen.gradle.kotlinter

import com.pinterest.ktlint.core.KtLint
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.Task
Expand All @@ -8,11 +9,15 @@ import org.jmailen.gradle.kotlinter.pluginapplier.AndroidSourceSetApplier
import org.jmailen.gradle.kotlinter.pluginapplier.KotlinJvmSourceSetApplier
import org.jmailen.gradle.kotlinter.pluginapplier.KotlinMultiplatformSourceSetApplier
import org.jmailen.gradle.kotlinter.support.reporterFileExtension
import org.jmailen.gradle.kotlinter.tasks.ConfigurableKtLintTask
import org.jmailen.gradle.kotlinter.tasks.FormatTask
import org.jmailen.gradle.kotlinter.tasks.InstallPreCommitHookTask
import org.jmailen.gradle.kotlinter.tasks.InstallPrePushHookTask
import org.jmailen.gradle.kotlinter.tasks.LintTask
import java.io.File
import kotlin.system.measureTimeMillis
import kotlin.time.ExperimentalTime
import kotlin.time.measureTime

class KotlinterPlugin : Plugin<Project> {

Expand All @@ -22,6 +27,7 @@ class KotlinterPlugin : Plugin<Project> {
"kotlin-android" to AndroidSourceSetApplier,
)

@OptIn(ExperimentalTime::class)
override fun apply(project: Project) = with(project) {
val kotlinterExtension = extensions.create("kotlinter", KotlinterExtension::class.java)

Expand All @@ -35,6 +41,14 @@ class KotlinterPlugin : Plugin<Project> {
val lintKotlin = registerParentLintTask()
val formatKotlin = registerParentFormatTask()

val duration = measureTime {
val thisShouldBeSourceSetRootConfiguredForEachTask = project.rootDir.toPath() // TODO
val projectEditorConfigFiles = KtLint.editorConfigFilePaths(thisShouldBeSourceSetRootConfiguredForEachTask)
tasks.withType(ConfigurableKtLintTask::class.java).configureEach { task ->
task.editorconfigFiles.from(projectEditorConfigFiles)
}
}
logger.quiet("Took $duration")
sourceResolver.applyToAll(this) { id, resolvedSources ->
val lintTaskPerSourceSet = tasks.register("lintKotlin${id.capitalize()}", LintTask::class.java) { lintTask ->
lintTask.source(resolvedSources)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import com.pinterest.ktlint.core.KtLint
import com.pinterest.ktlint.core.api.DefaultEditorConfigProperties
import com.pinterest.ktlint.core.api.EditorConfigOverride
import org.gradle.api.file.ConfigurableFileCollection
import org.gradle.api.file.ProjectLayout
import org.gradle.api.logging.Logger
import java.io.File

Expand All @@ -28,29 +27,3 @@ internal fun resetEditorconfigCacheIfNeeded(
changedFiles.map(File::toPath).forEach(KtLint::reloadEditorConfigFile)
}
}

internal fun ProjectLayout.findApplicableEditorConfigFiles(): Sequence<File> {
val projectEditorConfig = projectDirectory.file(".editorconfig").asFile

return generateSequence(seed = projectEditorConfig) { editorconfig ->
if (editorconfig.isRootEditorConfig) {
null
} else {
editorconfig.parentFile?.parentFile?.resolve(".editorconfig")
}
}
}

private val File.isRootEditorConfig: Boolean
get() {
if (!isFile || !canRead()) return false

return useLines { lines ->
lines.any { line -> line.matches(editorConfigRootRegex) }
}
}

/**
* According to https://editorconfig.org/ root-most EditorConfig file contains line with `root=true`
*/
private val editorConfigRootRegex = "^root\\s?=\\s?true".toRegex()
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.jmailen.gradle.kotlinter.tasks

import org.gradle.api.file.FileCollection
import org.gradle.api.file.ConfigurableFileCollection
import org.gradle.api.file.ProjectLayout
import org.gradle.api.model.ObjectFactory
import org.gradle.api.provider.ListProperty
Expand All @@ -19,7 +19,6 @@ import org.gradle.work.InputChanges
import org.jmailen.gradle.kotlinter.KotlinterExtension.Companion.DEFAULT_DISABLED_RULES
import org.jmailen.gradle.kotlinter.KotlinterExtension.Companion.DEFAULT_EXPERIMENTAL_RULES
import org.jmailen.gradle.kotlinter.support.KtLintParams
import org.jmailen.gradle.kotlinter.support.findApplicableEditorConfigFiles

abstract class ConfigurableKtLintTask(
projectLayout: ProjectLayout,
Expand All @@ -35,9 +34,7 @@ abstract class ConfigurableKtLintTask(
@get:InputFiles
@get:PathSensitive(PathSensitivity.RELATIVE)
@get:Incremental
internal val editorconfigFiles: FileCollection = objectFactory.fileCollection().apply {
from(projectLayout.findApplicableEditorConfigFiles().toList())
}
internal val editorconfigFiles: ConfigurableFileCollection = objectFactory.fileCollection()

@Internal
protected fun getKtLintParams(): KtLintParams = KtLintParams(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ abstract class LintWorkerAction : WorkAction<LintWorkerParameters> {
text = file.readText(),
ruleProviders = ruleProviders,
script = script,
debug = true,
editorConfigOverride = editorConfigOverride(ktLintParams),
cb = { error, _ -> onError(error) },
),
Expand Down

0 comments on commit 5274ac0

Please sign in to comment.