annotate src/main/groovy/org/ssdt_ohio/gradle/plugins/VersionClassPlugin.groovy @ 26:428c92e0ae10 version-0.4.3

USASR-2436: Set branch release version
author Christopher Springer <springer@nwoca.org>
date Wed, 21 Dec 2016 13:14:08 +0000
parents 278d28eea819
children bf31e9ad44cf
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
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
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
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
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
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
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
9
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
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
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
12 def genSrc = 'generated-src/version'
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
13 def generatedSrcDir = new File(project.buildDir, genSrc)
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
14
25
278d28eea819 USASR-2436: Create branch. Remove usages of leftShift in favor of doLast
Christopher Springer <springer@nwoca.org>
parents: 24
diff changeset
15 def makeVersionClassTask = project.task('makeVersionClass') {
278d28eea819 USASR-2436: Create branch. Remove usages of leftShift in favor of doLast
Christopher Springer <springer@nwoca.org>
parents: 24
diff changeset
16 doLast {
278d28eea819 USASR-2436: Create branch. Remove usages of leftShift in favor of doLast
Christopher Springer <springer@nwoca.org>
parents: 24
diff changeset
17 def now = new Date().format('yyyy-MM-dd hh:mm:ss')
2
4db19f1c5ad0 create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents: 0
diff changeset
18
25
278d28eea819 USASR-2436: Create branch. Remove usages of leftShift in favor of doLast
Christopher Springer <springer@nwoca.org>
parents: 24
diff changeset
19 def buildNumber = -1
278d28eea819 USASR-2436: Create branch. Remove usages of leftShift in favor of doLast
Christopher Springer <springer@nwoca.org>
parents: 24
diff changeset
20 if (project.rootProject?.file('build-number.txt').exists()) {
278d28eea819 USASR-2436: Create branch. Remove usages of leftShift in favor of doLast
Christopher Springer <springer@nwoca.org>
parents: 24
diff changeset
21 def props = new Properties()
278d28eea819 USASR-2436: Create branch. Remove usages of leftShift in favor of doLast
Christopher Springer <springer@nwoca.org>
parents: 24
diff changeset
22 project.rootProject?.file('build-number.txt').withInputStream {stream ->
278d28eea819 USASR-2436: Create branch. Remove usages of leftShift in favor of doLast
Christopher Springer <springer@nwoca.org>
parents: 24
diff changeset
23 props.load(stream)
278d28eea819 USASR-2436: Create branch. Remove usages of leftShift in favor of doLast
Christopher Springer <springer@nwoca.org>
parents: 24
diff changeset
24 }
278d28eea819 USASR-2436: Create branch. Remove usages of leftShift in favor of doLast
Christopher Springer <springer@nwoca.org>
parents: 24
diff changeset
25 buildNumber = props['build.number']
278d28eea819 USASR-2436: Create branch. Remove usages of leftShift in favor of doLast
Christopher Springer <springer@nwoca.org>
parents: 24
diff changeset
26 }
2
4db19f1c5ad0 create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents: 0
diff changeset
27
25
278d28eea819 USASR-2436: Create branch. Remove usages of leftShift in favor of doLast
Christopher Springer <springer@nwoca.org>
parents: 24
diff changeset
28 def targetPackage = getTargetClass(project)
0
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
29
25
278d28eea819 USASR-2436: Create branch. Remove usages of leftShift in favor of doLast
Christopher Springer <springer@nwoca.org>
parents: 24
diff changeset
30 if (!targetPackage) {
278d28eea819 USASR-2436: Create branch. Remove usages of leftShift in favor of doLast
Christopher Springer <springer@nwoca.org>
parents: 24
diff changeset
31 return
278d28eea819 USASR-2436: Create branch. Remove usages of leftShift in favor of doLast
Christopher Springer <springer@nwoca.org>
parents: 24
diff changeset
32 }
9
9f9063d3c17c cope with subproject without src files
smith@nwoca.org
parents: 6
diff changeset
33
25
278d28eea819 USASR-2436: Create branch. Remove usages of leftShift in favor of doLast
Christopher Springer <springer@nwoca.org>
parents: 24
diff changeset
34 def outFile = new File(generatedSrcDir, "groovy/" + targetPackage.replaceAll('\\.', "/") + "/ProjectVersion.groovy")
0
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
35
25
278d28eea819 USASR-2436: Create branch. Remove usages of leftShift in favor of doLast
Christopher Springer <springer@nwoca.org>
parents: 24
diff changeset
36 outFile.getParentFile().mkdirs()
278d28eea819 USASR-2436: Create branch. Remove usages of leftShift in favor of doLast
Christopher Springer <springer@nwoca.org>
parents: 24
diff changeset
37 logger.info("creating $targetPackage in $outFile")
278d28eea819 USASR-2436: Create branch. Remove usages of leftShift in favor of doLast
Christopher Springer <springer@nwoca.org>
parents: 24
diff changeset
38 def f = new FileWriter(outFile)
278d28eea819 USASR-2436: Create branch. Remove usages of leftShift in favor of doLast
Christopher Springer <springer@nwoca.org>
parents: 24
diff changeset
39 f.write("""
278d28eea819 USASR-2436: Create branch. Remove usages of leftShift in favor of doLast
Christopher Springer <springer@nwoca.org>
parents: 24
diff changeset
40 package $targetPackage
278d28eea819 USASR-2436: Create branch. Remove usages of leftShift in favor of doLast
Christopher Springer <springer@nwoca.org>
parents: 24
diff changeset
41 /**
278d28eea819 USASR-2436: Create branch. Remove usages of leftShift in favor of doLast
Christopher Springer <springer@nwoca.org>
parents: 24
diff changeset
42 * Generated by gradle build. $now ($buildNumber)
278d28eea819 USASR-2436: Create branch. Remove usages of leftShift in favor of doLast
Christopher Springer <springer@nwoca.org>
parents: 24
diff changeset
43 * @version ${project.version}
278d28eea819 USASR-2436: Create branch. Remove usages of leftShift in favor of doLast
Christopher Springer <springer@nwoca.org>
parents: 24
diff changeset
44 */
278d28eea819 USASR-2436: Create branch. Remove usages of leftShift in favor of doLast
Christopher Springer <springer@nwoca.org>
parents: 24
diff changeset
45 public class ProjectVersion {
0
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
46
25
278d28eea819 USASR-2436: Create branch. Remove usages of leftShift in favor of doLast
Christopher Springer <springer@nwoca.org>
parents: 24
diff changeset
47 final String name = "${project.name}"
278d28eea819 USASR-2436: Create branch. Remove usages of leftShift in favor of doLast
Christopher Springer <springer@nwoca.org>
parents: 24
diff changeset
48 final String group = "${project.group}"
278d28eea819 USASR-2436: Create branch. Remove usages of leftShift in favor of doLast
Christopher Springer <springer@nwoca.org>
parents: 24
diff changeset
49 final String version = "${project.version}"
278d28eea819 USASR-2436: Create branch. Remove usages of leftShift in favor of doLast
Christopher Springer <springer@nwoca.org>
parents: 24
diff changeset
50 final String buildDate = "$now"
278d28eea819 USASR-2436: Create branch. Remove usages of leftShift in favor of doLast
Christopher Springer <springer@nwoca.org>
parents: 24
diff changeset
51 final int buildNumber = $buildNumber
0
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
52
25
278d28eea819 USASR-2436: Create branch. Remove usages of leftShift in favor of doLast
Christopher Springer <springer@nwoca.org>
parents: 24
diff changeset
53 String getFullVersion() {
278d28eea819 USASR-2436: Create branch. Remove usages of leftShift in favor of doLast
Christopher Springer <springer@nwoca.org>
parents: 24
diff changeset
54 "\$version \$buildDate (#\$buildNumber)"
278d28eea819 USASR-2436: Create branch. Remove usages of leftShift in favor of doLast
Christopher Springer <springer@nwoca.org>
parents: 24
diff changeset
55 }
0
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
56
25
278d28eea819 USASR-2436: Create branch. Remove usages of leftShift in favor of doLast
Christopher Springer <springer@nwoca.org>
parents: 24
diff changeset
57 String getId() {
278d28eea819 USASR-2436: Create branch. Remove usages of leftShift in favor of doLast
Christopher Springer <springer@nwoca.org>
parents: 24
diff changeset
58 "\$group:\$name:\$version"
278d28eea819 USASR-2436: Create branch. Remove usages of leftShift in favor of doLast
Christopher Springer <springer@nwoca.org>
parents: 24
diff changeset
59 }
2
4db19f1c5ad0 create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents: 0
diff changeset
60
25
278d28eea819 USASR-2436: Create branch. Remove usages of leftShift in favor of doLast
Christopher Springer <springer@nwoca.org>
parents: 24
diff changeset
61 String toString() {
278d28eea819 USASR-2436: Create branch. Remove usages of leftShift in favor of doLast
Christopher Springer <springer@nwoca.org>
parents: 24
diff changeset
62 "\$id \$buildDate (#\$buildNumber)"
278d28eea819 USASR-2436: Create branch. Remove usages of leftShift in favor of doLast
Christopher Springer <springer@nwoca.org>
parents: 24
diff changeset
63 }
278d28eea819 USASR-2436: Create branch. Remove usages of leftShift in favor of doLast
Christopher Springer <springer@nwoca.org>
parents: 24
diff changeset
64 }
278d28eea819 USASR-2436: Create branch. Remove usages of leftShift in favor of doLast
Christopher Springer <springer@nwoca.org>
parents: 24
diff changeset
65 """)
278d28eea819 USASR-2436: Create branch. Remove usages of leftShift in favor of doLast
Christopher Springer <springer@nwoca.org>
parents: 24
diff changeset
66 f.close()
278d28eea819 USASR-2436: Create branch. Remove usages of leftShift in favor of doLast
Christopher Springer <springer@nwoca.org>
parents: 24
diff changeset
67 }
0
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 project.sourceSets {
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
71 main {
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
72 groovy {
3
1756755e4e71 improve up-to-date check so version does not cause recompile
smith@nwoca.org
parents: 2
diff changeset
73 srcDir project.buildDir.name + '/' + genSrc + '/groovy'
0
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
74 }
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
75 }
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
76 }
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
77
3
1756755e4e71 improve up-to-date check so version does not cause recompile
smith@nwoca.org
parents: 2
diff changeset
78 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
79 )
1756755e4e71 improve up-to-date check so version does not cause recompile
smith@nwoca.org
parents: 2
diff changeset
80 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
81
0
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
82 if (project.getBuildFile() != null && project.getBuildFile().exists()) {
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
83 makeVersionClassTask.getInputs().files(project.getBuildFile())
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
84 }
3
1756755e4e71 improve up-to-date check so version does not cause recompile
smith@nwoca.org
parents: 2
diff changeset
85
0
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
86 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
87
4db19f1c5ad0 create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents: 0
diff changeset
88
0
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
89 }
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
90
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
91
2
4db19f1c5ad0 create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents: 0
diff changeset
92
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
93 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
94 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
95 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
96 } 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
97
0
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
98 def source = project.sourceSets.main.groovy.getSrcDirTrees()
9
9f9063d3c17c cope with subproject without src files
smith@nwoca.org
parents: 6
diff changeset
99
0
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
100 def files = project.sourceSets.main.groovy as File[]
9
9f9063d3c17c cope with subproject without src files
smith@nwoca.org
parents: 6
diff changeset
101 if (!files ) {
9f9063d3c17c cope with subproject without src files
smith@nwoca.org
parents: 6
diff changeset
102 return null
9f9063d3c17c cope with subproject without src files
smith@nwoca.org
parents: 6
diff changeset
103 }
9f9063d3c17c cope with subproject without src files
smith@nwoca.org
parents: 6
diff changeset
104
24
cfbdbeba877f fix location of ProjectVersion class
smith@nwoca.org
parents: 18
diff changeset
105 def targetDir = files.sort {
cfbdbeba877f fix location of ProjectVersion class
smith@nwoca.org
parents: 18
diff changeset
106 it.parentFile.toString().replaceAll('(\\\\|/)','.').split(/\./).size()
cfbdbeba877f fix location of ProjectVersion class
smith@nwoca.org
parents: 18
diff changeset
107 }.first().parentFile.toString()
0
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
108
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
109 def targetPackage = targetDir
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
110 source.each {
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
111 targetPackage -= it.getDir().getPath().toString()
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
112 }
2
4db19f1c5ad0 create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents: 0
diff changeset
113 targetPackage = targetPackage.replaceAll("\\\\", '.').replaceAll('/', '.')
0
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
114 targetPackage = targetPackage.startsWith(".") ? targetPackage - "." : targetPackage
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
115
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
116 return targetPackage
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
117
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
118 }
2
4db19f1c5ad0 create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents: 0
diff changeset
119
0
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
120 }