forked from TerraformersMC/GradleScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ferry.gradle
350 lines (317 loc) · 11.9 KB
/
ferry.gradle
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
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "net.dumbcode.gradlehook:GradleHook:1.3.1"
classpath "gradle.plugin.com.matthewprenger:CurseGradle:1.4.0"
classpath "org.kohsuke:github-api:1.114"
classpath "org.ajoberstar.grgit:grgit-gradle:4.1.1"
classpath "gradle.plugin.com.modrinth.minotaur:Minotaur:1.1.0"
}
}
apply plugin: net.dumbcode.gradlehook.GradleWebhookPlugin
apply plugin: com.matthewprenger.cursegradle.CurseGradlePlugin
apply plugin: org.ajoberstar.grgit.gradle.GrgitPlugin
apply plugin: com.modrinth.minotaur.Minotaur
apply plugin: 'maven-publish'
apply from: 'https://raw.githubusercontent.com/jackassmc/GradleScripts/2.0/fabric-mod.gradle'
version = getVersion()
ext.curseReleaseType = "release"
ext.releaseChangelog = "No Changelog Available"
ext.stabilitySuffix = ""
ext.curseforgeDownloadLink = null
ext.modrinthDownloadLink = null
ext.githubDownloadLink = null
ext.releaseType = project.default_release_type
task generateChangelog {
def changes = StringBuilder.newInstance()
if (!project.hasProperty("lastTag") || !project.hasProperty("currentTag")) {
println "Missing lastTag or currentTag parameters, aborting changelog creation"
return;
}
def lastTag = project.getProperty("lastTag")
def currentTag = project.getProperty("currentTag")
def commits = "git log --max-count=$project.changelog_max_commit_search --pretty=format:\"%b\" $lastTag..$currentTag".execute()
println "Last version: $lastTag"
println "Current version: $currentTag"
if (currentTag.contains("-alpha")) {
releaseType = "alpha"
curseReleaseType = "alpha"
stabilitySuffix = " (Alpha)"
} else if (currentTag.contains("-beta")) {
releaseType = "beta"
curseReleaseType = "beta"
stabilitySuffix = " (Beta)"
} else if (currentTag.contains("-pre")) {
releaseType = "pre"
curseReleaseType = "beta"
stabilitySuffix = " (Pre-Release)"
} else if (currentTag.contains("-rc")) {
releaseType = "rc"
curseReleaseType = "beta"
stabilitySuffix = " (Release Candidate)"
} else if (!currentTag.contains("-")) {
releaseType = "stable"
}
println "Release type: $releaseType"
commits.in.eachLine { line -> // Loops over the lines the git log command returns
def processedLine = line.toString()
if (processedLine.startsWith("\"")) {
processedLine = processedLine.substring(1)
}
if (processedLine.endsWith("\"")) {
processedLine = processedLine.substring(0, processedLine.length() - 1)
}
println "Reading line: $processedLine"
if (processedLine.startsWith("- ")) {
println "Adding changelog line:"
println " $processedLine"
if (changes.length() == 0) {
changes << processedLine
} else {
changes << "\n$processedLine"
}
}
}
commits.err.eachLine { line -> println line }
commits.waitFor()
println "Changelog:"
releaseChangelog = changes.toString()
if (releaseChangelog.isEmpty()) {
releaseChangelog = "No Changelog Available"
}
println releaseChangelog
}
processResources {
inputs.property "version", version
filesMatching("fabric.mod.json") {
expand "version": project.version
}
}
curseforge {
if (System.getenv().CURSEFORGE_TOKEN) {
apiKey = System.getenv().CURSEFORGE_TOKEN
} else {
println "No CURSEFORGE_TOKEN specified"
}
project {
id = project.curseforge_id
releaseType = curseReleaseType
project.curseforge_game_versions.split(", ").each {
String gameVersion -> addGameVersion gameVersion
}
changelog = releaseChangelog
mainArtifact(file("${project.buildDir}/libs/${archivesBaseName}-${version}.jar"))
mainArtifact.displayName = "$project.project_name v$version for $project.minecraft_version"
if (!project.curseforge_required_dependencies.isEmpty() || !project.curseforge_optional_dependencies.isEmpty()) {
relations {
if (!project.curseforge_required_dependencies.isEmpty()) {
project.curseforge_required_dependencies.split(", ").each {
String dep -> requiredDependency dep
}
}
if (!project.curseforge_optional_dependencies.isEmpty()) {
project.curseforge_optional_dependencies.split(", ").each {
String dep -> optionalDependency dep
}
}
}
}
afterEvaluate {
uploadTask.dependsOn(remapJar)
}
}
options {
forgeGradleIntegration = false
}
}
tasks.getByName("curseforge").doLast {
def fileId = (tasks.getByName("curseforge$curseforge_id").property("mainArtifact"))['fileID']
curseforgeDownloadLink = "https://www.curseforge.com/minecraft/mc-mods/$project.curseforge_slug/files/$fileId";
sendDiscordWebhook()
}
task github {
onlyIf {
System.getenv().GITHUB_TOKEN
}
doLast {
def github = org.kohsuke.github.GitHub.connectUsingOAuth(System.getenv().GITHUB_TOKEN as String)
def repository = github.getRepository(System.getenv().GITHUB_REPOSITORY)
def releaseBuilder = new org.kohsuke.github.GHReleaseBuilder(repository, project.getProperty("currentTag"))
releaseBuilder.name("$project.project_name v$version for $project.minecraft_version")
releaseBuilder.body(releaseChangelog)
releaseBuilder.commitish(getBranch())
if (releaseType != "stable") {
releaseBuilder.prerelease(true)
}
def ghRelease = releaseBuilder.create()
ghRelease.uploadAsset(file("${project.buildDir}/libs/${archivesBaseName}-${version}.jar"), "application/java-archive");
githubDownloadLink = ghRelease.getHtmlUrl()
sendDiscordWebhook()
}
}
// Truncates changelogs for the Discord Webhook
ext.createDiscordChangelog = { fullChangelogUrl ->
def webhookChangelog = StringBuilder.newInstance()
def changelogLines = releaseChangelog.split("\n")
def maxLines = Integer.decode(project.discord_webhook_changelog_line_limit)
if (changelogLines.length <= maxLines) {
return releaseChangelog
} else {
def lines = 0
changelogLines.find { line ->
if (webhookChangelog.length() == 0) {
webhookChangelog << line
} else {
webhookChangelog << "\n$line"
}
lines++
if (lines >= maxLines) {
webhookChangelog << "\n(+ " + (changelogLines.length - lines) + " more) See [Full Changelog]($fullChangelogUrl)"
return true;
}
}
}
return webhookChangelog.toString()
}
void sendDiscordWebhook() {
println "CurseForge download: $curseforgeDownloadLink"
println "Modrinth download: $modrinthDownloadLink"
println "GitHub download: $githubDownloadLink"
if (curseforgeDownloadLink != null && modrinthDownloadLink != null && githubDownloadLink != null && System.getenv().DISCORD_ANNOUNCEMENT_WEBHOOK) {
println "Preparing webhook"
def release_url = curseforgeDownloadLink
def discordChangelog = createDiscordChangelog(release_url)
def result = DiscordBuilder.createForm {
if (project.use_project_username.toBoolean()) {
avatar_url = project.project_logo
username = project.project_name
}
embed {
color = Integer.decode(project.project_color)
author {
name = project.project_name + stabilitySuffix
url = project.project_url
}
thumbnail { url = project.project_logo }
title = "$project.project_name v$version for $project.minecraft_version Released"
url = release_url
field {
name = "Changes:"
value = discordChangelog
}
field {
name = "Downloads:"
value = "[$curseforge_emote CurseForge]($curseforgeDownloadLink)\n[$modrinth_emote Modrinth]($modrinthDownloadLink)\n[$github_emote GitHub]($githubDownloadLink)"
}
footer {
text = "A $project.loader_name Mod"
icon_url = project.loader_icon
}
}
}.send(System.getenv().DISCORD_ANNOUNCEMENT_WEBHOOK)
println "Discord Webhook Response: " + result.responseCode
}
}
String getBranch() {
def ENV = System.getenv()
if (ENV.GITHUB_REF) {
def branch = ENV.GITHUB_REF
return branch.substring(branch.lastIndexOf("/") + 1)
}
if (grgit == null) {
return "unknown"
}
def branch = grgit.branch.current().name
return branch.substring(branch.lastIndexOf("/") + 1)
}
task publishModrinth(type: com.modrinth.minotaur.TaskModrinthUpload) {
token = System.getenv().MODRINTH_TOKEN
projectId = project.modrinth_id
versionNumber = project.version
versionName = "$project.project_name v$version for $project.minecraft_version"
uploadFile = file("${project.buildDir}/libs/${archivesBaseName}-${version}.jar")
changelog = releaseChangelog
releaseType = curseReleaseType
project.modrinth_game_versions.split(", ").each {
String gameVersion -> addGameVersion gameVersion
}
project.modrinth_mod_loaders.split(", ").each {
String modLoader -> addLoader modLoader
}
doLast {
if (wasUploadSuccessful()) {
modrinthDownloadLink = "https://modrinth.com/mod/$project.modrinth_slug/version/$uploadInfo.id"
sendDiscordWebhook()
}
}
}
String getVersion() {
def version = "unknown"
if (project.hasProperty("currentTag")) {
return project.currentTag.replace("v", "")
}
def branchLines = "git branch --show-current".execute().in.readLines()
if (!branchLines.isEmpty()) {
version = branchLines.get(0)
}
def tagLines = "git describe --tags --exact-match --abbrev=0".execute().in.readLines()
def release = false
if (!tagLines.isEmpty()) {
def line = tagLines.get(0)
def regex = /v([0-9].[0-9].[0-9])/
if (line.matches(regex)) {
version = (line =~ regex)[0][1] // gets 1st match group of 1st match
release = true
}
}
if (!release) {
def lastTag = "git describe --tags --abbrev=0".execute().in.readLines()
def lastCommit = "git rev-parse --short HEAD".execute().in.readLines()
if (!lastTag.isEmpty() && !lastCommit.isEmpty()) {
version = lastTag.get(0) + "+" + version + "." + lastCommit.get(0)
}
}
return version
}
ext.mod = { String localOverrideKey, Object dep ->
File file = file("../.${localOverrideKey}-local");
if (file.exists()) {
dependencies.implementation(dep)
} else {
dependencies.modImplementation(dep)
}
}
ext.includeMod = { String localOverrideKey, Object dep ->
mod(localOverrideKey, dep)
dependencies.include(dep)
}
publishing {
publications {
mavenJava(MavenPublication) {
artifact(sourcesJar) {
builtBy remapSourcesJar
}
afterEvaluate {
artifact remapJar
}
}
}
setupRepositories(repositories)
}
void setupRepositories(RepositoryHandler repositories) {
//repositories.mavenLocal() // uncomment for testing
def ENV = System.getenv()
if (ENV.MAVEN_URL) {
repositories.maven {
url ENV.MAVEN_URL
credentials {
username ENV.MAVEN_USERNAME
password ENV.MAVEN_PASSWORD
}
}
}
}