|
1 |
| -import app.softwork.serviceloader.CreateServiceLoadersResourcesFileTask |
2 |
| -import app.softwork.serviceloader.ServiceLoader |
3 | 1 | import app.softwork.serviceloader.VERSION
|
4 |
| -import org.jetbrains.kotlin.gradle.dsl.KotlinJvmProjectExtension |
| 2 | +import org.gradle.api.tasks.SourceSet.MAIN_SOURCE_SET_NAME |
5 | 3 | import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
|
6 |
| -import org.jetbrains.kotlin.gradle.plugin.KotlinCompilation |
7 | 4 | import org.jetbrains.kotlin.gradle.targets.jvm.KotlinJvmTarget
|
8 |
| -import org.jetbrains.kotlin.gradle.tasks.KotlinJvmCompile |
9 | 5 |
|
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") |
23 | 8 | }
|
24 | 9 |
|
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") |
39 | 12 |
|
40 | 13 | 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) |
66 | 21 | }
|
67 |
| - classes.from(klasses) |
68 | 22 | }
|
69 | 23 | }
|
70 | 24 |
|
71 | 25 | pluginManager.withPlugin("org.jetbrains.kotlin.multiplatform") {
|
72 | 26 | val kotlin = extensions.getByName<KotlinMultiplatformExtension>("kotlin")
|
73 | 27 | val jvmTargets = kotlin.targets.withType(KotlinJvmTarget::class)
|
74 | 28 | 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) |
116 | 31 | }
|
117 | 32 |
|
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) |
129 | 35 | }
|
130 | 36 | }
|
0 commit comments