|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright (C) 2010 JFrog Ltd. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + * / |
| 17 | + */ |
| 18 | + |
1 | 19 | package org.jfrog.build
|
2 | 20 |
|
3 | 21 | import org.gradle.api.GradleException
|
4 | 22 | import org.gradle.api.Project
|
| 23 | +import org.gradle.api.DefaultTask |
| 24 | +import org.gradle.api.tasks.Input |
| 25 | +import org.gradle.api.tasks.OutputFile |
| 26 | +import org.gradle.api.tasks.TaskAction |
| 27 | + |
| 28 | +import java.text.DateFormat |
5 | 29 | import java.text.SimpleDateFormat
|
6 | 30 |
|
7 | 31 | class Version {
|
8 | 32 | String versionNumber
|
9 | 33 | Date buildTime
|
10 | 34 | Boolean release = null
|
| 35 | + int indexOfSnapshot |
11 | 36 |
|
12 | 37 | def Version(Project project) {
|
13 | 38 | this(project, null)
|
14 | 39 | }
|
15 | 40 |
|
16 | 41 | def Version(Project project, List<String> subProjects) {
|
17 | 42 | this.versionNumber = project.getProperty("${project.name}-version")
|
18 |
| - this.release = Boolean.valueOf(project.getProperty("${project.name}-release")) |
| 43 | + this.release = true |
| 44 | + if (versionNumber.contains('SNAPSHOT')) { |
| 45 | + this.release = false |
| 46 | + this.indexOfSnapshot = this.versionNumber.indexOf('SNAPSHOT') |
| 47 | + } |
19 | 48 | File timestampFile = new File(project.buildDir, 'timestamp.txt')
|
20 | 49 | if (timestampFile.isFile()) {
|
21 | 50 | boolean uptodate = true
|
@@ -47,27 +76,55 @@ class Version {
|
47 | 76 | }
|
48 | 77 | buildTime = new Date(timestampFile.lastModified())
|
49 | 78 | if (!release) {
|
50 |
| - //def ts = new SimpleDateFormat('yyyyMMddHHmmss').format(buildTime) |
51 |
| - this.versionNumber += "-SNAPSHOT" // + ts |
52 |
| - /* |
53 |
| - project.gradle.taskGraph.whenReady {graph -> |
54 |
| - if (graph.hasTask(':releaseVersion')) { |
55 |
| - release = true |
56 |
| - } else { |
57 |
| - this.versionNumber += "-" + getTimestamp() |
58 |
| - release = false |
59 |
| - } |
60 |
| - } |
61 |
| - */ |
| 79 | + this.versionNumber = this.versionNumber.substring(0, indexOfSnapshot - 1) |
| 80 | + this.versionNumber += "-" + getTimestamp() |
62 | 81 | }
|
63 | 82 | }
|
64 | 83 |
|
| 84 | + String toString() { |
| 85 | + versionNumber |
| 86 | + } |
| 87 | + |
| 88 | + String getTimestamp() { |
| 89 | + // Convert local file timestamp to UTC |
| 90 | + def format = new SimpleDateFormat('yyyyMMddHHmmss') |
| 91 | + format.setCalendar(Calendar.getInstance(TimeZone.getTimeZone('UTC'))); |
| 92 | + return format.format(buildTime) |
| 93 | + } |
| 94 | + |
65 | 95 | String getStatus() {
|
66 |
| - if (release) return 'release' |
67 |
| - return 'integration' |
| 96 | + return release ? "release" : "integration" |
68 | 97 | }
|
69 | 98 |
|
70 |
| - String toString() { |
71 |
| - versionNumber |
| 99 | + boolean isRelease() { |
| 100 | + if (release == null) { |
| 101 | + throw new GradleException("Can't determine whether this is a release build before the task graph is populated") |
| 102 | + } |
| 103 | + return release |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +class WriteVersionProperties extends DefaultTask { |
| 108 | + @Input |
| 109 | + String getVersion() { return project.version.toString() } |
| 110 | + |
| 111 | + @Input |
| 112 | + Date getBuildTime() { return project.version.buildTime } |
| 113 | + |
| 114 | + @OutputFile |
| 115 | + File propertiesFile |
| 116 | + |
| 117 | + @TaskAction |
| 118 | + def void generate() { |
| 119 | + logger.info('Write version properties to: {}', propertiesFile) |
| 120 | + Properties versionProperties = new Properties() |
| 121 | + versionProperties.putAll([ |
| 122 | + 'version': version, |
| 123 | + 'buildTime': DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL).format(buildTime) |
| 124 | + ]) |
| 125 | + propertiesFile.withOutputStream { |
| 126 | + versionProperties.store(it, '') |
| 127 | + } |
| 128 | + |
72 | 129 | }
|
73 | 130 | }
|
0 commit comments