diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 5420620..0a7ecec 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -153,7 +153,7 @@ fun getBaseUrl(): String? { configure { 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 diff --git a/build.gradle.kts b/build.gradle.kts index a584293..23a7332 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,5 +1,4 @@ // 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 @@ -7,4 +6,48 @@ plugins { alias(libs.plugins.hilt) apply false alias(libs.plugins.spotless) apply false } -true // Needed to make the Suppress annotation work for the plugins block \ No newline at end of file + +tasks.register("clean", Delete::class) { + delete(rootProject.buildDir) +} + +// For Git hooks +tasks { + register("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("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("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" +) + diff --git a/pre-commit b/pre-commit new file mode 100644 index 0000000..694613f --- /dev/null +++ b/pre-commit @@ -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 \ No newline at end of file