-
Notifications
You must be signed in to change notification settings - Fork 2
/
build.gradle.kts
278 lines (229 loc) · 8.22 KB
/
build.gradle.kts
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import java.security.MessageDigest
plugins {
signing
`maven-publish`
id("io.github.gradle-nexus.publish-plugin") version "2.0.0"
id("de.undercouch.download") version "5.6.0"
}
val HMCL_VERSION_PATTERN = Regex("^[0-9]+\\.[0-9]+\\.[0-9]+(\\.[0-9]+)?$")
val HMCL_BUILD_NUMBER_PATTERN = Regex("^[0-9]+$")
val exts = listOf("exe", "jar")
data class HMCLChannel(
val name: String,
val ciUrlBase: String
) {
val artifactId: String = "hmcl-$name"
}
val channels: List<HMCLChannel> = run {
val p = java.util.Properties()
rootProject.file("channels.properties").bufferedReader().use { p.load(it) }
p.getProperty("names")!!.split(',').map { name ->
val urlBase = p.getProperty("$name.ci.url") ?: "https://ci.huangyuhui.net/job/HMCL-$name"
HMCLChannel(name, urlBase)
}
}
logger.quiet(channels.toString())
val hmclVersion =
findProperty("hmcl.version")?.toString()
?: System.getenv("HMCL_VERSION") ?: throw GradleException("HMCL version not specified")
val ciBuildNumber =
findProperty("hmcl.ci.buildNumber")?.toString()
?: System.getenv("HMCL_CI_BUILD_NUMBER") ?: throw GradleException("HMCL CI build number not specified")
val hmclChannel =
(findProperty("hmcl.channel")?.toString()
?: System.getenv("HMCL_UPDATE_CHANNEL") ?: throw GradleException("HMCL channel not specified"))
.let {
for (value in channels) {
if (value.name == it) {
return@let value
}
}
throw GradleException("Unknown channel name: $it")
}
if (!HMCL_VERSION_PATTERN.matches(hmclVersion)) {
throw GradleException("Bad HMCL version: $hmclVersion")
}
if (!HMCL_BUILD_NUMBER_PATTERN.matches(ciBuildNumber)) {
throw GradleException("Bad HMCL CI build number: $ciBuildNumber")
}
val buildDir = layout.buildDirectory.get().asFile
val downloadDir = buildDir.resolve("downloads")
val downloadVerifyDir = downloadDir.resolve("sha1")
val downloadArtifacts = tasks.create<de.undercouch.gradle.tasks.download.Download>("downloadArtifacts") {
doFirst {
if (!downloadDir.exists()) {
downloadDir.mkdirs()
}
}
for (ext in exts) {
src("${hmclChannel.ciUrlBase}/$ciBuildNumber/artifact/HMCL/build/libs/HMCL-$hmclVersion.$ext")
}
overwrite(false)
quiet(false)
dest(downloadDir)
retries(5)
}
val downloadVerifyFiles = tasks.create<de.undercouch.gradle.tasks.download.Download>("downloadVerifyFiles") {
doFirst {
if (!downloadVerifyDir.exists()) {
downloadVerifyDir.mkdirs()
}
}
for (ext in exts) {
src("${hmclChannel.ciUrlBase}/$ciBuildNumber/artifact/HMCL/build/libs/HMCL-$hmclVersion.$ext.sha1")
}
overwrite(false)
quiet(false)
dest(downloadVerifyDir)
retries(5)
}
val verifyDownload = tasks.create("verifyDownload") {
dependsOn(downloadArtifacts, downloadVerifyFiles)
doLast {
var failed = false
val md = MessageDigest.getInstance("SHA-1")
for (ext in exts) {
val file = downloadDir.resolve("HMCL-$hmclVersion.$ext")
val hashFile = downloadVerifyDir.resolve("HMCL-$hmclVersion.$ext.sha1")
if (!file.exists()) {
logger.log(LogLevel.ERROR, "$file does not exist")
failed = true
continue
}
if (!hashFile.exists()) {
logger.log(LogLevel.ERROR, "$hashFile does not exist")
failed = true
continue
}
md.reset()
md.update(file.readBytes())
val hash = md.digest().joinToString(separator = "") { "%02x".format(it) }
val recordedHash = hashFile.readText().trim()
if (hash == recordedHash) {
logger.quiet("$file passed validation")
} else {
logger.log(LogLevel.ERROR, "SHA-1 of file $file ($hash) does not match $recordedHash")
file.delete()
hashFile.delete()
failed = true
}
}
if (failed) {
throw GradleException("Verification failed")
}
}
}
val updateJsonFile = downloadDir.resolve("HMCL-$hmclVersion.json")
val generateUpdateJson = tasks.create("generateUpdateJson") {
dependsOn(verifyDownload)
doLast {
if (!downloadDir.exists()) {
downloadDir.mkdirs()
}
fun downloadLink(ext: String) =
"https://maven.aliyun.com/repository/central/org/glavo/hmcl/${hmclChannel.artifactId}/$hmclVersion/${hmclChannel.artifactId}-$hmclVersion.$ext"
fun sha1(ext: String) =
downloadVerifyDir.resolve("HMCL-$hmclVersion.$ext.sha1").readText().trim()
val data = mapOf(
"jar" to downloadLink("jar"),
"jarsha1" to sha1("jar"),
"version" to hmclVersion,
"universal" to "https://www.mcbbs.net/forum.php?mod=viewthread&tid=142335"
)
downloadDir.resolve("HMCL-$hmclVersion.json").writeText(
data.map { (key, value) -> """"$key":"$value"""" }.joinToString(",", "{", "}")
)
}
}
tasks.withType<GenerateModuleMetadata>().configureEach {
enabled = false
}
tasks.withType<GenerateMavenPom>().configureEach {
dependsOn(verifyDownload, generateUpdateJson)
}
configure<PublishingExtension> {
publications {
create<MavenPublication>("hmcl") {
groupId = "org.glavo.hmcl"
artifactId = hmclChannel.artifactId
version = hmclVersion
for (ext in exts.plus("json")) {
artifact(downloadDir.resolve("HMCL-$hmclVersion.$ext")) {
extension = ext
classifier = ""
}
}
pom {
name.set("Hello Minecraft! Launcher ")
description.set("A Minecraft Launcher which is multi-functional, cross-platform and popular")
url.set("https://github.com/huanghongxun/HMCL")
licenses {
license {
name.set("GPL 3.0")
url.set("https://www.gnu.org/licenses/gpl-3.0.html")
}
}
developers {
developer {
id.set("huanghongxun")
name.set("Yuhui Huang")
email.set("[email protected]")
}
developer {
id.set("Glavo")
name.set("Glavo")
email.set("[email protected]")
}
}
scm {
url.set("https://github.com/HMCL-dev/HMCL")
}
}
}
}
}
// Maven Central
var secretPropsFile = project.rootProject.file("gradle/maven-central-publish.properties")
if (!secretPropsFile.exists()) {
secretPropsFile =
file(System.getProperty("user.home")).resolve(".gradle").resolve("maven-central-publish.properties")
}
if (secretPropsFile.exists()) {
// Read local.properties file first if it exists
val p = java.util.Properties()
secretPropsFile.reader().use {
p.load(it)
}
p.forEach { (name, value) ->
rootProject.ext[name.toString()] = value
}
}
listOf(
"sonatypeUsername" to "SONATYPE_USERNAME",
"sonatypePassword" to "SONATYPE_PASSWORD",
"sonatypeStagingProfileId" to "SONATYPE_STAGING_PROFILE_ID",
"signing.keyId" to "SIGNING_KEY_ID",
"signing.password" to "SIGNING_PASSWORD",
"signing.key" to "SIGNING_KEY"
).forEach { (p, e) ->
if (!rootProject.ext.has(p)) {
rootProject.ext[p] = System.getenv(e)
}
}
signing {
useInMemoryPgpKeys(
rootProject.ext["signing.keyId"].toString(),
rootProject.ext["signing.key"].toString(),
rootProject.ext["signing.password"].toString(),
)
sign(publishing.publications["hmcl"])
}
nexusPublishing {
repositories {
sonatype {
stagingProfileId.set(rootProject.ext["sonatypeStagingProfileId"].toString())
username.set(rootProject.ext["sonatypeUsername"].toString())
password.set(rootProject.ext["sonatypePassword"].toString())
}
}
}