Created
February 1, 2021 02:32
-
-
Save iurysza/d12837fe6cc59fb364844987a5cff10d to your computer and use it in GitHub Desktop.
Install git hooks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 team-props/git-hooks to the .git folder.' | |
from("${rootDir}/team-props/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 team-props/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.') } | |
} | |
afterEvaluate { | |
// We install the hook at the first occasion | |
tasks['clean'].dependsOn installGitHooks | |
tasks['assemble'].dependsOn installGitHooks | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment