-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathversionInfo.gradle
More file actions
142 lines (127 loc) · 6.49 KB
/
Copy pathversionInfo.gradle
File metadata and controls
142 lines (127 loc) · 6.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import java.text.SimpleDateFormat
project.ext.writeRepoInfo = { File repoDir, OutputStream os ->
// As long as we're not really publishing (and maybe just publishing locally for integration tests),
// allow repos (most likely, the local repo) to have no ".hg" folder.
boolean allowUnknown = !project.ext.getIsReallyPublishing()
new ByteArrayOutputStream().withStream { OutputStream errorStream ->
ExecResult result = exec {
executable = "hg"
args = ["paths", "default"]
errorOutput = errorStream
workingDir = repoDir
ignoreExitValue = allowUnknown
}
// If "hg paths default" returns 1 and outputs "not found!", that just means the folder has no default remote,
// which is perfectly valid for a new, never-pushed repo, or a dummy "local" repo.
if (result.exitValue == 0) {
errorStream.writeTo(os)
} else if (result.exitValue == 1 && errorStream.toString().replaceAll('[\\r\\n]', '') == 'not found!') {
os.println("unknown_remote")
} else {
result.assertNormalExitValue() // to throw the appropriate exception from exec
}
}
(new PrintStream(os)).withStream { it.print(" ") }
ExecResult result = exec {
executable = "hg"
args = ["id", "-ibt"]
standardOutput = os
workingDir = repoDir
ignoreExitValue = allowUnknown
}
if (result.exitValue != 0) {
os.println("unknown_id")
}
}
/**
* This task puts the Hg master repo URL and working directory version/branch/tags into a property and text file, which
* feed into the packaging tasks for all sub-projects, so all build outputs are stamped with info on where they came
* from.
*
* - JAR files put it in META-INF/MANIFEST/MF.
* - custom-gradle puts it in the init.d/holy-gradle-init.gradle.
* - credential-store puts it in the Windows file properties of the generated EXE.
*
* If you change the form or location of this info, please update not only this comment but also the wiki page at
* https://bitbucket.org/nm2501/holy-gradle-plugins/wiki/Home.
*/
task setHgVersionInfo {
project.ext.hgVersionInfoFile = new File(project.buildDir, 'hgVersionInfo.txt')
doLast {
new ByteArrayOutputStream().withStream { OutputStream os ->
writeRepoInfo(rootProject.projectDir, os)
// Store both command outputs in one string with no line breaks.
String infoWithoutDate = os.toString().replaceAll("[\\r\\n]", "")
project.ext.hgVersion = infoWithoutDate.split()[1]
// Add the date/time.
(new PrintStream(os)).withStream { it.print(" ") }
SimpleDateFormat rfc3339dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX")
if (project.hgVersion.endsWith('+')) {
// There are local changes, so just use the current date/time.
(new PrintStream(os)).withStream {
it.print(rfc3339dateFormat.format(new Date()))
}
} else {
// Pull from the Hg log.
exec {
executable = "hg"
args = ["log", "-r", project.hgVersion, "-T", '"{date|rfc3339date}"']
standardOutput = os
}
}
String infoWithDate = os.toString().replaceAll("[\\r\\n]", "")
// Only include the date/time if we're actually publishing. It will change any time you
// edit a file in the working copy, even if it's not an input to what you're building,
// so if we include it, various things always get rebuilt (e.g., credential-store.exe).
if (project.getIsPublishing()) {
project.ext.hgVersionInfo = infoWithDate
} else {
project.ext.hgVersionInfo = infoWithoutDate
}
Date dateFromInfo = rfc3339dateFormat.parse(infoWithDate.split().last())
SimpleDateFormat releaseNotesDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z")
releaseNotesDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
String releaseNotesDate = releaseNotesDateFormat.format(dateFromInfo)
project.ext.releaseNoteInfo = "${project.hgVersion} at ${releaseNotesDate}"
// Write the version to a file, so that the file can be seen as changed (or not) by
// other tasks, to force them to re-run.
if (!(project.buildDir.exists() || project.buildDir.mkdirs())) {
throw new RuntimeException("Failed to create output folder for ${project.hgVersionInfo}")
}
project.hgVersionInfoFile.text = project.hgVersionInfo
}
String hgLocalDocFullVersionInfo = project.releaseNoteInfo
final File localWebsiteDir = new File(project.WEBSITE_DIR, "local")
if (localWebsiteDir.exists()) {
new ByteArrayOutputStream().withStream { OutputStream os ->
final File localWebsiteRealDir = localWebsiteDir.toPath().toRealPath().toFile()
writeRepoInfo(localWebsiteRealDir, os)
// Store both command outputs in one string with no line breaks.
String infoWithoutDate = os.toString().replaceAll("[\\r\\n]", "")
String rev = infoWithoutDate.split()[1]
project.ext.hgLocalDocVersion = rev
}
hgLocalDocFullVersionInfo += " " + project.hgLocalDocVersion
}
project.ext.hgLocalDocFullVersionInfoURLEncoded = URLEncoder.encode(hgLocalDocFullVersionInfo, "UTF-8")
}
// Force setHgVersionInfo to always execute. For tasks which depend on this, Gradle will
// check the contents of the hgVersionInfoFile to see if they have really changed, and only
// run those tasks if so.
outputs.file(project.hgVersionInfoFile)
outputs.upToDateWhen { false }
}
subprojects {
// Add source version information for each plugin JAR.
if (project.plugins.hasPlugin("groovy")) {
// We don't need an "afterEvaluate" here because "jar" is a standard task for "groovy" projects.
project.jar {
inputs.files rootProject.setHgVersionInfo.outputs.files
doFirst {
manifest {
attributes(["Implementation-Version": rootProject.hgVersionInfo])
}
}
}
}
}