Skip to content

Commit c975bd3

Browse files
authored
Prepare supporting multiple sourceSets (#120)
* Unmute Kotlin test due wrong test code * Prepare supporting multiple sourceSets
1 parent a131bc2 commit c975bd3

File tree

7 files changed

+20
-435
lines changed

7 files changed

+20
-435
lines changed

Diff for: gradle-plugin/build.gradle.kts

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ plugins {
66
val pluginFiles by configurations.creating
77

88
dependencies {
9-
compileOnly(libs.kotlin.gradlePlugin)
9+
implementation(libs.ksp.gradlePlugin)
10+
implementation(libs.kotlin.gradlePlugin)
1011

1112
testImplementation(kotlin("test"))
1213
pluginFiles(libs.kotlin.gradlePlugin)
13-
pluginFiles(libs.ksp.gradlePlugin)
1414
}
1515

1616
tasks.validatePlugins {

Diff for: gradle-plugin/src/main/kotlin/app/softwork/serviceloader/CreateServiceLoadersResourcesFileAction.kt

-51
This file was deleted.

Diff for: gradle-plugin/src/main/kotlin/app/softwork/serviceloader/CreateServiceLoadersResourcesFileTask.kt

-47
This file was deleted.

Diff for: gradle-plugin/src/main/kotlin/app/softwork/serviceloader/ServiceLoader.kt

-11
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,130 +1,36 @@
1-
import app.softwork.serviceloader.CreateServiceLoadersResourcesFileTask
2-
import app.softwork.serviceloader.ServiceLoader
31
import app.softwork.serviceloader.VERSION
4-
import org.jetbrains.kotlin.gradle.dsl.KotlinJvmProjectExtension
2+
import org.gradle.api.tasks.SourceSet.MAIN_SOURCE_SET_NAME
53
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
6-
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilation
74
import org.jetbrains.kotlin.gradle.targets.jvm.KotlinJvmTarget
8-
import org.jetbrains.kotlin.gradle.tasks.KotlinJvmCompile
95

10-
private val CreateServiceLoadersResourcesFileTaskName = "createServiceLoadersResourcesFile"
11-
12-
private val serviceLoadersExtension = objects.domainObjectContainer(ServiceLoader::class.java)
13-
extensions.add("serviceLoaders", serviceLoadersExtension)
14-
15-
private val configExtension: CreateServiceLoadersResourcesFileTask.() -> Unit = {
16-
serviceLoaders.convention(provider<Map<String, List<String>>> {
17-
val map = mutableMapOf<String, List<String>>()
18-
for (serviceLoader in serviceLoadersExtension) {
19-
map[serviceLoader.name] = serviceLoader.implementationClasses.get()
20-
}
21-
map
22-
})
6+
plugins {
7+
id("com.google.devtools.ksp")
238
}
249

25-
pluginManager.withPlugin("org.gradle.java") {
26-
val serviceLoader = tasks.register(
27-
CreateServiceLoadersResourcesFileTaskName,
28-
CreateServiceLoadersResourcesFileTask::class,
29-
configExtension
30-
)
31-
extensions.getByName<JavaPluginExtension>("java").sourceSets.named("main") {
32-
resources.srcDir(serviceLoader)
33-
}
34-
serviceLoader {
35-
classpath.from(project.configurations.named(JavaPlugin.COMPILE_CLASSPATH_CONFIGURATION_NAME))
36-
classes.from(project.tasks.named(JavaPlugin.COMPILE_JAVA_TASK_NAME))
37-
}
38-
}
10+
private val kspPluginDep = dependencies.create("app.softwork.serviceloader:ksp-plugin:$VERSION")
11+
private val kspAnnotationDep = dependencies.create("app.softwork.serviceloader:ksp-annotation:$VERSION")
3912

4013
pluginManager.withPlugin("org.jetbrains.kotlin.jvm") {
41-
val kotlin = extensions.getByName<KotlinJvmProjectExtension>("kotlin")
42-
val serviceLoader = if (CreateServiceLoadersResourcesFileTaskName in tasks.names) {
43-
tasks.named<CreateServiceLoadersResourcesFileTask>(CreateServiceLoadersResourcesFileTaskName)
44-
} else {
45-
tasks.register(
46-
CreateServiceLoadersResourcesFileTaskName,
47-
CreateServiceLoadersResourcesFileTask::class,
48-
configExtension
49-
)
50-
}
51-
52-
kotlin.sourceSets.named("main") {
53-
resources.srcDir(serviceLoader)
54-
}
55-
serviceLoader {
56-
val jvmTarget = kotlin.target
57-
val mainComplication = jvmTarget.compilations.named(KotlinCompilation.MAIN_COMPILATION_NAME)
58-
59-
val compileTaskProvider = mainComplication.flatMap { it.compileTaskProvider }.map { it as KotlinJvmCompile }
60-
val classesPath = compileTaskProvider.map {
61-
it.libraries
62-
}
63-
classpath.from(classesPath)
64-
val klasses = compileTaskProvider.flatMap {
65-
it.destinationDirectory
14+
val sourceSets = extensions.getByName("sourceSets") as SourceSetContainer
15+
sourceSets.configureEach {
16+
dependencies.add(compileOnlyConfigurationName, kspAnnotationDep)
17+
if (name == MAIN_SOURCE_SET_NAME) {
18+
dependencies.add("ksp", kspPluginDep)
19+
} else {
20+
dependencies.add("ksp" + name.replaceFirstChar { it.uppercaseChar() }, kspPluginDep)
6621
}
67-
classes.from(klasses)
6822
}
6923
}
7024

7125
pluginManager.withPlugin("org.jetbrains.kotlin.multiplatform") {
7226
val kotlin = extensions.getByName<KotlinMultiplatformExtension>("kotlin")
7327
val jvmTargets = kotlin.targets.withType(KotlinJvmTarget::class)
7428
jvmTargets.configureEach {
75-
val jvmTarget = this
76-
val serviceLoader = tasks.register(
77-
name = CreateServiceLoadersResourcesFileTaskName + jvmTarget.name.replaceFirstChar { it.uppercaseChar() },
78-
type = CreateServiceLoadersResourcesFileTask::class,
79-
configurationAction = configExtension
80-
)
81-
82-
compilations.named(KotlinCompilation.MAIN_COMPILATION_NAME) {
83-
val mainComplication = this
84-
kotlinSourceSets.forAll {
85-
it.resources.srcDir(serviceLoader)
86-
}
87-
88-
serviceLoader {
89-
val compileTaskProvider =
90-
mainComplication.compileTaskProvider.map { it as KotlinJvmCompile }
91-
val classesPath = compileTaskProvider.map {
92-
it.libraries
93-
}
94-
classpath.from(classesPath)
95-
val klasses = compileTaskProvider.flatMap {
96-
it.destinationDirectory
97-
}
98-
classes.from(klasses)
99-
resourcesDir.convention(project.layout.buildDirectory.dir("generated/${jvmTarget.name}/resources/serviceloader"))
100-
}
101-
}
102-
}
103-
}
104-
105-
pluginManager.withPlugin("com.google.devtools.ksp") {
106-
val kspPluginDep = dependencies.create("app.softwork.serviceloader:ksp-plugin:$VERSION")
107-
val kspAnnotationDep = dependencies.create("app.softwork.serviceloader:ksp-annotation:$VERSION")
108-
109-
pluginManager.withPlugin("org.jetbrains.kotlin.jvm") {
110-
val kotlin = extensions.getByName<KotlinJvmProjectExtension>("kotlin")
111-
val jvmTarget = kotlin.target
112-
jvmTarget.compilations.named(KotlinCompilation.MAIN_COMPILATION_NAME) {
113-
dependencies.add(compileOnlyConfigurationName, kspAnnotationDep)
114-
dependencies.add("ksp", kspPluginDep)
115-
}
29+
val kspName = "ksp" + name.replaceFirstChar { it.uppercaseChar() }
30+
dependencies.add(kspName, kspPluginDep)
11631
}
11732

118-
pluginManager.withPlugin("org.jetbrains.kotlin.multiplatform") {
119-
val kotlin = extensions.getByName<KotlinMultiplatformExtension>("kotlin")
120-
val jvmTargets = kotlin.targets.withType(KotlinJvmTarget::class)
121-
jvmTargets.configureEach {
122-
val kspName = "ksp" + name.replaceFirstChar { it.uppercaseChar() }
123-
dependencies.add(kspName, kspPluginDep)
124-
}
125-
126-
kotlin.sourceSets.named("commonMain") {
127-
dependencies.add(compileOnlyConfigurationName, kspAnnotationDep)
128-
}
33+
kotlin.sourceSets.named("commonMain") {
34+
dependencies.add(compileOnlyConfigurationName, kspAnnotationDep)
12935
}
13036
}

0 commit comments

Comments
 (0)