mirror of
https://github.com/fankes/unmeta-gradle-plugin.git
synced 2025-09-06 02:55:15 +08:00
Initial commit
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
package com.ncorti.kotlin.gradle.template.plugin
|
||||
|
||||
import org.gradle.api.DefaultTask
|
||||
import org.gradle.api.file.RegularFileProperty
|
||||
import org.gradle.api.provider.Property
|
||||
import org.gradle.api.tasks.Input
|
||||
import org.gradle.api.tasks.Optional
|
||||
import org.gradle.api.tasks.OutputFile
|
||||
import org.gradle.api.tasks.TaskAction
|
||||
import org.gradle.api.tasks.options.Option
|
||||
|
||||
abstract class TemplateExampleTask : DefaultTask() {
|
||||
|
||||
init {
|
||||
description = "Just a sample template task"
|
||||
|
||||
// Don't forget to set the group here.
|
||||
// group = BasePlugin.BUILD_GROUP
|
||||
}
|
||||
|
||||
@get:Input
|
||||
@get:Option(option = "message", description = "A message to be printed in the output file")
|
||||
abstract val message: Property<String>
|
||||
|
||||
@get:Input
|
||||
@get:Option(option = "tag", description = "A Tag to be used for debug and in the output file")
|
||||
@get:Optional
|
||||
abstract val tag: Property<String>
|
||||
|
||||
@get:OutputFile
|
||||
abstract val outputFile: RegularFileProperty
|
||||
|
||||
@TaskAction
|
||||
fun sampleAction() {
|
||||
val prettyTag = tag.orNull?.let { "[$it]" } ?: ""
|
||||
|
||||
logger.lifecycle("$prettyTag message is: ${message.orNull}")
|
||||
logger.lifecycle("$prettyTag tag is: ${tag.orNull}")
|
||||
logger.lifecycle("$prettyTag outputFile is: ${outputFile.orNull}")
|
||||
|
||||
outputFile.get().asFile.writeText("$prettyTag ${message.get()}")
|
||||
}
|
||||
}
|
@@ -0,0 +1,26 @@
|
||||
package com.ncorti.kotlin.gradle.template.plugin
|
||||
|
||||
import org.gradle.api.Project
|
||||
import org.gradle.api.file.RegularFileProperty
|
||||
import org.gradle.api.provider.Property
|
||||
import javax.inject.Inject
|
||||
|
||||
const val DEFAULT_OUTPUT_FILE = "template-example.txt"
|
||||
|
||||
@Suppress("UnnecessaryAbstractClass")
|
||||
abstract class TemplateExtension @Inject constructor(project: Project) {
|
||||
|
||||
private val objects = project.objects
|
||||
|
||||
// Example of a property that is mandatory. The task will
|
||||
// fail if this property is not set as is annotated with @Optional.
|
||||
val message: Property<String> = objects.property(String::class.java)
|
||||
|
||||
// Example of a property that is optional.
|
||||
val tag: Property<String> = objects.property(String::class.java)
|
||||
|
||||
// Example of a property with a default set with .convention
|
||||
val outputFile: RegularFileProperty = objects.fileProperty().convention(
|
||||
project.layout.buildDirectory.file(DEFAULT_OUTPUT_FILE)
|
||||
)
|
||||
}
|
@@ -0,0 +1,21 @@
|
||||
package com.ncorti.kotlin.gradle.template.plugin
|
||||
|
||||
import org.gradle.api.Plugin
|
||||
import org.gradle.api.Project
|
||||
|
||||
const val EXTENSION_NAME = "templateExampleConfig"
|
||||
const val TASK_NAME = "templateExample"
|
||||
|
||||
abstract class TemplatePlugin : Plugin<Project> {
|
||||
override fun apply(project: Project) {
|
||||
// Add the 'template' extension object
|
||||
val extension = project.extensions.create(EXTENSION_NAME, TemplateExtension::class.java, project)
|
||||
|
||||
// Add a task that uses configuration from the extension object
|
||||
project.tasks.register(TASK_NAME, TemplateExampleTask::class.java) {
|
||||
it.tag.set(extension.tag)
|
||||
it.message.set(extension.message)
|
||||
it.outputFile.set(extension.outputFile)
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,44 @@
|
||||
package com.ncorti.kotlin.gradle.template.plugin
|
||||
|
||||
import org.gradle.testfixtures.ProjectBuilder
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertNotNull
|
||||
import org.junit.Test
|
||||
import java.io.File
|
||||
|
||||
class TemplatePluginTest {
|
||||
|
||||
@Test
|
||||
fun `plugin is applied correctly to the project`() {
|
||||
val project = ProjectBuilder.builder().build()
|
||||
project.pluginManager.apply("com.ncorti.kotlin.gradle.template.plugin")
|
||||
|
||||
assert(project.tasks.getByName("templateExample") is TemplateExampleTask)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `extension templateExampleConfig is created correctly`() {
|
||||
val project = ProjectBuilder.builder().build()
|
||||
project.pluginManager.apply("com.ncorti.kotlin.gradle.template.plugin")
|
||||
|
||||
assertNotNull(project.extensions.getByName("templateExampleConfig"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `parameters are passed correctly from extension to task`() {
|
||||
val project = ProjectBuilder.builder().build()
|
||||
project.pluginManager.apply("com.ncorti.kotlin.gradle.template.plugin")
|
||||
val aFile = File(project.projectDir, ".tmp")
|
||||
(project.extensions.getByName("templateExampleConfig") as TemplateExtension).apply {
|
||||
tag.set("a-sample-tag")
|
||||
message.set("just-a-message")
|
||||
outputFile.set(aFile)
|
||||
}
|
||||
|
||||
val task = project.tasks.getByName("templateExample") as TemplateExampleTask
|
||||
|
||||
assertEquals("a-sample-tag", task.tag.get())
|
||||
assertEquals("just-a-message", task.message.get())
|
||||
assertEquals(aFile, task.outputFile.get().asFile)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user