@@ -29,163 +29,181 @@ private const val TEST_IMPLEMENTATION = "testImplementation"
29
29
open class AtomicFUGradlePlugin : Plugin <Project > {
30
30
override fun apply (project : Project ) {
31
31
project.extensions.add(EXTENSION_NAME , AtomicFUPluginExtension ())
32
- val atomicFuPluginVersion = project.rootProject.buildscript.configurations.findByName(" classpath" )
33
- ?.allDependencies?.find { it.name == " atomicfu-gradle-plugin" }?.version
34
- project.withPlugins(" kotlin" ) {
35
- atomicFuPluginVersion?.let {
36
- dependencies.add(COMPILE_ONLY_CONFIGURATION , getAtomicfuDependencyNotation(" " , it))
37
- dependencies.add(TEST_IMPLEMENTATION , getAtomicfuDependencyNotation(" " , it))
38
- }
39
- configureTransformTasks(" compileTestKotlin" ) { sourceSet, transformedDir, originalDir, config ->
40
- createJvmTransformTask(sourceSet).configureJvmTask(
41
- sourceSet.compileClasspath,
42
- sourceSet.classesTaskName,
43
- transformedDir,
44
- originalDir,
45
- config
46
- )
47
- }
48
- }
49
- project.withPlugins(" kotlin2js" ) {
50
- atomicFuPluginVersion?.let {
51
- dependencies.add(COMPILE_ONLY_CONFIGURATION , getAtomicfuDependencyNotation(" -js" , it))
52
- dependencies.add(TEST_IMPLEMENTATION , getAtomicfuDependencyNotation(" -js" , it))
53
- }
54
- configureTransformTasks(" compileTestKotlin2Js" ) { sourceSet, transformedDir, originalDir, config ->
55
- createJsTransformTask(sourceSet).configureJsTask(
56
- sourceSet.classesTaskName,
57
- transformedDir,
58
- originalDir,
59
- config
60
- )
61
- }
62
- }
63
- project.withPlugins(" kotlin-native" ) {
64
- atomicFuPluginVersion?.let {
65
- dependencies.add(TEST_IMPLEMENTATION , getAtomicfuDependencyNotation(" -native" , it))
66
- }
32
+ project.configureTasks()
33
+ project.configureDependencies()
34
+ }
35
+ }
36
+
37
+ private fun Project.configureTasks () {
38
+ withPlugins(" kotlin" ) {
39
+ configureTransformTasks(" compileTestKotlin" ) { sourceSet, transformedDir, originalDir, config ->
40
+ createJvmTransformTask(sourceSet).configureJvmTask(
41
+ sourceSet.compileClasspath,
42
+ sourceSet.classesTaskName,
43
+ transformedDir,
44
+ originalDir,
45
+ config
46
+ )
67
47
}
68
- project.withPlugins(" kotlin-platform-common" ) {
69
- atomicFuPluginVersion?.let {
70
- dependencies.add(COMPILE_ONLY_CONFIGURATION , getAtomicfuDependencyNotation(" -common" , it))
71
- dependencies.add(TEST_IMPLEMENTATION , getAtomicfuDependencyNotation(" -common" , it))
72
- }
48
+ }
49
+ withPlugins(" kotlin2js" ) {
50
+ configureTransformTasks(" compileTestKotlin2Js" ) { sourceSet, transformedDir, originalDir, config ->
51
+ createJsTransformTask(sourceSet).configureJsTask(
52
+ sourceSet.classesTaskName,
53
+ transformedDir,
54
+ originalDir,
55
+ config
56
+ )
73
57
}
74
- project.withPlugins(" kotlin-multiplatform" ) {
75
- configureMultiplatformPlugin(atomicFuPluginVersion)
58
+ }
59
+ withPlugins(" kotlin-multiplatform" ) {
60
+ afterEvaluate {
61
+ configureMultiplatformPluginTasks()
76
62
}
77
63
}
78
64
}
79
65
66
+ private fun Project.configureDependencies () {
67
+ val version = project.rootProject.buildscript.configurations.findByName(" classpath" )
68
+ ?.allDependencies?.find { it.name == " atomicfu-gradle-plugin" }?.version ? : return
69
+ withPlugins(" kotlin" ) {
70
+ dependencies.add(COMPILE_ONLY_CONFIGURATION , getAtomicfuDependencyNotation(" " , version))
71
+ dependencies.add(TEST_IMPLEMENTATION , getAtomicfuDependencyNotation(" " , version))
72
+ }
73
+ withPlugins(" kotlin2js" ) {
74
+ dependencies.add(COMPILE_ONLY_CONFIGURATION , getAtomicfuDependencyNotation(" -js" , version))
75
+ dependencies.add(TEST_IMPLEMENTATION , getAtomicfuDependencyNotation(" -js" , version))
76
+ }
77
+ withPlugins(" kotlin-native" ) {
78
+ dependencies.add(TEST_IMPLEMENTATION , getAtomicfuDependencyNotation(" -native" , version))
79
+ }
80
+ withPlugins(" kotlin-platform-common" ) {
81
+ dependencies.add(COMPILE_ONLY_CONFIGURATION , getAtomicfuDependencyNotation(" -common" , version))
82
+ dependencies.add(TEST_IMPLEMENTATION , getAtomicfuDependencyNotation(" -common" , version))
83
+ }
84
+ withPlugins(" kotlin-multiplatform" ) {
85
+ configureMultiplatformPluginDependencies(version)
86
+ }
87
+ }
88
+
89
+ private fun Project.config () = extensions.findByName(EXTENSION_NAME ) as ? AtomicFUPluginExtension
90
+
80
91
private fun getAtomicfuDependencyNotation (platform : String , version : String ) =
81
92
" org.jetbrains.kotlinx:atomicfu$platform :$version "
82
93
83
94
fun Project.withPlugins (vararg plugins : String , fn : Project .() -> Unit ) {
84
95
plugins.forEach { pluginManager.withPlugin(it) { fn() } }
85
96
}
86
97
87
- fun Project.configureMultiplatformPlugin (version : String? ) {
88
- val originalDirsByCompilation = hashMapOf<KotlinCompilation <* >, FileCollection > ()
89
- val sourceSetsByCompilation = hashMapOf<KotlinSourceSet , MutableList <KotlinCompilation <* >>>()
90
- project.extensions.findByType(KotlinProjectExtension ::class .java)?.let { kotlinExtension ->
91
- val config = extensions.findByName(EXTENSION_NAME ) as ? AtomicFUPluginExtension
92
-
98
+ fun Project.withKotlinTargets (fn : (KotlinTarget ) -> Unit ) {
99
+ extensions.findByType(KotlinProjectExtension ::class .java)?.let { kotlinExtension ->
93
100
val targetsExtension = (kotlinExtension as ? ExtensionAware )?.extensions?.findByName(" targets" )
94
101
@Suppress(" UNCHECKED_CAST" )
95
102
val targets = targetsExtension as NamedDomainObjectContainer <KotlinTarget >
96
-
97
103
// find all compilations given sourceSet belongs to
98
- targets.all { target ->
99
- target.compilations.forEach { compilation ->
100
- compilation.allKotlinSourceSets.forEach { sourceSet ->
101
- sourceSetsByCompilation.getOrPut(sourceSet) { mutableListOf () }.add(compilation)
102
- }
103
- }
104
- }
104
+ targets.all { target -> fn(target) }
105
+ }
106
+ }
105
107
106
- // if a sourceSet belongs to several compilations than it is from common module otherwise it's platform = compilation.platformType
107
- sourceSetsByCompilation.forEach { (sourceSet, compilations) ->
108
- val platform = compilations[0 ].platformType.name
109
- val platformExt = when {
110
- compilations.size > 1 -> " -common"
111
- platform == " jvm" -> " "
112
- else -> " -$platform "
113
- }
114
- val configurationName = when (platform) {
115
- " native" -> sourceSet.implementationConfigurationName
116
- else -> if (compilations[0 ].name == MAIN_COMPILATION_NAME ) sourceSet.compileOnlyConfigurationName else sourceSet.implementationConfigurationName
117
- }
118
- version?.let {
119
- project.dependencies.add(configurationName, getAtomicfuDependencyNotation(platformExt, version))
120
- }
108
+ fun Project.configureMultiplatformPluginTasks () {
109
+ val originalDirsByCompilation = hashMapOf<KotlinCompilation <* >, FileCollection > ()
110
+ val config = config()
111
+ withKotlinTargets { target ->
112
+ if (target.name == KotlinMultiplatformPlugin .METADATA_TARGET_NAME ) {
113
+ return @withKotlinTargets // skip the metadata targets
121
114
}
115
+ target.compilations.all compilations@{ compilation ->
116
+ val classesDirs = compilation.output.classesDirs
117
+ // make copy of original classes directory
118
+ val originalClassesDirs: FileCollection =
119
+ project.files(classesDirs.from.toTypedArray()).filter { it.exists() }
120
+ originalDirsByCompilation[compilation] = originalClassesDirs
122
121
123
- targets.all { target ->
124
- if (target.name == KotlinMultiplatformPlugin .METADATA_TARGET_NAME ) {
125
- return @all // skip the metadata targets
122
+ val transformedClassesDir =
123
+ project.buildDir.resolve(" classes/atomicfu/${target.name} /${compilation.name} " )
124
+ // make transformedClassesDir the source path for output.classesDirs
125
+ if (target.platformType != KotlinPlatformType .native) { // do not change source path for unprocessed native output
126
+ classesDirs.setFrom(transformedClassesDir)
126
127
}
127
- target.compilations.all compilations@{ compilation ->
128
- val classesDirs = compilation.output.classesDirs
129
- // make copy of original classes directory
130
- val originalClassesDirs: FileCollection =
131
- project.files(classesDirs.from.toTypedArray()).filter { it.exists() }
132
- originalDirsByCompilation[compilation] = originalClassesDirs
133
-
134
- val transformedClassesDir =
135
- project.buildDir.resolve(" classes/atomicfu/${target.name} /${compilation.name} " )
136
- // make transformedClassesDir the source path for output.classesDirs
137
- if (target.platformType != KotlinPlatformType .native) { // do not change source path for unprocessed native output
138
- classesDirs.setFrom(transformedClassesDir)
128
+ val transformTask = when (target.platformType) {
129
+ KotlinPlatformType .jvm -> {
130
+ project.createJvmTransformTask(compilation).configureJvmTask(
131
+ compilation.compileDependencyFiles,
132
+ compilation.compileAllTaskName,
133
+ transformedClassesDir,
134
+ originalClassesDirs,
135
+ config
136
+ )
139
137
}
140
- val transformTask = when (target.platformType) {
141
- KotlinPlatformType .jvm -> {
142
- project.createJvmTransformTask(compilation).configureJvmTask(
143
- compilation.compileDependencyFiles,
144
- compilation.compileAllTaskName,
145
- transformedClassesDir,
146
- originalClassesDirs,
147
- config
148
- )
149
- }
150
- KotlinPlatformType .js -> {
151
- project.createJsTransformTask(compilation).configureJsTask(
152
- compilation.compileAllTaskName,
153
- transformedClassesDir,
154
- originalClassesDirs,
155
- config
156
- )
157
- }
158
- else -> {
159
- // todo KotlinPlatformType.android?
160
- return @compilations
161
- }
138
+ KotlinPlatformType .js -> {
139
+ project.createJsTransformTask(compilation).configureJsTask(
140
+ compilation.compileAllTaskName,
141
+ transformedClassesDir,
142
+ originalClassesDirs,
143
+ config
144
+ )
162
145
}
163
- // now transformTask is responsible for compiling this source set into the classes directory
164
- classesDirs.builtBy(transformTask)
165
- (tasks.findByName(target.artifactsTaskName) as ? Jar )?.apply {
166
- setupJarManifest(multiRelease = config?.variant?.toVariant() == Variant .BOTH )
146
+ else -> {
147
+ // todo KotlinPlatformType.android?
148
+ return @compilations
167
149
}
150
+ }
151
+ // now transformTask is responsible for compiling this source set into the classes directory
152
+ classesDirs.builtBy(transformTask)
153
+ (tasks.findByName(target.artifactsTaskName) as ? Jar )?.apply {
154
+ setupJarManifest(multiRelease = config?.variant?.toVariant() == Variant .BOTH )
155
+ }
168
156
169
- if (compilation.name == KotlinCompilation .TEST_COMPILATION_NAME ) {
170
- // test should compile and run against original production binaries
171
- val mainCompilation =
172
- compilation.target.compilations.getByName(KotlinCompilation . MAIN_COMPILATION_NAME )
173
- val originalMainClassesDirs = project.files(
174
- // use Callable because there is no guarantee that main is configured before test
175
- Callable { originalDirsByCompilation[mainCompilation]!! }
176
- )
157
+ if (compilation.name == KotlinCompilation .TEST_COMPILATION_NAME ) {
158
+ // test should compile and run against original production binaries
159
+ val mainCompilation =
160
+ compilation.target.compilations.getByName(MAIN_COMPILATION_NAME )
161
+ val originalMainClassesDirs = project.files(
162
+ // use Callable because there is no guarantee that main is configured before test
163
+ Callable { originalDirsByCompilation[mainCompilation]!! }
164
+ )
177
165
178
- (tasks.findByName(compilation.compileKotlinTaskName) as ? AbstractCompile )?.classpath =
179
- originalMainClassesDirs + compilation.compileDependencyFiles - mainCompilation.output.classesDirs
166
+ (tasks.findByName(compilation.compileKotlinTaskName) as ? AbstractCompile )?.classpath =
167
+ originalMainClassesDirs + compilation.compileDependencyFiles - mainCompilation.output.classesDirs
180
168
181
- (tasks.findByName(" ${target.name}${compilation.name.capitalize()} " ) as ? Test )?.classpath =
182
- originalMainClassesDirs + (compilation as KotlinCompilationToRunnableFiles ).runtimeDependencyFiles - mainCompilation.output.classesDirs
183
- }
169
+ (tasks.findByName(" ${target.name}${compilation.name.capitalize()} " ) as ? Test )?.classpath =
170
+ originalMainClassesDirs + (compilation as KotlinCompilationToRunnableFiles ).runtimeDependencyFiles - mainCompilation.output.classesDirs
184
171
}
185
172
}
186
173
}
187
174
}
188
175
176
+ fun Project.sourceSetsByCompilation (): Map <KotlinSourceSet , MutableList <KotlinCompilation <* >>> {
177
+ val sourceSetsByCompilation = hashMapOf<KotlinSourceSet , MutableList <KotlinCompilation <* >>>()
178
+ withKotlinTargets { target ->
179
+ target.compilations.forEach { compilation ->
180
+ compilation.allKotlinSourceSets.forEach { sourceSet ->
181
+ sourceSetsByCompilation.getOrPut(sourceSet) { mutableListOf () }.add(compilation)
182
+ }
183
+ }
184
+ }
185
+ return sourceSetsByCompilation
186
+ }
187
+
188
+ fun Project.configureMultiplatformPluginDependencies (version : String ) {
189
+ val sourceSetsByCompilation = sourceSetsByCompilation()
190
+ // if a sourceSet belongs to several compilations than it is from common module otherwise it's platform = compilation.platformType
191
+ sourceSetsByCompilation.forEach { (sourceSet, compilations) ->
192
+ val platform = compilations[0 ].platformType.name
193
+ val platformExt = when {
194
+ compilations.size > 1 -> " -common"
195
+ platform == " jvm" -> " "
196
+ else -> " -$platform "
197
+ }
198
+ val configurationName = when {
199
+ platform == " native" -> sourceSet.implementationConfigurationName
200
+ compilations[0 ].name == MAIN_COMPILATION_NAME -> sourceSet.compileOnlyConfigurationName
201
+ else -> sourceSet.implementationConfigurationName
202
+ }
203
+ dependencies.add(configurationName, getAtomicfuDependencyNotation(platformExt, version))
204
+ }
205
+ }
206
+
189
207
fun Project.configureTransformTasks (
190
208
testTaskName : String ,
191
209
createTransformTask : (sourceSet: SourceSet , transformedDir: File , originalDir: FileCollection , config: AtomicFUPluginExtension ? ) -> Task
0 commit comments