All Downloads are FREE. Search and download functionalities are using the official Maven repository.

metridoc.build.BuildHelper.groovy Maven / Gradle / Ivy

There is a newer version: 0.30
Show newest version
/*
 * 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 {

    static final home = System.getProperty("user.home")
    static final fileSeparator = System.getProperty("file.separator")
    static final currentDir = System.getProperty("user.dir")
    static final targetDir = "${currentDir}${fileSeparator}target"
    static final reportsDir = "${currentDir}${fileSeparator}reports"
    static final resourcesDir = "${currentDir}${fileSeparator}resources"
    static final testResourcesDir = "${currentDir}${fileSeparator}testResources"
    static final srcDir = "${currentDir}${fileSeparator}src"
    static final testSrcDir = "${currentDir}${fileSeparator}test"
    static final classesDir = "${targetDir}${fileSeparator}classes"
    static final testClassesDir = "${targetDir}${fileSeparator}testClasses"
    static final junitDir = "${targetDir}${fileSeparator}junit"
    static final mavenMetaUrl = "http://repo1.maven.org/maven2/com/googlecode/metridoc/metridoc-all/maven-metadata.xml"

    static final tab = "    "
    AntBuilder ant = new AntBuilder()

    def static run(Script self, String[] args) {
        def helper = new BuildHelper()

        args.each {command ->
            helper.runCommand(command)
        }
    }

    void getLibraries() {
        def console = new Console()
        def destination = console.readLine("Where would you like to store the metridoc libraries? ")
        def file = new File(destination)
        assert file.exists() : "file ${destination} does not exist"
        assert file.isDirectory() : "file ${destination} is not a directory"

        def uris = Grape.resolve([:], [group:"com.googlecode.metridoc", module:"metridoc-all", version:"0.22"])
        def ant = new AntBuilder()

        uris.each {
            println "copying ${it} to ${file}"
            def fromFile = new File(it)
            def fromFileName = fromFile.name
            ant.copy(file: fromFile, toFile:new File("${destination}${fileSeparator}${fromFileName}"))
        }
    }

    void runCommand(command) {
        this."${command}"()
    }

    String getEncoding() {
        return "UTF-8"
    }

    def createTarget() {
        ant.mkdir(dir: classesDir)
    }

    def createTestTarget() {
        ant.mkdir(dir: testClassesDir)
    }

    def clean() {
        ant.delete(dir: targetDir)
    }

    def copyResources() {
        if (new File(resourcesDir).exists()) {
            ant.copy(todir: classesDir) {
                fileset(dir: resourcesDir)
            }
        }
    }

    def copyTestResources() {
        if (new File(testResourcesDir).exists()) {
            ant.copy(todir: testClassesDir) {
                fileset(dir: testResourcesDir)
            }
        }
    }

    def compile() {
        createTarget()
        copyResources()
        ant.taskdef name: "groovyc", classname: "org.codehaus.groovy.ant.Groovyc"
        ant.groovyc(srcdir: srcDir, destdir: classesDir)
    }

    def testCompile() {
        compile()
        createTestTarget()
        copyTestResources()
        ant.taskdef name: "groovyc", classname: "org.codehaus.groovy.ant.Groovyc"
        ant.groovyc(srcdir: testSrcDir, destdir: testClassesDir) {
            classpath {
                pathelement(location: classesDir)
            }
        }
        ant.mkdir(dir: junitDir)
    }

    def test() {
        testCompile()
        ant.junit(haltonfailure: 'yes') {
            classpath {
                pathelement(location: classesDir)
                pathelement(location: testClassesDir)
            }
            formatter(type: 'plain', usefile: 'false')
            batchtest(todir: junitDir) {
                fileset(dir: testClassesDir) {
                    include(name: '**/*Test*')
                    exclude(name: "**/*\$*") //exclude the compiled closures
                }
            }
        }
    }

    static String metridocVersion() {
        def definedVersion = System.getProperty("metridoc.version")
        if (definedVersion) {
            return definedVersion
        }

        def url = new URL(mavenMetaUrl)
        def stream = url.openStream()
        def xml = new XmlParser().parse(stream)

        return xml.versioning.latest.text()
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy