Skip to content

Commit

Permalink
Refactor Gradle build logic
Browse files Browse the repository at this point in the history
  • Loading branch information
StaehliJ authored and MGaetan89 committed Mar 15, 2024
1 parent 0edbc63 commit 03aeb0f
Show file tree
Hide file tree
Showing 27 changed files with 363 additions and 366 deletions.
1 change: 1 addition & 0 deletions build-logic/gradle.properties
70 changes: 70 additions & 0 deletions build-logic/plugins/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright (c) SRG SSR. All rights reserved.
* License information is available from the LICENSE file.
*/

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
`kotlin-dsl`
}

group = "ch.srgssr.pillarbox.gradle"

java {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}

tasks.withType<KotlinCompile>().configureEach {
kotlinOptions {
jvmTarget = JavaVersion.VERSION_17.majorVersion
}
}

dependencies {
compileOnly(libs.android.gradle.api)
compileOnly(libs.kotlinx.kover.gradle)
compileOnly(libs.kotlin.gradle.plugin)
}

tasks {
validatePlugins {
enableStricterValidation = true
failOnWarning = true
}
}

gradlePlugin {
plugins {
register("PillarboxAndroidApplication") {
id = "ch.srgssr.pillarbox.gradle.android_application"
implementationClass = "ch.srgssr.pillarbox.gradle.PillarboxAndroidApplicationPlugin"
}

register("PillarboxAndroidLibrary") {
id = "ch.srgssr.pillarbox.gradle.android_library"
implementationClass = "ch.srgssr.pillarbox.gradle.PillarboxAndroidLibraryPlugin"
}

register("PillarboxAndroidLibraryCompose") {
id = "ch.srgssr.pillarbox.gradle.android_library_compose"
implementationClass = "ch.srgssr.pillarbox.gradle.PillarboxAndroidLibraryComposePlugin"
}

register("PillarboxAndroidLibraryLint") {
id = "ch.srgssr.pillarbox.gradle.android_library_lint"
implementationClass = "ch.srgssr.pillarbox.gradle.PillarboxAndroidLibraryLintPlugin"
}

register("PillarboxAndroidLibraryPublishing") {
id = "ch.srgssr.pillarbox.gradle.android_library_publishing"
implementationClass = "ch.srgssr.pillarbox.gradle.PillarboxAndroidLibraryPublishingPlugin"
}

register("PillarboxAndroidLibraryTestedModule") {
id = "ch.srgssr.pillarbox.gradle.android_library_tested_module"
implementationClass = "ch.srgssr.pillarbox.gradle.PillarboxAndroidLibraryTestedModulePlugin"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright (c) SRG SSR. All rights reserved.
* License information is available from the LICENSE file.
*/
package ch.srgssr.pillarbox.gradle

import ch.srgssr.pillarbox.gradle.internal.AppConfig
import ch.srgssr.pillarbox.gradle.internal.VersionConfig
import ch.srgssr.pillarbox.gradle.internal.configureAndroidLintModule
import ch.srgssr.pillarbox.gradle.internal.configureAndroidModule
import ch.srgssr.pillarbox.gradle.internal.configureComposeModule
import ch.srgssr.pillarbox.gradle.internal.configureKotlinModule
import com.android.build.api.dsl.ApplicationExtension
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.kotlin.dsl.configure
import org.gradle.kotlin.dsl.extra

/**
* Custom Gradle plugin to configure an Android library module for Pillarbox.
*/
class PillarboxAndroidApplicationPlugin : Plugin<Project> {
override fun apply(target: Project) = with(target) {
pluginManager.apply("com.android.application")
pluginManager.apply("org.jetbrains.kotlin.android")

extensions.configure<ApplicationExtension> {
configureAndroidLintModule(this)
configureAndroidModule(this)
configureComposeModule(this)

defaultConfig {
applicationId = namespace
targetSdk = AppConfig.targetSdk
versionCode = VersionConfig.versionCode()
versionName = VersionConfig.versionName()
vectorDrawables.useSupportLibrary = true
}

signingConfigs {
create("release") {
val password = System.getenv("DEMO_KEY_PASSWORD") ?: extra.properties["pillarbox.keystore.password"] as String?

storeFile = file("./demo.keystore")
storePassword = password
keyAlias = "demo"
keyPassword = password
}
}

buildTypes {
debug {
applicationIdSuffix = ".debug"
versionNameSuffix = "-debug"
}

release {
signingConfig = signingConfigs.getByName("release")
isMinifyEnabled = false
isDebuggable = true

proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
}
}
}

configureKotlinModule()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright (c) SRG SSR. All rights reserved.
* License information is available from the LICENSE file.
*/
package ch.srgssr.pillarbox.gradle

import ch.srgssr.pillarbox.gradle.internal.configureComposeModule
import com.android.build.api.dsl.LibraryExtension
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.kotlin.dsl.configure

/**
* Custom Gradle plugin to configure Compose in an Android library module for Pillarbox.
*/
class PillarboxAndroidLibraryComposePlugin : Plugin<Project> {
override fun apply(target: Project) = with(target) {
pluginManager.apply("com.android.library")

extensions.configure<LibraryExtension> {
configureComposeModule(this)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright (c) SRG SSR. All rights reserved.
* License information is available from the LICENSE file.
*/
package ch.srgssr.pillarbox.gradle

import ch.srgssr.pillarbox.gradle.internal.configureAndroidLintModule
import com.android.build.api.dsl.LibraryExtension
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.kotlin.dsl.configure

/**
* Custom Gradle plugin to configure Lint in an Android library module for Pillarbox.
*/
class PillarboxAndroidLibraryLintPlugin : Plugin<Project> {
override fun apply(target: Project) = with(target) {
pluginManager.apply("com.android.library")

extensions.configure<LibraryExtension> {
configureAndroidLintModule(this)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (c) SRG SSR. All rights reserved.
* License information is available from the LICENSE file.
*/
package ch.srgssr.pillarbox.gradle

import ch.srgssr.pillarbox.gradle.internal.configureAndroidModule
import ch.srgssr.pillarbox.gradle.internal.configureKotlinModule
import com.android.build.api.dsl.LibraryExtension
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.kotlin.dsl.configure

/**
* Custom Gradle plugin to configure an Android library module for Pillarbox.
*/
class PillarboxAndroidLibraryPlugin : Plugin<Project> {
override fun apply(target: Project) = with(target) {
pluginManager.apply("com.android.library")
pluginManager.apply("org.jetbrains.kotlin.android")

extensions.configure<LibraryExtension> {
configureAndroidModule(this)

defaultConfig {
consumerProguardFiles("consumer-rules.pro")
}

buildTypes {
release {
isMinifyEnabled = false

proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
}
}
}

configureKotlinModule()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/
package ch.srgssr.pillarbox.gradle

import VersionConfig
import ch.srgssr.pillarbox.gradle.internal.VersionConfig
import com.android.build.api.dsl.LibraryExtension
import org.gradle.api.Plugin
import org.gradle.api.Project
Expand All @@ -15,9 +15,9 @@ import org.gradle.kotlin.dsl.get
import org.gradle.kotlin.dsl.register

/**
* Custom Gradle plugin to manage publication of a Pillarbox's library modules.
* Custom Gradle plugin to configure publication in an Android library module for Pillarbox.
*/
class PillarboxPublishingPlugin : Plugin<Project> {
class PillarboxAndroidLibraryPublishingPlugin : Plugin<Project> {
override fun apply(target: Project) = with(target) {
pluginManager.apply("com.android.library")
pluginManager.apply("org.gradle.maven-publish")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import org.gradle.kotlin.dsl.configure
import org.gradle.kotlin.dsl.withType

/**
* Custom Gradle plugin to configure a Pillarbox module for testing.
* Custom Gradle plugin to configure testing in an Android library module for Pillarbox.
*/
class PillarboxTestedModulePlugin : Plugin<Project> {
class PillarboxAndroidLibraryTestedModulePlugin : Plugin<Project> {
override fun apply(target: Project) = with(target) {
pluginManager.apply("com.android.library")
pluginManager.apply("org.jetbrains.kotlinx.kover")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Copyright (c) SRG SSR. All rights reserved.
* License information is available from the LICENSE file.
*/
package ch.srgssr.pillarbox.gradle.internal

import org.gradle.api.JavaVersion

internal object AppConfig {
internal const val minSdk = 21
internal const val targetSdk = 34
internal const val compileSdk = 34
internal const val androidXComposeCompiler = "1.5.10"

// When changing this value, don't forget to also update the Detekt config in the root `build.gradle.kts` file
internal val javaVersion = JavaVersion.VERSION_17
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright (c) SRG SSR. All rights reserved.
* License information is available from the LICENSE file.
*/
package ch.srgssr.pillarbox.gradle.internal

import com.android.build.api.dsl.CommonExtension
import org.gradle.api.Project
import org.gradle.kotlin.dsl.withType
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

internal fun Project.configureAndroidModule(extension: CommonExtension<*, *, *, *, *, *>) = with(extension) {
namespace = "ch.srgssr.pillarbox." + name.removePrefix("pillarbox-").replace('-', '.')
compileSdk = AppConfig.compileSdk

defaultConfig {
minSdk = AppConfig.minSdk
}

compileOptions {
sourceCompatibility = AppConfig.javaVersion
targetCompatibility = AppConfig.javaVersion
}

buildFeatures {
resValues = false
shaders = false
}
}

internal fun configureComposeModule(extension: CommonExtension<*, *, *, *, *, *>) = with(extension) {
buildFeatures {
compose = true
}

composeOptions {
kotlinCompilerExtensionVersion = AppConfig.androidXComposeCompiler
}
}

internal fun Project.configureKotlinModule() {
tasks.withType<KotlinCompile>().configureEach {
kotlinOptions {
jvmTarget = AppConfig.javaVersion.majorVersion
}
}
}

internal fun Project.configureAndroidLintModule(extension: CommonExtension<*, *, *, *, *, *>) = with(extension) {
lint {
abortOnError = true
checkAllWarnings = true
checkDependencies = true
sarifReport = true
sarifOutput = file("${rootProject.rootDir}/build/reports/android-lint/$name.sarif")
disable.add("LogConditional")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,14 @@
* Copyright (c) SRG SSR. All rights reserved.
* License information is available from the LICENSE file.
*/
package ch.srgssr.pillarbox.gradle.internal

/**
* VersionConfig will build
* - VersionName for Pillarbox Demo
* - Version for Libraries
*/
object VersionConfig {
/**
* Environment variable automatically set by GitHub actions.
* @see [GitHub](https://docs.github.com/en/actions/learn-github-actions/variables#default-environment-variables)
*/
val isCI: Boolean = System.getenv("CI")?.toBooleanStrictOrNull() ?: false

internal object VersionConfig {
/**
* Environment variable set by workflow.
*/
Expand All @@ -26,7 +21,7 @@ object VersionConfig {
*
* @return "Local" if [ENV_VERSION_NAME] no set.
*/
fun versionName(): String {
internal fun versionName(): String {
return ENV_VERSION_NAME ?: "Local"
}

Expand All @@ -35,7 +30,7 @@ object VersionConfig {
* It assumes that major.minor.patch each <= 99
* 0.0.0, 0.0.99, 0.1.0, 0.99.99
*/
fun versionCode(): Int {
internal fun versionCode(): Int {
return ENV_VERSION_NAME
?.let { versionOnlyRegex.find(it)?.value }
?.let {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ dependencyResolutionManagement {
}
}
}

include(":plugins")
Loading

0 comments on commit 03aeb0f

Please sign in to comment.