-
Notifications
You must be signed in to change notification settings - Fork 4
/
stonecutter.gradle.kts
120 lines (106 loc) · 4.23 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
113
114
115
116
117
118
119
120
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.*
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" /* [SC] DO NOT EDIT */
stonecutter parameters {
val needsWorldNow = eval(node!!.name, ">=1.21.2")
const("needsWorldNow", needsWorldNow)
swaps["serverLevel"] = if (needsWorldNow) "ServerLevel level," else ""
}
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>
)
tasks.register("postUpdate") {
group = "mod"
val version = project(stonecutter.versions.first().project).version.toString()
val hyphenCount = version.count { it == '-' }
val featureVersion = when (hyphenCount) {
1 -> version.split("-").first()
2 -> {
val split = version.split("-")
if (split.last().contains("rc") || split.last().contains("pre")) split.first()
else "${split.first()}-${split[1]}"
}
3 -> {
val split = version.split("-")
"${split.first()}-${split[1]}"
}
else -> return@register
}
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 = "https://raw.githubusercontent.com/btwonion/magnetic/master/src/main/resources/assets/magnetic/icon.png",
embeds = listOf(
Embed(
title = "v$featureVersion of ${rootProject.name} released!",
description = "# Changelog\n$changelogText",
timestamp = Instant.now().toString(),
color = 0xff0080,
fields = listOf(
Field(
"Supported versions",
stonecutter.tree.nodes.map { it.property("supportedMcVersions").toString().split(',') }
.flatten().toSet().joinToString(),
false
),
Field("Modrinth", "https://modrinth.com/mod/magnetic", true),
Field("GitHub", "https://github.com/btwonion/magnetic", 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()
)
}