aboutsummaryrefslogtreecommitdiff
path: root/plugin
diff options
context:
space:
mode:
Diffstat (limited to 'plugin')
-rw-r--r--plugin/build.gradle.kts34
-rw-r--r--plugin/src/main/kotlin/moe/nea/shot/plugin/ShotExtension.kt31
-rw-r--r--plugin/src/main/kotlin/moe/nea/shot/plugin/ShotPlugin.kt10
-rw-r--r--plugin/src/main/kotlin/moe/nea/shot/plugin/ShotTransform.kt35
-rw-r--r--plugin/src/main/kotlin/moe/nea/shot/plugin/ShotWrapper.kt24
5 files changed, 134 insertions, 0 deletions
diff --git a/plugin/build.gradle.kts b/plugin/build.gradle.kts
new file mode 100644
index 0000000..9e248fb
--- /dev/null
+++ b/plugin/build.gradle.kts
@@ -0,0 +1,34 @@
+// SPDX-FileCopyrightText: 2024 Linnea Gräf <nea@nea.moe>
+//
+// SPDX-License-Identifier: CC0-1.0
+
+plugins {
+ `java-gradle-plugin`
+ kotlin("jvm")
+ id("com.gradle.plugin-publish") version "1.2.1"
+ id("com.github.johnrengelman.shadow") version ("8.1.1")
+}
+
+
+repositories {
+ mavenCentral()
+}
+
+val shade by configurations.creating
+dependencies {
+ implementation(project(":"))
+ shade(project(":")) { isTransitive = false }
+}
+tasks.jar {
+ archiveClassifier.set("thin")
+}
+tasks.shadowJar {
+ configurations = listOf(shade)
+ archiveClassifier.set("")
+}
+gradlePlugin {
+ val shot by plugins.creating {
+ id = "moe.nea.shot"
+ implementationClass = "moe.nea.shot.plugin.ShotPlugin"
+ }
+}
diff --git a/plugin/src/main/kotlin/moe/nea/shot/plugin/ShotExtension.kt b/plugin/src/main/kotlin/moe/nea/shot/plugin/ShotExtension.kt
new file mode 100644
index 0000000..95e79b0
--- /dev/null
+++ b/plugin/src/main/kotlin/moe/nea/shot/plugin/ShotExtension.kt
@@ -0,0 +1,31 @@
+package moe.nea.shot.plugin
+
+import moe.nea.shot.ShotParser
+import moe.nea.shot.Shots
+import org.gradle.api.Project
+import org.gradle.api.attributes.Attribute
+import java.io.File
+
+abstract class ShotExtension(val project: Project) {
+ val attribute: Attribute<String> = Attribute.of("moe.nea.shot", String::class.java)
+
+ init {
+ project.dependencies.attributesSchema.attribute(attribute)
+ project.configurations.all {
+ if (it.isCanBeResolved) {
+ it.attributes.attribute(attribute, "identity")
+ }
+ }
+ }
+
+ fun shot(name: String, file: File): ShotWrapper {
+ val shotParser = ShotParser()
+ val shots = Shots(shotParser.parse(file.readText().lines()))
+ project.dependencies.registerTransform(ShotTransform::class.java) {
+ it.parameters.shot = shots
+ it.from.attribute(attribute, name)
+ it.to.attribute(attribute, "identity")
+ }
+ return ShotWrapper(attribute, name)
+ }
+}
diff --git a/plugin/src/main/kotlin/moe/nea/shot/plugin/ShotPlugin.kt b/plugin/src/main/kotlin/moe/nea/shot/plugin/ShotPlugin.kt
new file mode 100644
index 0000000..7674dae
--- /dev/null
+++ b/plugin/src/main/kotlin/moe/nea/shot/plugin/ShotPlugin.kt
@@ -0,0 +1,10 @@
+package moe.nea.shot.plugin
+
+import org.gradle.api.Plugin
+import org.gradle.api.Project
+
+class ShotPlugin : Plugin<Project> {
+ override fun apply(target: Project) {
+ target.extensions.create("shots", ShotExtension::class.java, target)
+ }
+} \ No newline at end of file
diff --git a/plugin/src/main/kotlin/moe/nea/shot/plugin/ShotTransform.kt b/plugin/src/main/kotlin/moe/nea/shot/plugin/ShotTransform.kt
new file mode 100644
index 0000000..998afb3
--- /dev/null
+++ b/plugin/src/main/kotlin/moe/nea/shot/plugin/ShotTransform.kt
@@ -0,0 +1,35 @@
+package moe.nea.shot.plugin
+
+import moe.nea.shot.Shots
+import org.gradle.api.artifacts.transform.InputArtifact
+import org.gradle.api.artifacts.transform.TransformAction
+import org.gradle.api.artifacts.transform.TransformOutputs
+import org.gradle.api.artifacts.transform.TransformParameters
+import org.gradle.api.file.FileSystemLocation
+import org.gradle.api.provider.Provider
+import org.gradle.api.tasks.Input
+import java.io.FileOutputStream
+import java.io.Serializable
+import java.util.zip.ZipFile
+import java.util.zip.ZipOutputStream
+
+abstract class ShotTransform : TransformAction<ShotTransform.Props> {
+ interface Props : TransformParameters, Serializable {
+ @get:Input
+ var shot: Shots
+ }
+
+ @get:InputArtifact
+ abstract val inputArtifact: Provider<FileSystemLocation>
+
+ override fun transform(outputs: TransformOutputs) {
+ val input = inputArtifact.get().asFile
+ val output = outputs.file("${input.nameWithoutExtension}-shot.jar")
+ output.parentFile.mkdirs()
+ ZipFile(input).use { inputZipFile ->
+ ZipOutputStream(FileOutputStream(output)).use { zipOutputStream ->
+ parameters.shot.processZipFile(inputZipFile, zipOutputStream)
+ }
+ }
+ }
+}
diff --git a/plugin/src/main/kotlin/moe/nea/shot/plugin/ShotWrapper.kt b/plugin/src/main/kotlin/moe/nea/shot/plugin/ShotWrapper.kt
new file mode 100644
index 0000000..31581a8
--- /dev/null
+++ b/plugin/src/main/kotlin/moe/nea/shot/plugin/ShotWrapper.kt
@@ -0,0 +1,24 @@
+package moe.nea.shot.plugin
+
+import org.gradle.api.attributes.Attribute
+import org.gradle.api.attributes.AttributeContainer
+import org.gradle.api.attributes.HasConfigurableAttributes
+
+data class ShotWrapper(val attribute: Attribute<String>, val name: String) {
+ operator fun invoke(attributes: AttributeContainer) {
+ applyTo(attributes)
+ }
+
+ operator fun invoke(dep: HasConfigurableAttributes<*>) {
+ applyTo(dep)
+ }
+
+ fun applyTo(attributes: AttributeContainer) {
+ attributes.attribute(attribute, name)
+ }
+
+ fun applyTo(dep: HasConfigurableAttributes<*>) {
+ applyTo(dep.attributes)
+ }
+
+}