Skip to content

Commit 17540bf

Browse files
committed
Gradle plugin refactoring, build tests fixed
1 parent 2d3721d commit 17540bf

File tree

5 files changed

+151
-148
lines changed

5 files changed

+151
-148
lines changed

atomicfu-gradle-plugin/src/main/kotlin/kotlinx/atomicfu/plugin/gradle/AtomicFUGradlePlugin.kt

Lines changed: 143 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -29,163 +29,181 @@ private const val TEST_IMPLEMENTATION = "testImplementation"
2929
open class AtomicFUGradlePlugin : Plugin<Project> {
3030
override fun apply(project: Project) {
3131
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+
)
6747
}
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+
)
7357
}
74-
project.withPlugins("kotlin-multiplatform") {
75-
configureMultiplatformPlugin(atomicFuPluginVersion)
58+
}
59+
withPlugins("kotlin-multiplatform") {
60+
afterEvaluate {
61+
configureMultiplatformPluginTasks()
7662
}
7763
}
7864
}
7965

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+
8091
private fun getAtomicfuDependencyNotation(platform: String, version: String) =
8192
"org.jetbrains.kotlinx:atomicfu$platform:$version"
8293

8394
fun Project.withPlugins(vararg plugins: String, fn: Project.() -> Unit) {
8495
plugins.forEach { pluginManager.withPlugin(it) { fn() } }
8596
}
8697

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 ->
93100
val targetsExtension = (kotlinExtension as? ExtensionAware)?.extensions?.findByName("targets")
94101
@Suppress("UNCHECKED_CAST")
95102
val targets = targetsExtension as NamedDomainObjectContainer<KotlinTarget>
96-
97103
// 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+
}
105107

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
121114
}
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
122121

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)
126127
}
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+
)
139137
}
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+
)
162145
}
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
167149
}
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+
}
168156

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+
)
177165

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
180168

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
184171
}
185172
}
186173
}
187174
}
188175

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+
189207
fun Project.configureTransformTasks(
190208
testTaskName: String,
191209
createTransformTask: (sourceSet: SourceSet, transformedDir: File, originalDir: FileCollection, config: AtomicFUPluginExtension?) -> Task

atomicfu-gradle-plugin/src/test/kotlin/kotlinx/atomicfu/plugin/gradle/Project.kt

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,12 @@ class Project(val projectDir: File) {
8080
}
8181
}
8282
83-
allprojects {
84-
repositories {
85-
jcenter()
86-
mavenCentral()
87-
maven { url 'https://dl.bintray.com/kotlin/kotlin-eap' }
88-
maven { url 'https://dl.bintray.com/kotlin/kotlin-dev' }
89-
}
83+
repositories {
84+
jcenter()
85+
mavenCentral()
86+
maven { url 'https://dl.bintray.com/kotlin/kotlin-eap' }
87+
maven { url 'https://dl.bintray.com/kotlin/kotlin-dev' }
88+
maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
9089
}
9190
9291
def atomicfuJvm = files(${readFileList("atomicfu-jvm.txt")})

atomicfu-gradle-plugin/src/test/resources/projects/js-simple/build.gradle

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@
55
apply plugin: 'kotlinx-atomicfu'
66
apply plugin: 'kotlin2js'
77

8-
repositories {
9-
maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
10-
}
11-
128
dependencies {
139
compileOnly atomicfuJs
1410
testRuntime atomicfuJs

atomicfu-gradle-plugin/src/test/resources/projects/jvm-simple/build.gradle

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@
55
apply plugin: 'kotlinx-atomicfu'
66
apply plugin: 'kotlin'
77

8-
repositories {
9-
maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
10-
}
11-
128
dependencies {
139
compileOnly atomicfuJvm
1410
testRuntime atomicfuJvm

atomicfu-gradle-plugin/src/test/resources/projects/mpp-simple/build.gradle

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,9 @@
55
apply plugin: 'kotlinx-atomicfu'
66
apply plugin: 'kotlin-multiplatform'
77

8-
repositories {
9-
maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
10-
}
11-
128
kotlin {
13-
targets {
14-
fromPreset(presets.jvm, 'jvm')
15-
fromPreset(presets.js, 'js')
16-
}
9+
jvm()
10+
js()
1711

1812
sourceSets {
1913
commonMain.dependencies {

0 commit comments

Comments
 (0)