annotate src/main/groovy/org/ssdt_ohio/gradle/plugins/VersionClassPlugin.groovy @ 32:927f02ccc596 version-0.4.6

Update to Gradle 7.4.2
author Kyle Miller <kyle.miller@mcoecn.org>
date Fri, 05 Aug 2022 19:22:06 +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
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
8 package org.ssdt_ohio.gradle.plugins
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
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
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
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
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
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
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
16
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
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
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
19 def genSrc = 'generated-src/version'
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
20 def generatedSrcDir = new File(project.buildDir, genSrc)
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
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
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
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
9f9063d3c17c cope with subproject without src files
smith@nwoca.org
parents: 6
diff changeset
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
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
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
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
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
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
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
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
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
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 project.sourceSets {
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
78 main {
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
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
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
81 }
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
82 }
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
83 }
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 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
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
90 if (project.getBuildFile() != null && project.getBuildFile().exists()) {
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
91 makeVersionClassTask.getInputs().files(project.getBuildFile())
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
92 }
3
1756755e4e71 improve up-to-date check so version does not cause recompile
smith@nwoca.org
parents: 2
diff changeset
93
0
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
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
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
97 }
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
98
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
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
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
106 def source = project.sourceSets.main.groovy.getSrcDirTrees()
9
9f9063d3c17c cope with subproject without src files
smith@nwoca.org
parents: 6
diff changeset
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
9f9063d3c17c cope with subproject without src files
smith@nwoca.org
parents: 6
diff changeset
111 return null
9f9063d3c17c cope with subproject without src files
smith@nwoca.org
parents: 6
diff changeset
112 }
9f9063d3c17c cope with subproject without src files
smith@nwoca.org
parents: 6
diff changeset
113
24
cfbdbeba877f fix location of ProjectVersion class
smith@nwoca.org
parents: 18
diff changeset
114 def targetDir = files.sort {
cfbdbeba877f fix location of ProjectVersion class
smith@nwoca.org
parents: 18
diff changeset
115 it.parentFile.toString().replaceAll('(\\\\|/)','.').split(/\./).size()
cfbdbeba877f fix location of ProjectVersion class
smith@nwoca.org
parents: 18
diff changeset
116 }.first().parentFile.toString()
0
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
117
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
118 def targetPackage = targetDir
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
119 source.each {
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
120 targetPackage -= it.getDir().getPath().toString()
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
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
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
123 targetPackage = targetPackage.startsWith(".") ? targetPackage - "." : targetPackage
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
124
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
125 return targetPackage
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
126
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
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
c51874d4a5a7 initial ssdt plugin
smith@nwoca.org
parents:
diff changeset
129 }