Skip to content

Commit

Permalink
chore: use common kmp plugin (#1012)
Browse files Browse the repository at this point in the history
  • Loading branch information
aajtodd authored and lauzadis committed Aug 18, 2023
1 parent 7125cfa commit c2d89a0
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 277 deletions.
82 changes: 33 additions & 49 deletions aws-runtime/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,25 @@
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
import aws.sdk.kotlin.gradle.kmp.*

description = "AWS client runtime support for generated service clients"

plugins {
kotlin("multiplatform")
id("org.jetbrains.dokka")
id("org.jetbrains.kotlinx.binary-compatibility-validator") version "0.12.1"
jacoco
}

val platforms = listOf("common", "jvm")

// Allow subprojects to use internal API's
// See: https://kotlinlang.org/docs/reference/opt-in-requirements.html#opting-in-to-using-api
val optinAnnotations = listOf(
"kotlin.RequiresOptIn",
)

fun projectNeedsPlatform(project: Project, platform: String): Boolean {
val files = project.projectDir.listFiles()
val hasPosix = files.any { it.name == "posix" }
val hasDarwin = files.any { it.name == "darwin" }

if (hasPosix && platform == "darwin") return false
if (hasDarwin && platform == "posix") return false
if (!hasPosix && !hasDarwin && platform == "darwin") return false
// add implicit JVM target if it has a common module
return files.any { it.name == platform || (it.name == "common" && platform == "jvm") }
}

kotlin {
jvm() // Create a JVM target with the default name 'jvm'
}

val sdkVersion: String by project

val coroutinesVersion: String by project
val kotestVersion: String by project
val slf4jVersion: String by project

subprojects {
if (!needsKmpConfigured) return@subprojects

group = "aws.sdk.kotlin"
version = sdkVersion

Expand All @@ -47,39 +29,41 @@ subprojects {
plugin("org.jetbrains.dokka")
}

logger.info("configuring: $project")

// this works by iterating over each platform name and inspecting the projects files. If the project contains
// a directory with the corresponding platform name we apply the common configuration settings for that platform
// (which includes adding the multiplatform target(s)). This makes adding platform support easy and implicit in each
// subproject.
platforms.forEach { platform ->
if (projectNeedsPlatform(project, platform)) {
configure(listOf(project)) {
logger.info("${project.name} needs platform: $platform")
apply(from = rootProject.file("gradle/$platform.gradle"))
}
}
}
apply(from = rootProject.file("gradle/publish.gradle"))

kotlin {
explicitApi()

sourceSets {
all {
val srcDir = if (name.endsWith("Main")) "src" else "test"
val resourcesPrefix = if (name.endsWith("Test")) "test-" else ""
// the name is always the platform followed by a suffix of either "Main" or "Test" (e.g. jvmMain, commonTest, etc)
val platform = name.substring(0, name.length - 4)
kotlin.srcDir("$platform/$srcDir")
resources.srcDir("$platform/${resourcesPrefix}resources")
languageSettings.progressiveMode = true
optinAnnotations.forEach { languageSettings.optIn(it) }
// dependencies available for all subprojects
named("commonMain") {
dependencies {
// FIXME - refactor to only projects that need this
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesVersion")
}
}

named("commonTest") {
dependencies {
implementation("io.kotest:kotest-assertions-core:$kotestVersion")
}
}

named("jvmTest") {
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-debug:$coroutinesVersion")
implementation("io.kotest:kotest-assertions-core-jvm:$kotestVersion")
implementation("org.slf4j:slf4j-simple:$slf4jVersion")
}
}
}
}

apply(from = rootProject.file("gradle/publish.gradle"))
kotlin.sourceSets.all {
// Allow subprojects to use internal APIs
// See https://kotlinlang.org/docs/reference/opt-in-requirements.html#opting-in-to-using-api
listOf("kotlin.RequiresOptIn").forEach { languageSettings.optIn(it) }
}

dependencies {
dokkaPlugin(project(":dokka-aws"))
Expand Down
41 changes: 18 additions & 23 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,32 @@
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
import aws.sdk.kotlin.gradle.kmp.typedProp
import java.net.URL
import java.time.Duration
import java.util.Properties

buildscript {
dependencies {
// Add our custom gradle plugin(s) to buildscript classpath (comes from github source)
// NOTE: buildscript classpath for the root project is the parent classloader for the subprojects, we
// only need to include it here, imports in subprojects will work automagically
classpath("aws.sdk.kotlin:build-plugins") {
version {
require("0.1.1")
}
}
}
}

plugins {
kotlin("jvm") version "1.8.22" apply false
id("org.jetbrains.dokka")
id("io.github.gradle-nexus.publish-plugin") version "1.1.0"
}

// configures (KMP) subprojects with our own KMP conventions and some default dependencies
apply(plugin = "aws.sdk.kotlin.kmp")

allprojects {
repositories {
mavenLocal()
Expand Down Expand Up @@ -75,25 +91,7 @@ subprojects {
}
}

val localProperties: Map<String, Any> by lazy {
val props = Properties()

listOf(
File(rootProject.projectDir, "local.properties"), // Project-specific local properties
File(rootProject.projectDir.parent, "local.properties"), // Workspace-specific local properties
File(System.getProperty("user.home"), ".sdkdev/local.properties"), // User-specific local properties
)
.filter(File::exists)
.map(File::inputStream)
.forEach(props::load)

props.mapKeys { (k, _) -> k.toString() }
}

fun Project.prop(name: String): Any? =
this.properties[name] ?: localProperties[name]

if (project.prop("kotlinWarningsAsErrors")?.toString()?.toBoolean() == true) {
if (project.typedProp<Boolean>("kotlinWarningsAsErrors") == true) {
subprojects {
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
kotlinOptions.allWarningsAsErrors = true
Expand Down Expand Up @@ -180,9 +178,6 @@ tasks.register<JavaExec>("ktlintFormat") {
jvmArgs("--add-opens", "java.base/java.lang=ALL-UNNAMED")
}

// configure coverage for the entire project
apply(from = rootProject.file("gradle/codecoverage.gradle"))

tasks.register("showRepos") {
doLast {
println("All repos:")
Expand Down
113 changes: 0 additions & 113 deletions gradle/codecoverage.gradle

This file was deleted.

17 changes: 0 additions & 17 deletions gradle/common.gradle

This file was deleted.

38 changes: 0 additions & 38 deletions gradle/jvm.gradle

This file was deleted.

Loading

0 comments on commit c2d89a0

Please sign in to comment.