annotate src/main/groovy/org/ssdt_ohio/gradle/plugins/VersionClassPlugin.groovy @ 0:c51874d4a5a7

initial ssdt plugin
author smith@nwoca.org
date Fri, 18 May 2012 04:32:16 +0100
parents
children 4db19f1c5ad0
rev   line source
0
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
1 package org.ssdt_ohio.gradle.plugins
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
2
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
3 import org.gradle.api.Project
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
4 import org.gradle.api.Plugin
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
5 import org.gradle.api.plugins.JavaPlugin
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
6 import java.util.Date
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
7 import org.gradle.api.plugins.GroovyPlugin;
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
8
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
9 class VersionClassPlugin implements Plugin<Project> {
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
10
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
11 def void apply(Project project) {
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
12 project.getPlugins().apply( GroovyPlugin.class )
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
13 def genSrc = 'generated-src/version'
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
14 def generatedSrcDir = new File(project.buildDir, genSrc)
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
15
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
16 def makeVersionClassTask = project.task('makeVersionClass') << {
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
17
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
18 def now = new Date()
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
19 def targetPackage = getTargetClass(project)
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
20
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
21 def outFile = new File(generatedSrcDir,targetPackage.replaceAll('\\.',"/") + "/ProjectVersion.groovy")
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
22
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
23 outFile.getParentFile().mkdirs()
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
24 logger.info("creating $targetPackage in $outFile")
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
25 def f = new FileWriter(outFile)
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
26 f.write("""
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
27 package $targetPackage
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
28 /**
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
29 * Generated by gradle build.
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
30 */
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
31 public class ProjectVersion {
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
32
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
33 public static final String NAME = "${project.name}"
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
34 public static final String GROUP = "${project.group}"
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
35 public static final String VERSION = "${project.version}"
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
36 public static final String DATE = "$now"
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
37
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
38 public static String getDetailedVersion() {
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
39 "\$GROUP:\$NAME:\$VERSION \$DATE"
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
40 }
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
41
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
42 public static String getID() {
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
43 "\$GROUP:\$NAME:\$VERSION"
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
44 }
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
45 }
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
46 """)
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
47 f.close()
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
48 }
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
49
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
50 project.sourceSets {
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
51 main {
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
52 groovy {
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
53 srcDir project.buildDir.name+'/'+genSrc
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
54 }
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
55 }
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
56 }
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
57
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
58 makeVersionClassTask.getInputs().files(project.sourceSets.main.getAllSource() )
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
59 makeVersionClassTask.getOutputs().files(generatedSrcDir)
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
60 if (project.getBuildFile() != null && project.getBuildFile().exists()) {
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
61 makeVersionClassTask.getInputs().files(project.getBuildFile())
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
62 }
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
63 // project.getTasks().getByName('compileGroovy').dependsOn('compileVersionGroovy')
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
64 project.getTasks().getByName('compileGroovy').dependsOn('makeVersionClass')
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
65 // project.getTasks().getByName('jar') {
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
66 // from project.sourceSets.version.output
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
67 // }
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
68 }
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
69
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
70
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
71 private getTargetClass(project) {
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
72 def source = project.sourceSets.main.groovy.getSrcDirTrees()
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
73 def files = project.sourceSets.main.groovy as File[]
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
74 def targetDir = files.first().parentFile.toString()
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
75
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
76 def targetPackage = targetDir
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
77 source.each {
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
78 targetPackage -= it.getDir().getPath().toString()
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
79 }
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
80 targetPackage = targetPackage.replaceAll("\\\\",'.').replaceAll('/','.')
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
81 targetPackage = targetPackage.startsWith(".") ? targetPackage - "." : targetPackage
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
82
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
83 return targetPackage
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
84
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
85 }
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
86 }