-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsettings.gradle.kts
More file actions
335 lines (269 loc) · 8.24 KB
/
Copy pathsettings.gradle.kts
File metadata and controls
335 lines (269 loc) · 8.24 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
/* Copyright 2023 Ampflower
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import com.unascribed.flexver.FlexVerComparator
import java.io.BufferedReader
import java.io.Writer
import java.nio.file.Files
import java.nio.file.attribute.FileTime
import java.util.Properties
rootProject.name = "music-moods"
pluginManagement {
repositories {
maven("https://maven.fabricmc.net/") { name = "FabricMC" }
maven("https://maven.neoforged.net/releases/") { name = "Neoforged" }
maven("https://maven.architectury.dev/") { name = "Architectury" }
gradlePluginPortal()
maven("https://maven.kikugie.dev/snapshots") { name = "KikuGie Snapshots" }
}
}
plugins {
id("dev.kikugie.stonecutter") version "0.7.10"
}
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("com.unascribed:flexver-java:1.1.1")
}
}
include("witch")
val versions = Versions(file("versions.ini").toIni())
val gradleProperties = file("gradle.properties").toProperties()
dependencyResolutionManagement.versionCatalogs {
create("libs") {
versions.resolvePlugins(this) { throw NoSuchElementException("version $it") }
versions.resolveVersions(this, null) {}
for ((alias, version) in versions.versions) {
version(alias, version)
}
}
}
stonecutter {
create(rootProject) {
for ((loader, ini) in versions.cutters) {
for (k in ini.sections.keys) {
version("$loader-$k", k).apply {
buildscript = ini.header["buildscript"] as? String ?: "$loader.gradle.kts"
}
}
}
vcsVersion = gradleProperties.getProperty("vcs_version")!!
}
}
val stonecutterRoot = file("versions")
for ((loader, ini) in versions.cutters) {
val stonecutterLastModified = maxOf(
Files.getLastModifiedTime(file("$loader.ini").toPath())!!,
Files.getLastModifiedTime(file("versions.ini").toPath())!!,
)
val yellLoader = loader.toYellingSnake()
for ((k, v) in ini.sections) {
val root = stonecutterRoot.resolve("$loader-$k")
root.mkdirs()
val gradle = Properties()
gradle.putAll(ini.header)
gradle.putAll(v)
gradle.put("series", loader)
root.resolve("gradle.properties")
.updateOnMismatch(gradle, stonecutterLastModified, "== DO NOT MODIFY ==\n\nSee versions.ini instead")
val buildProperties = Properties()
for ((k, v) in ini.header) {
buildProperties[(k as String).toYellingSnake()] = v
}
for ((k, v) in v) {
buildProperties[(k as String).toYellingSnake()] = v
}
if (!buildProperties.containsKey(yellLoader)) {
buildProperties[yellLoader] = "true"
}
vcomp(buildProperties, k, versions.splices)
vcomp(buildProperties, k, ini.sections.keys, loader.toYellingSnake())
root.resolve("build.properties")
.updateOnMismatch(buildProperties, stonecutterLastModified)
}
}
fun vcomp(
buildProperties: Properties,
reference: String,
splices: Set<String>,
prefix: String = "MC",
) {
buildProperties["${prefix}_${reference.toYellingSnake()}"] = "true"
for (v in splices) {
val c = FlexVerComparator.compare(reference, v)
val snake = v.toYellingSnake()
if (c <= 0) {
buildProperties["${prefix}_${snake}_OR_OLDER"] = "true"
}
if (c >= 0) {
buildProperties["${prefix}_${snake}_OR_NEWER"] = "true"
}
}
}
class Versions(
val ini: Ini,
) {
val cutters: Map<String, Ini>
val splices: Set<String>
val versions: Map<String, String> = HashMap<String, String>().apply {
ini.sections["versions"]?.let {
it.forEach { alias, value -> this[alias as String] = value as String }
}
}
init {
val cutters = HashMap<String, Ini>()
val splices = HashSet<String>()
for (k in HashSet<String>().apply { addAll(ini.sections.keys); removeAll(reservedSections) }) {
if (k.contains('.')) {
continue
}
val ini = file("$k.ini").toIni()
cutters[k] = ini
splices.addAll(ini.sections.keys)
}
this.cutters = cutters
this.splices = splices
}
fun resolveVersions(
properties: Properties?,
missingVersion: (version: String) -> Unit,
action: (alias: String, module: String, version: String) -> Unit
) {
for ((alias, value) in properties ?: return) {
value as String
val colon = value.lastIndexOf(':')
val version = when {
value[colon + 1] == '$' -> gradleProperties[value.substring(colon + 2)]
value[colon + 1].isDigit() -> value.substring(colon + 1)
else -> versions[value.substring(colon + 1)]
} as? String
if (version == null) {
missingVersion(value.substring(colon + 1))
continue
}
action(
alias as String,
value.substring(0, colon),
version
)
}
}
fun resolvePlugins(
builder: VersionCatalogBuilder,
missingVersion: (version: String) -> Unit,
) {
resolveVersions(ini.sections["plugins"], missingVersion) { alias, module, version ->
logger.info("{} => {}:{}", alias, module, version)
builder.plugin(alias, module).version(version)
}
}
fun resolveVersions(
builder: VersionCatalogBuilder,
section: String?,
missingVersion: (version: String) -> Unit,
) {
val properties = if (section == null) {
ini.header
} else {
ini.sections[section] ?: throw NoSuchElementException("not found: $section")
}
resolveVersions(properties, missingVersion) { alias, module, version ->
logger.info("{} => {}:{}", alias, module, version)
val value = module.split(':')
builder.library(alias, value[0], value[1]).version(version)
}
}
companion object {
val reservedSections = setOf("plugins", "versions", "dependencies", "repositories")
}
}
fun File.updateOnMismatch(expected: Properties, lastModified: FileTime, message: String? = null) {
whenMismatch(expected, lastModified) { expected.store(it, message) }
}
fun File.whenMismatch(expected: Properties, lastModified: FileTime, action: (Writer) -> Unit) {
if (matches(this, expected, lastModified)) {
return
}
this.bufferedWriter().use(action)
}
fun matches(properties: File, expected: Properties, lastModified: FileTime): Boolean {
if (!properties.exists()) {
return false
}
val propertiesPath = properties.toPath()
// Microcache
if (Files.getLastModifiedTime(propertiesPath) >= lastModified) {
return true
}
val temp = Properties()
properties.bufferedReader().use(temp::load)
// I'd hate to do needless writes on SSDs.
if (temp == expected) {
Files.setLastModifiedTime(propertiesPath, lastModified)
return true
}
return false
}
fun String.toYellingSnake(): String {
val builder = StringBuilder()
for (i in indices) {
val char = this[i]
if (char.isUpperCase() && i != 0) {
builder.append('_')
}
builder.append(
when (char) {
'.', ',', '-' -> '_'
else -> char.uppercaseChar()
}
)
}
return builder.toString()
}
// Pretty much a Windows-spec ini parser with the concept of a null header section.
// Why here? There's not a buildSrc for settings.gradle.kts, so this is the best place to shove this,
// outside of making a plugin.
data class Ini(val header: Properties, val sections: HashMap<String, Properties>) {
companion object {
fun read(reader: BufferedReader): Ini {
val sections = HashMap<String, Properties>()
val nullSection = Properties()
var properties = nullSection
var line: String?
while (reader.readLine().apply { line = this?.trim() } != null) {
val line = line
if (line.isNullOrEmpty()) {
continue
}
if (line.startsWith(';')) {
continue
}
if (line.startsWith('[') && line.endsWith(']')) {
val section = line.substring(1, line.length - 1)
properties = Properties()
sections.put(section, properties)
continue
}
val equals = line.indexOf('=')
if (equals >= 0) {
properties.put(
line.substring(0, equals).trimEnd(),
line.substring(equals + 1).trimStart()
)
}
}
return Ini(nullSection, sections)
}
}
fun getHeader(key: String) = header[key] as String?
fun get(section: String, key: String) = sections[section]?.get(key) as String?
}
fun File.toIni(): Ini = bufferedReader().use { Ini.read(it) }
fun java.nio.file.Path.toIni(): Ini = Files.newBufferedReader(this).use { Ini.read(it) }
fun File.toProperties(): Properties = bufferedReader().use { Properties().apply { load(it) } }
inline fun <reified K, reified V> forwardTo(src: Properties, dest: MutableMap<K, V>) =
src.forEach { k, v -> dest[k as K] = v as V }