Skip to content

Commit

Permalink
Merge pull request #15 from kanyun-inc/feature/kotlin-1.8
Browse files Browse the repository at this point in the history
Feature/kotlin 1.8
  • Loading branch information
RicardoJiang authored Feb 6, 2023
2 parents bb9b327 + 0db4741 commit 549de19
Show file tree
Hide file tree
Showing 18 changed files with 92 additions and 56 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,10 @@ kace {
The Kace currently supports the above four most commonly used types. Other types supported by kotlin-android-extensions such as android.app.Fragment, android.app.Dialog, kotlinx.android.extensions.LayoutContainer are deprecated or rarely used, currently not supported by the Kace

## Version compatible
| | Kotlin | AGP | Gradle |
|---------------------------|--------|-------|--------|
| Minimum Supported Version | 1.7.0 | 4.2.0 | 6.7.1 |
| Kace | Kotlin | AGP | Gradle |
|----------------------|--------|-------|--------|
| 1.0.2 | 1.7.0 | 4.2.0 | 6.7.1 |
| 1.8.0-1.0.2-SNAPSHOT | 1.8.0 | 4.2.0 | 6.8.3 |

Since the goal of the Kace is to help developers easily upgrade to Kotlin 1.8, the minimum supported version of Kotlin is relatively high

Expand Down
7 changes: 4 additions & 3 deletions README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,10 @@ kace {
Kace 目前支持了以上四种最常用的类型,其他 kotlin-android-extensions 支持的类型如 android.app.Fragment, android.app.Dialog, kotlinx.android.extensions.LayoutContainer 等,由于被废弃或者使用较少,Kace 目前没有做支持

## 版本兼容
| | Kotlin | AGP | Gradle |
|--------|--------|-------|--------|
| 最低支持版本 | 1.7.0 | 4.2.0 | 6.7.1 |
| Kace | Kotlin | AGP | Gradle |
|----------------------|--------|-------|--------|
| 1.0.2 | 1.7.0 | 4.2.0 | 6.7.1 |
| 1.8.0-1.0.2-SNAPSHOT | 1.8.0 | 4.2.0 | 6.8.3 |

由于 Kace 的目标是帮助开发者更方便地迁移到 Kotlin 1.8,因此 Kotlin 最低支持版本比较高

Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
plugins {
id("com.android.application") version "4.2.0" apply false
id("com.android.library") version "4.2.0" apply false
id("org.jetbrains.kotlin.android") version "1.7.0" apply false
id("org.jetbrains.kotlin.android") version "1.8.0" apply false
id("com.vanniktech.maven.publish") version "0.18.0" apply false
id("com.github.gmazzo.buildconfig") version "2.1.0" apply false
}
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ kotlin.code.style=official
android.nonTransitiveRClass=true

KOTLIN_PLUGIN_ID=com.kanyun.kace
VERSION_NAME=1.7.0-1.0.3
VERSION_NAME=1.8.0-1.0.3-SNAPSHOT

GROUP=com.kanyun.kace

Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Thu Sep 29 08:11:34 CST 2022
distributionBase=GRADLE_USER_HOME
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.3-bin.zip
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
3 changes: 1 addition & 2 deletions kace-compiler/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ dependencies {
testImplementation(kotlin("test-junit"))
testImplementation("org.jetbrains.kotlin:kotlin-compiler-embeddable")

testImplementation("com.bennyhuo.kotlin:kotlin-compile-testing-extensions:1.7.10.2")
testImplementation("com.github.tschuchortdev:kotlin-compile-testing:1.4.9")
testImplementation("com.bennyhuo.kotlin:kotlin-compile-testing-extensions:1.8.0.0-SNAPSHOT")
}

val compileKotlin: KotlinCompile by tasks
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ import com.kanyun.kace.compiler.options.Options
import org.jetbrains.kotlin.compiler.plugin.AbstractCliOption
import org.jetbrains.kotlin.compiler.plugin.CliOption
import org.jetbrains.kotlin.compiler.plugin.CommandLineProcessor
import org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi
import org.jetbrains.kotlin.config.CompilerConfiguration

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,19 @@
package com.kanyun.kace.compiler

import com.google.auto.service.AutoService
import com.kanyun.kace.compiler.utils.Logger
import org.jetbrains.kotlin.backend.common.extensions.IrGenerationExtension
import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys
import org.jetbrains.kotlin.com.intellij.mock.MockProject
import org.jetbrains.kotlin.compiler.plugin.ComponentRegistrar
import org.jetbrains.kotlin.compiler.plugin.CompilerPluginRegistrar
import org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi
import org.jetbrains.kotlin.config.CompilerConfiguration
import org.jetbrains.kotlin.resolve.extensions.SyntheticResolveExtension

lateinit var logger: Logger
@OptIn(ExperimentalCompilerApi::class)
@AutoService(CompilerPluginRegistrar::class)
class KaceCompilerPluginRegistrar : CompilerPluginRegistrar() {

@AutoService(ComponentRegistrar::class)
class KaceComponentRegistrar : ComponentRegistrar {

override fun registerProjectComponents(
project: MockProject,
configuration: CompilerConfiguration
) {
logger = Logger(configuration.get(CLIConfigurationKeys.ORIGINAL_MESSAGE_COLLECTOR_KEY)!!)
IrGenerationExtension.registerExtension(project, KaceIrGenerationExtension())
SyntheticResolveExtension.registerExtension(project, KaceSyntheticResolveExtension())
override val supportsK2: Boolean = false
override fun ExtensionStorage.registerExtensions(configuration: CompilerConfiguration) {
IrGenerationExtension.registerExtension(KaceIrGenerationExtension())
SyntheticResolveExtension.registerExtension(KaceSyntheticResolveExtension())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.kanyun.kace.compiler.options

import com.kanyun.kace.compiler.logger
import java.lang.ref.WeakReference
import org.jetbrains.kotlin.compiler.plugin.AbstractCliOption
import org.jetbrains.kotlin.compiler.plugin.CliOption
Expand Down Expand Up @@ -94,12 +93,4 @@ object Options {
fun setup(compilerConfiguration: CompilerConfiguration) {
this.compilerConfiguration = WeakReference(compilerConfiguration)
}

fun dump() {
logger.warn(
allOptions.joinToString {
"${it.option.optionName}: ${it()}"
}
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ package com.kanyun.kace.compiler.utils

import org.jetbrains.kotlin.name.FqName

val ANDROID_EXTENSIONS_PACKAGE_NAME = "com.kanyun.kace"
val ANDROID_EXTENSIONS_CLASS_NAME = "AndroidExtensions"
val ANDROID_EXTENSIONS_FULL_NAME = "com.kanyun.kace.AndroidExtensions"
const val ANDROID_EXTENSIONS_PACKAGE_NAME = "com.kanyun.kace"
const val ANDROID_EXTENSIONS_CLASS_NAME = "AndroidExtensions"

val ANDROID_EXTENSIONS_FQNAME = FqName(ANDROID_EXTENSIONS_FULL_NAME)
val ANDROID_EXTENSIONS_BASE_FQNAME = FqName("com.kanyun.kace.AndroidExtensionsBase")
val ANDROID_EXTENSIONS_IMPL_FQNAME = FqName("com.kanyun.kace.AndroidExtensionsImpl")
const val ANDROID_EXTENSIONS_FULL_NAME = "com.kanyun.kace.AndroidExtensions"
const val ANDROID_EXTENSIONS_BASE_FULL_NAME = "com.kanyun.kace.AndroidExtensionsBase"
const val ANDROID_EXTENSIONS_IMPL_FULL_NAME = "com.kanyun.kace.AndroidExtensionsImpl"
const val ANDROID_VIEW_FULL_NAME = "android.view.View"

val ANDROID_VIEW_FQNAME = FqName("android.view.View")
val ANDROID_EXTENSIONS_FQNAME = FqName(ANDROID_EXTENSIONS_FULL_NAME)

val IMPLICIT_ANDROID_EXTENSIONS_TYPES = setOf(
"android.app.Activity",
Expand Down
14 changes: 8 additions & 6 deletions kace-compiler/src/main/java/com/kanyun/kace/compiler/utils/Ir.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import org.jetbrains.kotlin.ir.declarations.IrSimpleFunction
import org.jetbrains.kotlin.ir.types.classFqName
import org.jetbrains.kotlin.ir.types.defaultType
import org.jetbrains.kotlin.ir.util.functions
import org.jetbrains.kotlin.name.ClassId

fun IrClass.findViewByIdCached(pluginContext: IrPluginContext): IrSimpleFunction? {
return functions.find {
Expand All @@ -33,9 +34,10 @@ fun IrClass.findViewByIdCached(pluginContext: IrPluginContext): IrSimpleFunction
fun IrFunction.isFindViewByIdCached(pluginContext: IrPluginContext): Boolean {
return name.identifier == FIND_VIEW_BY_ID_CACHED_NAME &&
valueParameters.size == 2 &&
valueParameters[0].type == pluginContext.referenceClass(
ANDROID_EXTENSIONS_BASE_FQNAME
)?.defaultType &&
valueParameters[0].type ==
pluginContext.referenceClass(
ClassId.fromString(ANDROID_EXTENSIONS_BASE_FULL_NAME)
)?.defaultType &&
valueParameters[1].type == pluginContext.symbols.int.defaultType
}

Expand All @@ -44,10 +46,10 @@ fun IrClass.isAndroidExtensions(): Boolean {
}

fun IrPluginContext.typeOfAndroidExtensionsBase() =
referenceClass(ANDROID_EXTENSIONS_BASE_FQNAME)!!.defaultType
referenceClass(ClassId.fromString(ANDROID_EXTENSIONS_BASE_FULL_NAME))!!.defaultType

fun IrPluginContext.typeOfView() =
referenceClass(ANDROID_VIEW_FQNAME)!!.defaultType
referenceClass(ClassId.fromString(ANDROID_VIEW_FULL_NAME))!!.defaultType

fun IrPluginContext.symbolOfAndroidExtensionImpl() =
referenceClass(ANDROID_EXTENSIONS_IMPL_FQNAME)!!
referenceClass(ClassId.fromString(ANDROID_EXTENSIONS_IMPL_FULL_NAME))!!
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.kanyun.kace.compiler.utils

import org.jetbrains.kotlin.backend.common.ir.allOverridden
import org.jetbrains.kotlin.backend.jvm.ir.erasedUpperBound
import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.ir.builders.declarations.addFunction
Expand All @@ -27,6 +26,7 @@ import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.expressions.impl.IrGetValueImpl
import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.ir.types.classOrNull
import org.jetbrains.kotlin.ir.util.allOverridden
import org.jetbrains.kotlin.ir.util.fqNameWhenAvailable
import org.jetbrains.kotlin.ir.util.functions
import org.jetbrains.kotlin.ir.util.parentClassOrNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import com.bennyhuo.kotlin.compiletesting.extensions.module.KotlinModule
import com.bennyhuo.kotlin.compiletesting.extensions.module.checkResult
import com.bennyhuo.kotlin.compiletesting.extensions.source.FileBasedModuleInfoLoader
import com.kanyun.kace.compiler.options.Options
import org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi
import org.junit.Test

class KaceTest {
Expand All @@ -29,20 +30,22 @@ class KaceTest {
testBase("basic.txt")
}

@OptIn(ExperimentalCompilerApi::class)
private fun testBase(fileName: String) {
val loader = FileBasedModuleInfoLoader("testData/$fileName")
val sourceModuleInfos = loader.loadSourceModuleInfos()

Options.isEnabled.set(true)

val modules = sourceModuleInfos.map {
KotlinModule(it, componentRegistrars = listOf(KaceComponentRegistrar()))
KotlinModule(it, compilerPluginRegistrars = listOf(KaceCompilerPluginRegistrar()))
}

modules.checkResult(
loader.loadExpectModuleInfos(),
executeEntries = true,
checkCompilerOutput = true
checkCompilerOutput = true,
checkGeneratedIr = true
)
}
}
45 changes: 44 additions & 1 deletion kace-compiler/testData/basic.txt
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,47 @@ Third!!
class FourthActivity
3
class FifthActivity
4
4
// FILE: Main.kt.ir
class MainActivity : AndroidExtensions {
private val $$delegate_0: AndroidExtensionsImpl = AndroidExtensionsImpl()
override fun <T: View?> findViewByIdCached(owner: AndroidExtensionsBase, id: Int): T? {
return <this>.$$delegate_0.findViewByIdCached(owner, id)
}
}
class SecondActivity : AndroidExtensions {
private var $$androidExtensionsImpl: AndroidExtensionsImpl = AndroidExtensionsImpl()
override fun <T: View> findViewByIdCached(owner: AndroidExtensionsBase, id: Int): T? {
return <this>.$$androidExtensionsImpl.findViewByIdCached(owner, id)
}
}
class ThirdActivity : AndroidExtensions {
override fun <T: View?> findViewByIdCached(owner: AndroidExtensionsBase, id: Int): T? {
println(<<IrGetClassImpl>>)
return View("Third!!")
}
}
class FourthActivity : Activity, AndroidExtensions {
private var $$androidExtensionsImpl: AndroidExtensionsImpl = AndroidExtensionsImpl()
override fun <T: View> findViewByIdCached(owner: AndroidExtensionsBase, id: Int): T? {
return <this>.$$androidExtensionsImpl.findViewByIdCached(owner, id)
}
}
class FifthActivity : BaseActivity, AndroidExtensions {
private var $$androidExtensionsImpl: AndroidExtensionsImpl = AndroidExtensionsImpl()
override fun <T: View> findViewByIdCached(owner: AndroidExtensionsBase, id: Int): T? {
return <this>.$$androidExtensionsImpl.findViewByIdCached(owner, id)
}
}
fun main() {
val main = MainActivity()
println(main.findViewByIdCached(main, 0))
val second = SecondActivity()
println(second.findViewByIdCached(second, 1))
val third = ThirdActivity()
println(third.findViewByIdCached(third, 2))
val fouth = FourthActivity()
println(fouth.findViewByIdCached(fouth, 3))
val fifth = FifthActivity()
println(fifth.findViewByIdCached(fifth, 4))
}
2 changes: 1 addition & 1 deletion kace-sample/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
plugins {
id("com.android.application") apply false
id("com.android.library") apply false
id("org.jetbrains.kotlin.android") version "1.7.10" apply false
id("org.jetbrains.kotlin.android") version "1.8.0" apply false
id("com.kanyun.kace") version "0.0.0-SNAPSHOT" apply false
}

Expand Down
2 changes: 1 addition & 1 deletion kace-sample/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.3-bin.zip
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ rootProject.name = "Kace"

include(":kace-compiler")
include(":kace-gradle-plugin")
include(":kace-runtime")
include(":kace-runtime")
2 changes: 1 addition & 1 deletion test_agp.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function testUnderAGPVersion() {

cd kace-sample

setGradleVersion 6.7.1
setGradleVersion 6.8.3
testUnderAGPVersion 4.2.0

setGradleVersion 7.3.3
Expand Down

0 comments on commit 549de19

Please sign in to comment.