Skip to content

Commit

Permalink
Compose Support: GrowingIO Kotlin Compiler Plugin (#29)
Browse files Browse the repository at this point in the history
* GrowingIO Kotlin Compiler Plugin
---------

Co-authored-by: tianhui12 <[email protected]>
  • Loading branch information
cpacm and tianhui12 authored Dec 30, 2024
1 parent 6baa40d commit 00ee757
Show file tree
Hide file tree
Showing 12 changed files with 486 additions and 8 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ growingAutotracker {
}
giokit {
enabled true
autoInstallVersion "2.1.1"
autoInstallVersion "2.1.2"
}
}

Expand All @@ -54,7 +54,7 @@ dependencies {
implementation 'com.google.android.material:material:1.6.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'

implementation 'com.growingio.android:autotracker:4.1.0'
implementation 'com.growingio.android:autotracker:4.3.2'

testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
Expand Down
34 changes: 30 additions & 4 deletions autotracker-gradle-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ buildscript {
set("kotlin_version", "1.8.20")
set("agp_version", "8.1.0")
set("low_agp_version", "4.2.2")
set("releaseVersion", "4.3.1-SNAPSHOT")
set("releaseVersionCode", 40301)
set("releaseVersion", "4.4.0")
set("releaseVersionCode", 40400)
set("composeVersion", "1.0.0")
}
}

Expand All @@ -13,6 +14,7 @@ plugins {
`java-gradle-plugin`

id("com.gradle.plugin-publish") version ("1.2.0")
id("com.github.gmazzo.buildconfig") version ("3.1.0")
}

group = providers.gradleProperty("GROUP").get()
Expand All @@ -32,9 +34,23 @@ gradlePlugin {
description = providers.gradleProperty("POM_DESCRIPTION").get()
tags.set(listOf("growingio", "autotracker", "agp8"))
}

create("compose") {
id = "com.growingio.compose.plugin"
implementationClass = "com.growingio.compose.plugin.GrowingKotlinCompilerGradlePlugin"
tags.set(listOf("growingio", "compose", "kotlin compiler plugin"))
}
}
}

// provide by "com.github.gmazzo.buildconfig" plugin
buildConfig {
packageName("com.growingio")
className("BuildConfig")

buildConfigField("String", "Version", provider { "\"${project.extra["composeVersion"]}\"" })
}

val testPluginImplementation: Configuration by configurations.creating {
isCanBeResolved = true
isCanBeConsumed = false
Expand All @@ -55,6 +71,7 @@ configurations {
}
}


dependencies {

shadowed(project(":agp-wrapper-impl"))
Expand All @@ -70,6 +87,9 @@ dependencies {
compileOnly("com.android.tools.build:gradle-api:${rootProject.extra["agp_version"]}")
compileOnly("com.android.tools.build:gradle:${rootProject.extra["low_agp_version"]}")

// kotlin compiler plugin
compileOnly("org.jetbrains.kotlin:kotlin-gradle-plugin:1.8.20")

testImplementation(gradleTestKit())
testImplementation("junit:junit:4.13.2")
testImplementation("com.google.truth:truth:1.1.3")
Expand All @@ -92,7 +112,7 @@ java {
targetCompatibility = JavaVersion.VERSION_11
}

tasks.jar{
tasks.jar {
val dependencies = shadowed.filter {
it.name.startsWith("agp-")
}.map {
Expand All @@ -114,4 +134,10 @@ tasks.clean {
// 2. 放断点
// 3. 新建 remote JVM debug ,运行 debug
// 4. 运行构建,开始调试 ./gradlew assembleDebug
// apply("publishMavenWithPluginMarker.gradle")
// apply("publishMavenWithPluginMarker.gradle")

// fix sign plugin error
// val signingTasks = tasks.withType<Sign>()
// tasks.withType<AbstractPublishToMaven>().configureEach {
// mustRunAfter(signingTasks)
// }
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.growingio.compose.plugin

import com.growingio.BuildConfig
import org.gradle.api.provider.Provider
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilation
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilerPluginSupportPlugin
import org.jetbrains.kotlin.gradle.plugin.SubpluginArtifact
import org.jetbrains.kotlin.gradle.plugin.SubpluginOption

class GrowingKotlinCompilerGradlePlugin : KotlinCompilerPluginSupportPlugin {
override fun applyToCompilation(kotlinCompilation: KotlinCompilation<*>): Provider<List<SubpluginOption>> {
val project = kotlinCompilation.target.project
return project.provider {
emptyList()
}
}

override fun getCompilerPluginId(): String = "com.growingio.compose.plugin"

override fun getPluginArtifact(): SubpluginArtifact {
return SubpluginArtifact(
groupId = "com.growingio",
artifactId = "growingio-compose-plugin",
version = BuildConfig.Version
)
}

override fun isApplicable(kotlinCompilation: KotlinCompilation<*>): Boolean = true
}
6 changes: 4 additions & 2 deletions gradle/publishPluginToMavenLocal.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@

echo "packaging autotracker-gradle-plugin ..."
./gradlew :autotracker-gradle-plugin:clean \
&& ./gradlew :autotracker-gradle-plugin:saas-gradle-plugin:publishToMavenLocal
# ./gradlew :autotracker-gradle-plugin:publishToMavenLocal
&& ./gradlew :autotracker-gradle-plugin:publishToMavenLocal
# && ./gradlew :autotracker-gradle-plugin:saas-gradle-plugin:publishToMavenLocal
# && ./gradlew :growingio-compose-plugin:publishToMavenLocal

105 changes: 105 additions & 0 deletions growingio-compose-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import com.vanniktech.maven.publish.SonatypeHost

plugins {
kotlin("jvm") version "1.9.24"
kotlin("kapt") version "1.9.24"
id("distribution")
id("com.vanniktech.maven.publish") version "0.29.0"
id("org.jlleitschuh.gradle.ktlint") version "10.2.1"
}

allprojects {
repositories {
google()
mavenCentral()
}
}

ktlint {
debug.set(false)
verbose.set(true)
android.set(true)
outputToConsole.set(true)
ignoreFailures.set(false)
enableExperimentalRules.set(true)
filter {
exclude("**/generated/**")
include("**/kotlin/**")
}
}

val sep = File.separator
distributions {
main {
contents {
from("build${sep}libs")
from("build${sep}publications${sep}maven")
}
}
}

mavenPublishing {
coordinates("com.growingio", "growingio-compose-plugin", "1.0.0")

pom {
name.set("growingio-compose-plugin")
description.set("GrowingIO SDK Compose Kotlin Compile Plugin.")
url.set("https://github.com/growingio/growingio-sdk-android-plugin")
licenses {
license {
name.set("The Apache License, Version 2.0")
url.set("http://www.apache.org/licenses/LICENSE-2.0.txt")
distribution.set("http://www.apache.org/licenses/LICENSE-2.0.txt")
}
}
developers {
developer {
id.set("growingio")
name.set("GrowingIO SDK Team.")
url.set("https://github.com/growingio/")
}
}
scm {
url.set("https://github.com/growingio/growingio-sdk-android-plugin")
connection.set("scm:[email protected]:growingio/growingio-sdk-android-plugin.git")
developerConnection.set("scm:[email protected]:growingio/growingio-sdk-android-plugin.git")
}
}

publishToMavenCentral(SonatypeHost.S01)
signAllPublications()

// How to publish
// 1. set mavenCentralUsername=<> mavenCentralPassword=<> in your gradle.properties
// 2. ./gradlew :growingio-compose-plugin:publishToMavenCentral
}

tasks.named("distZip") {
dependsOn("publishToMavenLocal")
onlyIf {
inputs.sourceFiles.isEmpty.not().also {
require(it) { "No distribution to zip." }
}
}
}

repositories {
google()
maven("https://maven.pkg.jetbrains.space/public/p/compose/dev")
}

dependencies {
compileOnly("org.jetbrains.kotlin:kotlin-compiler-embeddable")

kapt("com.google.auto.service:auto-service:1.0.1")
compileOnly("com.google.auto.service:auto-service-annotations:1.0.1")

testImplementation(kotlin("test-junit"))
testImplementation("org.jetbrains.kotlin:kotlin-compiler-embeddable")
testImplementation("com.github.tschuchortdev:kotlin-compile-testing:1.6.0")
testImplementation("org.jetbrains.compose.desktop:desktop:1.6.10")
}

kapt {
correctErrorTypes = true
}
1 change: 1 addition & 0 deletions growingio-compose-plugin/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
kotlin.experimental.tryK2=true
1 change: 1 addition & 0 deletions growingio-compose-plugin/settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rootProject.name = ("growingio-compose-plugin")
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.growingio.compose.plugin

import com.google.auto.service.AutoService
import com.growingio.compose.plugin.compose.JetpackComposeTracingIrExtension
import org.jetbrains.kotlin.backend.common.extensions.IrGenerationExtension
import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
import org.jetbrains.kotlin.compiler.plugin.CompilerPluginRegistrar
import org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi
import org.jetbrains.kotlin.config.CompilerConfiguration

@OptIn(ExperimentalCompilerApi::class)
@AutoService(CompilerPluginRegistrar::class)
class GrowingIOKotlinCompilerPlugin : CompilerPluginRegistrar() {

override val supportsK2: Boolean
get() = true

override fun ExtensionStorage.registerExtensions(configuration: CompilerConfiguration) {
val messageCollector = configuration.get(
CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY,
MessageCollector.NONE
)
IrGenerationExtension.registerExtension(
extension = JetpackComposeTracingIrExtension(messageCollector)
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.growingio.compose.plugin

import com.google.auto.service.AutoService
import org.jetbrains.kotlin.compiler.plugin.AbstractCliOption
import org.jetbrains.kotlin.compiler.plugin.CommandLineProcessor
import org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi


@OptIn(ExperimentalCompilerApi::class)
@AutoService(CommandLineProcessor::class)
class GrowingIOKotlinCompilerPluginCommandLineProcessor : CommandLineProcessor {

override val pluginId: String = "com.growingio.compose.plugin"

override val pluginOptions: Collection<AbstractCliOption> = emptyList()

}
Loading

0 comments on commit 00ee757

Please sign in to comment.