Mercurial > public > gradleplugins
annotate src/main/groovy/org/ssdt_ohio/gradle/plugins/VersionClassPlugin.groovy @ 20:e8e14d5d6be2
improve formatting. exclude @transient and version
author | smith@nwoca.org |
---|---|
date | Fri, 08 Jan 2016 20:16:44 +0000 |
parents | 4ff2030d2074 |
children | cfbdbeba877f |
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 |
18
4ff2030d2074
USASR-1921: Update VersionClassPlugin to allow buildscript to specify package in which to generate ProjectVersion.groovy file
Christopher Springer <springer@nwoca.org>
parents:
9
diff
changeset
|
5 import org.gradle.api.plugins.ExtraPropertiesExtension.UnknownPropertyException |
2
4db19f1c5ad0
create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents:
0
diff
changeset
|
6 import org.gradle.api.plugins.GroovyPlugin |
0 | 7 |
2
4db19f1c5ad0
create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents:
0
diff
changeset
|
8 class VersionClassPlugin implements Plugin<Project> { |
0 | 9 |
10 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
|
11 project.getPlugins().apply(GroovyPlugin.class) |
0 | 12 def genSrc = 'generated-src/version' |
13 def generatedSrcDir = new File(project.buildDir, genSrc) | |
14 | |
15 def makeVersionClassTask = project.task('makeVersionClass') << { | |
16 | |
2
4db19f1c5ad0
create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents:
0
diff
changeset
|
17 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
|
18 |
4db19f1c5ad0
create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents:
0
diff
changeset
|
19 def buildNumber = -1 |
4db19f1c5ad0
create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents:
0
diff
changeset
|
20 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
|
21 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
|
22 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
|
23 props.load(stream) |
4db19f1c5ad0
create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents:
0
diff
changeset
|
24 } |
4db19f1c5ad0
create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents:
0
diff
changeset
|
25 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
|
26 } |
4db19f1c5ad0
create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents:
0
diff
changeset
|
27 |
0 | 28 def targetPackage = getTargetClass(project) |
29 | |
9 | 30 if (!targetPackage) { |
31 return | |
32 } | |
33 | |
3
1756755e4e71
improve up-to-date check so version does not cause recompile
smith@nwoca.org
parents:
2
diff
changeset
|
34 def outFile = new File(generatedSrcDir, "groovy/" + targetPackage.replaceAll('\\.', "/") + "/ProjectVersion.groovy") |
0 | 35 |
36 outFile.getParentFile().mkdirs() | |
37 logger.info("creating $targetPackage in $outFile") | |
38 def f = new FileWriter(outFile) | |
39 f.write(""" | |
40 package $targetPackage | |
41 /** | |
2
4db19f1c5ad0
create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents:
0
diff
changeset
|
42 * 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
|
43 * @version ${project.version} |
0 | 44 */ |
45 public class ProjectVersion { | |
46 | |
2
4db19f1c5ad0
create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents:
0
diff
changeset
|
47 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
|
48 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
|
49 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
|
50 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
|
51 final int buildNumber = $buildNumber |
0 | 52 |
2
4db19f1c5ad0
create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents:
0
diff
changeset
|
53 String getFullVersion() { |
4db19f1c5ad0
create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents:
0
diff
changeset
|
54 "\$version \$buildDate (#\$buildNumber)" |
0 | 55 } |
56 | |
2
4db19f1c5ad0
create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents:
0
diff
changeset
|
57 String getId() { |
4db19f1c5ad0
create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents:
0
diff
changeset
|
58 "\$group:\$name:\$version" |
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 |
4db19f1c5ad0
create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents:
0
diff
changeset
|
61 String toString() { |
4db19f1c5ad0
create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents:
0
diff
changeset
|
62 "\$id \$buildDate (#\$buildNumber)" |
0 | 63 } |
64 } | |
65 """) | |
66 f.close() | |
67 } | |
68 | |
69 project.sourceSets { | |
70 main { | |
71 groovy { | |
3
1756755e4e71
improve up-to-date check so version does not cause recompile
smith@nwoca.org
parents:
2
diff
changeset
|
72 srcDir project.buildDir.name + '/' + genSrc + '/groovy' |
0 | 73 } |
74 } | |
75 } | |
76 | |
3
1756755e4e71
improve up-to-date check so version does not cause recompile
smith@nwoca.org
parents:
2
diff
changeset
|
77 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
|
78 ) |
1756755e4e71
improve up-to-date check so version does not cause recompile
smith@nwoca.org
parents:
2
diff
changeset
|
79 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
|
80 |
0 | 81 if (project.getBuildFile() != null && project.getBuildFile().exists()) { |
82 makeVersionClassTask.getInputs().files(project.getBuildFile()) | |
83 } | |
3
1756755e4e71
improve up-to-date check so version does not cause recompile
smith@nwoca.org
parents:
2
diff
changeset
|
84 |
0 | 85 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
|
86 |
4db19f1c5ad0
create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents:
0
diff
changeset
|
87 |
0 | 88 } |
89 | |
90 | |
2
4db19f1c5ad0
create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents:
0
diff
changeset
|
91 |
18
4ff2030d2074
USASR-1921: Update VersionClassPlugin to allow buildscript to specify package in which to generate ProjectVersion.groovy file
Christopher Springer <springer@nwoca.org>
parents:
9
diff
changeset
|
92 private getTargetClass(Project project) { |
4ff2030d2074
USASR-1921: Update VersionClassPlugin to allow buildscript to specify package in which to generate ProjectVersion.groovy file
Christopher Springer <springer@nwoca.org>
parents:
9
diff
changeset
|
93 try { |
4ff2030d2074
USASR-1921: Update VersionClassPlugin to allow buildscript to specify package in which to generate ProjectVersion.groovy file
Christopher Springer <springer@nwoca.org>
parents:
9
diff
changeset
|
94 return project.extensions.extraProperties.get('versionPackage') |
4ff2030d2074
USASR-1921: Update VersionClassPlugin to allow buildscript to specify package in which to generate ProjectVersion.groovy file
Christopher Springer <springer@nwoca.org>
parents:
9
diff
changeset
|
95 } catch (UnknownPropertyException ex) {} |
4ff2030d2074
USASR-1921: Update VersionClassPlugin to allow buildscript to specify package in which to generate ProjectVersion.groovy file
Christopher Springer <springer@nwoca.org>
parents:
9
diff
changeset
|
96 |
0 | 97 def source = project.sourceSets.main.groovy.getSrcDirTrees() |
9 | 98 |
0 | 99 def files = project.sourceSets.main.groovy as File[] |
9 | 100 if (!files ) { |
101 return null | |
102 } | |
103 | |
0 | 104 def targetDir = files.first().parentFile.toString() |
105 | |
106 def targetPackage = targetDir | |
107 source.each { | |
108 targetPackage -= it.getDir().getPath().toString() | |
109 } | |
2
4db19f1c5ad0
create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents:
0
diff
changeset
|
110 targetPackage = targetPackage.replaceAll("\\\\", '.').replaceAll('/', '.') |
0 | 111 targetPackage = targetPackage.startsWith(".") ? targetPackage - "." : targetPackage |
112 | |
113 return targetPackage | |
114 | |
115 } | |
2
4db19f1c5ad0
create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents:
0
diff
changeset
|
116 |
0 | 117 } |