SmartToolFactory / Static-Code-Analysis

Playground to test KtLint, Detekt, Git Hooks, Kotlin DSL

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Gradle Kotlin DSL, ktLint, detekt, Git Hooks Sample

ktlint Kotlin Version API

Sample boiler plate code to implement Kotlin DSL, ktlint, detekt and Git Hooks to app on app, library and dynamic feature module step by step with each commit. Also added git hooks using script below in .git\hooks folder with a excutable pre-commit.sh to turn simple text to excutable call chmod a+x pre-commit

#!/bin/sh


echo "Running static analysis..."


# Inspect code using KtLint, and Detekt

# Run KtLint only
#./gradlew app:ktlintCheck --daemon

# Format code using KtLint, then run Detekt and KtLint static analysis
./gradlew app:ktlintFormat app:detekt app:ktlintCheck --daemon

status=$?


if [ "$status" = 0 ] ; then

    echo "Static analysis found no problems."

    exit 0

else

    echo 1>&2 "Static analysis found violations it could not fix."

    exit 1

fi

which calls in order

  1. ktLint to format code before commit
  2. runs detekt for static code analysis
  3. ktlintCheck to check code,

if after these tasks status is 0 then commit is successul, otherwise you need to check out console for links to your code and fix those errors.

Steps taken to migrate to Gradle with Kotlin

  • Rename settings.gradle to settings.gradle.kts
  • Rename project level build.gradle to build.gradle.kts
  • Add plugins using the snippet below
plugins {
    id(Plugins.KTLINT) version PluginVersion.KTLINT_VERSION
    id(Plugins.DETEKT) version PluginVersion.DETEKT_VERSION
}

Then us the snippet below to add ktlint and detekt to each module used in project

subprojects {

    // KtLint
    apply(plugin = Plugins.KTLINT) // Version should be inherited from parent

    // Optionally configure plugin
    ktlint {
        debug.set(true)
    }

    // Detekt
    apply(plugin = Plugins.DETEKT)

    detekt {
        config = files("${project.rootDir}/config/detekt/detekt.yml")
        parallel = true

        reports {
            xml {
                enabled = true
                destination = file("${project.rootDir}/build/reports/detekt_report.xml")
            }
            html {
                enabled = true
                destination = file("${project.rootDir}/build/reports/detekt_report.html")
            }
            txt {
                enabled = true
                destination = file("${project.rootDir}/build/reports/detekt_report.txt")
            }
        }
    }
}
  • Create a buildSrc folder and add it's build.gradle.kts with
plugins {
    `kotlin-dsl`
}

to turn it a dsl folder

Created DependencyHandler extension functions to not repeat adding same dependencies to different modules over and over again

For instance

fun DependencyHandler.addCoreModuleDependencies() {

    implementation(Deps.KOTLIN)
    implementation(Deps.ANDROIDX_CORE_KTX)

    // Support and Widgets
    implementation(Deps.APPCOMPAT)
    implementation(Deps.MATERIAL)
    implementation(Deps.CONSTRAINT_LAYOUT)
    implementation(Deps.RECYCLER_VIEW)
    implementation(Deps.VIEWPAGER2)

    // Lifecycle, LiveData, ViewModel
    implementation(Deps.ARCH_LIFECYCLE)

    // Navigation Components
    implementation(Deps.NAVIGATION_FRAGMENT)
    implementation(Deps.NAVIGATION_UI)
    implementation(Deps.NAVIGATION_RUNTIME)
    implementation(Deps.NAVIGATION_DYNAMIC)

    // Dagger
    implementation(Deps.DAGGER_HILT_ANDROID)
    kapt(Deps.DAGGER_HILT_COMPILER)

    // Room
    implementation(Deps.ROOM_RUNTIME)
    // For Kotlin use kapt instead of annotationProcessor
    kapt(Deps.ROOM_COMPILER)
    // optional - Kotlin Extensions and Coroutines support for Room
    implementation(Deps.ROOM_KTX)
    // optional - RxJava support for Room
    implementation(Deps.ROOM_RXJAVA2)

    // RxJava
    implementation(Deps.RX_JAVA)
    // RxAndroid
    implementation(Deps.RX_ANDRIOD)

    // Coroutines
    implementation(Deps.COROUTINES_CORE)
    implementation(Deps.COROUTINES_ANDROID)

    // Retrofit
    implementation(Deps.RETROFIT)
    implementation(Deps.RETROFIT_GSON_CONVERTER)
    implementation(Deps.RETROFIT_RX_JAVA2_ADAPTER)
    // change base url runtime
    implementation(Deps.RETROFIT_URL_MANAGER)
    // Gson
    implementation(Deps.GSON)

}

About

Playground to test KtLint, Detekt, Git Hooks, Kotlin DSL


Languages

Language:Kotlin 98.4%Language:Shell 1.6%