Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ fun getBaseUrl(): String? {
configure<com.diffplug.gradle.spotless.SpotlessExtension> {
kotlin {
target("**/*.kt")
// by default the target is every '.kt' and '.kts` file in the java sourcesets

ktfmt().kotlinlangStyle() // has its own section below
// ktlint() // has its own section below
// diktat() // has its own section below
Expand Down
47 changes: 45 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,10 +1,53 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
@Suppress("DSL_SCOPE_VIOLATION") // TODO: Remove once KTIJ-19369 is fixed
plugins {
alias(libs.plugins.androidApplication) apply false
alias(libs.plugins.kotlinAndroid) apply false
alias(libs.plugins.ksp) apply false
alias(libs.plugins.hilt) apply false
alias(libs.plugins.spotless) apply false
}
true // Needed to make the Suppress annotation work for the plugins block

tasks.register("clean", Delete::class) {
delete(rootProject.buildDir)
}

// For Git hooks
tasks {
register<Copy>("copyGitHooks") {
description = "Copies the git hooks from scripts/git-hooks to the .git folder."
group = "git hooks"
from("${rootProject.rootDir}/pre-commit")
into(".git/hooks")
}

register<Exec>("installGitHooks") {
description = "Installs the pre-commit git hooks from scripts/git-hooks."
group = "git hooks"
workingDir(rootDir)
commandLine("chmod")
args("-R", "+x", ".git/hooks/")
dependsOn(named("copyGitHooks"))
onlyIf {
isLinuxOrMacOs()
}
doLast {
logger.info("Git hooks installed successfully.")
}
}
register<Delete>("deleteGitHooks") {
description = "Delete the pre-commit git hooks."
group = "git hooks"
delete(fileTree(".git/hooks/"))
}
}

afterEvaluate {
tasks["clean"].dependsOn(tasks.named("installGitHooks"))
}

fun isLinuxOrMacOs() = System.getProperty("os.name").lowercase(java.util.Locale.ROOT) in listOf(
"linux",
"mac os",
"macos"
)

33 changes: 33 additions & 0 deletions pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/bin/bash
echo "*********************************************************"
echo "Running git pre-commit hook. Running Static analysis... "
echo "*********************************************************"

./gradlew spotlessApply

status=$?

if [ "$status" = 0 ] ; then
echo "Static analysis found no problems."
exit 0
else
echo "*********************************************************"
echo " ******************************************** "
echo 1>&2 "Static analysis found violations it could not fix."
echo "Run ./gradlew ktlintFormat to fix formatting related issues..."
echo " ******************************************** "
echo "*********************************************************"
exit 1
fi

echo "*********************************************************"
echo "Running git pre-commit hook. Running test cases... "
echo "*********************************************************"

./gradlew test connectedAndroidTest

# Check if tests failed
if [ $? -ne 0 ]; then
echo "Error: Tests failed. Please fix before committing."
exit 1
fi