-
Notifications
You must be signed in to change notification settings - Fork 0
/
stonecutter.gradle.kts
112 lines (99 loc) · 4.19 KB
/
stonecutter.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
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.*
import org.gradle.configurationcache.extensions.capitalized
import java.net.URI
import java.net.http.HttpClient
import java.net.http.HttpRequest
import java.net.http.HttpResponse
import java.time.Instant
plugins {
id("dev.kikugie.stonecutter")
}
stonecutter active "1.21-fabric" /* [SC] DO NOT EDIT */
stonecutter parameters {
val platform = node!!.property("loom.platform")
val platforms = listOf("fabric", "neoforge").map { it to (platform == it) }
consts(platforms)
}
stonecutter registerChiseled tasks.register("buildAllVersions", stonecutter.chiseled) {
group = "mod"
ofTask("build")
}
stonecutter registerChiseled tasks.register("releaseAllVersions", stonecutter.chiseled) {
group = "mod"
ofTask("releaseMod")
}
private data class Field(val name: String, val value: String, val inline: Boolean)
private data class Embed(
val title: String, val description: String, val timestamp: String, val color: Int, val fields: List<Field>
)
private data class DiscordWebhook(
val username: String, val avatarUrl: String, val embeds: List<Embed>
)
val majorVersion = property("mod.major-version").toString()
val betaVersion = property("mod.beta").toString().toInt()
val slug = property("mod.slug").toString()
val repo = property("mod.repo").toString()
val avatar = property("mod.icon-url").toString()
val color = property("mod.color").toString().toInt()
val supportedLoaders = property("mod.supported-loaders").toString().split(',').map { it.replaceFirstChar { it.uppercase() } }
tasks.register("postUpdate") {
group = "mod"
val featureVersion = "$majorVersion${if (betaVersion != 0) "-beta$betaVersion" else ""}"
val url = providers.environmentVariable("DISCORD_WEBHOOK").orNull ?: return@register
val roleId = providers.environmentVariable("DISCORD_ROLE_ID").orNull ?: return@register
val changelogText = rootProject.file("changelog.md").readText()
val webhook = DiscordWebhook(
username = "${rootProject.name} Release Notifier", avatarUrl = avatar, embeds = listOf(
Embed(
title = "v$featureVersion of ${rootProject.name} released!",
description = "# Changelog\n$changelogText",
timestamp = Instant.now().toString(),
color = color,
fields = listOf(
Field(
"Supported versions", stonecutter.tree.nodes.map { it.property("vers.supportedMcVersions").toString().split(',') }
.flatten().toSet().joinToString(), false
),
Field(
"Supported loaders", supportedLoaders.joinToString(), false
),
Field("Modrinth", "https://modrinth.com/mod/$slug", true),
Field("GitHub", "https://github.com/$repo", true)
)
)
)
)
@OptIn(ExperimentalSerializationApi::class)
val embedsJson = buildJsonArray {
webhook.embeds.map { embed ->
add(buildJsonObject {
put("title", embed.title)
put("description", embed.description)
put("timestamp", embed.timestamp)
put("color", embed.color)
putJsonArray("fields") {
addAll(embed.fields.map { field ->
buildJsonObject {
put("name", field.name)
put("value", field.value)
put("inline", field.inline)
}
})
}
})
}
}
val json = buildJsonObject {
put("username", webhook.username)
put("avatar_url", webhook.avatarUrl)
put("content", "<@&$roleId>")
put("embeds", embedsJson)
}
val jsonString = Json.encodeToString(json)
HttpClient.newHttpClient().send(
HttpRequest.newBuilder(URI.create(url)).header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(jsonString)).build(), HttpResponse.BodyHandlers.ofString()
)
}