Skip to content

Instantly share code, notes, and snippets.

@aadrian
Last active June 29, 2017 15:46
Show Gist options
  • Save aadrian/cd9481ff66d96c3e2aec4212040184ce to your computer and use it in GitHub Desktop.
Save aadrian/cd9481ff66d96c3e2aec4212040184ce to your computer and use it in GitHub Desktop.
Gradle getWebAppVersion based on time and GIT hash
@aadrian
Copy link
Author

aadrian commented Jun 29, 2017

plugins {
    id 'org.ajoberstar.grgit' version '1.7.2'
}
// ...
version = getWebAppVersion(project)
// ...
def getWebAppVersion(project){
    print "----> Generating 'getWebAppVersion'..."
    def now = new Date()
    def result = now.format("yyyy.MM.dd")
    def history = grgit.log {
        maxCommits = 1
    }
    result +="-"+history[0].id?.substring(0,7)
    if(project.hasProperty("env")&&project.env=='prod') {
        print "for Production env: "
    } else {
        print "for Development env: "
        result+="-SNAPSHOT"
    }
    println "v"+result
    return result
}
// ...
def getUncommitted(project) {
    def result = [:]
    def status = grgit.status()
    if(!status.staged.added?.isEmpty()) {result.put("staged.added",status.staged.added)}
    if(!status.unstaged.added?.isEmpty()) {result.put("unstaged.added",status.unstaged.added)}

    if(!status.staged.modified?.isEmpty()) {result.put("staged.modified",status.staged.modified)}
    if(!status.unstaged.modified?.isEmpty()) {result.put("unstaged.modified",status.unstaged.modified)}

    if(!status.staged.removed?.isEmpty()) {result.put("staged.removed",status.staged.removed)}
    if(!status.unstaged.removed?.isEmpty()) {result.put("unstaged.removed",status.unstaged.removed)}

    if(!status.conflicts?.isEmpty()) {result.put("conflicts",status.conflicts)}
    return result;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment