Skip to content

Commit

Permalink
build: refactor internal plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
fractalwrench committed Feb 7, 2025
1 parent 3ee7398 commit bbaf8d2
Show file tree
Hide file tree
Showing 18 changed files with 145 additions and 45 deletions.
12 changes: 12 additions & 0 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

group = "io.embrace.internal"
version = "1.0.0"

plugins {
`kotlin-dsl`
`kotlin-dsl-precompiled-script-plugins`
Expand All @@ -22,6 +25,15 @@ dependencies {
implementation("org.jetbrains.kotlinx:kover-gradle-plugin:0.9.1")
}

gradlePlugin {
plugins {
create("embracePlugin") {
id = "io.embrace.internal.build-logic"
implementationClass = "io.embrace.internal.BuildPlugin"
}
}
}

// ensure the Kotlin + Java compilers both use the same language level.
project.tasks.withType(JavaCompile::class.java).configureEach {
sourceCompatibility = JavaVersion.VERSION_1_8.toString()
Expand Down
31 changes: 0 additions & 31 deletions buildSrc/src/main/kotlin/embrace-test-defaults.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,6 @@ plugins {
id("io.gitlab.arturbosch.detekt") apply false
}

android {
compileSdk = Versions.COMPILE_SDK

defaultConfig {
minSdk = Versions.MIN_SDK
}

compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}

lint {
abortOnError = true
warningsAsErrors = true
checkAllWarnings = true
checkReleaseBuilds = false // run on CI instead, speeds up release builds
baseline = project.file("lint-baseline.xml")
disable.addAll(mutableSetOf("GradleDependency", "NewerVersionAvailable"))
}

kotlin {
compilerOptions {
apiVersion.set(KotlinVersion.KOTLIN_1_8)
languageVersion.set(KotlinVersion.KOTLIN_1_8)
jvmTarget.set(JvmTarget.JVM_1_8)
allWarningsAsErrors = true
}
}
}

dependencies {
add("detektPlugins", findLibrary("detekt.formatting"))
}
Expand Down
12 changes: 0 additions & 12 deletions buildSrc/src/main/kotlin/enable-explicit-api-mode.gradle.kts

This file was deleted.

76 changes: 76 additions & 0 deletions buildSrc/src/main/kotlin/io/embrace/internal/BuildPlugin.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package io.embrace.internal

import gradle.kotlin.dsl.accessors._3413c52528fd46b4e275cc05d0f006bb.android
import gradle.kotlin.dsl.accessors._3413c52528fd46b4e275cc05d0f006bb.kotlin
import gradle.kotlin.dsl.accessors._3413c52528fd46b4e275cc05d0f006bb.kotlinOptions
import io.embrace.gradle.Versions
import org.gradle.api.JavaVersion
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.kotlin.dsl.assign
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.dsl.KotlinVersion

class BuildPlugin : Plugin<Project> {

override fun apply(project: Project) {
val extension = project.extensions.create("embrace", EmbraceBuildLogicExtension::class.java)
project.configureAndroidExtension()

if (extension.explicitApiMode.get()) {
project.configureExplicitApiMode()
}
// project.logger.lifecycle(extension.printSomething.get())
// }
// project.logger.lifecycle("Embrace Build Plugin applied!")
//
// // Example: Apply common settings to all subprojects
// project.subprojects {
// plugins.apply("java-library")
//
// // Configure dependencies
// dependencies.add("implementation", "org.jetbrains.kotlin:kotlin-stdlib")
// }
}

private fun Project.configureAndroidExtension() {
android {
compileSdk = Versions.COMPILE_SDK

defaultConfig {
minSdk = Versions.MIN_SDK
}

compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}

lint {
abortOnError = true
warningsAsErrors = true
checkAllWarnings = true
checkReleaseBuilds = false // run on CI instead, speeds up release builds
baseline = project.file("lint-baseline.xml")
disable.addAll(mutableSetOf("GradleDependency", "NewerVersionAvailable"))
}

kotlin {
compilerOptions {
apiVersion.set(KotlinVersion.KOTLIN_1_8)
languageVersion.set(KotlinVersion.KOTLIN_1_8)
jvmTarget.set(JvmTarget.JVM_1_8)
allWarningsAsErrors = true
}
}
}
}
private fun Project.configureExplicitApiMode() {
android {
kotlinOptions {
freeCompilerArgs = freeCompilerArgs + listOf("-Xexplicit-api=strict")
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.embrace.internal

import org.gradle.api.model.ObjectFactory
import org.gradle.api.provider.Property

/**
* Controls Embrace build logic for a given module.
*/
abstract class EmbraceBuildLogicExtension(objectFactory: ObjectFactory) { // TODO: support Java modules

/**
* Whether binary compatibility checks should be enabled for this module to avoid adding
* unintentional API changes.
*/
val apiCompatChecks: Property<Boolean> =
objectFactory.property(Boolean::class.java).convention(false) // TODO: support

/**
* Whether Kotlin's explicit API mode should be enabled.
*/
val explicitApiMode: Property<Boolean> =
objectFactory.property(Boolean::class.java).convention(false) // TODO: support

/**
* If this is false then the module will not be published & will be treated as something that
* contains internal code used for testing.
*/
val productionModule: Property<Boolean> =
objectFactory.property(Boolean::class.java).convention(true) // TODO: support
}
7 changes: 6 additions & 1 deletion embrace-android-api/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
plugins {
id("embrace-prod-defaults")
id("enable-explicit-api-mode")
id("io.embrace.internal.build-logic")
}

embrace {
explicitApiMode.set(true)
apiCompatChecks.set(true)
}

description = "Embrace Android SDK: API"
Expand Down
1 change: 1 addition & 0 deletions embrace-android-compose/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
plugins {
id("embrace-prod-defaults")
id("io.embrace.internal.build-logic")
}

description = "Embrace Android SDK: Jetpack Compose"
Expand Down
5 changes: 5 additions & 0 deletions embrace-android-core/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
plugins {
id("embrace-prod-defaults")
id("io.embrace.internal.build-logic")
}

embrace {
// printSomething.set("Hello from Embrace!")
}

description = "Embrace Android SDK: Core"
Expand Down
1 change: 1 addition & 0 deletions embrace-android-delivery/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
plugins {
id("embrace-prod-defaults")
id("io.embrace.internal.build-logic")
}

description = "Embrace Android SDK: Delivery"
Expand Down
1 change: 1 addition & 0 deletions embrace-android-fcm/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
plugins {
id("embrace-prod-defaults")
id("io.embrace.internal.build-logic")
}

description = "Embrace Android SDK: Firebase Cloud Messaging"
Expand Down
1 change: 1 addition & 0 deletions embrace-android-features/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
plugins {
id("embrace-prod-defaults")
id("io.embrace.internal.build-logic")
}

description = "Embrace Android SDK: Features"
Expand Down
1 change: 1 addition & 0 deletions embrace-android-infra/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
plugins {
id("embrace-prod-defaults")
id("io.embrace.internal.build-logic")
}

description = "Embrace Android SDK: Infra"
Expand Down
1 change: 1 addition & 0 deletions embrace-android-okhttp3/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
plugins {
id("embrace-prod-defaults")
id("io.embrace.internal.build-logic")
}

description = "Embrace Android SDK: OkHttp3"
Expand Down
1 change: 1 addition & 0 deletions embrace-android-payload/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
plugins {
id("embrace-prod-defaults")
id("io.embrace.internal.build-logic")
id("com.google.devtools.ksp")
}

Expand Down
7 changes: 6 additions & 1 deletion embrace-android-sdk/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ import io.embrace.gradle.Versions

plugins {
id("embrace-prod-defaults")
id("enable-explicit-api-mode")
id("io.embrace.internal.build-logic")
id("com.google.devtools.ksp")
}

embrace {
explicitApiMode.set(true)
apiCompatChecks.set(true)
}

description = "Embrace Android SDK: Core"

android {
Expand Down
1 change: 1 addition & 0 deletions embrace-internal-api/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
plugins {
id("embrace-prod-defaults")
id("io.embrace.internal.build-logic")
}

description = "Embrace Android SDK: Internal API"
Expand Down
1 change: 1 addition & 0 deletions embrace-test-common/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
plugins {
id("embrace-test-defaults")
id("io.embrace.internal.build-logic")
}

android {
Expand Down
1 change: 1 addition & 0 deletions embrace-test-fakes/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
plugins {
id("embrace-test-defaults")
id("io.embrace.internal.build-logic")
}

android {
Expand Down

0 comments on commit bbaf8d2

Please sign in to comment.