Reputation: 3270
In my Android Studio app project, I've integrated ktlint gradle.
Is it possible to configure the ktlint gradle plugin, so that it runs continuously on the fly (while coding on the keyboard)?? Like ESlint is used for JS, it warns me immediately if any lint warnings occur..
Upvotes: 0
Views: 1394
Reputation: 1147
After lot of googling and exploring about ktlint we can setup like this
module(ex: app) level gradle
apply plugin: 'org.jlleitschuh.gradle.ktlint'
dependencies {}
ktlint {
version = "0.48.2"
debug = true
verbose = true
android = true
outputToConsole = true
outputColorName = "RED"
ignoreFailures = true
enableExperimentalRules = true
additionalEditorconfig = [ // not supported until ktlint 0.49
"max_line_length": "20"
]
disabledRules = ["final-newline"] // not supported with ktlint 0.48+
baseline = file("${projectDir}/config/ktlint/baseline.xml")
reporters {
reporter "plain"
reporter "checkstyle"
reporter "sarif"
}
kotlinScriptAdditionalPaths {
include fileTree("scripts/")
}
filter {
exclude("**/generated/**")
include("**/kotlin/**")
}
}
project level build
plugins {
id("org.jlleitschuh.gradle.ktlint") version "12.1.0" apply false
}
allprojects {
task copyGitHooks(type: Copy) {
description = "Copies the git hooks from /git-hooks to the .git folder."
group = "git hooks"
from "$rootDir/scripts/pre-commit"
into "$rootDir/.git/hooks/"
}
task installGitHooks(type: Exec) {
description = "Installs the pre-commit git hooks from /git-hooks."
group = "git hooks"
workingDir rootDir
commandLine "chmod", "-R", "+x", ".git/hooks/"
dependsOn copyGitHooks
doLast {
logger.info("Git hook installed successfully.")
}
}
afterEvaluate { project ->
if (project.tasks.findByName("preBuild") != null) {
project.tasks.named("preBuild") { dependsOn "installGitHooks" }
}
}
}
Create a pre-commit(no extension) file inside a scripts folder [project top level]
#!/bin/bash
echo "............Starting ktlint check......................................"
echo "Started Formitting...."
./gradlew ktlintFormat
#
#echo ".....Staging all file changed during ktlint formatting................."
#
#git add .
echo "Completed.............................................................."
To enable git hook you need to run git config core.hooksPath .git/hooks
You need to create ktlint baseline for each module
Steps:
or from terminal you can run ./gradlew :app:ktlintGenerateBaseline
Using extension you can setup this by installing
Upvotes: 2
Reputation: 709
This is possible in Intellij / Android Studio if you install the ktlint IDEA plugin.
It's also configurable so you can represent findings as warnings or errors.
Upvotes: 1
Reputation: 4951
It is not possible to do that. klint is not a static analysis tool.
Alternatively, you can write custom lint rules for the same. Here are some resources for doing so:
Upvotes: 0