mirror of
				https://github.com/GayPizzaSpecifications/foundation.git
				synced 2025-11-04 11:39:39 +00:00 
			
		
		
		
	Refactor Manifest Generation into Gradle Plugin
This commit is contained in:
		@ -1,24 +1,18 @@
 | 
				
			|||||||
 | 
					import cloud.kubelet.foundation.gradle.FoundationProjectPlugin
 | 
				
			||||||
 | 
					import cloud.kubelet.foundation.gradle.isFoundationPlugin
 | 
				
			||||||
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
 | 
					import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
 | 
				
			||||||
import org.jetbrains.kotlin.com.google.gson.Gson
 | 
					 | 
				
			||||||
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
 | 
					import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
 | 
				
			||||||
import java.io.FileWriter
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
plugins {
 | 
					plugins {
 | 
				
			||||||
  java
 | 
					  java
 | 
				
			||||||
  id("cloud.kubelet.foundation.gradle")
 | 
					  id("cloud.kubelet.foundation.gradle")
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
fun Project.isFoundationPlugin() = name.startsWith("foundation-")
 | 
					 | 
				
			||||||
fun Project.isFoundationTool() = !isFoundationPlugin()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// Disable the JAR task for the root project.
 | 
					 | 
				
			||||||
tasks["jar"].enabled = false
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
allprojects {
 | 
					allprojects {
 | 
				
			||||||
  repositories {
 | 
					  repositories {
 | 
				
			||||||
    mavenCentral()
 | 
					    mavenCentral()
 | 
				
			||||||
    maven {
 | 
					    maven {
 | 
				
			||||||
      name = "papermc-repo"
 | 
					      name = "papermc"
 | 
				
			||||||
      url = uri("https://papermc.io/repo/repository/maven-public/")
 | 
					      url = uri("https://papermc.io/repo/repository/maven-public/")
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -29,59 +23,20 @@ allprojects {
 | 
				
			|||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
val manifestsDir = buildDir.resolve("manifests")
 | 
					 | 
				
			||||||
manifestsDir.mkdirs()
 | 
					 | 
				
			||||||
val gson = Gson()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
tasks.create("updateManifests") {
 | 
					 | 
				
			||||||
  // TODO: not using task dependencies, outputs, blah blah blah.
 | 
					 | 
				
			||||||
  doLast {
 | 
					 | 
				
			||||||
    val updateFile = manifestsDir.resolve("update.json")
 | 
					 | 
				
			||||||
    val writer = FileWriter(updateFile)
 | 
					 | 
				
			||||||
    writer.use {
 | 
					 | 
				
			||||||
      val rootPath = rootProject.rootDir.toPath()
 | 
					 | 
				
			||||||
      val updateManifest = subprojects.mapNotNull { project ->
 | 
					 | 
				
			||||||
        if (project.isFoundationTool()) {
 | 
					 | 
				
			||||||
          return@mapNotNull null
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        val files = project.tasks.getByName("shadowJar").outputs
 | 
					 | 
				
			||||||
        val paths = files.files.map { rootPath.relativize(it.toPath()).toString() }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        if (paths.isNotEmpty()) project.name to mapOf(
 | 
					 | 
				
			||||||
          "version" to project.version,
 | 
					 | 
				
			||||||
          "artifacts" to paths,
 | 
					 | 
				
			||||||
        )
 | 
					 | 
				
			||||||
        else null
 | 
					 | 
				
			||||||
      }.toMap()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
      gson.toJson(
 | 
					 | 
				
			||||||
        updateManifest,
 | 
					 | 
				
			||||||
        writer
 | 
					 | 
				
			||||||
      )
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
tasks.assemble {
 | 
					tasks.assemble {
 | 
				
			||||||
  dependsOn("updateManifests")
 | 
					  dependsOn("updateManifests")
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					version = "0.2"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
subprojects {
 | 
					subprojects {
 | 
				
			||||||
  plugins.apply("org.jetbrains.kotlin.jvm")
 | 
					  plugins.apply("org.jetbrains.kotlin.jvm")
 | 
				
			||||||
  plugins.apply("org.jetbrains.kotlin.plugin.serialization")
 | 
					  plugins.apply("org.jetbrains.kotlin.plugin.serialization")
 | 
				
			||||||
  plugins.apply("com.github.johnrengelman.shadow")
 | 
					  plugins.apply("com.github.johnrengelman.shadow")
 | 
				
			||||||
 | 
					  plugins.apply(FoundationProjectPlugin::class)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  version = "0.2"
 | 
					 | 
				
			||||||
  group = "io.gorence"
 | 
					  group = "io.gorence"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  // Add build number if running under CI.
 | 
					 | 
				
			||||||
  val versionWithBuild = if (System.getenv("CI_PIPELINE_IID") != null) {
 | 
					 | 
				
			||||||
    version as String + ".${System.getenv("CI_PIPELINE_IID")}"
 | 
					 | 
				
			||||||
  } else {
 | 
					 | 
				
			||||||
    "DEV"
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
  version = versionWithBuild
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  dependencies {
 | 
					  dependencies {
 | 
				
			||||||
    // Kotlin dependencies
 | 
					    // Kotlin dependencies
 | 
				
			||||||
    implementation(platform("org.jetbrains.kotlin:kotlin-bom"))
 | 
					    implementation(platform("org.jetbrains.kotlin:kotlin-bom"))
 | 
				
			||||||
 | 
				
			|||||||
@ -0,0 +1,7 @@
 | 
				
			|||||||
 | 
					package cloud.kubelet.foundation.gradle
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import com.google.gson.Gson
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					object FoundationGlobals {
 | 
				
			||||||
 | 
					  val gson = Gson()
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -17,5 +17,8 @@ class FoundationGradlePlugin : Plugin<Project> {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
    val runPaperServer = project.tasks.create<RunPaperServer>("runPaperServer")
 | 
					    val runPaperServer = project.tasks.create<RunPaperServer>("runPaperServer")
 | 
				
			||||||
    runPaperServer.dependsOn(setupPaperServer)
 | 
					    runPaperServer.dependsOn(setupPaperServer)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    val updateManifests = project.tasks.create<UpdateManifestTask>("updateManifests")
 | 
				
			||||||
 | 
					    project.tasks.getByName("assemble").dependsOn(updateManifests)
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -0,0 +1,16 @@
 | 
				
			|||||||
 | 
					package cloud.kubelet.foundation.gradle
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import org.gradle.api.Plugin
 | 
				
			||||||
 | 
					import org.gradle.api.Project
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class FoundationProjectPlugin : 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"
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    project.version = versionWithBuild
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -6,9 +6,11 @@ import java.net.http.HttpClient
 | 
				
			|||||||
import java.net.http.HttpRequest
 | 
					import java.net.http.HttpRequest
 | 
				
			||||||
import java.net.http.HttpResponse
 | 
					import java.net.http.HttpResponse
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class PaperVersionClient(val client: HttpClient = HttpClient.newHttpClient()) {
 | 
					class PaperVersionClient(
 | 
				
			||||||
 | 
					  val client: HttpClient = HttpClient.newHttpClient(),
 | 
				
			||||||
 | 
					  private val gson: Gson = FoundationGlobals.gson
 | 
				
			||||||
 | 
					) {
 | 
				
			||||||
  private val apiBaseUrl = URI.create("https://papermc.io/api/v2/")
 | 
					  private val apiBaseUrl = URI.create("https://papermc.io/api/v2/")
 | 
				
			||||||
  private val gson = Gson()
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
  fun getVersionBuilds(group: String): List<PaperBuild> {
 | 
					  fun getVersionBuilds(group: String): List<PaperBuild> {
 | 
				
			||||||
    val response = client.send(
 | 
					    val response = client.send(
 | 
				
			||||||
 | 
				
			|||||||
@ -0,0 +1,32 @@
 | 
				
			|||||||
 | 
					package cloud.kubelet.foundation.gradle
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					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, FoundationGlobals.gson.toJson(updateManifest))
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  private fun ensureManifestsDir(): Path {
 | 
				
			||||||
 | 
					    val manifestsDir = project.buildDir.resolve("manifests")
 | 
				
			||||||
 | 
					    manifestsDir.mkdirs()
 | 
				
			||||||
 | 
					    return manifestsDir.toPath()
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -0,0 +1,17 @@
 | 
				
			|||||||
 | 
					package cloud.kubelet.foundation.gradle
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import org.gradle.api.Project
 | 
				
			||||||
 | 
					import org.gradle.api.tasks.TaskOutputs
 | 
				
			||||||
 | 
					import java.nio.file.FileSystems
 | 
				
			||||||
 | 
					import java.nio.file.Path
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					fun Project.isFoundationPlugin() = name.startsWith("foundation-")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					fun Project.findPluginProjects() = rootProject.subprojects.filter { project -> project.isFoundationPlugin() }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					val Project.shadowJarOutputs: TaskOutputs
 | 
				
			||||||
 | 
					  get() = project.tasks.getByName("shadowJar").outputs
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					fun TaskOutputs.allFilesRelativeToPath(root: Path): List<Path> = files.map { root.relativize(it.toPath()) }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					fun Path.toUnixString() = toString().replace(FileSystems.getDefault().separator, "/")
 | 
				
			||||||
		Reference in New Issue
	
	Block a user