e.metridoc.metridoc-project.0.21.source-code.run.groovy Maven / Gradle / Ivy
/*
* Copyright 2010 Trustees of the University of Pennsylvania Licensed under the
* Educational Community License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may
* obtain a copy of the License at
*
* http://www.osedu.org/licenses/ECL-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an "AS IS"
* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
import metridoc.dsl.JobBuilder
import metridoc.build.RunHelper
def packagePrefix = ""
def String scriptDir = new File(getClass().protectionDomain.codeSource.location.path).parent
assert args: "there must be at least one argument to indicate what jobs to run"
checkClassPath(scriptDir)
JobBuilder.isJob(this)
args.each {String jobName ->
if("help" == jobName) {
doHelp(scriptDir)
} else {
Script script = getScript(scriptDir, packagePrefix, jobName)
runScript script
}
}
def doHelp(String scriptDir) {
println "Usage: run one of the following jobs with groovy run "
def jobs = RunHelper.getJobs(scriptDir + "/src")
jobs.each {String jobName ->
println "job: ${jobName}"
}
}
def getJobs(String scriptDir) {
def jobDir = new File("${scriptDir}/src/jobs")
return getJobsFromDir(jobDir)
}
def getJobsFromDir(File directory) {
def result = []
if(directory.isDirectory()) {
directory.listFiles().each {File file ->
if(file.isDirectory()) {
result.addAll(getJobsFromDir(file))
}
else {
def fileName = file.name
if(fileName.endsWith(".groovy")) {
m = (fileName =~ /([^\.]+)\.groovy/)
m.matches()
result.add(m.group(1))
}
}
}
}
return result
}
/**
*
* searches for class with the name / or /jobs/
*
* @param packagePrefix
* @param jobName
* @return
*/
Script getScript(String scriptDir, String packagePrefix, String jobName) {
def srcDir = "${scriptDir}/src"
jobName = jobName.replaceAll("\\.", "/")
if (packagePrefix) {
def folders = packagePrefix.replaceAll("\\.", "/")
jobName = jobName.replaceAll("\\.", "/")
def file = new File("${srcDir}/${folders}/${jobName}.groovy")
if (file.exists()) {
return compileScript(file)
}
file = new File("${srcDir}/${folders}/jobs/${jobName}.groovy")
if (file.exists()) {
return compileScript(file)
}
}
def file = new File("${srcDir}/${jobName}.groovy")
if (file.exists()) {
return compileScript(file)
}
file = new File("${srcDir}/jobs/${jobName}.groovy")
if (file.exists()) {
return compileScript(file)
}
throw new FileNotFoundException("could not find job ${jobName}")
}
Script compileScript(File file) {
def classLoader = Thread.currentThread().contextClassLoader
def shell = new GroovyShell(classLoader, binding)
return shell.parse(new FileInputStream(file), file.name)
}
def checkClassPath(String scriptDir) {
addToClassPath("${scriptDir}/resources")
addToClassPath("${scriptDir}/src")
}
def addToClassPath(String path) {
getClass().classLoader.rootLoader.addURL(toUrl(path))
}
def toUrl(String path) {
new File(path).toURI().toURL()
}