Skip to content

Commit

Permalink
Integrate buildconfig plugin to project to replace old version templa…
Browse files Browse the repository at this point in the history
…tes work (#677)

This uses a nicer plugin to handle buildconfig constants

<!--
  ⬆ Put your description above this! ⬆

  Please be descriptive and detailed.
  
Please read our [Contributing
Guidelines](https://github.com/tinyspeck/slack-gradle-plugin/blob/main/.github/CONTRIBUTING.md)
and [Code of Conduct](https://slackhq.github.io/code-of-conduct).

Don't worry about deleting this, it's not visible in the PR!
-->
  • Loading branch information
ZacSweers committed Dec 11, 2023
1 parent f0bbc3e commit b5627ca
Show file tree
Hide file tree
Showing 18 changed files with 172 additions and 137 deletions.
16 changes: 0 additions & 16 deletions agp-handlers/agp-handler-80/build.gradle.kts

This file was deleted.

3 changes: 0 additions & 3 deletions agp-handlers/agp-handler-80/gradle.properties

This file was deleted.

26 changes: 26 additions & 0 deletions agp-handlers/agp-handler-82/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
plugins {
kotlin("jvm")
alias(libs.plugins.ksp)
alias(libs.plugins.mavenPublish)
alias(libs.plugins.buildConfig)
}

buildConfig {
packageName("slack.gradle.agphandler.v82")
buildConfigField("String", "AGP_VERSION", libs.versions.agp.map { "\"$it\"" })
useKotlinOutput {
topLevelConstants = true
internalVisibility = true
}
}

dependencies {
ksp(libs.autoService.ksp)

api(projects.agpHandlers.agpHandlerApi)

implementation(libs.autoService.annotations)

compileOnly(gradleApi())
compileOnly(libs.agp)
}
3 changes: 3 additions & 0 deletions agp-handlers/agp-handler-82/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
POM_ARTIFACT_ID=sgp-agp-handler-82
POM_NAME=Slack Gradle Plugin (AGP Handler 8.2)
POM_DESCRIPTION=Slack Gradle Plugin (AGP Handler 8.2)
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,35 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package slack.gradle.agphandler.v80
package slack.gradle.agphandler.v82

import com.android.build.api.AndroidPluginVersion
import com.android.build.gradle.internal.SdkLocator
import com.google.auto.service.AutoService
import java.io.File
import org.gradle.api.provider.ProviderFactory
import slack.gradle.agp.AgpHandler
import slack.gradle.agp.computeAndroidPluginVersion
import slack.gradle.agp.internal.NoOpIssueReporter

public class AgpHandler80 private constructor(override val agpVersion: AndroidPluginVersion) :
public class AgpHandler82 private constructor(override val agpVersion: AndroidPluginVersion) :
AgpHandler {

override fun getAndroidSdkDirectory(projectRootDir: File, providers: ProviderFactory): File =
SdkLocator.getSdkDirectory(projectRootDir, NoOpIssueReporter)

@AutoService(AgpHandler.Factory::class)
public class Factory : AgpHandler.Factory {
override val minVersion: AndroidPluginVersion = AndroidPluginVersion(8, 0, 0)
override val minVersion: AndroidPluginVersion by lazy {
computeAndroidPluginVersion(AGP_VERSION)
}

// TODO Remove once it's public
// https://issuetracker.google.com/issues/297440098
// Public in AGP 8.3 https://issuetracker.google.com/issues/297440098
@Suppress("invisible_reference", "invisible_member")
override val currentVersion: AndroidPluginVersion by lazy {
com.android.build.api.extension.impl.CURRENT_AGP_VERSION
}

override fun create(): AgpHandler = AgpHandler80(currentVersion)
override fun create(): AgpHandler = AgpHandler82(currentVersion)
}
}
12 changes: 11 additions & 1 deletion agp-handlers/agp-handler-83/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@ plugins {
kotlin("jvm")
alias(libs.plugins.ksp)
alias(libs.plugins.mavenPublish)
alias(libs.plugins.buildConfig)
}

buildConfig {
packageName("slack.gradle.agphandler.v83")
buildConfigField("String", "AGP_VERSION", libs.versions.agpAlpha.map { "\"$it\"" })
useKotlinOutput {
topLevelConstants = true
internalVisibility = true
}
}

dependencies {
Expand All @@ -11,6 +21,6 @@ dependencies {

implementation(libs.autoService.annotations)

compileOnly("com.android.tools.build:gradle:8.3.0-alpha17")
compileOnly(gradleApi())
compileOnly(libs.agpAlpha)
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import com.google.auto.service.AutoService
import java.io.File
import org.gradle.api.provider.ProviderFactory
import slack.gradle.agp.AgpHandler
import slack.gradle.agp.computeAndroidPluginVersion
import slack.gradle.agp.internal.NoOpIssueReporter

public class AgpHandler83 private constructor(override val agpVersion: AndroidPluginVersion) :
Expand All @@ -36,13 +37,9 @@ public class AgpHandler83 private constructor(override val agpVersion: AndroidPl

@AutoService(AgpHandler.Factory::class)
public class Factory : AgpHandler.Factory {
override val minVersion: AndroidPluginVersion =
AndroidPluginVersion(
8,
3,
0,
)
.alpha(13)
override val minVersion: AndroidPluginVersion by lazy {
computeAndroidPluginVersion(AGP_VERSION)
}

override val currentVersion: AndroidPluginVersion by lazy { AndroidPluginVersion.getCurrent() }

Expand Down
5 changes: 5 additions & 0 deletions agp-handlers/agp-handler-api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,9 @@ dependencies {

compileOnly(gradleApi())
compileOnly(libs.agp)

testImplementation(gradleApi())
testImplementation(libs.agp)
testImplementation(libs.junit)
testImplementation(libs.truth)
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import com.android.build.api.AndroidPluginVersion
import java.io.File
import org.gradle.api.provider.ProviderFactory

private val NUMBER_REGEX = Regex("d")

/** An interface for handling different AGP versions via (mostly) version-agnostic APIs. */
public interface AgpHandler {
/** The current AGP version. */
Expand Down Expand Up @@ -46,3 +48,32 @@ public interface AgpHandler {
/** Returns a new [AndroidPluginVersion] with any preview information stripped. */
public val AndroidPluginVersion.baseVersion: AndroidPluginVersion
get() = AndroidPluginVersion(major, minor, micro)

/** Returns a computed [AndroidPluginVersion] for the given [input] version string. */
public fun computeAndroidPluginVersion(input: String): AndroidPluginVersion {
val split = input.split('-')
require(split.isNotEmpty()) { "Could not parse AGP version from '$input'" }
val baseVersionNumberStrings = split[0].split('.')
val (major, minor, micro) =
Array(3) { index ->
if (baseVersionNumberStrings.size >= index + 1) {
baseVersionNumberStrings[index].toInt()
} else {
0
}
}
val baseVersion = AndroidPluginVersion(major, minor, micro)
return if (split.size == 2) {
// There's a preview here
val (previewType, number) = split[1].partition { !it.isDigit() }
when (previewType) {
"alpha" -> baseVersion.alpha(number.toInt())
"beta" -> baseVersion.beta(number.toInt())
"rc" -> baseVersion.rc(number.toInt())
"dev" -> baseVersion.dev()
else -> error("Unrecognized preview type '$previewType' with version '$number'")
}
} else {
baseVersion
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (C) 2023 Slack Technologies, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package slack.gradle.agp

import com.google.common.truth.Truth.assertThat
import org.junit.Test

class AgpHandlersTest {
@Test
fun parseTest() {
assertVersion("8.0.0-alpha01")
assertVersion("8.0.0-beta01")
assertVersion("8.0.0-beta11")
assertVersion("8.0.0-rc11")
assertVersion("8.0.0-dev")
}

private fun assertVersion(version: String) {
val versionToCheck =
if (version[version.lastIndex - 1] == '0') {
// AGP doesn't print the leading zero in their versions here
version.substring(0, version.lastIndex - 1) + version[version.lastIndex]
} else {
version
}
assertThat(computeAndroidPluginVersion(version).toString())
.isEqualTo("Android Gradle Plugin version $versionToCheck")
}
}
30 changes: 13 additions & 17 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
import com.diffplug.gradle.spotless.KotlinExtension
import com.diffplug.gradle.spotless.SpotlessExtension
import com.github.gmazzo.gradle.plugins.BuildConfigExtension
import com.vanniktech.maven.publish.MavenPublishBaseExtension
import dev.bmac.gradle.intellij.GenerateBlockMapTask
import dev.bmac.gradle.intellij.PluginUploader
Expand Down Expand Up @@ -56,6 +57,7 @@ plugins {
alias(libs.plugins.sortDependencies) apply false
alias(libs.plugins.intellij) apply false
alias(libs.plugins.pluginUploader) apply false
alias(libs.plugins.buildConfig) apply false
}

configure<DetektExtension> {
Expand Down Expand Up @@ -167,14 +169,6 @@ data class KotlinBuildConfig(val kotlin: String) {
// https://kotlinlang.org/docs/whatsnew1520.html#support-for-jspecify-nullness-annotations
"-Xjspecify-annotations=strict",
)

fun asTemplatesMap(): Map<String, String> {
return mapOf(
"kotlinCompilerArgs" to kotlinCompilerArgs.joinToString(", ") { "\"$it\"" },
"kotlinJvmCompilerArgs" to kotlinJvmCompilerArgs.joinToString(", ") { "\"$it\"" },
"kotlinVersion" to kotlin
)
}
}

tasks.dokkaHtmlMultiModule {
Expand All @@ -186,16 +180,18 @@ val kotlinVersion = libs.versions.kotlin.get()
val kotlinBuildConfig = KotlinBuildConfig(kotlinVersion)

subprojects {
// This is overly magic but necessary in order to plumb this
// down to subprojects
tasks
.withType<Copy>()
.matching { it.name == "copyVersionTemplates" }
.configureEach {
val templatesMap = kotlinBuildConfig.asTemplatesMap()
inputs.property("buildversions", templatesMap.hashCode())
expand(templatesMap)
if (project.path == ":slack-plugin") {
project.pluginManager.withPlugin("com.github.gmazzo.buildconfig") {
configure<BuildConfigExtension> {
buildConfigField("String", "KOTLIN_VERSION", "\"$kotlinVersion\"")
// Using Any here due to https://github.com/gmazzo/gradle-buildconfig-plugin/issues/9
buildConfigField("kotlin.Any", "KOTLIN_COMPILER_ARGS",
"listOf(${kotlinBuildConfig.kotlinCompilerArgs.joinToString(", ") { "\"$it\"" }})")
buildConfigField("kotlin.Any", "KOTLIN_JVM_COMPILER_ARGS",
"listOf(${kotlinBuildConfig.kotlinJvmCompilerArgs.joinToString(", ") { "\"$it\"" }})")
}
}
}

pluginManager.withPlugin("java") {
configure<JavaPluginExtension> {
Expand Down
3 changes: 3 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[versions]
agp = "8.2.0"
agpAlpha = "8.3.0-alpha17"
anvil = "2.4.8"
bugsnagGradle = "8.1.0"
compose-jb = "1.5.11"
Expand Down Expand Up @@ -28,6 +29,7 @@ wire = "4.9.3"

[plugins]
bestPracticesPlugin = { id = "com.autonomousapps.plugin-best-practices-plugin", version = "0.10" }
buildConfig = { id = "com.github.gmazzo.buildconfig", version = "4.2.0" }
dependencyAnalysis = { id = "com.autonomousapps.dependency-analysis", version.ref = "dependencyAnalysisPlugin" }
detekt = { id = "io.gitlab.arturbosch.detekt", version.ref = "detekt" }
dokka = { id = "org.jetbrains.dokka", version.ref = "dokka" }
Expand All @@ -46,6 +48,7 @@ wire = { id = "com.squareup.wire", version.ref = "wire" }

[libraries]
agp = { module = "com.android.tools.build:gradle", version.ref = "agp" }
agpAlpha = { module = "com.android.tools.build:gradle", version.ref = "agpAlpha" }
autoService-annotations = "com.google.auto.service:auto-service-annotations:1.1.1"
autoService-ksp = "dev.zacsweers.autoservice:auto-service-ksp:1.1.0"
bugsnag = "com.bugsnag:bugsnag:3.7.1"
Expand Down
7 changes: 6 additions & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ pluginManagement {
exclusiveContent {
forRepository(::gradlePluginPortal)
filter {
includeModule("com.github.gmazzo.buildconfig", "plugin")
includeModule(
"com.github.gmazzo.buildconfig",
"com.github.gmazzo.buildconfig.gradle.plugin"
)
includeModule("com.github.ben-manes", "gradle-versions-plugin")
includeModule(
"com.github.ben-manes.versions",
Expand Down Expand Up @@ -174,7 +179,7 @@ rootProject.name = "slack-gradle-plugin"

// Please keep these in alphabetical order!
include(
":agp-handlers:agp-handler-80",
":agp-handlers:agp-handler-82",
":agp-handlers:agp-handler-83",
":agp-handlers:agp-handler-api",
":skate-plugin",
Expand Down
36 changes: 10 additions & 26 deletions skate-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import com.jetbrains.plugin.structure.base.utils.exists
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import java.nio.file.Paths
import java.util.Locale
import kotlin.io.path.readText
Expand All @@ -9,6 +8,7 @@ plugins {
alias(libs.plugins.kotlin.jvm)
alias(libs.plugins.intellij)
alias(libs.plugins.pluginUploader)
alias(libs.plugins.buildConfig)
}

group = "com.slack.intellij"
Expand Down Expand Up @@ -61,32 +61,16 @@ fun readGitRepoCommit(): String? {
}
}

// region Version.kt template for setting the project version in the build
sourceSets {
main { java.srcDir(layout.buildDirectory.dir("generated/sources/version-templates/kotlin/main")) }
}

val copyVersionTemplatesProvider =
tasks.register<Copy>("copySkateVersionTemplates") {
inputs.property("version", project.property("VERSION_NAME"))
from(project.layout.projectDirectory.dir("version-templates"))
into(project.layout.buildDirectory.dir("generated/sources/version-templates/kotlin/main"))
expand(
mapOf(
"projectVersion" to project.property("VERSION_NAME").toString(),
"bugsnagKey" to project.findProperty("SgpIntellijBugsnagKey")?.toString().orEmpty(),
"gitSha" to readGitRepoCommit().orEmpty(),
)
)
filteringCharset = "UTF-8"
buildConfig {
packageName("com.slack.sgp.intellij")
buildConfigField("String", "VERSION", "\"${project.property("VERSION_NAME")}\"")
buildConfigField("String", "BUGSNAG_KEY", "\"${project.findProperty("SgpIntellijBugsnagKey")?.toString().orEmpty()}\"")
buildConfigField("String", "GIT_SHA", provider { "\"${readGitRepoCommit().orEmpty()}\"" })
useKotlinOutput {
topLevelConstants = true
internalVisibility = true
}

tasks.withType<KotlinCompile>().configureEach { dependsOn(copyVersionTemplatesProvider) }

tasks
.matching { it.name == "kotlinSourcesJar" }
.configureEach { dependsOn(copyVersionTemplatesProvider) }
// endregion
}

dependencies {
implementation(libs.bugsnag) { exclude(group = "org.slf4j") }
Expand Down
Loading

0 comments on commit b5627ca

Please sign in to comment.