Mercurial > public > gradleplugins
annotate src/main/groovy/org/ssdt_ohio/gradle/plugins/VersionClassPlugin.groovy @ 7:5749629ceeec version-0.2.0
set version 0.2.0
author | smith@nwoca.org |
---|---|
date | Sat, 19 May 2012 14:14:14 +0100 |
parents | c32864e7d65b |
children | 9f9063d3c17c |
rev | line source |
---|---|
0 | 1 package org.ssdt_ohio.gradle.plugins |
2 | |
2
4db19f1c5ad0
create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents:
0
diff
changeset
|
3 import org.gradle.api.Plugin |
0 | 4 import org.gradle.api.Project |
2
4db19f1c5ad0
create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents:
0
diff
changeset
|
5 import org.gradle.api.plugins.GroovyPlugin |
0 | 6 |
2
4db19f1c5ad0
create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents:
0
diff
changeset
|
7 class VersionClassPlugin implements Plugin<Project> { |
0 | 8 |
9 def void apply(Project project) { | |
2
4db19f1c5ad0
create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents:
0
diff
changeset
|
10 project.getPlugins().apply(GroovyPlugin.class) |
0 | 11 def genSrc = 'generated-src/version' |
12 def generatedSrcDir = new File(project.buildDir, genSrc) | |
13 | |
14 def makeVersionClassTask = project.task('makeVersionClass') << { | |
15 | |
2
4db19f1c5ad0
create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents:
0
diff
changeset
|
16 def now = new Date().format('yyyy-MM-dd hh:mm:ss') |
4db19f1c5ad0
create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents:
0
diff
changeset
|
17 |
4db19f1c5ad0
create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents:
0
diff
changeset
|
18 def buildNumber = -1 |
4db19f1c5ad0
create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents:
0
diff
changeset
|
19 if (project.rootProject?.file('build-number.txt').exists()) { |
4db19f1c5ad0
create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents:
0
diff
changeset
|
20 def props = new Properties() |
4db19f1c5ad0
create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents:
0
diff
changeset
|
21 project.rootProject?.file('build-number.txt').withInputStream {stream -> |
4db19f1c5ad0
create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents:
0
diff
changeset
|
22 props.load(stream) |
4db19f1c5ad0
create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents:
0
diff
changeset
|
23 } |
4db19f1c5ad0
create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents:
0
diff
changeset
|
24 buildNumber = props['build.number'] |
4db19f1c5ad0
create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents:
0
diff
changeset
|
25 } |
4db19f1c5ad0
create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents:
0
diff
changeset
|
26 |
0 | 27 def targetPackage = getTargetClass(project) |
28 | |
3
1756755e4e71
improve up-to-date check so version does not cause recompile
smith@nwoca.org
parents:
2
diff
changeset
|
29 def outFile = new File(generatedSrcDir, "groovy/" + targetPackage.replaceAll('\\.', "/") + "/ProjectVersion.groovy") |
0 | 30 |
31 outFile.getParentFile().mkdirs() | |
32 logger.info("creating $targetPackage in $outFile") | |
33 def f = new FileWriter(outFile) | |
34 f.write(""" | |
35 package $targetPackage | |
36 /** | |
2
4db19f1c5ad0
create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents:
0
diff
changeset
|
37 * Generated by gradle build. $now ($buildNumber) |
4db19f1c5ad0
create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents:
0
diff
changeset
|
38 * @version ${project.version} |
0 | 39 */ |
40 public class ProjectVersion { | |
41 | |
2
4db19f1c5ad0
create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents:
0
diff
changeset
|
42 final String name = "${project.name}" |
4db19f1c5ad0
create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents:
0
diff
changeset
|
43 final String group = "${project.group}" |
4db19f1c5ad0
create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents:
0
diff
changeset
|
44 final String version = "${project.version}" |
4db19f1c5ad0
create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents:
0
diff
changeset
|
45 final String buildDate = "$now" |
4db19f1c5ad0
create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents:
0
diff
changeset
|
46 final int buildNumber = $buildNumber |
0 | 47 |
2
4db19f1c5ad0
create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents:
0
diff
changeset
|
48 String getFullVersion() { |
4db19f1c5ad0
create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents:
0
diff
changeset
|
49 "\$version \$buildDate (#\$buildNumber)" |
0 | 50 } |
51 | |
2
4db19f1c5ad0
create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents:
0
diff
changeset
|
52 String getId() { |
4db19f1c5ad0
create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents:
0
diff
changeset
|
53 "\$group:\$name:\$version" |
4db19f1c5ad0
create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents:
0
diff
changeset
|
54 } |
4db19f1c5ad0
create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents:
0
diff
changeset
|
55 |
4db19f1c5ad0
create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents:
0
diff
changeset
|
56 String toString() { |
4db19f1c5ad0
create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents:
0
diff
changeset
|
57 "\$id \$buildDate (#\$buildNumber)" |
0 | 58 } |
59 } | |
60 """) | |
61 f.close() | |
62 } | |
63 | |
64 project.sourceSets { | |
65 main { | |
66 groovy { | |
3
1756755e4e71
improve up-to-date check so version does not cause recompile
smith@nwoca.org
parents:
2
diff
changeset
|
67 srcDir project.buildDir.name + '/' + genSrc + '/groovy' |
0 | 68 } |
69 } | |
70 } | |
71 | |
3
1756755e4e71
improve up-to-date check so version does not cause recompile
smith@nwoca.org
parents:
2
diff
changeset
|
72 makeVersionClassTask.getInputs().files(project.sourceSets.main.getAllSource().findAll{ !it.name.startsWith('ProjectVersion')} |
1756755e4e71
improve up-to-date check so version does not cause recompile
smith@nwoca.org
parents:
2
diff
changeset
|
73 ) |
1756755e4e71
improve up-to-date check so version does not cause recompile
smith@nwoca.org
parents:
2
diff
changeset
|
74 makeVersionClassTask.getOutputs().file( project.buildDir.name + '/' + genSrc) |
1756755e4e71
improve up-to-date check so version does not cause recompile
smith@nwoca.org
parents:
2
diff
changeset
|
75 |
0 | 76 if (project.getBuildFile() != null && project.getBuildFile().exists()) { |
77 makeVersionClassTask.getInputs().files(project.getBuildFile()) | |
78 } | |
3
1756755e4e71
improve up-to-date check so version does not cause recompile
smith@nwoca.org
parents:
2
diff
changeset
|
79 |
0 | 80 project.getTasks().getByName('compileGroovy').dependsOn('makeVersionClass') |
2
4db19f1c5ad0
create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents:
0
diff
changeset
|
81 |
6 | 82 // project.getTasks().getByName('jar').manifest.attributes( |
83 // [ | |
84 // "Specification-Title": project.name, | |
85 // "Specification-Version": project.version, | |
86 // "Specification-Vendor": project.group, | |
87 // "Implementation-Title": project.name, | |
88 // "Implementation-Version": project.version, | |
89 // "Implementation-Vendor": project.group, | |
90 // "Built-By": System.properties['user.name'], | |
91 // "Build-Jdk": System.properties['java.version'], | |
92 // ] | |
93 // ) | |
2
4db19f1c5ad0
create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents:
0
diff
changeset
|
94 |
0 | 95 } |
96 | |
97 | |
2
4db19f1c5ad0
create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents:
0
diff
changeset
|
98 |
0 | 99 private getTargetClass(project) { |
100 def source = project.sourceSets.main.groovy.getSrcDirTrees() | |
101 def files = project.sourceSets.main.groovy as File[] | |
102 def targetDir = files.first().parentFile.toString() | |
103 | |
104 def targetPackage = targetDir | |
105 source.each { | |
106 targetPackage -= it.getDir().getPath().toString() | |
107 } | |
2
4db19f1c5ad0
create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents:
0
diff
changeset
|
108 targetPackage = targetPackage.replaceAll("\\\\", '.').replaceAll('/', '.') |
0 | 109 targetPackage = targetPackage.startsWith(".") ? targetPackage - "." : targetPackage |
110 | |
111 return targetPackage | |
112 | |
113 } | |
2
4db19f1c5ad0
create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents:
0
diff
changeset
|
114 |
0 | 115 } |