Skip to content

Instantly share code, notes, and snippets.

@kalaiselvan369
Created April 17, 2020 10:49
Show Gist options
  • Save kalaiselvan369/65ef47c5823178c1a06a0b4df25a00d8 to your computer and use it in GitHub Desktop.
Save kalaiselvan369/65ef47c5823178c1a06a0b4df25a00d8 to your computer and use it in GitHub Desktop.
Spotless and pre-commit
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: "androidx.navigation.safeargs.kotlin"
apply plugin: 'com.google.gms.google-services'
apply from: "$rootDir/spotless.gradle"
def keystorePropertiesFile = rootProject.file("keystore.properties")
def keystoreProperties = new Properties()
keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
android {
signingConfigs {
config {
keyAlias keystoreProperties['keyAlias']
keyPassword keystoreProperties['keyPassword']
storeFile file(keystoreProperties['storeFile'])
storePassword keystoreProperties['storePassword']
}
}
compileSdkVersion project.ext.compileSdkVersion
buildToolsVersion project.ext.buildToolsVersion
defaultConfig {
applicationId "com.example.android.boilerplate"
minSdkVersion project.ext.minSdkVersion
targetSdkVersion project.ext.targetSdkVersion
versionCode rootProject.gitCommitCount
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
shrinkResources true
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.config
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
// add your support dependency here
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:$androidGradlePluginVersion"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
classpath "com.diffplug.spotless:spotless-plugin-gradle:3.28.1"
}
}
allprojects {
repositories {
google()
jcenter()
}
}
ext {
// query git for the commit count to automate versioning.
gitCommitCount = 100 +
Integer.parseInt('git rev-list --count HEAD'.execute([], project.rootDir).text.trim())
}
// adding pre-commit to .git/hooks directory before app starts building
task installGitHook(type: Copy) {
from new File(rootProject.rootDir, 'pre-commit')
into { new File(rootProject.rootDir, '.git/hooks') }
fileMode 0777
}
tasks.getByPath('app:preBuild').dependsOn installGitHook
#!/bin/bash
LC_ALL=C
RED='\033[0;1;31m'
NC='\033[0m' # No Color
echo "Running git pre-commit hook"
local_branch="$(git rev-parse --abbrev-ref HEAD)"
valid_branch_regex="^(feature|bugfix|improvement|library|prerelease|release|hotfix)\/[a-z0-9._-]+$"
message="Branch names in this project must adhere to this contract: $valid_branch_regex. Your commit is aborted. You should rename your branch to a valid name and try again."
if [[ ! $local_branch =~ $valid_branch_regex ]]
then
echo -e "${RED}ERROR:${NC} $message"
exit 1
fi
./gradlew spotlessCheck
RESULT=$?
exit $RESULT
apply plugin: 'com.diffplug.gradle.spotless'
spotless {
kotlin {
target '**/*.kt'
ktlint("0.36.0")
//ktlint("0.36.0").userData(['disabled_rules': 'no-wildcard-imports'])
trimTrailingWhitespace()
indentWithSpaces()
endWithNewline()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment