Skip to content

Instantly share code, notes, and snippets.

@KillerGoldFisch
Last active October 20, 2016 04:35
Show Gist options
  • Save KillerGoldFisch/fbfecc467dc51c3d04a5 to your computer and use it in GitHub Desktop.
Save KillerGoldFisch/fbfecc467dc51c3d04a5 to your computer and use it in GitHub Desktop.
Gradle script to inject version/git informations
///////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////// TEMPLATE /////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////
// Get Git informations
def branch = "git rev-parse --abbrev-ref HEAD".execute().text.trim()
def revision = "git rev-parse --short HEAD".execute().text.trim()
def origin = "git config --get remote.origin.url".execute().text.trim()
def timestamp = new Date().format("yyyy-MM-dd HH:mm:ss")
// Inject Variablet into templates
def binding = [
"timestamp": timestamp,
"projectDir": projectDir,
"version": version,
"user": System.getProperty("user.name"),
"machine": InetAddress.getLocalHost().getHostName(),
"branch": branch,
"revision": revision,
"origin": origin,
]
def templengine = new groovy.text.SimpleTemplateEngine()
projectDir.eachDirRecurse() { dir ->
dir.eachFileMatch(~/.*.templ/) { file ->
def targetfile = new File(file.getParent() + "/" + file.name.split("\\.")[0..-2].join("."))
println file.getPath()
println targetfile
targetfile.write(templengine.createTemplate(file).make(binding).toString())
}
}
///////////////////////////////////////////////////////////////////////////////////////////
/*
public class BuildInfo {
public static final String TIMESTAMP = "${timestamp}";
public static final String PROJECTDIR = "${projectDir.toString().replace("\\", "\\\\")}";
public static final String VERSION = "${version}";
public static final String USER = "${user}";
public static final String MACHINE = "${machine}";
public static final String GIT_BRANCH = "${branch}";
public static final String GIT_ORIGIN = "${origin}";
public static final String GIT_REVISION = "${revision}";
public static String getInfoText() {
return
"TIMESTAMP: " + TIMESTAMP + "\\n" +
"PROJECTDIR: " + PROJECTDIR + "\\n" +
"VERSION: " + VERSION + "\\n" +
"USER: " + USER + "\\n" +
"MACHINE: " + MACHINE + "\\n" +
"GIT_BRANCH: " + GIT_BRANCH + "\\n" +
"GIT_ORIGIN: " + GIT_ORIGIN + "\\n" +
"GIT_REVISION: " + GIT_REVISION;
}
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment