-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Automatically format generated files using ktlint if possible
- Loading branch information
Showing
7 changed files
with
116 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
...gin/src/main/kotlin/io/github/sgrishchenko/karakum/gradle/plugin/service/KtLintService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package io.github.sgrishchenko.karakum.gradle.plugin.service | ||
|
||
import com.pinterest.ktlint.rule.engine.api.KtLintRuleEngine | ||
import com.pinterest.ktlint.ruleset.standard.StandardRuleSetProvider | ||
import org.gradle.api.services.BuildService | ||
import org.gradle.api.services.BuildServiceParameters.None | ||
|
||
abstract class KtLintService : BuildService<None> { | ||
val ktlint = KtLintRuleEngine( | ||
ruleProviders = StandardRuleSetProvider().getRuleProviders() | ||
).apply { | ||
// ktlint stores .editorconfig files in a companion object and thus as static state | ||
// this is not good when running multiple builds within the same Gradle daemon | ||
// and the .editorconfig file might have changed, so clear that cache | ||
// so that .editorconfig files are read freshly | ||
trimMemory() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
...src/main/kotlin/io/github/sgrishchenko/karakum/gradle/plugin/worker/KtLintFormatWorker.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package io.github.sgrishchenko.karakum.gradle.plugin.worker | ||
|
||
import com.pinterest.ktlint.rule.engine.api.Code | ||
import com.pinterest.ktlint.rule.engine.core.api.AutocorrectDecision.ALLOW_AUTOCORRECT | ||
import com.pinterest.ktlint.rule.engine.core.api.AutocorrectDecision.NO_AUTOCORRECT | ||
import io.github.sgrishchenko.karakum.gradle.plugin.service.KtLintService | ||
import org.gradle.api.file.RegularFileProperty | ||
import org.gradle.api.logging.Logging | ||
import org.gradle.api.provider.Property | ||
import org.gradle.api.services.ServiceReference | ||
import org.gradle.workers.WorkAction | ||
import org.gradle.workers.WorkParameters | ||
|
||
abstract class KtLintFormatWorker : WorkAction<KtLintFormatWorker.Parameters> { | ||
private val logger = Logging.getLogger(KtLintFormatWorker::class.java) | ||
|
||
@get:ServiceReference | ||
abstract val ktlintService: Property<KtLintService> | ||
|
||
override fun execute() { | ||
val file = parameters.file.get().asFile | ||
logger.info("Formatting ${file.absolutePath}") | ||
runCatching { | ||
ktlintService | ||
.get() | ||
.ktlint | ||
.format(Code.fromFile(file)) { lintError -> | ||
if (lintError.canBeAutoCorrected) ALLOW_AUTOCORRECT else NO_AUTOCORRECT | ||
}.also(file::writeText) | ||
}.onFailure { | ||
logger.info("Formatting failed for ${file.absolutePath}") | ||
logger.info(it.stackTraceToString()) | ||
}.onSuccess { | ||
logger.info("Formatting successful for ${file.absolutePath}") | ||
} | ||
} | ||
|
||
interface Parameters : WorkParameters { | ||
val file: RegularFileProperty | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters