diff options
Diffstat (limited to 'plugin/src/main/kotlin')
4 files changed, 100 insertions, 0 deletions
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) + } + +} |