-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.gradle
More file actions
386 lines (371 loc) · 16.6 KB
/
build.gradle
File metadata and controls
386 lines (371 loc) · 16.6 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
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
import me.modmuss50.mpp.ReleaseType
import util.EnvUtil
import util.PropUtil
import util.StaticUtil
import java.nio.file.Files
import java.nio.file.StandardCopyOption
import java.time.LocalDate
import java.util.regex.Pattern
plugins {
id("fabric-loom") version("${loom_version}") apply(false)
id("net.neoforged.moddev") version("${moddev_version}") apply(false)
id("net.neoforged.licenser") version("${licenser_version}") apply(false)
id("me.modmuss50.mod-publish-plugin") version("${mpp_version}")
id("org.ajoberstar.grgit.service") version("${grgitservice_version}")
}
subprojects {
final env = new EnvUtil(providers)
final prop = new PropUtil(project)
final cLog = StaticUtil.versionChangelog(rootProject.file("changelog.md"), mod_version)
// Append Minecraft version extension
mod_version = "${mod_version}+${minecraft_version}"
// Append dependency version extension, if configured
if (prop.has("dep_ext_${name}")) {
final data = prop.get("dep_ext_${name}").split(":")
final dep_abbrev = data[0]
// Strip meta from dependency version
final dep_ver = prop.get(data[1]).split("\\+")[0]
mod_version = "${mod_version}+${dep_abbrev}${dep_ver}"
}
version = mod_version
group = mod_group
// Configure license headers
apply(plugin: "net.neoforged.licenser")
final licenseDir = "src/main/resources/assets/${mod_id}/license/"
license {
include("**/*.java") // Java files only
// Apply main license header
header = rootProject.project("common").file("${licenseDir}HEADER.txt")
properties {
project_name = mod_name
owner_name = mod_owner
year = LocalDate.now().getYear().toString()
}
// Apply attribution license headers, if configured
if (att_license_mods != "") {
att_license_mods.split(",").each { String modId ->
//noinspection GroovyAssignabilityCheck
matching({ include(prop.list("att_license_files_${modId}")) }) {
header = rootProject.project("common").file("${licenseDir}${modId}/HEADER.txt")
it
}
}
}
}
// Configure multi-site publishing
if (name != "common") {
apply(plugin: "me.modmuss50.mod-publish-plugin")
apply(plugin: "org.ajoberstar.grgit.service")
afterEvaluate { sp ->
publishMods {
// Common configuration
file = sp.name == "fabric" ? remapJar.archiveFile : jar.archiveFile
version = mod_version
type = ReleaseType.of(mod_version_type)
displayName = "v${mod_version}-${StaticUtil.capsLoader(sp.name)}"
modLoaders.addAll(prop.safeList("mod_loaders_${sp.name}"))
maxRetries = 5
dryRun = !env.has("GITHUB_TOKEN")
&& !env.has("MODRINTH_TOKEN")
&& !env.has("CURSEFORGE_TOKEN")
// GitHub configuration
github {
parent project(":").tasks.named("publishGithub")
accessToken = env.safe("GITHUB_TOKEN")
if (build_sources_jar == "true") additionalFiles.from(sourcesJar.archiveFile)
if (build_javadoc_jar == "true") additionalFiles.from(javadocJar.archiveFile)
return void
}
// Modrinth configuration
modrinth {
accessToken = env.safe("MODRINTH_TOKEN")
projectId = prop.safe("mod_modrinth_id")
minecraftVersions.addAll(prop.safeList("mc_versions_${sp.name}"))
changelog = cLog
prop.safeList("${sp.name}_deps").each { dep ->
try {
final depData = prop.list("d_${sp.name}_${dep}")
if (depData.length > 2 && depData[2] != "-") {
final mrDepData = depData[2].split(":")
final type = mrDepData[0]
if (type == "req") {
requires { id = mrDepData[1] }
} else if (type == "opt") {
optional { id = mrDepData[1] }
} else if (type == "inc") {
incompatible { id = mrDepData[1] }
} else if (type == "emb") {
embeds { id = mrDepData[1] }
} else {
throw new IllegalArgumentException(
"Unrecognized dependency type: ${mrDepData[0]}"
)
}
}
} catch (Exception ex) {
logger.error("Error processing Modrinth dependency '${dep}' for "
+ "subproject '${sp.name}'. Check dependency property format.")
throw ex
}
}
// Sync Modrinth description with README
if (prop.has("mod_github_repo")) {
projectDescription = rootProject.file("README.md").text.replaceAll(
"src=\"\\./",
"src=\"https://raw.githubusercontent.com/${prop.get("mod_github_repo")}/HEAD/"
)
return
}
}
// CurseForge configuration
curseforge {
accessToken = env.safe("CURSEFORGE_TOKEN")
projectId = prop.safe("mod_curseforge_id")
projectSlug = prop.safe("mod_curseforge_slug")
minecraftVersions.addAll(prop.safeList("mc_versions_${sp.name}"))
changelog = cLog
prop.safeList("${sp.name}_deps").each { dep ->
try {
final depData = prop.list("d_${sp.name}_${dep}")
if (depData.length > 3 && depData[3] != "-") {
final cfDepData = depData[3].split(":")
final type = cfDepData[0]
if (type == "req") {
requires(cfDepData[1])
} else if (type == "opt") {
optional(cfDepData[1])
} else if (type == "inc") {
incompatible(cfDepData[1])
} else if (type == "emb") {
embeds(cfDepData[1])
} else {
throw new IllegalArgumentException(
"Unrecognized dependency type: ${cfDepData[0]}"
)
}
}
} catch (Exception ex) {
logger.error("Error processing CurseForge dependency '${dep}' for "
+ "subproject '${sp.name}'. Check dependency property format.")
throw ex
}
}
return void
}
}
final hasSpConf = prop.has("mod_loaders_${name}") && prop.has("mc_versions_${name}")
tasks.named("publishGithub") {
onlyIf {
return hasSpConf
&& prop.has("mod_github_repo")
&& (it["dryRun"].get() || env.has("GITHUB_TOKEN"))
}
}
tasks.named("publishModrinth") {
onlyIf {
return hasSpConf
&& prop.has("mod_modrinth_id")
&& (it["dryRun"].get() || env.has("MODRINTH_TOKEN"))
}
}
tasks.named("publishCurseforge") {
onlyIf {
return hasSpConf
&& prop.has("mod_curseforge_id")
&& prop.has("mod_curseforge_slug")
&& (it["dryRun"].get() || env.has("CURSEFORGE_TOKEN"))
}
}
}
}
}
/*
This configuration, combined with the subproject configuration above, allows
multiple files to be uploaded to a single GitHub release.
*/
publishMods {
final env = new EnvUtil(providers)
final prop = new PropUtil(project)
version = "${mod_version}+${minecraft_version}"
displayName = "v${mod_version}+${minecraft_version}"
type = ReleaseType.of(mod_version_type)
dryRun = !env.has("GITHUB_TOKEN")
github {
accessToken = env.safe("GITHUB_TOKEN")
repository = prop.safe("mod_github_repo")
commitish = grgitService.service.get().grgit.branch.current().name
tagName = "v${mod_version}+${minecraft_version}"
allowEmptyFiles = true
// Link to header in changelog file
changelog = "[Changelog](./changelog.md#${mod_version.replaceAll("\\.", "")})"
}
tasks.named("publishGithub") {
onlyIf {
return prop.has("mod_github_repo")
&& (it["dryRun"].get() || env.has("GITHUB_TOKEN"))
}
}
}
/*
This task applies changes defined the 'rebrand.properties' file to packages,
file names and file contents. Only root files not starting with '.' and files
in subproject 'src/main' directories are affected.
<p>
This task should be used with caution, since unexpected behavior can occur
especially when running the task more than one time off the template project.
Refer to the 'rebrand.properties' file for more information.
*/
tasks.register("rebrandProject") {
println("Starting task '${it.name}'...")
// Check for the properties file
final String propFileName = "rebrand.properties"
if (!file(propFileName).exists()) {
logger.error("Rebrand file '${propFileName}' not found.")
return
}
doLast {
// Load new properties
final props = new Properties()
file(propFileName).withInputStream { stream ->
props.load(stream)
}
final sep = File.separatorChar
// Rename the source package
final oldSrcPkgName =
"src/main/java/${mod_group}/${mod_id}".replaceAll("\\.", "/")
final newSrcPkgName =
"src/main/java/${props.get(mod_group)}/${props.get(mod_id)}".replaceAll("\\.", "/")
if (file(oldSrcPkgName).exists()) {
logger.error("Old source package '${oldSrcPkgName} does not exist")
return
} else if (file(newSrcPkgName) == file(oldSrcPkgName)) {
println("New and old source packages are equal, skipping rename")
} else {
// Find the divergence root of new and old paths
final srcPkgRootSplit = new ArrayList<>()
final oldSrcPkgSplit = oldSrcPkgName.split("/")
final newSrcPkgSplit = newSrcPkgName.split("/")
final cap = Math.min(oldSrcPkgSplit.length, newSrcPkgSplit.length)
String oldSrcPkgRootName = ""
String newSrcPkgRootName = ""
for (int i = 0; i < cap; i++) {
// OS-independent comparison
if (file(newSrcPkgSplit[i]) == file(oldSrcPkgSplit[i])) {
srcPkgRootSplit.add(oldSrcPkgSplit[i])
} else {
// Divergence found, assemble old and new root paths
oldSrcPkgRootName = "${srcPkgRootSplit.join("/")}/${oldSrcPkgSplit[i]}"
newSrcPkgRootName = "${srcPkgRootSplit.join("/")}/${newSrcPkgSplit[i]}"
break
}
}
if (newSrcPkgRootName == oldSrcPkgRootName) {
logger.error("Error searching for divergence of '${newSrcPkgName}' and "
+ "'${oldSrcPkgName}'")
return
} else {
println("Renaming source package from '${oldSrcPkgName}' to '${newSrcPkgName}'")
// Perform the operation
subprojects {
// Delete the new src root, if it exists
file(newSrcPkgRootName).deleteDir()
// Copy the files from old to new
copy {
from file(oldSrcPkgName)
into file(newSrcPkgName)
}
// Delete the old src root
file(oldSrcPkgRootName).deleteDir()
}
}
}
// Rename the assets directory
final oldAssetsDirName = "common/src/main/resources/assets/${mod_id}"
final newAssetsDirName = "common/src/main/resources/assets/${props.get(mod_id)}"
if (file(newAssetsDirName) == file(oldAssetsDirName)) {
println("New and old assets directories are equal, skipping rename")
} else {
println("Renaming assets directory from '${oldAssetsDirName}' to '${newAssetsDirName}'")
// Delete the new assets dir, if it exists
file(newAssetsDirName).deleteDir()
// Copy the files from old to new
copy {
from file(oldAssetsDirName)
into file(newAssetsDirName)
}
// Delete the old assets dir
file(oldAssetsDirName).deleteDir()
}
// Rename all files
file(".").eachFileRecurse { f ->
// Filter
if (f.isFile()
&& (f.relativePath(project.rootDir) == ".." || f.path.contains("src${sep}main"))
) {
logger.error(f.path)
// Iterate over all properties
String fileName = f.name
props.each { key, value ->
if (file(key) != file(value) && fileName.contains(key.toString())) {
fileName = fileName.replaceAll(key.toString(), value.toString())
}
}
// Rename file
try {
println("Renaming file '${f.name}' to '${fileName}'")
Files.move(
f.toPath(),
new File(f.parentFile, fileName).toPath(),
StandardCopyOption.REPLACE_EXISTING
)
} catch (IOException ex) {
logger.error("Error renaming file '${f.path}': ${ex}")
ex.printStackTrace()
}
}
}
// Edit file contents
file(".").eachFileRecurse { f ->
// Filter
if (f.isFile()
&& (f.relativePath(project.rootDir) == ".." || f.path.contains("src${sep}main"))
) {
try {
// Iterate over all properties
String contents = f.text
props.each { key, value ->
if (key != value) {
// Replace all occurrences in the contents
final matcher = Pattern.compile(Pattern.quote(key.toString()))
.matcher(contents)
final count = matcher.count
if (count > 0) {
println("Replacing ${count} instance(s) of '${key}' with "
+ "'${value}' in file '${f.name}'")
contents = matcher.replaceAll(value.toString())
}
}
}
// Write the altered contents
f.withWriter("UTF-8") { writer -> writer::write(contents) }
} catch (IOException ex) {
logger.error("Error updating contents of file '${f.path}': ${ex}")
ex.printStackTrace()
}
}
}
// Delete template-only files
final deleteFiles = [
"template-changelog.md"
]
deleteFiles.each {
try {
file(it).delete()
} catch (IOException ex) {
logger.error("Error deleting '${it}': ${ex}")
ex.printStackTrace()
}
}
}
println("Task '${it.name}' finished")
}