Skip to content

Commit 8db9532

Browse files
committed
Kotlinify PlugPlugin.
1 parent 7a3eaeb commit 8db9532

File tree

2 files changed

+97
-102
lines changed

2 files changed

+97
-102
lines changed

atplug-plugin-gradle/src/main/java/com/diffplug/atplug/tooling/gradle/PlugPlugin.java

Lines changed: 0 additions & 102 deletions
This file was deleted.
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* Copyright (C) 2020-2023 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.atplug.tooling.gradle
17+
18+
import java.io.File
19+
import java.io.Serializable
20+
import org.gradle.api.Action
21+
import org.gradle.api.Plugin
22+
import org.gradle.api.Project
23+
import org.gradle.api.Task
24+
import org.gradle.api.UnknownTaskException
25+
import org.gradle.api.artifacts.Configuration
26+
import org.gradle.api.plugins.JavaPlugin
27+
import org.gradle.api.plugins.JavaPluginExtension
28+
import org.gradle.api.tasks.SourceSet
29+
import org.gradle.jvm.tasks.Jar
30+
31+
/**
32+
* `plugGenerate` task uses `@Plug` to generate files in `src/main/resources/ATPLUG-INF` as a
33+
* dependency of `processResources`.
34+
*/
35+
class PlugPlugin : Plugin<Project> {
36+
override fun apply(project: Project) {
37+
// get the classes we're compiling
38+
project.plugins.apply(JavaPlugin::class.java)
39+
val javaExtension = project.extensions.getByType(JavaPluginExtension::class.java)
40+
val main = javaExtension.sourceSets.getByName(SourceSet.MAIN_SOURCE_SET_NAME)
41+
val dep = project.dependencies.create("org.jetbrains.kotlin:kotlin-reflect:1.8.20")
42+
val plugGenConfig =
43+
project.configurations.create("plugGenerate") { plugGen: Configuration ->
44+
plugGen.extendsFrom(
45+
project.configurations.getByName(JavaPlugin.RUNTIME_CLASSPATH_CONFIGURATION_NAME))
46+
plugGen.dependencies.add(dep)
47+
}
48+
49+
// jar --dependsOn--> plugGenerate
50+
val generateTask =
51+
project.tasks.register<PlugGenerateTask>(GENERATE, PlugGenerateTask::class.java) {
52+
task: PlugGenerateTask ->
53+
task.setClassesFolders(main.output.classesDirs)
54+
task.jarsToLinkAgainst.setFrom(plugGenConfig)
55+
task.resourcesFolder = main.resources.sourceDirectories.singleFile
56+
// dep on java
57+
for (taskName in mutableListOf("compileJava", "compileKotlin")) {
58+
try {
59+
task.dependsOn(project.tasks.named(taskName))
60+
} catch (e: UnknownTaskException) {
61+
// not a problem
62+
}
63+
}
64+
}
65+
project.tasks.named(JavaPlugin.JAR_TASK_NAME).configure { jarTaskUntyped: Task ->
66+
val jarTask = jarTaskUntyped as Jar
67+
val metadataTask = generateTask.get()
68+
jarTask.inputs.dir(metadataTask.atplugInfFolder)
69+
jarTask.doFirst(
70+
"Set " + SERVICE_COMPONENT + " header",
71+
SetServiceComponentHeader(metadataTask.atplugInfFolder))
72+
}
73+
project.tasks.named(JavaPlugin.PROCESS_RESOURCES_TASK_NAME).configure { t: Task ->
74+
t.dependsOn(generateTask)
75+
}
76+
}
77+
78+
internal class SetServiceComponentHeader(private val atplugInfFolder: File) :
79+
Serializable, Action<Task> {
80+
override fun execute(task: Task) {
81+
val serviceComponents = PlugGenerateTask.atplugComponents(atplugInfFolder)
82+
val jarTask = task as Jar
83+
if (serviceComponents == null) {
84+
jarTask.manifest.attributes.remove(SERVICE_COMPONENT)
85+
} else {
86+
jarTask.manifest.attributes[SERVICE_COMPONENT] = serviceComponents
87+
}
88+
}
89+
}
90+
91+
companion object {
92+
const val GENERATE = "plugGenerate"
93+
const val SERVICE_COMPONENT = "AtPlug-Component"
94+
const val DOT_JSON = ".json"
95+
const val ATPLUG_INF = "ATPLUG-INF/"
96+
}
97+
}

0 commit comments

Comments
 (0)