Skip to content

Instantly share code, notes, and snippets.

@renatoathaydes
Last active August 29, 2015 14:01
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save renatoathaydes/3bc0bb353f4a875a1739 to your computer and use it in GitHub Desktop.
Update all Saas projects
//#!/usr/bin/env groovy
// Change this to your projects home
home = "D:/Programming/Projects"
// Change this to where your Maven installation is located
mvn = "C:/Program Files (x86)/Maven/apache-maven-3.0.4"
if ('help' in args || 'h' in args) {
println """
Checks out the default branch of each project, then pulls from origin.
Usage:
updateAll.groovy [option] [project]
option:
h | help = Print this help text
v | verbose = Print all output
i | include = Provide a list of projects to include (defaults to all)
x | exclude = Provide a list of projects to exclude (defaults to none)
b | build = Build projects after updating
d | deploy = Deploy projects which support deployment to Vagrant VM
Example:
# update all except loadui and loadui-pro
updateAll.groovy exclude loadui loadui-pro
"""
return
}
userHome = System.getProperty( 'user.home' )
if (!userHome) throw new RuntimeException('You need a user.home to run this script')
if (!new File(home).exists()) throw new RuntimeException('You must set the home variable to the directory where your projects are located')
if (!new File(mvn).exists()) throw new RuntimeException('You must set the mvn variable to the directory where you have installed Maven')
if (!System.getenv('JAVA7')) throw new RuntimeException('Please set an environment variable called JAVA7 with the location of your Java 7 installation')
if (!System.getenv('JAVA8')) throw new RuntimeException('Please set an environment variable called JAVA8 with the location of your Java 8 installation')
def scripts = userHome + '/scripts'
allOptions = ['h', 'help', 'v', 'verbose', 'i', 'include', 'x', 'exclude', 'b', 'build', 'd', 'deploy']
defaultJava = 'JAVA7'
if (hasInclusions() && hasExclusions()) {
println "You cannot have both inclusions and exclusions"
return
}
branchByProject = [
// Load Engine
'loadui': [branch:'next', java: defaultJava], 'loadui-pro': [branch:'next', java: defaultJava],
// Saas Load
'JCeleryWorker': [branch: 'master', java: defaultJava], 'loadui-saas': [branch: 'master', java: defaultJava],
// Load-RS-API
'saas-sso': [branch: 'master', java: defaultJava],
'Dropwizard-EnvVar-Interpolation': [branch: 'master', java: defaultJava],
'load-rs-api': [branch: 'master', java: 'JAVA8']
]
for (arg in args) {
if (!(arg in allOptions + branchByProject.keySet())) {
println "Unrecognised option or project: $arg"
return
}
}
deployScriptByProject = [
'saas-load': 'deploy-saas.sh',
'load-rs-api': 'deploy-api.sh'
]
successfulJobs = []
for ( proj in branchByProject.keySet() ) {
if (isExcluded( proj ) || !isIncluded( proj ))
continue
def dir = "$home/$proj"
def info = branchByProject[proj]
def error = runAndCheckForError "git checkout ${info.branch}", dir
if (!error)
error = runAndCheckForError "git pull", dir
if (!error && shouldBuild())
error = runAndCheckForError( [ "mvn.bat", "-f", "$dir/pom.xml", "clean", "install" ],
mvn + '/bin', info.java == defaultJava ? null : ['JAVA_HOME=' + System.getenv(info.java)] )
if (!error && shouldDeploy())
error = runAndCheckForError deployScriptByProject[proj], scripts
if (error) return "ERROR!"
successfulJobs << proj
}
println "\n\nSuccessfully updated " + (shouldBuild() ? "and built " : "") +
"the following projects:\n" + successfulJobs.join('\n')
boolean runAndCheckForError( cmd, dir, envp = null ) {
if (!cmd) return
envp = envp ? allEnvVariablesPlus(envp) : null
println "--------- Running $dir/$cmd"
def proc = cmd.execute(envp?: null, dir as File)
if (isVerbose())
proc.consumeProcessOutput( System.out, System.out )
else
proc.consumeProcessOutput()
proc.waitFor()
if (proc.exitValue() != 0) {
println "Command $dir/$cmd returned ${proc.exitValue()}, stopping!!"
return true
}
return false
}
String[] allEnvVariablesPlus( envp ) {
System.getenv().collect { k, v -> "$k=$v" } + envp + ["M2_HOME=$mvn"]
}
boolean shouldBuild() { 'b' in args || 'build' in args }
boolean shouldDeploy() { 'd' in args || 'deploy' in args }
boolean isVerbose() { 'v' in args || 'verbose' in args }
boolean hasInclusions() { 'i' in args || 'include' in args }
boolean hasExclusions() { 'x' in args || 'exclude' in args }
boolean isIncluded( proj ) {
if (hasInclusions()) {
return proj in optionsAfter( 'i', 'include' )
} else {
return true
}
}
boolean isExcluded( proj ) {
if (hasExclusions()) {
return proj in optionsAfter('x', 'exclude')
} else {
return false
}
}
def optionsAfter( option, longOption ) {
def index = args.findIndexOf { it == option }
if (index < 0) index = args.findIndexOf { it == longOption }
if (index >= args.size() - 1) return []
args[(index+1)..-1].takeWhile { !(it in allOptions) }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment