All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
metridoc.build.BuildHelper.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 groovy.grape.Grape
import org.codehaus.groovy.tools.GrapeMain
import groovy.xml.MarkupBuilder
/**
* Created by IntelliJ IDEA.
* User: tbarker
* Date: 8/15/11
* Time: 10:15 AM
*/
class BuildHelper {
private String _root
private static final separator = System.getProperty("file.separator")
private static final home = System.getProperty("user.home")
private static final tab = " "
private static final groovyStarterConf = "groovy.starter.conf"
AntBuilder ant = new AntBuilder()
private List dependencies = []
private Map dependencyArgs = [:]
void addDependency(Map dependency) {
dependencies.add(dependency)
}
void addDependencyArg(String name, Object value) {
dependencyArgs.put(name, value)
}
String getRoot() {
if (_root == null) {
throw new IllegalStateException("root has not been set")
}
return _root
}
void setRoot(String root) {
_root = root
}
String getMetridocProject() {
return "http://metridoc.googlecode.com/svn/trunk/core/metridoc-project/src/main/groovy"
}
URL getJobUrl() {
return new URL("${metridocProject}/jobs/fooJob.groovy")
}
URL getLogbackUrl() {
return new URL("${metridocProject}/logback.groovy")
}
URL getRunUrl() {
return new URL("${metridocProject}/run.groovy")
}
String getTarget() {
return "${root}${separator}target"
}
String getReports() {
return "${target}${separator}reports"
}
String getTargetClasses() {
return "${target}${separator}classes"
}
String getTargetTestClasses() {
return "${target}${separator}testClasses"
}
String getResources() {
return "${root}${separator}resources"
}
String getTestResources() {
return "${root}${separator}testResources"
}
String getMain() {
return "${root}${separator}/src"
}
String getTest() {
return "${root}${separator}/test"
}
String getTargetJunit() {
return "${target}${separator}junit"
}
String getJobs() {
return "${main}${separator}jobs"
}
String getMetridocLibraryDirectory() {
return "${home}${separator}.metridoc${separator}lib"
}
String getGroovy() {
"${home}/.groovy"
}
String getGrapeConfig() {
return "${groovy}/grapeConfig.xml"
}
String getEncoding() {
return "UTF-8"
}
String getMetridocLibraryConfig() {
return "load !{user.home}/.metridoc/lib/*.jar"
}
void run(String... buildSteps) {
buildSteps.each {String step ->
this."${step}"()
}
}
def createTarget() {
ant.mkdir(dir: targetClasses)
}
def createTestTarget() {
ant.mkdir(dir: targetTestClasses)
}
def clean() {
ant.delete(dir: target)
}
def copyResources() {
if (new File(resources).exists()) {
ant.copy(todir: targetClasses) {
fileset(dir: resources)
}
}
}
def copyTestResources() {
if (new File(testResources).exists()) {
ant.copy(todir: targetTestClasses) {
fileset(dir: testResources)
}
}
}
def compile() {
createTarget()
copyResources()
ant.taskdef name: "groovyc", classname: "org.codehaus.groovy.ant.Groovyc"
ant.groovyc(srcdir: main, destdir: targetClasses)
}
def testCompile() {
compile()
createTestTarget()
copyTestResources()
ant.taskdef name: "groovyc", classname: "org.codehaus.groovy.ant.Groovyc"
ant.groovyc(srcdir: test, destdir: targetTestClasses) {
classpath {
pathelement(location: targetClasses)
}
}
ant.mkdir(dir: targetJunit)
}
def test() {
testCompile()
ant.junit(haltonfailure: 'yes') {
classpath {
pathelement(location: targetClasses)
pathelement(location: targetTestClasses)
}
formatter(type: 'plain', usefile: 'false')
batchtest(todir: targetJunit) {
fileset(dir: targetTestClasses) {
include(name: '**/*Test*')
exclude(name: "**/*\$*") //exclude the compiled closures
}
}
}
}
def createFolders() {
ant.mkdir(dir: main)
ant.mkdir(dir: jobs)
ant.mkdir(dir: test)
ant.mkdir(dir: testResources)
ant.mkdir(dir: resources)
}
def createProject() {
createGrapeConfigXml()
installMetridoc()
createFolders()
createBasicJobs()
}
def reset() {
resetGrape()
resetMetridoc()
}
def resetMetridoc() {
ant.delete(dir: metridocLibraryDirectory)
ant.mkdir(dir: metridocLibraryDirectory)
}
def installMetridoc() {
configGroovy()
if (new File(grapeConfig).exists()) {
System.setProperty("grape.config", grapeConfig)
}
resetMetridoc()
dependencies.each {Map dependency ->
def group = dependency.get("group")
def module = dependency.get("module")
def version = dependency.get("version")
version = version ? version : "*"
println "Installing ${group}:${module}:${version}"
GrapeMain.main("install", group, module, version)
}
Map[] dependencyArray = dependencies.toArray()
def uris = Grape.resolve(dependencyArgs, dependencyArray)
uris.each {URI uri ->
def output
def from = new File(uri)
def to = new File("${metridocLibraryDirectory}/${from.name}")
assert to.createNewFile(): "Could not create new file"
println "copying ${from} to ${to}"
to.append(from.bytes)
}
}
def resetGrape() {
ant.delete(dir: "${groovy}/grapes")
createGrapeConfigXml()
}
def upgrade() {
reset()
createFolders()
grabAndWrite(runUrl, "${root}/run.groovy")
def logback = "${resources}/logback.groovy"
if (!(new File(logback).exists())) {
grabAndWrite(logbackUrl, logback)
}
installMetridoc()
}
def configGroovy() {
def configFilePath = System.getProperty(groovyStarterConf)
def configFile
configFile = new File(configFilePath)
if (!configFile) {
def message = "the system property groovy.starter.conf could not be found. The property gives the " +
"location of the groovy config file that indicates which libraries to load"
throw new FileNotFoundException(message)
}
if (!fileHasMetridocLibrary(configFile)) {
appendLibRefToConfig(configFile)
}
}
def createBasicJobs() {
grabAndWrite(runUrl, "${root}/run.groovy")
grabAndWrite(jobUrl, "${jobs}/fooJob.groovy")
grabAndWrite(logbackUrl, "${resources}/logback.groovy")
}
/**
* creates the groovy directory and a better grape xml file that includes maven central while excluding java.net
* due to reliability
*/
def createGrapeConfigXml() {
def grapeConfig = new File("${groovy}/grapeConfig.xml")
if (grapeConfig.exists()) return
ant.mkdir(dir: groovy)
def writer = new FileWriter(grapeConfig)
def xml = new MarkupBuilder(writer)
//local->sonatype_snapshot->central->codehaus->ibiblio
xml.ivysettings() {
def defaultResolver = "downloadGrapes"
settings(defaultResolver: defaultResolver)
resolvers() {
chain(name: defaultResolver) {
filesystem(name: "cachedGrapes") {
ivy(pattern: "${groovy}/grapes/[organisation]/[module]/ivy-[revision].xml")
artifact(pattern: "${groovy}/grapes/[organisation]/[module]/[type]s/[artifact]-[revision].[ext]")
}
ibiblio(name: "sonatype_snapshot", root: "https://oss.sonatype.org/content/repositories/snapshots", m2compatible: "true")
ibiblio(name: "central", m2compatible: "true")
ibiblio(name: "codehaus", root: "http://repository.codehaus.org/", m2compatible: "true")
ibiblio(name: "ibiblio", m2compatible: "true")
}
}
}
}
private boolean fileHasMetridocLibrary(File file) {
def result = false
file.eachLine {String line ->
if (line.contains(metridocLibraryConfig)) {
result = true
}
}
return result
}
private void appendLibRefToConfig(File file) {
def writer = new FileWriter(file, true)
writer.println()
writer.println("${tab}# load metridoc libraries")
writer.println("${tab}${metridocLibraryConfig}")
writer.close()
}
private grabAndWrite(URL from, String to) {
def stream
try {
from.openConnection()
stream = from.openStream()
def content = stream.getText(encoding)
new File(to).write(content, encoding)
} finally {
try {
stream.close()
} catch (Exception ex) {}
}
}
}