Last active
November 10, 2019 13:15
-
-
Save Hesamedin/b66fdca0cd1aa00c670e7a7b39ae9e31 to your computer and use it in GitHub Desktop.
KTLint integration
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
apply plugin: 'com.android.application' | |
apply plugin: 'kotlin-android' | |
apply plugin: 'kotlin-android-extensions' | |
apply plugin: 'kotlin-kapt' | |
apply plugin: 'io.fabric' | |
apply from: '../gradle/afsTasks.gradle' <======== | |
android { | |
compileSdkVersion 28 | |
buildToolsVersion '28.0.3' | |
defaultConfig { | |
applicationId 'my.package.name' | |
minSdkVersion 24 | |
targetSdkVersion 28 | |
versionCode 1 | |
versionName "1.0" | |
} | |
... | |
} |
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
// Github repo: https://github.com/pinterest/ktlint | |
repositories { | |
jcenter() | |
} | |
configurations { | |
ktlint | |
} | |
dependencies { | |
ktlint "com.pinterest:ktlint:0.35.0" | |
// additional 3rd party ruleset(s) can be specified here | |
// just add them to the classpath (e.g. ktlint 'groupId:artifactId:version') and | |
// ktlint will pick them up | |
} | |
task ktlint(type: JavaExec, group: "verification") { | |
description = "Check Kotlin code style." | |
classpath = configurations.ktlint | |
main = "com.pinterest.ktlint.Main" | |
args "src/**/*.kt" | |
// to generate report in checkstyle format prepend following args: | |
// "--reporter=plain", "--reporter=checkstyle,output=${buildDir}/ktlint.xml" | |
// see https://github.com/pinterest/ktlint#usage for more | |
} | |
task ktlintFormat(type: JavaExec, group: "formatting") { | |
description = "Fix Kotlin code style deviations." | |
classpath = configurations.ktlint | |
main = "com.pinterest.ktlint.Main" | |
args "-F", "src/**/*.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
#!/bin/sh | |
set -e | |
# Read more info at: https://medium.com/@alistair.cerio/android-ktlint-and-pre-commit-git-hook-5dd606e230a9 | |
echo "***************************" | |
echo "Running git pre-commit hook" | |
echo "***************************" | |
# Run afs_ktlint to make sure there is no lint issue | |
./gradlew afs_ktlint | |
RESULT=$? | |
# return 1 exit code if running checks fails | |
[[ ${RESULT} -ne 0 ]] && exit 1 | |
exit 0 | |
exit 0 |
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
apply from: '../gradle/ktlint.gradle' | |
task installGitHook(type: Copy) { | |
from file("$rootProject.rootDir/scripts/pre-commit") | |
into file("$rootProject.rootDir/.git/hooks") | |
fileMode 0777 | |
} | |
if (!System.getenv('CI')) { | |
println("This is not CI.....") | |
apply from: "$rootProject.rootDir/gradle/ktlint.gradle" | |
tasks.getByPath(':app:preBuild').dependsOn installGitHook | |
tasks.getByPath(':app:preBuild').dependsOn ktlintFormat | |
} | |
check.dependsOn ktlint |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment