view src/main/groovy/org/ssdt_ohio/gradle/plugins/VersionClassPlugin.groovy @ 1:619ed31de9ac

fix mode for gradlew
author smith@nwoca.org
date Thu, 17 May 2012 23:33:24 -0400
parents c51874d4a5a7
children 4db19f1c5ad0
line wrap: on
line source
package org.ssdt_ohio.gradle.plugins

import org.gradle.api.Project
import org.gradle.api.Plugin
import org.gradle.api.plugins.JavaPlugin
import java.util.Date
import org.gradle.api.plugins.GroovyPlugin;

class VersionClassPlugin implements Plugin<Project>  {

    def void apply(Project project) {
        project.getPlugins().apply( GroovyPlugin.class )
        def genSrc = 'generated-src/version'
        def generatedSrcDir = new File(project.buildDir, genSrc)

        def makeVersionClassTask = project.task('makeVersionClass') << {

            def now = new Date()
            def targetPackage = getTargetClass(project)

            def outFile = new File(generatedSrcDir,targetPackage.replaceAll('\\.',"/") + "/ProjectVersion.groovy")

            outFile.getParentFile().mkdirs()
            logger.info("creating $targetPackage in $outFile")
            def f = new FileWriter(outFile)
            f.write("""
package $targetPackage
/**
 * Generated by gradle build.
 */
public class ProjectVersion {

    public static final String NAME = "${project.name}"
    public static final String GROUP = "${project.group}"
    public static final String VERSION = "${project.version}"
    public static final String DATE = "$now"

    public static String getDetailedVersion() {
        "\$GROUP:\$NAME:\$VERSION \$DATE"
    }

    public static String getID() {
        "\$GROUP:\$NAME:\$VERSION"
    }
}
""")
            f.close()
        }

        project.sourceSets {
            main {
                groovy {
                    srcDir project.buildDir.name+'/'+genSrc
                }
            }
        }

        makeVersionClassTask.getInputs().files(project.sourceSets.main.getAllSource() )
        makeVersionClassTask.getOutputs().files(generatedSrcDir)
        if (project.getBuildFile() != null && project.getBuildFile().exists()) {
            makeVersionClassTask.getInputs().files(project.getBuildFile())
        }
//        project.getTasks().getByName('compileGroovy').dependsOn('compileVersionGroovy')
        project.getTasks().getByName('compileGroovy').dependsOn('makeVersionClass')
//        project.getTasks().getByName('jar') {
//            from project.sourceSets.version.output
//        }
    }


    private getTargetClass(project) {
        def source = project.sourceSets.main.groovy.getSrcDirTrees()
        def files = project.sourceSets.main.groovy as File[]
        def targetDir = files.first().parentFile.toString()

        def targetPackage = targetDir
        source.each {
            targetPackage -= it.getDir().getPath().toString()
        }
        targetPackage = targetPackage.replaceAll("\\\\",'.').replaceAll('/','.')
        targetPackage = targetPackage.startsWith(".") ? targetPackage - "." : targetPackage

        return targetPackage

    }
}