Skip to content

Commit

Permalink
Initial Detekt, Ktlint and Spotless Setup for Static Analysis (#2671)
Browse files Browse the repository at this point in the history
  • Loading branch information
niyajali authored Aug 27, 2024
1 parent 8146fce commit eb0f4b4
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 56 deletions.
69 changes: 17 additions & 52 deletions .github/workflows/master_dev_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,64 +12,29 @@ jobs:
name: Build APK
runs-on: ubuntu-latest
steps:
- name: Checking out repository
- name: Checkout code
uses: actions/checkout@v4

# Set up JDK
- name: Set Up JDK 1.8
uses: actions/setup-java@v1
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: 11
distribution: 'zulu'
java-version: 17

# Install NDK
# - name: Install NDK
# run: echo "y" | sudo ${ANDROID_HOME}/tools/bin/sdkmanager --install "ndk;20.0.5594570" --sdk_root=${ANDROID_SDK_ROOT}
- name: Setup Gradle
uses: gradle/actions/setup-gradle@v4

# Update Gradle Permission
- name: Change gradlew Permission
run: chmod +x gradlew

# Build App
- name: Build with Gradle
run: ./gradlew assemble

# Upload Built APK
- name: Upload Build Artifacts
uses: actions/upload-artifact@v3.1.3
- name: Cache Gradle and build outputs
uses: actions/cache@v4
with:
name: mifos-mobile
path: app/build/outputs/apk/debug/
path: |
~/.gradle/caches
~/.gradle/wrapper
build
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
restore-keys: ${{ runner.os }}-gradle-

lintCheck:
name: Static Analysis
runs-on: ubuntu-latest
steps:
- name: Checking out repository
uses: actions/checkout@v4

- name: Static Analysis
run: ./gradlew lint
- name: Build With Gradle
run: ./gradlew build

- name: Upload Static Analysis Report For Mifos-Mobile Module
uses: actions/upload-artifact@v3.1.3
if: failure()
with:
name: Static Analysis Report
path: app/build/reports/

pmd:
name: PMD
runs-on: ubuntu-latest
steps:
- name: Checking out repository
uses: actions/checkout@v4

- name: PMD Check
run: ./gradlew pmd

- name: Upload PMD Report
uses: actions/upload-artifact@v3.1.3
if: failure()
with:
name: PMD Report
path: app/build/reports/
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ build/
# Gradle cache
.gradle

# Kotlin
.kotlin

# Android Studio captures folder
captures/

Expand Down
55 changes: 51 additions & 4 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath(libs.google.oss.licenses.plugin) {
exclude(group = "com.google.protobuf")
}

classpath(libs.spotless.gradle)
}
}

Expand All @@ -30,4 +28,53 @@ plugins {
alias(libs.plugins.kotlin.android) apply false
alias(libs.plugins.spotbugs) apply false
alias(libs.plugins.kotlinMultiplatform) apply false
alias(libs.plugins.detekt) apply true
alias(libs.plugins.spotless) apply true
}

val detektFormatting = libs.detekt.formatting
val twitterComposeRules = libs.twitter.detekt.compose
val ktlintVersion = "1.0.1"

subprojects {
apply {
plugin("io.gitlab.arturbosch.detekt")
plugin("com.diffplug.spotless")
}

tasks.withType<io.gitlab.arturbosch.detekt.Detekt>().configureEach {
config.from(rootProject.files("config/detekt/detekt.yml"))
reports.xml.required.set(true)
reports.html.required.set(true)
}

extensions.configure<com.diffplug.gradle.spotless.SpotlessExtension> {
kotlin {
target("**/*.kt")
targetExclude("**/build/**/*.kt")
ktlint(ktlintVersion).editorConfigOverride(
mapOf(
"android" to "true",
),
)
licenseHeaderFile(rootProject.file("spotless/copyright.kt"))
}
format("kts") {
target("**/*.kts")
targetExclude("**/build/**/*.kts")
// Look for the first line that doesn't have a block comment (assumed to be the license)
licenseHeaderFile(rootProject.file("spotless/copyright.kts"), "(^(?![\\/ ]\\*).*$)")
}
format("xml") {
target("**/*.xml")
targetExclude("**/build/**/*.xml")
// Look for the first XML tag that isn't a comment (<!--) or the xml declaration (<?xml)
licenseHeaderFile(rootProject.file("spotless/copyright.xml"), "(<[^!?])")
}
}

dependencies {
detektPlugins(detektFormatting)
detektPlugins(twitterComposeRules)
}
}
10 changes: 10 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ coreKtxVersion = "1.12.0"
coreTestingVersion = "2.2.0"
coreVersion = "3.5.2"
countrycodechooserVersion = "1.0"
detekt = "1.23.5"
espressoContribVersion = "3.5.1"
kotlinxCoroutinesAndroidVersion = "1.6.4"
kotlinxCoroutinesTestVersion = "1.7.3"
Expand Down Expand Up @@ -69,13 +70,15 @@ dbflowVersion = "4.2.4"
preference = "1.0.0"
playServicesVersion = "17.0.1"
spotbugs = "4.8.0"
spotlessVersion = "6.23.3"
material3 = "1.2.1"
playServicesCodeScanner = "16.1.0"
googleAppCodeScanner = "17.2.0"
cameraxVersion = "1.3.1"
compose-material = "1.6.8"
googleMaps = "4.4.1"
turbineVersion = "1.1.0"
twitter-detekt-compose = "0.0.26"
uihouseVersion = "alpha-2.1"
vectordrawableVersion = "1.1.0"
zxingVersion = "1.9.13"
Expand Down Expand Up @@ -163,6 +166,10 @@ androidx-camera-view = { group = "androidx.camera", name = "camera-view", versio
androidx-camera-core = { group = "androidx.camera", name = "camera-core", version.ref = "cameraxVersion" }
google-map-compose = { group = "com.google.maps.android", name = "maps-compose", version.ref = "googleMaps" }

#Detekt
detekt-formatting = { group = "io.gitlab.arturbosch.detekt", name = "detekt-formatting", version.ref = "detekt" }
twitter-detekt-compose = { group = "com.twitter.compose.rules", name = "detekt", version.ref = "twitter-detekt-compose" }

# Dependencies of the included build-logic
android-gradlePlugin = { group = "com.android.tools.build", name = "gradle", version.ref = "androidGradlePlugin" }
android-tools-common = { group = "com.android.tools", name = "common", version.ref = "androidTools" }
Expand All @@ -178,10 +185,12 @@ androidx-test-ext-junit = { group = "androidx.test.ext", name = "junit", version
espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "espresso-core" }
material = { group = "com.google.android.material", name = "material", version.ref = "material" }
zxing = { module = "me.dm7.barcodescanner:zxing", version.ref = "zxingVersion" }
spotless-gradle = { group = "com.diffplug.spotless", name = "spotless-plugin-gradle", version.ref = "spotlessVersion" }

[plugins]
android-application = { id = "com.android.application", version.ref = "androidGradlePlugin" }
android-library = { id = "com.android.library", version.ref = "androidGradlePlugin" }
detekt = { id = "io.gitlab.arturbosch.detekt", version.ref = "detekt" }
gms = { id = "com.google.gms.google-services", version.ref = "gmsPlugin" }
hilt = { id = "com.google.dagger.hilt.android", version.ref = "hilt" }
firebase-crashlytics = { id = "com.google.firebase.crashlytics", version.ref = "firebaseCrashlyticsPlugin" }
Expand All @@ -198,6 +207,7 @@ roborazzi = { id = "io.github.takahirom.roborazzi", version.ref = "roborazzi" }
ksp = { id = "com.google.devtools.ksp", version.ref = "ksp"}
room = { id = "androidx.room", version.ref = "room" }
spotbugs = { id = "com.github.spotbugs", version.ref = "spotbugs" }
spotless = { id = "com.diffplug.spotless", version.ref = "spotlessVersion" }
kotlinMultiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }

# Plugins defined by this project
Expand Down
9 changes: 9 additions & 0 deletions spotless/copyright.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* Copyright $YEAR Mifos Initiative
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* See https://github.com/openMF/mobile-mobile/blob/master/LICENSE.md
*/
9 changes: 9 additions & 0 deletions spotless/copyright.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* Copyright $YEAR Mifos Initiative
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* See https://github.com/openMF/mobile-mobile/blob/master/LICENSE.md
*/
10 changes: 10 additions & 0 deletions spotless/copyright.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright $YEAR Mifos Initiative
This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
If a copy of the MPL was not distributed with this file,
You can obtain one at https://mozilla.org/MPL/2.0/.
See https://github.com/openMF/mobile-mobile/blob/master/LICENSE.md
-->

0 comments on commit eb0f4b4

Please sign in to comment.