metridoc.build.RunHelper.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.
*/
package metridoc.build
import metridoc.utils.Assert
/**
* Created by IntelliJ IDEA.
* User: tbarker
* Date: 9/26/11
* Time: 2:18 PM
*/
class RunHelper {
static List getJobs(File baseDirectory) {
Assert.isTrue(baseDirectory.isDirectory(), "the file ${baseDirectory} is not a directory")
def result = []
doGetJobs(result, baseDirectory, [])
return result
}
private static void doGetJobs(List jobs, File fileOrDirectory, List position) {
def fileOrDirectoryName = fileOrDirectory.name
if (fileOrDirectory.isDirectory()) {
def files = fileOrDirectory.listFiles()
files.each {
def clonePosition = []
clonePosition.addAll(position)
if (it.isDirectory()) {
clonePosition.add(it.name)
}
doGetJobs(jobs, it, clonePosition)
}
} else {
if (isScript(fileOrDirectory)) {
def longName = getLongName(fileOrDirectory, position)
jobs.add(longName)
}
}
}
private static boolean isScript(File file) {
if (!file.name.endsWith(".groovy")) {
return false
}
GroovyClassLoader loader = new GroovyClassLoader()
Class groovyClass = loader.parseClass(file)
try {
groovyClass.asSubclass(Script)
return true
} catch (ClassCastException ex) {
return false
}
}
private static String getLongName(File file, List position) {
String name = file.name
def nameWithNoExtension = getFileNameNoExtension(file)
def result = nameWithNoExtension
if (position) {
result = new String()
position.each {
result += it
result += "."
}
result += nameWithNoExtension
}
return result
}
private static String getFileNameNoExtension(File file) {
String name = file.name
def m = name =~ /^(\w+)\.groovy$/
m.find()
return m.group(1)
}
static List getJobs(String baseDirectory) {
getJobs(new File(baseDirectory))
}
}