mirror of
https://github.com/GayPizzaSpecifications/concrete.git
synced 2025-08-10 16:41:32 +00:00
Migration to Gay Pizza
This commit is contained in:
@ -0,0 +1,23 @@
|
||||
package gay.pizza.foundation.concrete
|
||||
|
||||
import org.gradle.api.Project
|
||||
import org.gradle.kotlin.dsl.repositories
|
||||
import java.net.URI
|
||||
|
||||
open class ConcreteBaseBukkitPlugin : ConcreteBasePlugin() {
|
||||
override fun apply(project: Project) {
|
||||
super.apply(project)
|
||||
|
||||
project.repositories {
|
||||
maven {
|
||||
name = "papermc"
|
||||
url = URI.create("https://papermc.io/repo/repository/maven-public/")
|
||||
}
|
||||
}
|
||||
|
||||
project.afterEvaluate {
|
||||
val paperApiVersion = project.concreteRootExtension.paperApiVersion.get()
|
||||
project.dependencies.add("compileOnly", "io.papermc.paper:paper-api:${paperApiVersion}")
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
package gay.pizza.foundation.concrete
|
||||
|
||||
import org.gradle.api.JavaVersion
|
||||
import org.gradle.api.Plugin
|
||||
import org.gradle.api.Project
|
||||
import org.gradle.api.plugins.JavaPluginExtension
|
||||
import org.gradle.kotlin.dsl.getByType
|
||||
import org.gradle.kotlin.dsl.withType
|
||||
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
|
||||
|
||||
open class ConcreteBasePlugin : Plugin<Project> {
|
||||
override fun apply(project: Project) {
|
||||
val versionWithBuild = if (System.getenv("CI_PIPELINE_IID") != null) {
|
||||
project.rootProject.version.toString() + ".${System.getenv("CI_PIPELINE_IID")}"
|
||||
} else {
|
||||
"DEV"
|
||||
}
|
||||
|
||||
if (project.repositories.none { it.name == "MavenRepo" }) {
|
||||
project.repositories.mavenCentral()
|
||||
}
|
||||
|
||||
project.version = versionWithBuild
|
||||
|
||||
project.plugins.apply("org.jetbrains.kotlin.jvm")
|
||||
project.plugins.apply("org.jetbrains.kotlin.plugin.serialization")
|
||||
|
||||
project.extensions.getByType<JavaPluginExtension>().apply {
|
||||
val javaVersion = JavaVersion.toVersion(17)
|
||||
sourceCompatibility = javaVersion
|
||||
targetCompatibility = javaVersion
|
||||
}
|
||||
|
||||
project.tasks.withType<KotlinCompile>().forEach {
|
||||
it.apply {
|
||||
kotlinOptions.apply {
|
||||
jvmTarget = "17"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package gay.pizza.foundation.concrete
|
||||
|
||||
import org.gradle.api.provider.Property
|
||||
|
||||
interface ConcreteExtension {
|
||||
val paperVersionGroup: Property<String>
|
||||
val paperApiVersion: Property<String>
|
||||
val minecraftServerPath: Property<String>
|
||||
val acceptServerEula: Property<Boolean>
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
package gay.pizza.foundation.concrete
|
||||
|
||||
class ConcreteLibraryPlugin : ConcreteBaseBukkitPlugin()
|
@ -0,0 +1,31 @@
|
||||
package gay.pizza.foundation.concrete
|
||||
|
||||
import org.gradle.api.Project
|
||||
import org.gradle.kotlin.dsl.get
|
||||
import org.gradle.language.jvm.tasks.ProcessResources
|
||||
|
||||
@Suppress("UnstableApiUsage")
|
||||
class ConcretePluginPlugin : ConcreteBaseBukkitPlugin() {
|
||||
override fun apply(project: Project) {
|
||||
super.apply(project)
|
||||
|
||||
project.plugins.apply("com.github.johnrengelman.shadow")
|
||||
|
||||
project.tasks.find<ProcessResources>("processResources")!!.apply {
|
||||
val props = mapOf("version" to project.version.toString())
|
||||
inputs.properties(props)
|
||||
filteringCharset = "UTF-8"
|
||||
filesMatching("plugin.yml") {
|
||||
expand(props)
|
||||
}
|
||||
}
|
||||
|
||||
project.shadowJarTask!!.apply {
|
||||
archiveClassifier.set("plugin")
|
||||
}
|
||||
|
||||
project.tasks.addTaskDependency("assemble", "shadowJar")
|
||||
|
||||
project.concreteRootProject.tasks["setupPaperServer"].dependsOn(project.tasks["shadowJar"])
|
||||
}
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
package gay.pizza.foundation.concrete
|
||||
|
||||
import org.gradle.api.Plugin
|
||||
import org.gradle.api.Project
|
||||
import org.gradle.kotlin.dsl.apply
|
||||
import org.gradle.kotlin.dsl.create
|
||||
|
||||
class ConcreteRootPlugin : Plugin<Project> {
|
||||
override fun apply(project: Project) {
|
||||
project.apply(plugin = "base")
|
||||
project.extensions.create<ConcreteExtension>("concrete")
|
||||
val setupPaperServer = project.tasks.create<SetupPaperServer>("setupPaperServer")
|
||||
val runPaperServer = project.tasks.create<RunPaperServer>("runPaperServer")
|
||||
runPaperServer.dependsOn(setupPaperServer)
|
||||
|
||||
val updateManifests = project.tasks.create<UpdateManifestTask>("updateManifests")
|
||||
project.tasks.getByName("assemble").dependsOn(updateManifests)
|
||||
}
|
||||
}
|
7
src/main/kotlin/gay/pizza/foundation/concrete/Globals.kt
Normal file
7
src/main/kotlin/gay/pizza/foundation/concrete/Globals.kt
Normal file
@ -0,0 +1,7 @@
|
||||
package gay.pizza.foundation.concrete
|
||||
|
||||
import com.google.gson.Gson
|
||||
|
||||
object Globals {
|
||||
val gson = Gson()
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
package gay.pizza.foundation.concrete
|
||||
|
||||
import com.google.gson.Gson
|
||||
import java.net.URI
|
||||
import java.net.http.HttpClient
|
||||
import java.net.http.HttpRequest
|
||||
import java.net.http.HttpResponse
|
||||
|
||||
class PaperVersionClient(
|
||||
private val client: HttpClient = HttpClient.newHttpClient(),
|
||||
private val gson: Gson = Globals.gson
|
||||
) {
|
||||
private val apiBaseUrl = URI.create("https://papermc.io/api/v2/")
|
||||
|
||||
fun getVersionBuilds(group: String): List<PaperBuild> {
|
||||
val response = client.send(
|
||||
HttpRequest.newBuilder()
|
||||
.GET()
|
||||
.uri(apiBaseUrl.resolve("projects/paper/version_group/${group}/builds"))
|
||||
.build(),
|
||||
HttpResponse.BodyHandlers.ofString()
|
||||
)
|
||||
|
||||
val body = response.body()
|
||||
val root = gson.fromJson(body, PaperVersionRoot::class.java)
|
||||
return root.builds
|
||||
}
|
||||
|
||||
fun resolveDownloadUrl(build: PaperBuild, download: PaperVersionDownload): URI =
|
||||
apiBaseUrl.resolve("projects/paper/versions/${build.version}/builds/${build.build}/downloads/${download.name}")
|
||||
|
||||
data class PaperVersionRoot(
|
||||
val builds: List<PaperBuild>
|
||||
)
|
||||
|
||||
data class PaperBuild(
|
||||
val version: String,
|
||||
val build: Int,
|
||||
val downloads: Map<String, PaperVersionDownload>
|
||||
)
|
||||
|
||||
data class PaperVersionDownload(
|
||||
val name: String,
|
||||
val sha256: String
|
||||
)
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
package gay.pizza.foundation.concrete
|
||||
|
||||
import org.gradle.api.DefaultTask
|
||||
import org.gradle.api.tasks.Input
|
||||
import org.gradle.api.tasks.TaskAction
|
||||
import org.gradle.kotlin.dsl.getByType
|
||||
import java.io.File
|
||||
import java.util.jar.JarFile
|
||||
|
||||
open class RunPaperServer : DefaultTask() {
|
||||
init {
|
||||
outputs.upToDateWhen { false }
|
||||
}
|
||||
|
||||
@get:Input
|
||||
var additionalServerArguments = mutableListOf<String>()
|
||||
|
||||
@get:Input
|
||||
var disableServerGui = true
|
||||
|
||||
@TaskAction
|
||||
fun runPaperServer() {
|
||||
val concrete = project.extensions.getByType<ConcreteExtension>()
|
||||
|
||||
val minecraftServerDirectory = project.file(concrete.minecraftServerPath.get())
|
||||
val paperJarFile = minecraftServerDirectory.resolve("paper.jar")
|
||||
val mainClassName = readMainClass(paperJarFile)
|
||||
|
||||
project.javaexec {
|
||||
classpath(paperJarFile.absolutePath)
|
||||
workingDir(minecraftServerDirectory)
|
||||
|
||||
val allServerArguments = mutableListOf<String>()
|
||||
allServerArguments.addAll(additionalServerArguments)
|
||||
if (disableServerGui) {
|
||||
allServerArguments.add("nogui")
|
||||
}
|
||||
|
||||
args(allServerArguments)
|
||||
mainClass.set(mainClassName)
|
||||
}
|
||||
}
|
||||
|
||||
private fun readMainClass(file: File): String = JarFile(file).use { jar ->
|
||||
jar.manifest.mainAttributes.getValue("Main-Class")!!
|
||||
}
|
||||
}
|
@ -0,0 +1,80 @@
|
||||
package gay.pizza.foundation.concrete
|
||||
|
||||
import org.gradle.api.DefaultTask
|
||||
import org.gradle.api.tasks.Input
|
||||
import org.gradle.api.tasks.TaskAction
|
||||
import org.gradle.api.tasks.options.Option
|
||||
import org.gradle.kotlin.dsl.getByType
|
||||
import java.io.File
|
||||
import java.nio.file.Files
|
||||
import java.util.Properties
|
||||
|
||||
open class SetupPaperServer : DefaultTask() {
|
||||
init {
|
||||
outputs.upToDateWhen { false }
|
||||
}
|
||||
|
||||
@get:Input
|
||||
@set:Option(option = "update", description = "Update Paper Server")
|
||||
var shouldUpdatePaperServer = true
|
||||
|
||||
private val paperVersionClient = PaperVersionClient()
|
||||
|
||||
@TaskAction
|
||||
fun downloadPaperTask() {
|
||||
val concrete = project.extensions.getByType<ConcreteExtension>()
|
||||
val minecraftServerDirectory = project.file(concrete.minecraftServerPath.get())
|
||||
|
||||
if (!minecraftServerDirectory.exists()) {
|
||||
minecraftServerDirectory.mkdirs()
|
||||
}
|
||||
|
||||
val paperJarFile = project.file("${concrete.minecraftServerPath.get()}/paper.jar")
|
||||
if (!paperJarFile.exists() || shouldUpdatePaperServer) {
|
||||
downloadLatestBuild(concrete.paperVersionGroup.get(), paperJarFile)
|
||||
}
|
||||
|
||||
val paperPluginsDirectory = minecraftServerDirectory.resolve("plugins")
|
||||
|
||||
if (!paperPluginsDirectory.exists()) {
|
||||
paperPluginsDirectory.mkdirs()
|
||||
}
|
||||
|
||||
for (project in project.findPluginProjects()) {
|
||||
val task = project.shadowJarTask!!
|
||||
val pluginJarFile = task.outputs.files.first()
|
||||
val pluginLinkFile = paperPluginsDirectory.resolve("${project.name}.jar")
|
||||
pluginLinkFile.delete()
|
||||
Files.createSymbolicLink(pluginLinkFile.toPath(), pluginJarFile.toPath())
|
||||
}
|
||||
|
||||
if (concrete.acceptServerEula.isPresent && concrete.acceptServerEula.get()) {
|
||||
val writer = minecraftServerDirectory.resolve("eula.txt").bufferedWriter()
|
||||
val properties = Properties()
|
||||
properties.setProperty("eula", "true")
|
||||
properties.store(writer, "Written by Concrete")
|
||||
writer.close()
|
||||
}
|
||||
}
|
||||
|
||||
private fun downloadLatestBuild(paperVersionGroup: String, paperJarFile: File) {
|
||||
if (project.gradle.startParameter.isOffline) {
|
||||
if (!paperJarFile.exists()) {
|
||||
throw RuntimeException("Offline mode is enabled and Paper has not been downloaded.")
|
||||
} else {
|
||||
logger.lifecycle("Offline mode is enabled, skipping Paper update.")
|
||||
return
|
||||
}
|
||||
}
|
||||
val builds = paperVersionClient.getVersionBuilds(paperVersionGroup)
|
||||
val build = builds.last()
|
||||
val download = build.downloads["application"]!!
|
||||
val url = paperVersionClient.resolveDownloadUrl(build, download)
|
||||
val downloader = SmartDownloader(paperJarFile.toPath(), url, download.sha256)
|
||||
if (downloader.download()) {
|
||||
logger.lifecycle("Installed Paper Server ${build.version} build ${build.build}")
|
||||
} else {
|
||||
logger.lifecycle("Paper Server ${build.version} build ${build.build} is up-to-date")
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,78 @@
|
||||
package gay.pizza.foundation.concrete
|
||||
|
||||
import java.net.URI
|
||||
import java.nio.file.Files
|
||||
import java.nio.file.Path
|
||||
import java.security.MessageDigest
|
||||
|
||||
class SmartDownloader(
|
||||
private val localFilePath: Path,
|
||||
private val remoteDownloadUrl: URI,
|
||||
private val sha256: String
|
||||
) {
|
||||
fun download(): Boolean {
|
||||
val hashResult = checkLocalFileHash()
|
||||
if (hashResult != HashResult.ValidHash) {
|
||||
downloadRemoteFile()
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
private fun downloadRemoteFile() {
|
||||
val url = remoteDownloadUrl.toURL()
|
||||
val remoteFileStream = url.openStream()
|
||||
val localFileStream = Files.newOutputStream(localFilePath)
|
||||
remoteFileStream.transferTo(localFileStream)
|
||||
val hashResult = checkLocalFileHash()
|
||||
if (hashResult != HashResult.ValidHash) {
|
||||
throw RuntimeException("Download of $remoteDownloadUrl did not result in valid hash.")
|
||||
}
|
||||
}
|
||||
|
||||
private fun checkLocalFileHash(): HashResult {
|
||||
if (!Files.exists(localFilePath)) {
|
||||
return HashResult.DoesNotExist
|
||||
}
|
||||
|
||||
val digest = MessageDigest.getInstance("SHA-256")
|
||||
val localFileStream = Files.newInputStream(localFilePath)
|
||||
val buffer = ByteArray(16 * 1024)
|
||||
|
||||
while (true) {
|
||||
val size = localFileStream.read(buffer)
|
||||
if (size <= 0) {
|
||||
break
|
||||
}
|
||||
|
||||
val bytes = buffer.take(size).toByteArray()
|
||||
digest.update(bytes)
|
||||
}
|
||||
|
||||
val sha256Bytes = digest.digest()
|
||||
val localSha256Hash = bytesToHex(sha256Bytes)
|
||||
return if (localSha256Hash.equals(sha256, ignoreCase = true)) {
|
||||
HashResult.ValidHash
|
||||
} else {
|
||||
HashResult.InvalidHash
|
||||
}
|
||||
}
|
||||
|
||||
private fun bytesToHex(hash: ByteArray): String {
|
||||
val hexString = StringBuilder(2 * hash.size)
|
||||
for (i in hash.indices) {
|
||||
val hex = Integer.toHexString(0xff and hash[i].toInt())
|
||||
if (hex.length == 1) {
|
||||
hexString.append('0')
|
||||
}
|
||||
hexString.append(hex)
|
||||
}
|
||||
return hexString.toString()
|
||||
}
|
||||
|
||||
enum class HashResult {
|
||||
DoesNotExist,
|
||||
InvalidHash,
|
||||
ValidHash
|
||||
}
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
package gay.pizza.foundation.concrete
|
||||
|
||||
import org.gradle.api.DefaultTask
|
||||
import org.gradle.api.tasks.TaskAction
|
||||
import java.nio.file.Files
|
||||
import java.nio.file.Path
|
||||
|
||||
open class UpdateManifestTask : DefaultTask() {
|
||||
@TaskAction
|
||||
fun update() {
|
||||
val manifestsDir = ensureManifestsDir()
|
||||
val updateFile = manifestsDir.resolve("update.json")
|
||||
val rootPath = project.rootProject.rootDir.toPath()
|
||||
val updateManifest = project.findPluginProjects().mapNotNull { project ->
|
||||
val paths = project.shadowJarOutputs!!.allFilesRelativeToPath(rootPath)
|
||||
if (paths.isNotEmpty()) {
|
||||
project.name to mapOf(
|
||||
"version" to project.version,
|
||||
"artifacts" to paths.map { it.toUnixString() }
|
||||
)
|
||||
} else null
|
||||
}.toMap()
|
||||
|
||||
Files.writeString(updateFile, Globals.gson.toJson(updateManifest))
|
||||
}
|
||||
|
||||
private fun ensureManifestsDir(): Path {
|
||||
val manifestsDir = project.buildDir.resolve("manifests")
|
||||
manifestsDir.mkdirs()
|
||||
return manifestsDir.toPath()
|
||||
}
|
||||
}
|
70
src/main/kotlin/gay/pizza/foundation/concrete/extensions.kt
Normal file
70
src/main/kotlin/gay/pizza/foundation/concrete/extensions.kt
Normal file
@ -0,0 +1,70 @@
|
||||
package gay.pizza.foundation.concrete
|
||||
|
||||
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
|
||||
import org.gradle.api.Project
|
||||
import org.gradle.api.tasks.TaskContainer
|
||||
import org.gradle.api.tasks.TaskOutputs
|
||||
import java.nio.file.FileSystems
|
||||
import java.nio.file.Path
|
||||
|
||||
/**
|
||||
* Checks if the project has the [ConcretePluginPlugin] applied.
|
||||
*/
|
||||
internal fun Project.isPluginProject() = plugins.hasPlugin(ConcretePluginPlugin::class.java)
|
||||
|
||||
/**
|
||||
* Finds all projects in the project's hierarchy that are plugins.
|
||||
*/
|
||||
internal fun Project.findPluginProjects() = allprojects.filter { project -> project.isPluginProject() }
|
||||
|
||||
internal fun TaskContainer.addTaskDependency(dependent: String, dependency: String) {
|
||||
getByName(dependent).dependsOn(getByName(dependency))
|
||||
}
|
||||
|
||||
internal inline fun <reified T> TaskContainer.find(name: String) =
|
||||
findByName(name) as T?
|
||||
|
||||
internal val Project.shadowJarTask: ShadowJar?
|
||||
get() = project.tasks.find<ShadowJar>("shadowJar")
|
||||
|
||||
internal val Project.shadowJarOutputs: TaskOutputs?
|
||||
get() = shadowJarTask?.outputs
|
||||
|
||||
internal val Project.concreteRootExtension: ConcreteExtension
|
||||
get() = findTargetParent(
|
||||
valid = { extensions.findByType(ConcreteExtension::class.java) != null },
|
||||
extract = { extensions.findByType(ConcreteExtension::class.java)!! },
|
||||
error = { "Failed to find concrete root. Did you apply the concrete root plugin?" }
|
||||
)
|
||||
|
||||
/**
|
||||
* Finds the concrete root project, which is the first project in the project hierarchy
|
||||
* that has the concrete extension.
|
||||
*/
|
||||
internal val Project.concreteRootProject: Project
|
||||
get() = findTargetParent(
|
||||
valid = { extensions.findByType(ConcreteExtension::class.java) != null },
|
||||
extract = { this },
|
||||
error = { "Failed to find concrete root. Did you apply the concrete root plugin?" }
|
||||
)
|
||||
|
||||
/**
|
||||
* Scans a project hierarchy looking for a project matching the criteria specified in [valid].
|
||||
* If found, [extract] is called and the result is returned. If no matching project is found, [error] is called
|
||||
* and passed to RuntimeException as an error string.
|
||||
*/
|
||||
internal fun <T> Project.findTargetParent(valid: Project.() -> Boolean, extract: Project.() -> T, error: () -> String): T {
|
||||
if (valid(this)) {
|
||||
return extract(this)
|
||||
}
|
||||
|
||||
if (parent != null) {
|
||||
return parent!!.findTargetParent(valid, extract, error)
|
||||
}
|
||||
|
||||
throw RuntimeException(error())
|
||||
}
|
||||
|
||||
internal fun TaskOutputs.allFilesRelativeToPath(root: Path): List<Path> = files.map { root.relativize(it.toPath()) }
|
||||
|
||||
internal fun Path.toUnixString() = toString().replace(FileSystems.getDefault().separator, "/")
|
Reference in New Issue
Block a user