Skip to content

Commit

Permalink
Configured Git Hooks & Migrated to Convention Plugin (#1759)
Browse files Browse the repository at this point in the history
* Configured Git Hooks & Migrated to Convention Plugin

* Renamed branch name

* updated proguard-rules.pro
  • Loading branch information
niyajali authored Sep 4, 2024
1 parent 5059e62 commit 2451ba5
Show file tree
Hide file tree
Showing 42 changed files with 888 additions and 304 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,14 @@ All your pull requests must pass the CI build only then, it will be allowed to m
Sometimes, when the build doesn't pass you can use these commands in your local terminal and check for the errors,</br>

**We've commited to use Material3 design in our project. And added lint check for not to use any M2 libraries in our project.</br>**
**And when adding new library, please make sure to follow the naming convention and place in sequential order(A->Z).**
**And when adding new library, please make sure to follow the naming convention and place in sequential order(A->Z).</br>**
In MacOS, Windows or Linux, you should run the following commands before opening a PR, and make sure to pass all the commands:
**In order to enhance our development process, we have implemented Git hooks in our project.
To install these hooks locally, simply run the command `./gradlew installGitHooks`.
This will ensure that the Git hooks are installed on your local machine.**</br>
* `./gradlew check -p build-logic` this checks build-logic configured properly.</br>
* `./gradlew spotlessApply --no-configuration-cache` an check and apply formatting to any file.</br>
* `./gradlew dependencyGuardBaseline` to generate dependency-guard baseline.</br>
Expand Down
23 changes: 23 additions & 0 deletions build-logic/convention/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ dependencies {
compileOnly(libs.kotlin.gradlePlugin)
compileOnly(libs.ksp.gradlePlugin)
compileOnly(libs.room.gradlePlugin)
compileOnly(libs.detekt.gradlePlugin)
compileOnly(libs.ktlint.gradlePlugin)
compileOnly(libs.spotless.gradle)
implementation(libs.truth)
}

Expand Down Expand Up @@ -88,5 +91,25 @@ gradlePlugin {
id = "mifospay.android.application.flavors"
implementationClass = "AndroidApplicationFlavorsConventionPlugin"
}
register("detekt") {
id = "mifos.detekt.plugin"
implementationClass = "MifosDetektConventionPlugin"
description = "Configures detekt for the project"
}
register("spotless") {
id = "mifos.spotless.plugin"
implementationClass = "MifosSpotlessConventionPlugin"
description = "Configures spotless for the project"
}
register("ktlint") {
id = "mifos.ktlint.plugin"
implementationClass = "MifosKtlintConventionPlugin"
description = "Configures kotlinter for the project"
}
register("gitHooks") {
id = "mifos.git.hooks"
implementationClass = "MifosGitHooksConventionPlugin"
description = "Installs git hooks for the project"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ class AndroidApplicationConventionPlugin : Plugin<Project> {
apply("org.jetbrains.kotlin.android")
apply("mifospay.android.lint")
apply("com.dropbox.dependency-guard")
apply("mifos.detekt.plugin")
apply("mifos.spotless.plugin")
apply("mifos.ktlint.plugin")
apply("mifos.git.hooks")
}

extensions.configure<ApplicationExtension> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ class AndroidLibraryConventionPlugin : Plugin<Project> {
apply("com.android.library")
apply("org.jetbrains.kotlin.android")
apply("mifospay.android.lint")
apply("mifos.detekt.plugin")
apply("mifos.spotless.plugin")
apply("mifos.ktlint.plugin")
}

extensions.configure<LibraryExtension> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

import org.gradle.api.Plugin
import org.gradle.api.Project
import org.mifospay.configureDetekt
import org.mifospay.detektGradle

class MifosDetektConventionPlugin : Plugin<Project> {
override fun apply(target: Project) {
with(target) {
applyPlugins()

detektGradle {
configureDetekt(this)
}
}
}

private fun Project.applyPlugins() {
pluginManager.apply {
apply("io.gitlab.arturbosch.detekt")
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@

import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.tasks.Copy
import org.gradle.api.tasks.Exec
import org.gradle.kotlin.dsl.register
import java.util.Locale

class MifosGitHooksConventionPlugin : Plugin<Project> {
override fun apply(project: Project) {
// Define a function to check if the OS is Linux or MacOS
fun isLinuxOrMacOs(): Boolean {
val osName = System.getProperty("os.name").lowercase(Locale.getDefault())
return osName.contains("linux") || osName.contains("mac os") || osName.contains("macos")
}

// Define the copyGitHooks task
project.tasks.register<Copy>("copyGitHooks") {
description = "Copies the git hooks from /scripts to the .git/hooks folder."
from("${project.rootDir}/scripts/") {
include("**/*.sh")
rename { it.removeSuffix(".sh") }
}
into("${project.rootDir}/.git/hooks")
}

// Define the installGitHooks task
project.tasks.register<Exec>("installGitHooks") {
description = "Installs the pre-commit git hooks from the scripts directory."
group = "git hooks"
workingDir = project.rootDir

if (isLinuxOrMacOs()) {
commandLine("chmod", "-R", "+x", ".git/hooks/")
}else {
commandLine("cmd", "/c", "attrib", "-R", "+X", ".git/hooks/*.*")
}
dependsOn(project.tasks.named("copyGitHooks"))

doLast {
println("Git hooks installed successfully.")
}
}

// Configure task dependencies after evaluation
project.afterEvaluate {
project.tasks.matching {
it.name in listOf("preBuild", "build", "assembleDebug", "assembleRelease", "installDebug", "installRelease", "clean")
}.configureEach {
dependsOn(project.tasks.named("installGitHooks"))
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import org.gradle.api.Plugin
import org.gradle.api.Project

class MifosKtlintConventionPlugin : Plugin<Project> {
override fun apply(target: Project) {
with(target) {
applyPlugins()
}
}

private fun Project.applyPlugins() {
pluginManager.apply {
apply("org.jlleitschuh.gradle.ktlint")
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.mifospay.configureSpotless
import org.mifospay.spotlessGradle

class MifosSpotlessConventionPlugin : Plugin<Project> {
override fun apply(target: Project) {
with(target) {
applyPlugins()

spotlessGradle {
configureSpotless(this)
}
}
}

private fun Project.applyPlugins() {
pluginManager.apply {
apply("com.diffplug.spotless")
}
}
}
23 changes: 23 additions & 0 deletions build-logic/convention/src/main/kotlin/org/mifospay/Detekt.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.mifospay

import io.gitlab.arturbosch.detekt.Detekt
import io.gitlab.arturbosch.detekt.extensions.DetektExtension
import org.gradle.api.Project
import org.gradle.kotlin.dsl.dependencies
import org.gradle.kotlin.dsl.named

internal fun Project.configureDetekt(extension: DetektExtension) = extension.apply {
tasks.named<Detekt>("detekt") {
reports {
xml.required.set(true)
html.required.set(true)
txt.required.set(true)
sarif.required.set(true)
md.required.set(true)
}
}
dependencies {
"detektPlugins"(libs.findLibrary("detekt-formatting").get())
"detektPlugins"(libs.findLibrary("twitter-detekt-compose").get())
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
package org.mifospay

import com.diffplug.gradle.spotless.SpotlessExtension
import io.gitlab.arturbosch.detekt.extensions.DetektExtension
import org.gradle.api.Project
import org.gradle.api.artifacts.VersionCatalog
import org.gradle.api.artifacts.VersionCatalogsExtension
import org.gradle.kotlin.dsl.configure
import org.gradle.kotlin.dsl.getByType

val Project.libs
get(): VersionCatalog = extensions.getByType<VersionCatalogsExtension>().named("libs")

inline fun Project.detektGradle(crossinline configure: DetektExtension.() -> Unit) =
extensions.configure<DetektExtension> {
configure()
}

inline fun Project.spotlessGradle(crossinline configure: SpotlessExtension.() -> Unit) =
extensions.configure<SpotlessExtension> {
configure()
}
31 changes: 31 additions & 0 deletions build-logic/convention/src/main/kotlin/org/mifospay/Spotless.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.mifospay

import com.diffplug.gradle.spotless.SpotlessExtension
import org.gradle.api.Project

val ktlintVersion = "1.0.1"

internal fun Project.configureSpotless(extension: SpotlessExtension) = extension.apply {
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"), "(<[^!?])")
}
}
1 change: 1 addition & 0 deletions build-logic/settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ dependencyResolutionManagement {
repositories {
google()
mavenCentral()
gradlePluginPortal()
}
versionCatalogs {
create("libs") {
Expand Down
65 changes: 4 additions & 61 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ buildscript {
classpath(libs.google.oss.licenses.plugin) {
exclude(group = "com.google.protobuf")
}
classpath(libs.spotless.gradle)
}
}

Expand All @@ -25,70 +24,14 @@ plugins {
alias(libs.plugins.secrets) apply false
alias(libs.plugins.room) apply false
alias(libs.plugins.kotlin.android) apply false
alias(libs.plugins.detekt) apply true
// Plugin applied to allow module graph generation
alias(libs.plugins.module.graph) apply true
alias(libs.plugins.spotless) apply true
alias(libs.plugins.detekt) apply false
alias(libs.plugins.spotless) apply false
alias(libs.plugins.ktlint) apply false
alias(libs.plugins.version.catalog.linter) apply true
// Multiplatform plugins
alias(libs.plugins.jetbrainsCompose) apply false
alias(libs.plugins.compose.compiler) apply false
alias(libs.plugins.kotlinMultiplatform) apply false
alias(libs.plugins.wire) apply false
}

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)
}
}

// Task to print all the module paths in the project e.g. :core:data
// Used by module graph generator script
tasks.register("printModulePaths") {
subprojects {
if (subprojects.size == 0) {
println(this.path)
}
}
}
1 change: 1 addition & 0 deletions ci-prebuild.bat → ci-prepush.bat
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ echo Starting all checks and tests...
call :run_gradle_task "check -p build-logic"
call :run_gradle_task "spotlessApply --no-configuration-cache"
call :run_gradle_task "dependencyGuardBaseline"
call :run_gradle_task "formatVersionCatalog"
call :run_gradle_task "detekt"
call :run_gradle_task "testDemoDebug :lint:test :mifospay:lintProdRelease :lint:lint"
call :run_gradle_task "build"
Expand Down
1 change: 1 addition & 0 deletions ci-prebuild.sh → ci-prepush.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ tasks=(
"spotlessApply --no-configuration-cache"
"dependencyGuardBaseline"
"detekt"
"formatVersionCatalog"
"testDemoDebug :lint:test :lint:lint :mifospay:lintProdRelease"
"build"
"updateProdReleaseBadging"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ class BlockUnblockCommand @Inject constructor(
requestValues.command,
)
withContext(Dispatchers.Main) {
Log.d("BlockUnblockCommand@@@@","$res")
Log.d("BlockUnblockCommand@@@@", "$res")
useCaseCallback.onSuccess(ResponseValue)
}
} catch (e: Exception) {
Log.d("BlockUnblockCommand@@@@","${e.message}")
Log.d("BlockUnblockCommand@@@@", "${e.message}")
useCaseCallback.onError(
"Error " + requestValues.command + "ing account",
)
Expand Down
Loading

0 comments on commit 2451ba5

Please sign in to comment.