Ralf Wickum
Ralf Wickum

Reputation: 3270

How to configure ktlint gradle plugin in a continuous mode?

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

Answers (3)

Shubham Kumar Gupta
Shubham Kumar Gupta

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:

  • Sync
  • Gradle Tasks
  • App Module Tasks
  • help
  • ktlintGenerateBaseline

or from terminal you can run ./gradlew :app:ktlintGenerateBaseline

Using extension you can setup this by installing

https://plugins.jetbrains.com/files/15057/287040/ktlint-0.12.0.zip?updateId=287040&pluginId=15057&family=INTELLIJ

enter image description here

Upvotes: 2

amram99
amram99

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

Xid
Xid

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:

  1. Android Dev summit 2018 talk
  2. Android Dev summit 2019 talk
  3. Android source code for standard lint rules
  4. Repos
    1. https://github.com/googlesamples/android-custom-lint-rules
    2. https://github.com/tikurahul/lint-experiments
    3. https://github.com/vanniktech/lint-rules

Upvotes: 0

Related Questions