Mercurial > public > gradleplugins
annotate src/main/groovy/org/ssdt_ohio/gradle/plugins/VersionClassPlugin.groovy @ 12:0a2819478262
update gradle wrapper
author | smith@nwoca.org |
---|---|
date | Wed, 14 Aug 2013 19:28:51 +0100 |
parents | 9f9063d3c17c |
children | 4ff2030d2074 |
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 | |
9 | 29 if (!targetPackage) { |
30 return | |
31 } | |
32 | |
3
1756755e4e71
improve up-to-date check so version does not cause recompile
smith@nwoca.org
parents:
2
diff
changeset
|
33 def outFile = new File(generatedSrcDir, "groovy/" + targetPackage.replaceAll('\\.', "/") + "/ProjectVersion.groovy") |
0 | 34 |
35 outFile.getParentFile().mkdirs() | |
36 logger.info("creating $targetPackage in $outFile") | |
37 def f = new FileWriter(outFile) | |
38 f.write(""" | |
39 package $targetPackage | |
40 /** | |
2
4db19f1c5ad0
create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents:
0
diff
changeset
|
41 * 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
|
42 * @version ${project.version} |
0 | 43 */ |
44 public class ProjectVersion { | |
45 | |
2
4db19f1c5ad0
create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents:
0
diff
changeset
|
46 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
|
47 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
|
48 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
|
49 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
|
50 final int buildNumber = $buildNumber |
0 | 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 getFullVersion() { |
4db19f1c5ad0
create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents:
0
diff
changeset
|
53 "\$version \$buildDate (#\$buildNumber)" |
0 | 54 } |
55 | |
2
4db19f1c5ad0
create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents:
0
diff
changeset
|
56 String getId() { |
4db19f1c5ad0
create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents:
0
diff
changeset
|
57 "\$group:\$name:\$version" |
4db19f1c5ad0
create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents:
0
diff
changeset
|
58 } |
4db19f1c5ad0
create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents:
0
diff
changeset
|
59 |
4db19f1c5ad0
create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents:
0
diff
changeset
|
60 String toString() { |
4db19f1c5ad0
create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents:
0
diff
changeset
|
61 "\$id \$buildDate (#\$buildNumber)" |
0 | 62 } |
63 } | |
64 """) | |
65 f.close() | |
66 } | |
67 | |
68 project.sourceSets { | |
69 main { | |
70 groovy { | |
3
1756755e4e71
improve up-to-date check so version does not cause recompile
smith@nwoca.org
parents:
2
diff
changeset
|
71 srcDir project.buildDir.name + '/' + genSrc + '/groovy' |
0 | 72 } |
73 } | |
74 } | |
75 | |
3
1756755e4e71
improve up-to-date check so version does not cause recompile
smith@nwoca.org
parents:
2
diff
changeset
|
76 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
|
77 ) |
1756755e4e71
improve up-to-date check so version does not cause recompile
smith@nwoca.org
parents:
2
diff
changeset
|
78 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
|
79 |
0 | 80 if (project.getBuildFile() != null && project.getBuildFile().exists()) { |
81 makeVersionClassTask.getInputs().files(project.getBuildFile()) | |
82 } | |
3
1756755e4e71
improve up-to-date check so version does not cause recompile
smith@nwoca.org
parents:
2
diff
changeset
|
83 |
0 | 84 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
|
85 |
4db19f1c5ad0
create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents:
0
diff
changeset
|
86 |
0 | 87 } |
88 | |
89 | |
2
4db19f1c5ad0
create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents:
0
diff
changeset
|
90 |
0 | 91 private getTargetClass(project) { |
92 def source = project.sourceSets.main.groovy.getSrcDirTrees() | |
9 | 93 |
0 | 94 def files = project.sourceSets.main.groovy as File[] |
9 | 95 if (!files ) { |
96 return null | |
97 } | |
98 | |
0 | 99 def targetDir = files.first().parentFile.toString() |
100 | |
101 def targetPackage = targetDir | |
102 source.each { | |
103 targetPackage -= it.getDir().getPath().toString() | |
104 } | |
2
4db19f1c5ad0
create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents:
0
diff
changeset
|
105 targetPackage = targetPackage.replaceAll("\\\\", '.').replaceAll('/', '.') |
0 | 106 targetPackage = targetPackage.startsWith(".") ? targetPackage - "." : targetPackage |
107 | |
108 return targetPackage | |
109 | |
110 } | |
2
4db19f1c5ad0
create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents:
0
diff
changeset
|
111 |
0 | 112 } |