-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
599ece9
commit 3789a02
Showing
12 changed files
with
366 additions
and
178 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
version.packageName=gratatouille.gradle |
36 changes: 36 additions & 0 deletions
36
gratatouille-gradle-plugin/src/main/kotlin/gratatouille/gradle/GenerateDescriptorTask.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package gratatouille.gradle | ||
|
||
import org.gradle.api.DefaultTask | ||
import org.gradle.api.file.DirectoryProperty | ||
import org.gradle.api.provider.Property | ||
import org.gradle.api.tasks.CacheableTask | ||
import org.gradle.api.tasks.Input | ||
import org.gradle.api.tasks.OutputDirectory | ||
import org.gradle.api.tasks.TaskAction | ||
import java.util.* | ||
|
||
@CacheableTask | ||
internal abstract class GenerateDescriptorTask: DefaultTask() { | ||
@get:Input | ||
abstract val id: Property<String> | ||
|
||
@get:Input | ||
abstract val implementationClass: Property<String> | ||
|
||
@get:OutputDirectory | ||
abstract val output: DirectoryProperty | ||
|
||
@TaskAction | ||
fun taskAction() { | ||
output.get().asFile.resolve("META-INF/gradle-plugins").apply { | ||
mkdirs() | ||
val properties = Properties().apply { | ||
this.put("implementation-class", implementationClass.get()) | ||
} | ||
|
||
resolve(id.get() + ".properties").outputStream().use { | ||
properties.store(it, "Gradle plugin descriptor (auto-generated)") | ||
} | ||
} | ||
} | ||
} |
95 changes: 95 additions & 0 deletions
95
gratatouille-gradle-plugin/src/main/kotlin/gratatouille/gradle/GratatouilleExtension.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package gratatouille.gradle | ||
|
||
import org.gradle.api.Action | ||
import org.gradle.api.Project | ||
import org.gradle.api.file.CopySpec | ||
import org.gradle.api.file.Directory | ||
import org.gradle.api.provider.Provider | ||
import org.gradle.api.publish.PublishingExtension | ||
import org.gradle.api.publish.maven.MavenPublication | ||
import org.gradle.api.tasks.Copy | ||
|
||
class DescriptorConnection(val directory: Provider<Directory>) | ||
|
||
class PluginSpec(private val id: String, private val project: Project) { | ||
/** | ||
* Registers a `generate${pluginId}Descriptor` task that generates a [plugin descriptor](https://docs.gradle.org/current/userguide/java_gradle_plugin.html#sec:gradle_plugin_dev_usage) for the plugin. | ||
* That plugin descriptor is copied into the `.jar` file during the `processResources` task. | ||
* This allows to load a plugin by id. | ||
* | ||
* @param implementationClass the fully qualified class name for the plugin implementation. Example: `com.example.ExamplePlugin` | ||
*/ | ||
fun implementationClass(implementationClass: String) { | ||
implementationClass(implementationClass) { connection -> | ||
// From https://github.com/gradle/gradle/blob/master/platforms/extensibility/plugin-development/src/main/java/org/gradle/plugin/devel/plugins/JavaGradlePluginPlugin.java#L253 | ||
project.tasks.named("processResources", Copy::class.java) { | ||
val copyPluginDescriptors: CopySpec = it.rootSpec.addChild() | ||
copyPluginDescriptors.into("META-INF/gradle-plugins") | ||
copyPluginDescriptors.from(connection.directory) | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Registers a `generate${pluginId}Descriptor` task that generates a [plugin descriptor](https://docs.gradle.org/current/userguide/java_gradle_plugin.html#sec:gradle_plugin_dev_usage) for the plugin. | ||
* Use [DescriptorConnection] to wire the descriptor to other tasks | ||
* | ||
* @param implementationClass the fully qualified class name for the plugin implementation. Example: `com.example.ExamplePlugin` | ||
*/ | ||
fun implementationClass(implementationClass: String, action: Action<DescriptorConnection>) { | ||
val task = project.tasks.register("generate${id.displayName()}Descriptor", GenerateDescriptorTask::class.java) { | ||
it.id.set(id) | ||
it.implementationClass.set(implementationClass) | ||
|
||
it.output.set(project.layout.buildDirectory.dir("gratatouille/descriptor/$id")) | ||
} | ||
|
||
val connection = DescriptorConnection(task.flatMap { it.output }) | ||
action.execute(connection) | ||
} | ||
|
||
/** | ||
* Creates a new `${pluginId}PluginMarkerMaven` publication allowing to locate the implementation from the plugin id. | ||
* | ||
* @param mainPublication the publication to redirect to. | ||
*/ | ||
fun marker(mainPublication: MavenPublication) { | ||
project.withRequiredPlugins("maven-publish") { | ||
project.extensions.getByType(PublishingExtension::class.java).apply { | ||
// https://github.com/gradle/gradle/blob/master/platforms/extensibility/plugin-development/src/main/java/org/gradle/plugin/devel/plugins/MavenPluginPublishPlugin.java#L88 | ||
publications.create(id.displayName() + "PluginMarkerMaven", MavenPublication::class.java) { markerPublication -> | ||
markerPublication.trySetAlias() | ||
markerPublication.artifactId = id | ||
markerPublication.artifactId = "$id.gradle.plugin"; | ||
markerPublication.groupId = id; | ||
|
||
val groupProvider = project.provider { mainPublication.groupId } | ||
val artifactIdProvider = project.provider { mainPublication.artifactId } | ||
val versionProvider = project.provider { mainPublication.version } | ||
markerPublication.pom.withXml { xmlProvider -> | ||
val root = xmlProvider.asElement() | ||
val document = root.ownerDocument | ||
val dependencies = root.appendChild(document.createElement("dependencies")) | ||
val dependency = dependencies.appendChild(document.createElement("dependency")) | ||
val groupId = dependency.appendChild(document.createElement("groupId")) | ||
groupId.textContent = groupProvider.get() | ||
val artifactId = dependency.appendChild(document.createElement("artifactId")) | ||
artifactId.textContent = artifactIdProvider.get() | ||
val version = dependency.appendChild(document.createElement("version")) | ||
version.textContent = versionProvider.get() | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun String.displayName() = this.split(".").joinToString(separator = "") { it.replaceFirstChar { it.uppercase() } } | ||
|
||
abstract class GratatouilleExtension(private val project: Project) { | ||
fun plugin(id: String, action: Action<PluginSpec>) { | ||
val spec = PluginSpec(id, project) | ||
action.execute(spec) | ||
} | ||
} | ||
|
13 changes: 13 additions & 0 deletions
13
gratatouille-gradle-plugin/src/main/kotlin/gratatouille/gradle/MavenPublicationInternal.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package gratatouille.gradle | ||
|
||
import org.gradle.api.publish.maven.MavenPublication | ||
import org.gradle.api.publish.maven.internal.publication.MavenPublicationInternal | ||
|
||
fun MavenPublication.trySetAlias() { | ||
try { | ||
this as MavenPublicationInternal | ||
this.isAlias = true | ||
} catch (_: Exception) { | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.