Skip to content

Commit

Permalink
Merge pull request #140 from icerockdev/#133-custom-common-sourceset-…
Browse files Browse the repository at this point in the history
…crash

#133 custom common sourceset crash
  • Loading branch information
Alex009 committed Dec 29, 2020
2 parents b1e1c6b + c9d657a commit 6717697
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class MultiplatformResourcesPlugin : Plugin<Project> {
}
}

@Suppress("LongMethod")
private fun configureGenerators(
target: Project,
mrExtension: MultiplatformResourcesPluginExtension,
Expand Down Expand Up @@ -98,7 +99,13 @@ class MultiplatformResourcesPlugin : Plugin<Project> {
)
val targets: List<KotlinTarget> = multiplatformExtension.targets.toList()

setupCommonGenerator(commonSourceSet, generatedDir, mrClassPackage, features, target)
val commonGenerationTask = setupCommonGenerator(
commonSourceSet = commonSourceSet,
generatedDir = generatedDir,
mrClassPackage = mrClassPackage,
features = features,
target = target
)
setupAndroidGenerator(
targets,
androidMainSourceSet,
Expand All @@ -121,7 +128,6 @@ class MultiplatformResourcesPlugin : Plugin<Project> {
}

val generationTasks = target.tasks.filterIsInstance<GenerateMultiplatformResourcesTask>()
val commonGenerationTask = generationTasks.first { it.name == "generateMRcommonMain" }
generationTasks.filter { it != commonGenerationTask }
.forEach { it.dependsOn(commonGenerationTask) }
}
Expand All @@ -132,9 +138,9 @@ class MultiplatformResourcesPlugin : Plugin<Project> {
mrClassPackage: String,
features: List<ResourceGeneratorFeature<out MRGenerator.Generator>>,
target: Project
) {
): GenerateMultiplatformResourcesTask {
val commonGeneratorSourceSet: MRGenerator.SourceSet = createSourceSet(commonSourceSet)
CommonMRGenerator(
return CommonMRGenerator(
generatedDir,
commonGeneratorSourceSet,
mrClassPackage,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,20 @@ abstract class MRGenerator(
afterMRGeneration()
}

fun apply(project: Project) {
fun apply(project: Project): GenerateMultiplatformResourcesTask {
val name = sourceSet.name
val genTaskName = "generateMR$name"
val genTask = runCatching {
project.tasks.getByName(genTaskName)
project.tasks.getByName(genTaskName) as GenerateMultiplatformResourcesTask
}.getOrNull() ?: project.tasks.create(genTaskName, GenerateMultiplatformResourcesTask::class.java) {
it.doLast {
this@MRGenerator.generate()
}
}

apply(generationTask = genTask, project = project)

return genTask
}

protected open fun beforeMRGeneration() = Unit
Expand Down
34 changes: 25 additions & 9 deletions sample/mpp-hierarhical/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ kotlin {
android()
ios()
macosX64("macos")
jvm()

cocoapods {
// Configure fields required by CocoaPods.
Expand All @@ -34,19 +35,34 @@ kotlin {
}

// export correct artifact to use all classes of moko-resources directly from Swift
targets.configureEach {
if (this !is org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget) return@configureEach

this.binaries.configureEach {
if (this is org.jetbrains.kotlin.gradle.plugin.mpp.Framework) {
this.export(project(":resources"))
}
targets.withType(org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget::class.java).all {
binaries.withType(org.jetbrains.kotlin.gradle.plugin.mpp.Framework::class.java).all {
export(project(":resources"))
}
}

sourceSets {
val commonMain by getting
val commonTest by getting

val clientMain by creating { dependsOn(commonMain) }
val clientTest by creating { dependsOn(commonTest) }

val iosMain by getting { dependsOn(clientMain) }
val iosTest by getting { dependsOn(clientTest) }

val macosMain by getting { dependsOn(clientMain) }
val macosTest by getting { dependsOn(clientTest) }

val androidMain by getting { dependsOn(clientMain) }
val androidTest by getting { dependsOn(clientTest) }

val jvmMain by getting
}
}

dependencies {
commonMainApi(Deps.Libs.MultiPlatform.mokoResources)
"clientMainApi"(Deps.Libs.MultiPlatform.mokoResources)

androidTestImplementation(Deps.Libs.Android.Tests.kotlinTestJUnit)
androidTestImplementation(Deps.Libs.Android.Tests.testCore)
Expand All @@ -58,5 +74,5 @@ dependencies {
multiplatformResources {
multiplatformResourcesPackage = "com.icerockdev.library"
disableStaticFrameworkWarning = true
multiplatformResourcesSourceSet = "clientMain"
}

0 comments on commit 6717697

Please sign in to comment.