Skip to content

Commit

Permalink
Kotlin Gradle MPP 1.3 (#544)
Browse files Browse the repository at this point in the history
* Basic setup

* Ignore failing test

* Gradle 5.0

* Better native config

* Fix jvm tests

* JS testing setup

* Native config

* Use EAP dependencies

* Japicmp setup

* Correct path to japicmp report

* Basic JVM + AnimalSniffer setup

* Enable samples module

* Use jvmWithJava for running Java tests

* Include more Native platforms to force test task generation

* Use internal UTF-8 decoder for Kotlin/Native

* Kotlin 1.3.20

* Gradle 5.1

* Move JMH setup into a separate module

* Properly configure jmhJar task

* Remove old jvm build.gradle

* Replace publishing plugin with SQLDelight's custom script

* Use default artifactId for JVM target

* Revert JVM build.gradle rename, use new DSL for defining targets

* Remove old module definitions in settings

* Feedback fixes
  • Loading branch information
Egor Andreevich authored and swankjesse committed Jan 30, 2019
1 parent d1bc009 commit 0078fca
Show file tree
Hide file tree
Showing 35 changed files with 450 additions and 234 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ after_success:
- .buildscript/deploy_snapshot.sh

after_failure:
- cat okio/jvm/build/reports/japi.txt
- cat okio/jvm/japicmp/build/reports/japi.txt

env:
global:
Expand Down
18 changes: 5 additions & 13 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
buildscript {
ext.versions = [
'kotlin': '1.2.60',
'kotlinNative': '0.8.2',
'jmhPlugin': '0.4.5',
'animalSnifferPlugin': '1.4.3',
'kotlin': '1.3.20',
'jmhPlugin': '0.4.8',
'animalSnifferPlugin': '1.4.6',
'dokka': '0.9.16',
'jmh': '1.20',
'jmh': '1.21',
'animalSniffer': '1.16',
'junit': '4.12',
'assertj': '1.7.0',
'shadowPlugin': '2.0.2',
'shadowPlugin': '4.0.4',
'nodePlugin': '1.2.0',
'mavenPublishPlugin': '0.4.0',
'ktlintPlugin': '5.1.0',
]

Expand All @@ -31,9 +29,6 @@ buildscript {
'jdk': "org.jetbrains.kotlin:kotlin-test-junit",
'js': "org.jetbrains.kotlin:kotlin-test-js",
],
'native': [
'gradlePlugin': "org.jetbrains.kotlin:kotlin-native-gradle-plugin:${versions.kotlinNative}",
]
],
'jmh': [
'gradlePlugin': "me.champeau.gradle:jmh-gradle-plugin:${versions.jmhPlugin}",
Expand All @@ -48,7 +43,6 @@ buildscript {
'dokka': "org.jetbrains.dokka:dokka-gradle-plugin:${versions.dokka}",
'shadow': "com.github.jengelman.gradle.plugins:shadow:${versions.shadowPlugin}",
'node': "com.moowork.gradle:gradle-node-plugin:${versions.nodePlugin}",
'mavenPublish': "com.vanniktech:gradle-maven-publish-plugin:${versions.mavenPublishPlugin}",
'ktlint': "gradle.plugin.org.jlleitschuh.gradle:ktlint-gradle:${versions.ktlintPlugin}",
'test': [
'junit': "junit:junit:${versions.junit}",
Expand All @@ -58,14 +52,12 @@ buildscript {

dependencies {
classpath deps.kotlin.gradlePlugin
classpath deps.kotlin.native.gradlePlugin
classpath deps.animalSniffer.gradlePlugin
classpath deps.japicmp
classpath deps.dokka
classpath deps.shadow
classpath deps.jmh.gradlePlugin
classpath deps.node
classpath deps.mavenPublish
classpath deps.ktlint
}

Expand Down
143 changes: 143 additions & 0 deletions gradle/gradle-mvn-mpp-push.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
apply plugin: 'maven-publish'
apply plugin: 'signing'
apply plugin: 'org.jetbrains.dokka'

dokka {
impliedPlatforms = ["Common"] // This will force platform tags for all non-common sources e.g. "JVM"
kotlinTasks {
// dokka fails to retrieve sources from MPP-tasks so they must be set empty to avoid exception
// use sourceRoot instead (see below)
[]
}
packageOptions {
prefix = "com.squareup.okio"
suppress = true
}
sourceRoot {
// assuming there is only a single source dir...
path = kotlin.sourceSets.commonMain.kotlin.srcDirs[0]
platforms = ["Common"]
}
if (kotlin.sourceSets.getNames().contains("jvmMain")) {
sourceRoot {
// assuming there is only a single source dir...
path = kotlin.sourceSets.jvmMain.kotlin.srcDirs[0]
platforms = ["JVM"]
}
}
if (kotlin.sourceSets.getNames().contains("jsMain")) {
sourceRoot {
// assuming there is only a single source dir...
path = kotlin.sourceSets.jsMain.kotlin.srcDirs[0]
platforms = ["js"]
}
}
if (kotlin.sourceSets.getNames().contains("nativeMain")) {
sourceRoot {
// assuming there is only a single source dir...
path = kotlin.sourceSets.nativeMain.kotlin.srcDirs[0]
platforms = ["native"]
}
}
}

def isReleaseBuild() {
return VERSION_NAME.contains("SNAPSHOT") == false
}

def getReleaseRepositoryUrl() {
return hasProperty('RELEASE_REPOSITORY_URL') ? RELEASE_REPOSITORY_URL :
"https://oss.sonatype.org/service/local/staging/deploy/maven2/"
}

def getSnapshotRepositoryUrl() {
return hasProperty('SNAPSHOT_REPOSITORY_URL') ? SNAPSHOT_REPOSITORY_URL :
"https://oss.sonatype.org/content/repositories/snapshots/"
}

def getRepositoryUsername() {
return hasProperty('SONATYPE_NEXUS_USERNAME') ? SONATYPE_NEXUS_USERNAME : ""
}

def getRepositoryPassword() {
return hasProperty('SONATYPE_NEXUS_PASSWORD') ? SONATYPE_NEXUS_PASSWORD : ""
}

task emptySourcesJar(type: Jar) {
classifier = 'sources'
}

task javadocsJar(type: Jar, dependsOn: dokka) {
classifier = 'javadoc'
from dokka.outputDirectory
}

signing {
required { isReleaseBuild() && gradle.taskGraph.hasTask("uploadArchives") }
sign(publishing.publications)
}

publishing {
publications.all {
artifact javadocsJar

pom.withXml {
def root = asNode()

root.children().last() + {
resolveStrategy = Closure.DELEGATE_FIRST

description POM_DESCRIPTION
name POM_NAME
url POM_URL
licenses {
license {
name POM_LICENCE_NAME
url POM_LICENCE_URL
distribution POM_LICENCE_DIST
}
}
scm {
url POM_SCM_URL
connection POM_SCM_CONNECTION
developerConnection POM_SCM_DEV_CONNECTION
}
developers {
developer {
id POM_DEVELOPER_ID
name POM_DEVELOPER_NAME
}
}
}
}
}

// Use default artifact name for the JVM target until we start publishing artifacts for all
// platforms
publications {
jvm {
artifactId = 'okio'
}
}

afterEvaluate {
publications.getByName('kotlinMultiplatform') {
// Source jars are only created for platforms, not the common artifact.
artifact emptySourcesJar
}
}

repositories {
maven {
url isReleaseBuild() ? getReleaseRepositoryUrl() : getSnapshotRepositoryUrl()
credentials {
username getRepositoryUsername()
password getRepositoryPassword()
}
}
maven {
name 'test'
url "file://${rootProject.buildDir}/localMaven"
}
}
}
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-5.1-all.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
2 changes: 1 addition & 1 deletion gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ APP_NAME="Gradle"
APP_BASE_NAME=`basename "$0"`

# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS=""
DEFAULT_JVM_OPTS='"-Xmx64m"'

# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD="maximum"
Expand Down
2 changes: 1 addition & 1 deletion gradlew.bat
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%

@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS=
set DEFAULT_JVM_OPTS="-Xmx64m"

@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome
Expand Down
88 changes: 83 additions & 5 deletions okio/build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,85 @@
apply plugin: 'org.jetbrains.kotlin.platform.common'
apply plugin: 'org.jetbrains.kotlin.multiplatform'

dependencies {
implementation deps.kotlin.stdLib.common
testImplementation deps.kotlin.test.common
testImplementation deps.kotlin.test.annotations
kotlin {
targets {
// egor: jvmWithJava is getting deprecated but it's not clear what the replacement is. For now
// gonna keep it inside targets {}
fromPreset(presets.jvmWithJava, 'jvm') {
project.sourceSets.test.java.srcDir('jvm/src/test/java')
}
}
js() {
configure([compilations.main, compilations.test]) {
tasks.getByName(compileKotlinTaskName).kotlinOptions {
moduleKind = "commonjs"
}
}
}
iosX64() {
compilations.main.outputKinds 'framework'
}
iosArm64() {
compilations.main.outputKinds 'framework'
}
// Required to generate tests tasks: https://youtrack.jetbrains.com/issue/KT-26547
linuxX64()
macosX64()
mingwX64('winX64')
sourceSets {
commonMain {
kotlin.srcDir('src/main/kotlin')
dependencies {
implementation deps.kotlin.stdLib.common
}
}
commonTest {
kotlin.srcDir('src/test/kotlin')
dependencies {
implementation deps.kotlin.test.common
implementation deps.kotlin.test.annotations
}
}
jvmMain {
kotlin.srcDir('jvm/src/main/java')
resources.srcDir('jvm/src/main/resources')
dependencies {
implementation deps.kotlin.stdLib.jdk6
compileOnly deps.animalSniffer.annotations
}
}
jvmTest {
kotlin.srcDir('jvm/src/test/java')
dependencies {
implementation deps.test.junit
implementation deps.test.assertj
implementation deps.kotlin.test.jdk
}
}
jsMain {
kotlin.srcDir('js/src/main/kotlin')
dependencies {
implementation deps.kotlin.stdLib.js
}
}
jsTest {
kotlin.srcDir('js/src/test/kotlin')
dependencies {
implementation deps.kotlin.test.js
}
}
nativeMain {
kotlin.srcDir('native/src/main/kotlin')
}
nativeTest {
kotlin.srcDir('native/src/test/kotlin')
}
}
configure([targets.iosX64, targets.iosArm64, targets.linuxX64, targets.macosX64, targets.winX64]) {
compilations.main.source(sourceSets.nativeMain)
compilations.test.source(sourceSets.nativeTest)
}
}

apply from: 'js/build.gradle'
apply from: 'jvm/jvm.gradle'
apply from: "$rootDir/gradle/gradle-mvn-mpp-push.gradle"
1 change: 0 additions & 1 deletion okio/jvm/gradle.properties → okio/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
POM_ARTIFACT_ID=okio
POM_NAME=Okio
POM_PACKAGING=jar
55 changes: 29 additions & 26 deletions okio/js/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,8 @@
* limitations under the License.
*/

apply plugin: 'org.jetbrains.kotlin.platform.js'
apply plugin: 'com.moowork.node'

dependencies {
expectedBy project(':okio')

implementation deps.kotlin.stdLib.js
testImplementation deps.kotlin.test.js
}

task populateNodeModules(type: Copy, dependsOn: compileKotlin2Js) {
from compileKotlin2Js.destinationDir
configurations.testCompileClasspath.each {
from zipTree(it.absolutePath).matching { include '*.js' }
}

into "${buildDir}/node_modules"
}

[compileKotlin2Js, compileTestKotlin2Js]*.configure {
kotlinOptions.moduleKind = "commonjs"
}

node {
download = true
}
Expand All @@ -45,9 +24,33 @@ task installMocha(type: NpmTask) {
args = ['install', 'mocha']
}

task runMocha(type: NodeTask, dependsOn: [compileTestKotlin2Js, populateNodeModules, installMocha]) {
script = file('node_modules/mocha/bin/mocha')
args = [compileTestKotlin2Js.outputFile]
}
kotlin.targets.matching { it.platformType.name == 'js' }.all { target ->
def testCompilation = target.compilations["test"]
def mainCompilation = target.compilations["main"]
def targetName = target.name

def testCompilationTask = tasks[testCompilation.compileKotlinTaskName]
def mainCompilationTask = tasks[mainCompilation.compileKotlinTaskName]

def populateNodeModules = task populateNodeModules(type: Copy, dependsOn: mainCompilationTask) {
from mainCompilationTask.destinationDir
into "$node.nodeModulesDir/node_modules"

def configuration = testCompilation.runtimeDependencyFiles
from(files {
configuration.collect { File file ->
file.name.endsWith(".jar") ?
zipTree(file.absolutePath).matching {
include '*.js'
} :
files()
}
}.builtBy(configuration))
}

test.dependsOn runMocha
task runMocha(type: NodeTask, dependsOn: [testCompilationTask, populateNodeModules, installMocha]) {
script = file("$node.nodeModulesDir/node_modules/mocha/bin/mocha")
args = [testCompilationTask.outputFile]
}
tasks["${targetName}Test"].dependsOn runMocha
}
Loading

0 comments on commit 0078fca

Please sign in to comment.