Kotlin DSL - Kotlin conventions (JVM & JS)

Kotlin DSL - Kotlin conventions (JVM & JS)

This is an adapter version of PR sent by Victor Turansky

Co-authored-by: Victor Turansky <victor.turansky@gmail.com>
diff --git a/build.gradle b/build.gradle
index 866ee13..8ba7a6a 100644
--- a/build.gradle
+++ b/build.gradle
@@ -1,5 +1,5 @@
 /*
- * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
+ * Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
  */
 import org.jetbrains.kotlin.konan.target.HostManager
 import org.gradle.util.VersionNumber
@@ -183,7 +183,7 @@
 configure(subprojects.findAll { !sourceless.contains(it.name) && it.name != coreModule }) {
     evaluationDependsOn(":$coreModule")
     def platform = PlatformKt.platformOf(it)
-    apply from: rootProject.file("gradle/compile-${platform}.gradle")
+    apply plugin: "kotlin-${platform}-conventions"
     dependencies {
         // See comment below for rationale, it will be replaced with "project" dependency
         compile project(":$coreModule")
diff --git a/buildSrc/src/main/kotlin/kotlin-js-conventions.gradle.kts b/buildSrc/src/main/kotlin/kotlin-js-conventions.gradle.kts
new file mode 100644
index 0000000..c1897ca
--- /dev/null
+++ b/buildSrc/src/main/kotlin/kotlin-js-conventions.gradle.kts
@@ -0,0 +1,40 @@
+/*
+ * Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
+ */
+
+// Platform-specific configuration to compile JS modules
+
+import org.jetbrains.kotlin.gradle.dsl.KotlinJsCompile
+
+plugins {
+    kotlin("js")
+}
+
+dependencies {
+    testImplementation(kotlin("test-js"))
+}
+
+kotlin {
+    js(LEGACY) {
+        moduleName = project.name.removeSuffix("-js")
+    }
+
+    sourceSets {
+        main {
+            kotlin.srcDirs("src")
+            resources.srcDirs("resources")
+        }
+        test {
+            kotlin.srcDirs("test")
+            resources.srcDirs("test-resources")
+        }
+    }
+}
+
+tasks.withType<KotlinJsCompile> {
+    kotlinOptions {
+        moduleKind = "umd"
+        sourceMap = true
+        metaInfo = true
+    }
+}
diff --git a/buildSrc/src/main/kotlin/kotlin-jvm-conventions.gradle.kts b/buildSrc/src/main/kotlin/kotlin-jvm-conventions.gradle.kts
new file mode 100644
index 0000000..8900771
--- /dev/null
+++ b/buildSrc/src/main/kotlin/kotlin-jvm-conventions.gradle.kts
@@ -0,0 +1,46 @@
+/*
+ * Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
+ */
+
+// Platform-specific configuration to compile JVM modules
+
+import org.gradle.api.*
+
+plugins {
+    kotlin("jvm")
+}
+
+java {
+    sourceCompatibility = JavaVersion.VERSION_1_6
+    targetCompatibility = JavaVersion.VERSION_1_6
+}
+
+if (rootProject.extra.get("jvm_ir_enabled") as Boolean) {
+    kotlin.target.compilations.configureEach {
+        kotlinOptions.useIR = true
+    }
+}
+
+dependencies {
+    testCompile(kotlin("test"))
+    // Workaround to make addSuppressed work in tests
+    testCompile(kotlin("reflect"))
+    testCompile(kotlin("stdlib-jdk7"))
+    testCompile(kotlin("test-junit"))
+    testCompile("junit:junit:${version("junit")}")
+}
+
+tasks.compileKotlin {
+    kotlinOptions {
+        freeCompilerArgs += listOf("-Xexplicit-api=strict")
+    }
+}
+
+tasks.withType<Test> {
+    testLogging {
+        showStandardStreams = true
+        events("passed", "failed")
+    }
+    val stressTest = project.properties["stressTest"]
+    if (stressTest != null) systemProperties["stressTest"] = stressTest
+}
diff --git a/gradle/compile-js.gradle b/gradle/compile-js.gradle
deleted file mode 100644
index 55c81fe..0000000
--- a/gradle/compile-js.gradle
+++ /dev/null
@@ -1,32 +0,0 @@
-/*
- * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
- */
-
-// Platform-specific configuration to compile JS modules
-
-apply plugin: 'org.jetbrains.kotlin.js'
-
-dependencies {
-    testImplementation "org.jetbrains.kotlin:kotlin-test-js:$kotlin_version"
-}
-
-kotlin {
-    js(LEGACY) {
-        moduleName = project.name - "-js"
-    }
-
-    sourceSets {
-        main.kotlin.srcDirs = ['src']
-        test.kotlin.srcDirs = ['test']
-        main.resources.srcDirs = ['resources']
-        test.resources.srcDirs = ['test-resources']
-    }
-}
-
-tasks.withType(compileKotlinJs.getClass()) {
-    kotlinOptions {
-        moduleKind = "umd"
-        sourceMap = true
-        metaInfo = true
-    }
-}
diff --git a/gradle/compile-jvm.gradle b/gradle/compile-jvm.gradle
deleted file mode 100644
index bd2ae14..0000000
--- a/gradle/compile-jvm.gradle
+++ /dev/null
@@ -1,40 +0,0 @@
-/*
- * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
- */
-
-// Platform-specific configuration to compile JVM modules
-
-apply plugin: 'org.jetbrains.kotlin.jvm'
-
-sourceCompatibility = 1.6
-targetCompatibility = 1.6
-
-if (rootProject.ext.jvm_ir_enabled) {
-    kotlin.target.compilations.all {
-        kotlinOptions.useIR = true
-    }
-}
-
-dependencies {
-    testCompile "org.jetbrains.kotlin:kotlin-test:$kotlin_version"
-    // Workaround to make addSuppressed work in tests
-    testCompile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
-    testCompile "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
-    testCompile "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
-    testCompile "junit:junit:$junit_version"
-}
-
-compileKotlin {
-    kotlinOptions {
-        freeCompilerArgs += ['-Xexplicit-api=strict']
-    }
-}
-
-tasks.withType(Test) {
-    testLogging {
-        showStandardStreams = true
-        events "passed", "failed"
-    }
-    def stressTest = project.properties['stressTest']
-    if (stressTest != null) systemProperties['stressTest'] = stressTest
-}
diff --git a/integration-testing/build.gradle b/integration-testing/build.gradle
index b1cbc08..c5a551a 100644
--- a/integration-testing/build.gradle
+++ b/integration-testing/build.gradle
@@ -1,10 +1,12 @@
-import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType
-
 /*
- * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
+ * Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
  */
 
-apply from: rootProject.file("gradle/compile-jvm.gradle")
+import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType
+
+plugins {
+    id("kotlin-jvm-conventions")
+}
 
 repositories {
     mavenLocal()