Skip to content

Commit

Permalink
Add Hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
jxareas committed Jan 8, 2023
1 parent 5225af5 commit 6819780
Show file tree
Hide file tree
Showing 9 changed files with 153 additions and 2 deletions.
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*]
insert_final_newline = true
trim_trailing_whitespace = true

[*.{kt,kts}]
ij_kotlin_allow_trailing_comma = true
ij_kotlin_allow_trailing_comma_on_call_site = true
1 change: 1 addition & 0 deletions _config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
theme: jekyll-theme-cayman
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ plugins {
.version(Versions.DETEKT)
}

apply(from = BuildScripts.HOOKS)
subprojects {
apply(from = "${rootProject.projectDir}/${BuildScripts.KTLINT}")
apply(from = "${rootProject.projectDir}/${BuildScripts.VERSIONS}")
Expand Down
7 changes: 5 additions & 2 deletions buildSrc/src/main/java/BuildScripts.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
object BuildScripts {
const val KTLINT = "buildscripts/ktlint.gradle"
const val VERSIONS = "buildscripts/versions.gradle"
private const val DEFAULT_PATH = "buildscripts/"

const val KTLINT = DEFAULT_PATH.plus("ktlint.gradle")
const val VERSIONS = DEFAULT_PATH.plus("versions.gradle")
const val HOOKS = DEFAULT_PATH.plus("hooks.gradle")
}
28 changes: 28 additions & 0 deletions buildscripts/hooks.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
static def isLinuxOrMacOs() {
def osName = System.getProperty('os.name').toLowerCase(Locale.ROOT)
return osName.contains('linux') || osName.contains('mac os') || osName.contains('macos')
}

task copyGitHooks(type: Copy) {
description 'Copies the git hooks from /git-hooks to the .git folder.'
group 'git hooks'
from("${rootDir}/git-hooks/") {
include '**/*.sh'
rename '(.*).sh', '$1'
}
into "${rootDir}/.git/hooks"
onlyIf { isLinuxOrMacOs() }
}

task installGitHooks(type: Exec) {
description 'Installs the pre-commit git hooks from /git-hooks.'
group 'git hooks'
workingDir rootDir
commandLine 'chmod'
args '-R', '+x', '.git/hooks/'
dependsOn copyGitHooks
onlyIf { isLinuxOrMacOs() }
doLast {
logger.info('Git hook installed successfully.')
}
}
21 changes: 21 additions & 0 deletions docs/Hooks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Git Hooks

This project has some Git hooks included inside the [git-hooks](/hooks) folder. These hooks can be
installed automatically via the Gradle commands `./gradlew copyGitHooks`
and `./gradlew installGitHooks`. You can find these commands
in [this Gradle file](/buildscripts/githooks.gradle), but it's also good to know that the hooks are
installed automatically just by running a `clean` task.

## Pre-Commit

There is a [pre-commit](/hooks/pre-commit.sh) hook that will automatically run Ktlint formatting
over any modified Kotlin files.
This way you can just commit your code and trust that formatting happens behind the scenes,
without having to consciously worry about it.

## Pre-Push

There is a [pre-push](/hooks/pre-push.sh) hook that will run static analysis checks before any code
is pushed up to the remote repository.
This way, if you have any code smells, you can be alerted right away without waiting for
some Github Actions to fail.
26 changes: 26 additions & 0 deletions hooks/pre-commit.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/bin/sh

######## KTLINT-GRADLE HOOK START ########

CHANGED_FILES="$(git --no-pager diff --name-status --no-color --cached | awk '$1 != "D" && $2 ~ /\.kts|\.kt/ { print $2}')"

if [ -z "$CHANGED_FILES" ]; then
echo "No Kotlin staged files."
exit 0
fi;

echo "Running ktlint over these files:"
echo "$CHANGED_FILES"

./gradlew --quiet ktlintFormat -PinternalKtlintGitFilter="$CHANGED_FILES"

echo "Completed ktlint run."

echo "$CHANGED_FILES" | while read -r file; do
if [ -f $file ]; then
git add $file
fi
done

echo "Completed ktlint hook."
######## KTLINT-GRADLE HOOK END ########
6 changes: 6 additions & 0 deletions hooks/pre-push.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh

echo "Running static analysis."

./gradlew ktlintCheck
./gradlew detektAll
56 changes: 56 additions & 0 deletions scripts/combine_ktlint_reports.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# This takes all ktlint output XML files and combines them into one `ktlint-report.xml` file.

import sys
import os.path
from xml.etree import ElementTree

first = None
parentDirectory = "../"
ktlintMain = "/build/reports/ktlint/ktlintMainSourceSetCheck/ktlintMainSourceSetCheck.xml"
ktlintTest = "/build/reports/ktlint/ktlintTestSourceSetCheck/ktlintTestSourceSetCheck.xml"
ktlintAndroidTest = "/build/reports/ktlint/ktlintAndroidTestSourceSetCheck/ktlintAndroidTestSourceSetCheck.xml"
detekt = "/build/reports/detekt/detekt.xml"
lint = "/build/reports/lint-results.xml"

file_list = []

module_list = [
"app",
"database",
"analytics"
]

for module in module_list:
file_list.append(parentDirectory + module + ktlintMain)
file_list.append(parentDirectory + module + ktlintTest)
file_list.append(parentDirectory + module + ktlintAndroidTest)
file_list.append(parentDirectory + module + detekt)

print(file_list)

ktlintFile = 'ktlint-report-orig.xml'
editedKtlintFile = 'ktlint-report.xml'

for filename in file_list:
if os.path.isfile(filename):
data = ElementTree.parse(filename).getroot()
if first is None:
first = data
else:
first.extend(data)
if first is not None:
f = open( ktlintFile, 'w+' )
f.write( "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" )
f.write( ElementTree.tostring(first) )
f.close()

delete_list = []
fin = open(ktlintFile)
fout = open(editedKtlintFile, "w+")
for line in fin:
for word in delete_list:
line = line.replace(word, "")
fout.write(line)
print(line)
fin.close()
fout.close()

0 comments on commit 6819780

Please sign in to comment.