Mercurial > public > gradleplugins
annotate src/main/groovy/org/ssdt_ohio/gradle/plugins/VersionClassPlugin.groovy @ 31:bd084c7d01df version-0.4.6
Created version-0.4.6 branch
author | Kyle Miller <kyle.miller@mcoecn.org> |
---|---|
date | Fri, 05 Aug 2022 16:41:42 +0100 |
parents | bf31e9ad44cf |
children |
rev | line source |
---|---|
30
bf31e9ad44cf
SC-181 Upgrade to gradle 5.0 for version 0.4.5, fix problems with VersionClassPlugin
Greg Shepherd <shepherd@ssdt-ohio.org>
parents:
25
diff
changeset
|
1 /* |
bf31e9ad44cf
SC-181 Upgrade to gradle 5.0 for version 0.4.5, fix problems with VersionClassPlugin
Greg Shepherd <shepherd@ssdt-ohio.org>
parents:
25
diff
changeset
|
2 * Copyright (c) 2020. Ohio Department of Education. - All Rights Reserved. |
bf31e9ad44cf
SC-181 Upgrade to gradle 5.0 for version 0.4.5, fix problems with VersionClassPlugin
Greg Shepherd <shepherd@ssdt-ohio.org>
parents:
25
diff
changeset
|
3 * Unauthorized copying of this file, in any medium, is strictly prohibited. |
bf31e9ad44cf
SC-181 Upgrade to gradle 5.0 for version 0.4.5, fix problems with VersionClassPlugin
Greg Shepherd <shepherd@ssdt-ohio.org>
parents:
25
diff
changeset
|
4 * Written by the State Software Development Team (http://ssdt.oecn.k12.oh.us/) |
bf31e9ad44cf
SC-181 Upgrade to gradle 5.0 for version 0.4.5, fix problems with VersionClassPlugin
Greg Shepherd <shepherd@ssdt-ohio.org>
parents:
25
diff
changeset
|
5 * |
bf31e9ad44cf
SC-181 Upgrade to gradle 5.0 for version 0.4.5, fix problems with VersionClassPlugin
Greg Shepherd <shepherd@ssdt-ohio.org>
parents:
25
diff
changeset
|
6 */ |
bf31e9ad44cf
SC-181 Upgrade to gradle 5.0 for version 0.4.5, fix problems with VersionClassPlugin
Greg Shepherd <shepherd@ssdt-ohio.org>
parents:
25
diff
changeset
|
7 |
0 | 8 package org.ssdt_ohio.gradle.plugins |
9 | |
2
4db19f1c5ad0
create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents:
0
diff
changeset
|
10 import org.gradle.api.Plugin |
0 | 11 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
|
12 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
|
13 import org.gradle.api.plugins.GroovyPlugin |
0 | 14 |
2
4db19f1c5ad0
create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents:
0
diff
changeset
|
15 class VersionClassPlugin implements Plugin<Project> { |
0 | 16 |
17 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
|
18 project.getPlugins().apply(GroovyPlugin.class) |
0 | 19 def genSrc = 'generated-src/version' |
20 def generatedSrcDir = new File(project.buildDir, genSrc) | |
21 | |
25
278d28eea819
USASR-2436: Create branch. Remove usages of leftShift in favor of doLast
Christopher Springer <springer@nwoca.org>
parents:
24
diff
changeset
|
22 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
|
23 doLast { |
278d28eea819
USASR-2436: Create branch. Remove usages of leftShift in favor of doLast
Christopher Springer <springer@nwoca.org>
parents:
24
diff
changeset
|
24 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
|
25 |
25
278d28eea819
USASR-2436: Create branch. Remove usages of leftShift in favor of doLast
Christopher Springer <springer@nwoca.org>
parents:
24
diff
changeset
|
26 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
|
27 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
|
28 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
|
29 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
|
30 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
|
31 } |
278d28eea819
USASR-2436: Create branch. Remove usages of leftShift in favor of doLast
Christopher Springer <springer@nwoca.org>
parents:
24
diff
changeset
|
32 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
|
33 } |
2
4db19f1c5ad0
create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents:
0
diff
changeset
|
34 |
25
278d28eea819
USASR-2436: Create branch. Remove usages of leftShift in favor of doLast
Christopher Springer <springer@nwoca.org>
parents:
24
diff
changeset
|
35 def targetPackage = getTargetClass(project) |
0 | 36 |
25
278d28eea819
USASR-2436: Create branch. Remove usages of leftShift in favor of doLast
Christopher Springer <springer@nwoca.org>
parents:
24
diff
changeset
|
37 if (!targetPackage) { |
278d28eea819
USASR-2436: Create branch. Remove usages of leftShift in favor of doLast
Christopher Springer <springer@nwoca.org>
parents:
24
diff
changeset
|
38 return |
278d28eea819
USASR-2436: Create branch. Remove usages of leftShift in favor of doLast
Christopher Springer <springer@nwoca.org>
parents:
24
diff
changeset
|
39 } |
9 | 40 |
25
278d28eea819
USASR-2436: Create branch. Remove usages of leftShift in favor of doLast
Christopher Springer <springer@nwoca.org>
parents:
24
diff
changeset
|
41 def outFile = new File(generatedSrcDir, "groovy/" + targetPackage.replaceAll('\\.', "/") + "/ProjectVersion.groovy") |
0 | 42 |
25
278d28eea819
USASR-2436: Create branch. Remove usages of leftShift in favor of doLast
Christopher Springer <springer@nwoca.org>
parents:
24
diff
changeset
|
43 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
|
44 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
|
45 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
|
46 f.write(""" |
278d28eea819
USASR-2436: Create branch. Remove usages of leftShift in favor of doLast
Christopher Springer <springer@nwoca.org>
parents:
24
diff
changeset
|
47 package $targetPackage |
278d28eea819
USASR-2436: Create branch. Remove usages of leftShift in favor of doLast
Christopher Springer <springer@nwoca.org>
parents:
24
diff
changeset
|
48 /** |
278d28eea819
USASR-2436: Create branch. Remove usages of leftShift in favor of doLast
Christopher Springer <springer@nwoca.org>
parents:
24
diff
changeset
|
49 * 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
|
50 * @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
|
51 */ |
278d28eea819
USASR-2436: Create branch. Remove usages of leftShift in favor of doLast
Christopher Springer <springer@nwoca.org>
parents:
24
diff
changeset
|
52 public class ProjectVersion { |
0 | 53 |
25
278d28eea819
USASR-2436: Create branch. Remove usages of leftShift in favor of doLast
Christopher Springer <springer@nwoca.org>
parents:
24
diff
changeset
|
54 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
|
55 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
|
56 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
|
57 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
|
58 final int buildNumber = $buildNumber |
0 | 59 |
25
278d28eea819
USASR-2436: Create branch. Remove usages of leftShift in favor of doLast
Christopher Springer <springer@nwoca.org>
parents:
24
diff
changeset
|
60 String getFullVersion() { |
278d28eea819
USASR-2436: Create branch. Remove usages of leftShift in favor of doLast
Christopher Springer <springer@nwoca.org>
parents:
24
diff
changeset
|
61 "\$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
|
62 } |
0 | 63 |
25
278d28eea819
USASR-2436: Create branch. Remove usages of leftShift in favor of doLast
Christopher Springer <springer@nwoca.org>
parents:
24
diff
changeset
|
64 String getId() { |
278d28eea819
USASR-2436: Create branch. Remove usages of leftShift in favor of doLast
Christopher Springer <springer@nwoca.org>
parents:
24
diff
changeset
|
65 "\$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
|
66 } |
2
4db19f1c5ad0
create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents:
0
diff
changeset
|
67 |
25
278d28eea819
USASR-2436: Create branch. Remove usages of leftShift in favor of doLast
Christopher Springer <springer@nwoca.org>
parents:
24
diff
changeset
|
68 String toString() { |
278d28eea819
USASR-2436: Create branch. Remove usages of leftShift in favor of doLast
Christopher Springer <springer@nwoca.org>
parents:
24
diff
changeset
|
69 "\$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
|
70 } |
278d28eea819
USASR-2436: Create branch. Remove usages of leftShift in favor of doLast
Christopher Springer <springer@nwoca.org>
parents:
24
diff
changeset
|
71 } |
278d28eea819
USASR-2436: Create branch. Remove usages of leftShift in favor of doLast
Christopher Springer <springer@nwoca.org>
parents:
24
diff
changeset
|
72 """) |
278d28eea819
USASR-2436: Create branch. Remove usages of leftShift in favor of doLast
Christopher Springer <springer@nwoca.org>
parents:
24
diff
changeset
|
73 f.close() |
278d28eea819
USASR-2436: Create branch. Remove usages of leftShift in favor of doLast
Christopher Springer <springer@nwoca.org>
parents:
24
diff
changeset
|
74 } |
0 | 75 } |
76 | |
77 project.sourceSets { | |
78 main { | |
79 groovy { | |
3
1756755e4e71
improve up-to-date check so version does not cause recompile
smith@nwoca.org
parents:
2
diff
changeset
|
80 srcDir project.buildDir.name + '/' + genSrc + '/groovy' |
0 | 81 } |
82 } | |
83 } | |
84 | |
3
1756755e4e71
improve up-to-date check so version does not cause recompile
smith@nwoca.org
parents:
2
diff
changeset
|
85 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
|
86 ) |
30
bf31e9ad44cf
SC-181 Upgrade to gradle 5.0 for version 0.4.5, fix problems with VersionClassPlugin
Greg Shepherd <shepherd@ssdt-ohio.org>
parents:
25
diff
changeset
|
87 |
bf31e9ad44cf
SC-181 Upgrade to gradle 5.0 for version 0.4.5, fix problems with VersionClassPlugin
Greg Shepherd <shepherd@ssdt-ohio.org>
parents:
25
diff
changeset
|
88 makeVersionClassTask.getOutputs().dir(project.buildDir.name + '/' + genSrc) |
3
1756755e4e71
improve up-to-date check so version does not cause recompile
smith@nwoca.org
parents:
2
diff
changeset
|
89 |
0 | 90 if (project.getBuildFile() != null && project.getBuildFile().exists()) { |
91 makeVersionClassTask.getInputs().files(project.getBuildFile()) | |
92 } | |
3
1756755e4e71
improve up-to-date check so version does not cause recompile
smith@nwoca.org
parents:
2
diff
changeset
|
93 |
0 | 94 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
|
95 |
4db19f1c5ad0
create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents:
0
diff
changeset
|
96 |
0 | 97 } |
98 | |
99 | |
2
4db19f1c5ad0
create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents:
0
diff
changeset
|
100 |
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
|
101 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
|
102 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
|
103 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
|
104 } 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
|
105 |
0 | 106 def source = project.sourceSets.main.groovy.getSrcDirTrees() |
9 | 107 |
30
bf31e9ad44cf
SC-181 Upgrade to gradle 5.0 for version 0.4.5, fix problems with VersionClassPlugin
Greg Shepherd <shepherd@ssdt-ohio.org>
parents:
25
diff
changeset
|
108 def files = project.sourceSets.main.groovy |
bf31e9ad44cf
SC-181 Upgrade to gradle 5.0 for version 0.4.5, fix problems with VersionClassPlugin
Greg Shepherd <shepherd@ssdt-ohio.org>
parents:
25
diff
changeset
|
109 |
bf31e9ad44cf
SC-181 Upgrade to gradle 5.0 for version 0.4.5, fix problems with VersionClassPlugin
Greg Shepherd <shepherd@ssdt-ohio.org>
parents:
25
diff
changeset
|
110 if (!files || files.size() == 0) { |
9 | 111 return null |
112 } | |
113 | |
24 | 114 def targetDir = files.sort { |
115 it.parentFile.toString().replaceAll('(\\\\|/)','.').split(/\./).size() | |
116 }.first().parentFile.toString() | |
0 | 117 |
118 def targetPackage = targetDir | |
119 source.each { | |
120 targetPackage -= it.getDir().getPath().toString() | |
121 } | |
2
4db19f1c5ad0
create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents:
0
diff
changeset
|
122 targetPackage = targetPackage.replaceAll("\\\\", '.').replaceAll('/', '.') |
0 | 123 targetPackage = targetPackage.startsWith(".") ? targetPackage - "." : targetPackage |
124 | |
125 return targetPackage | |
126 | |
127 } | |
2
4db19f1c5ad0
create ProjectVersion as instance instead of static. Add build info to jar manifest
smith@nwoca.org
parents:
0
diff
changeset
|
128 |
0 | 129 } |