daily pastebin goal
26%
SHARE
TWEET

Untitled

a guest Jan 18th, 2019 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @file:Suppress("UNCHECKED_CAST")
  2.  
  3. import org.gradle.api.Project
  4. import org.gradle.api.plugins.ExtensionAware
  5. import org.gradle.kotlin.dsl.get
  6. import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
  7. import org.jetbrains.kotlin.gradle.plugin.*
  8. import org.jetbrains.kotlin.gradle.plugin.mpp.*
  9. import org.jetbrains.kotlin.gradle.plugin.sources.DefaultKotlinSourceSet
  10. import kotlin.reflect.KProperty
  11. import org.gradle.api.NamedDomainObjectCollection as NdoCollection
  12. import org.gradle.api.NamedDomainObjectContainer as NdoContainer
  13.  
  14. internal typealias Presets = NdoCollection<KotlinTargetPreset<*>>
  15. internal typealias Targets = NdoCollection<KotlinTarget>
  16.  
  17. val Presets.jvm get() = this["jvm"] as KotlinJvmTargetPreset
  18. val Presets.js get() = this["js"] as KotlinJsTargetPreset
  19. val Presets.linuxX64 get() = this["linuxX64"] as KotlinNativeTargetPreset
  20. val Presets.macosX64 get() = this["macosX64"] as KotlinNativeTargetPreset
  21. val Presets.mingwX64 get() = this["mingwX64"] as KotlinNativeTargetPreset
  22. val Presets.iosX64 get() = this["iosX64"] as KotlinNativeTargetPreset
  23. val <T : KotlinCompilation> NdoCollection<T>.main get() = this["main"]
  24.  
  25. internal val hostOs by lazy { System.getProperty("os.name") }
  26.  
  27. val isLinux get() = hostOs == "Linux"
  28. val isMacos get() = hostOs == "Mac OS X"
  29. val isWindows get() = hostOs.startsWith("Windows")
  30.  
  31. val Presets.host
  32.     get() = when {
  33.         isLinux -> linuxX64
  34.         isMacos -> macosX64
  35.         isWindows -> mingwX64
  36.         else -> error("Unknown host platform '$hostOs'")
  37.     }
  38.  
  39. fun <T : KotlinTarget> Targets.fromPreset(preset: KotlinTargetPreset<T>, name: String, handler: T.() -> Unit = {}): T {
  40.     val target = preset.createTarget(name)
  41.     add(target)
  42.     handler(target)
  43.     return target
  44. }
  45.  
  46. fun <T : KotlinTarget> Targets.gettingTyped(configure: T.() -> Unit) =
  47.         KotlinTargetDelegateProvider(this, configure)
  48.  
  49. class KotlinTargetDelegateProvider<T : KotlinTarget>(val targets: Targets, val configure: T.() -> Unit) {
  50.     operator fun getValue(thisRef: Any?, property: KProperty<*>): T =
  51.             (targets.getByName(property.name) as T).apply(configure)
  52. }
  53.  
  54.  
  55. val KotlinMultiplatformExtension.extensions get() = (this as ExtensionAware).extensions
  56.  
  57. fun KotlinMultiplatformExtension.`sourceSets`(configure: NdoContainer<KotlinSourceSet>.() -> Unit): Unit =
  58.         extensions.configure("sourceSets", configure)
  59.  
  60. fun KotlinMultiplatformExtension.`targets`(configure: NdoContainer<KotlinTarget>.() -> Unit): Unit =
  61.         extensions.configure("targets", configure)
  62.  
  63. val NdoContainer<KotlinSourceSet>.commonMain get() = getByName("commonMain")
  64. val NdoContainer<KotlinSourceSet>.iosMain get() = getByName("iosMain")
  65. val NdoContainer<KotlinSourceSet>.jvmMain get() = getByName("jvmMain")
  66.  
  67. operator fun KotlinSourceSet.invoke(configure: KotlinSourceSet.() -> Unit) {
  68.     configure()
  69. }
  70.  
  71.  
  72. class KotlinDefaultConfigureProperties(val presets: Presets) {
  73.     var hasJvm = true
  74.     var hasIos = true
  75.     val dependencyProjects = mutableListOf<Project>()
  76.  
  77.     fun dependsOn(project: Project) {
  78.         dependencyProjects.add(project)
  79.     }
  80.  
  81.     /*
  82.     fun dependencies(configure: KotlinDependencyHandler.() -> Unit) {
  83.         TODO("not implemented")
  84.     }
  85.     */
  86. }
  87.  
  88. private val KotlinDefaultConfigureProperties.targets
  89.     get() = listOf(presets.jvm.takeIf { hasJvm }, presets.iosX64.takeIf { hasIos }).filterNotNull()
  90.  
  91. fun KotlinMultiplatformExtension.defaultConfigure(configure: KotlinDefaultConfigureProperties.() -> Unit = {}) {
  92.     val props = KotlinDefaultConfigureProperties(presets).apply(configure)
  93.  
  94.     targets {
  95.         for (preset in props.targets) {
  96.             val name = when (preset.name) {
  97.                 "jvm", "androidJvm" -> "jvm"
  98.                 "iosX64", "iosArm64" -> "ios"
  99.                 else -> throw IllegalArgumentException()
  100.             }
  101.  
  102.             fromPreset(preset, name) {
  103.                 if (platformType == KotlinPlatformType.native) {
  104.                     this as KotlinNativeTarget
  105.                     compilations.main.outputKinds(NativeOutputKind.FRAMEWORK)
  106.                 }
  107.             }
  108.         }
  109.     }
  110.  
  111.     sourceSets {
  112.         forEach {
  113.             it.languageSettings.apply {
  114.                 apiVersion = "1.3"
  115.                 languageVersion = "1.3"
  116.  
  117.                 useExperimentalAnnotation("kotlin.Experimental")
  118.             }
  119.         }
  120.  
  121.         getByName("commonMain").apply {
  122.             dependencies {
  123.                 commonDependencies()
  124.  
  125.                 for (project in props.dependencyProjects) {
  126.                     api(project)
  127.                 }
  128.             }
  129.         }
  130.  
  131.         for (target in targets) {
  132.             if (target.platformType == KotlinPlatformType.common) continue
  133.  
  134.             target.compilations.main.apply {
  135.  
  136.                 for (sourceSet in sourceSets) {
  137.                     sourceSet as DefaultKotlinSourceSet
  138.                     if (!sourceSet.displayName.startsWith(name)) continue
  139.                     val compilationName = sourceSet.displayName.substring(name.length)
  140.                     sourceSet.dependsOn(getByName("common$compilationName"))
  141.                 }
  142.  
  143.                 dependencies {
  144.                     when (target.name) {
  145.                         "jvm" -> jvmDependencies()
  146.                         "androidJvm" -> androidDependencies()
  147.                         "iosX64", "iosArm64" -> nativeDependencies()
  148.                     }
  149.                 }
  150.             }
  151.         }
  152.     }
  153.  
  154. }
  155.  
  156. private fun KotlinDependencyHandler.commonDependencies() {
  157.     api("org.jetbrains.kotlin:kotlin-stdlib-common")
  158.     api("org.jetbrains.kotlinx:kotlinx-coroutines-core-common:$coroutinesVersion")
  159. }
  160.  
  161. private fun KotlinDependencyHandler.jvmDependencies() {
  162.     api("org.jetbrains.kotlin:kotlin-stdlib")
  163.     api("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesVersion")
  164. }
  165.  
  166. private fun KotlinDependencyHandler.androidDependencies() {
  167.     api("org.jetbrains.kotlin:kotlin-stdlib-android")
  168.     api("org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutinesVersion")
  169. }
  170.  
  171. private fun KotlinDependencyHandler.nativeDependencies() {
  172.     api("org.jetbrains.kotlin:kotlin-stdlib-native")
  173.     api("org.jetbrains.kotlinx:kotlinx-coroutines-core-native:$coroutinesVersion")
  174. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top