https://github.com/smarttoolfactory/static-code-analysis
Playground to test KtLint, Detekt, Git Hooks, Kotlin DSL
https://github.com/smarttoolfactory/static-code-analysis
detekt-plugin git-hooks gradle-kotlin-dsl ktlint ktlint-gradle
Last synced: 3 months ago
JSON representation
Playground to test KtLint, Detekt, Git Hooks, Kotlin DSL
- Host: GitHub
- URL: https://github.com/smarttoolfactory/static-code-analysis
- Owner: SmartToolFactory
- Created: 2020-07-25T12:32:12.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2020-07-31T06:30:22.000Z (almost 5 years ago)
- Last Synced: 2025-01-08T14:11:53.983Z (5 months ago)
- Topics: detekt-plugin, git-hooks, gradle-kotlin-dsl, ktlint, ktlint-gradle
- Language: Kotlin
- Homepage:
- Size: 207 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Gradle Kotlin DSL, ktLint, detekt, Git Hooks Sample
[](https://ktlint.github.io/)
[](https://kotlinlang.org)
[](https://android-arsenal.com/api?level=21)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/shecho "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 --daemonstatus=$?
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 = truereports {
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 folderCreated [DependencyHandler](buildSrc/src/main/java/extension/DependencyHandlerExtension.kt) 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)}
```