Skip to content

Commit

Permalink
Remove KSP and make experimentalTransformEnabled default (#141)
Browse files Browse the repository at this point in the history
* Remove KSP and make experimentalTransformEnabled default

* Remove additional unuseds

* Fix

* More
  • Loading branch information
rbro112 authored May 23, 2024
1 parent 042f921 commit 2dfd431
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 120 deletions.
1 change: 0 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ plugins {
alias(libs.plugins.kotlin.android) apply false
alias(libs.plugins.kotlin.jvm) apply false
alias(libs.plugins.kotlin.serialization) apply false
alias(libs.plugins.ksp) apply false
}

allprojects {
Expand Down
4 changes: 0 additions & 4 deletions gradle-plugin/plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,6 @@ dependencies {
// from one dependency conflicting with that of dexlib2, so we'll use the same version here.
implementation(libs.guava)

// Needs to be impl as users might not always have the plugin applied, which compileOnly
// would require.
implementation(libs.ksp.gradle.plugin)

testImplementation(libs.junit.jupiter.api)
testImplementation(libs.google.truth)
testRuntimeOnly(libs.junit.jupiter.engine)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import com.android.build.api.variant.TestVariant
import com.android.build.api.variant.Variant
import com.android.build.gradle.internal.dsl.BaseAppModuleExtension
import com.android.build.gradle.internal.tasks.factory.dependsOn
import com.android.build.gradle.internal.utils.KOTLIN_ANDROID_PLUGIN_ID
import com.emergetools.android.gradle.instrumentation.snapshots.SnapshotsPreviewRuntimeRetentionTransformFactory
import com.emergetools.android.gradle.tasks.internal.SaveExtensionConfigTask
import com.emergetools.android.gradle.tasks.perf.GeneratePerfProject
Expand All @@ -28,14 +27,11 @@ import com.emergetools.android.gradle.tasks.upload.BaseUploadTask.Companion.setU
import com.emergetools.android.gradle.util.AgpVersions
import com.emergetools.android.gradle.util.capitalize
import com.emergetools.android.gradle.util.orEmpty
import com.google.devtools.ksp.gradle.KspExtension
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.artifacts.ProjectDependency
import org.gradle.api.provider.Property
import org.gradle.api.tasks.StopExecutionException
import org.gradle.api.tasks.TaskProvider
import org.jetbrains.kotlin.gradle.dsl.KotlinAndroidProjectExtension

class EmergePlugin : Plugin<Project> {

Expand Down Expand Up @@ -97,16 +93,6 @@ class EmergePlugin : Plugin<Project> {
appProject: Project,
emergeExtension: EmergePluginExtension,
) {
// Only configure KSP for snapshot generation if the experimental transform is disabled
if (!emergeExtension.snapshotOptions.experimentalTransformEnabled.getOrElse(false)) {
appProject.afterEvaluate {
configureAppProjectSnapshots(
appProject = appProject,
emergeExtension = emergeExtension
)
}
}

appProject.pluginManager.withPlugin(ANDROID_APPLICATION_PLUGIN_ID) { _ ->
val androidComponents = appProject.extensions.getByType(
ApplicationAndroidComponentsExtension::class.java
Expand Down Expand Up @@ -318,16 +304,14 @@ class EmergePlugin : Plugin<Project> {
variant: ApplicationVariant,
androidTest: AndroidTest,
) {
if (extension.snapshotOptions.experimentalTransformEnabled.getOrElse(false)) {
variant.instrumentation.let { instrumentation ->
instrumentation.transformClassesWith(
SnapshotsPreviewRuntimeRetentionTransformFactory::class.java,
InstrumentationScope.ALL,
) { params ->
// Force invalidate/reinstrument classes if debug option is set
if (extension.debugOptions.forceInstrumentation.getOrElse(false)) {
params.invalidate.set(System.currentTimeMillis())
}
variant.instrumentation.let { instrumentation ->
instrumentation.transformClassesWith(
SnapshotsPreviewRuntimeRetentionTransformFactory::class.java,
InstrumentationScope.ALL,
) { params ->
// Force invalidate/reinstrument classes if debug option is set
if (extension.debugOptions.forceInstrumentation.getOrElse(false)) {
params.invalidate.set(System.currentTimeMillis())
}
}
}
Expand All @@ -348,7 +332,9 @@ class EmergePlugin : Plugin<Project> {
return appProject.tasks.register(taskName, PackageSnapshotArtifacts::class.java) {
it.artifactDir.set(variant.artifacts.get(SingleArtifact.APK))
it.testArtifactDir.set(androidTest.artifacts.get(SingleArtifact.APK))
it.outputDirectory.set(appProject.layout.buildDirectory.dir("${BUILD_OUTPUT_DIR_NAME}/snapshots/artifacts"))
it.outputDirectory.set(
appProject.layout.buildDirectory.dir("${BUILD_OUTPUT_DIR_NAME}/snapshots/artifacts")
)
it.artifactMetadataPath.set(appProject.layout.buildDirectory.file("${BUILD_OUTPUT_DIR_NAME}/snapshots/artifacts/${ArtifactMetadata.JSON_FILE_NAME}"))
it.agpVersion.set(AgpVersions.CURRENT.toString())
}
Expand Down Expand Up @@ -410,87 +396,6 @@ class EmergePlugin : Plugin<Project> {
}
}

private fun configureAppProjectSnapshots(
appProject: Project,
emergeExtension: EmergePluginExtension,
) {
applyMainSourceSetConfig(
project = appProject,
appProject = appProject,
emergeExtension = emergeExtension
)

// Configure all dependent modules to apply the same configuration as the appProject so
// generated snapshots don't run into compilation issues.
appProject.configurations
.flatMap { it.dependencies }
.filterIsInstance<ProjectDependency>()
.map { it.dependencyProject }
.distinct()
.filter { !it.state.executed }
.forEach { subproject ->
subproject.afterEvaluate {
applyMainSourceSetConfig(
project = it,
appProject = appProject,
emergeExtension = emergeExtension
)
}
}
}

private fun applyMainSourceSetConfig(
project: Project,
appProject: Project,
emergeExtension: EmergePluginExtension,
) {
val errorMessages = mutableListOf<String>()
if (!project.plugins.hasPlugin(KSP_PLUGIN_ID)) {
errorMessages.add(
"${project.name} does not have the KSP plugin applied. " +
"Please apply the KSP plugin to the ${project.name} module " +
"for Emerge snapshot test generation of composables in this module."
)
}

if (!project.plugins.hasPlugin(KOTLIN_ANDROID_PLUGIN_ID)) {
errorMessages.add(
"${project.name} does not have the Kotlin Android plugin applied. " +
"Please apply the Kotlin Android plugin to the ${project.name} module " +
"for Emerge snapshot test generation of composables in this module."
)
}

// Return early with an info-level log if the KSP/Kotlin Android plugin isn't applied.
if (errorMessages.isNotEmpty()) {
errorMessages.forEach(appProject.logger::info)
return
}

// TODO: Ryan: Explore using variants API for finding proper ourput dir.
val emergeSrcDir = "${project.buildDir}/$BUILD_OUTPUT_DIR_NAME/ksp/debugAndroidTest/kotlin"

val internalSnapshotsEnabled =
emergeExtension.snapshotOptions.experimentalInternalSnapshotsEnabled.getOrElse(false)
val internalEnabledArg = if (internalSnapshotsEnabled) "true" else "false"

appProject.logger.info(
"Configuring ${project.name} for Emerge snapshot testing, outputting to $emergeSrcDir"
)
project.extensions.getByType(KspExtension::class.java).apply {
arg(OUTPUT_SRC_DIR_OPTION_NAME, emergeSrcDir)
arg(INTERNAL_ENABLED_OPTION_NAME, internalEnabledArg)
}

appProject.extensions.getByType(KotlinAndroidProjectExtension::class.java).apply {
val androidTestSourceSet = sourceSets.getByName("androidTest")
appProject.logger.info(
"Configuring ${project.name} for Emerge snapshot testing, adding $emergeSrcDir to sourceSet ${androidTestSourceSet.name}"
)
sourceSets.getByName("androidTest").kotlin.srcDir(emergeSrcDir)
}
}

@Suppress("UnstableApiUsage")
private fun configurePerformanceProject(
performanceProject: Project,
Expand Down Expand Up @@ -599,12 +504,8 @@ class EmergePlugin : Plugin<Project> {

private const val ANDROID_APPLICATION_PLUGIN_ID = "com.android.application"
private const val ANDROID_TEST_PLUGIN_ID = "com.android.test"
private const val KSP_PLUGIN_ID = "com.google.devtools.ksp"
const val EMERGE_JUNIT_RUNNER = "com.emergetools.test.EmergeJUnitRunner"

private const val OUTPUT_SRC_DIR_OPTION_NAME = "emerge.outputDir"
private const val INTERNAL_ENABLED_OPTION_NAME = "emerge.experimentalInternalEnabled"

private const val GENERATE_PERF_PROJECT_TASK_NAME = "emergeGeneratePerformanceProject"

private val PERFORMANCE_PROJECT_DEPENDENCIES = listOf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,6 @@ abstract class SnapshotOptions : ProductOptions() {

abstract val snapshotsStorageDirectory: DirectoryProperty

abstract val experimentalInternalSnapshotsEnabled: Property<Boolean>

abstract val experimentalTransformEnabled: Property<Boolean>

abstract val apiVersion: Property<Int>
}

Expand Down
1 change: 0 additions & 1 deletion snapshots/sample/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ emerge {

snapshots {
tag.setFromVariant()
experimentalTransformEnabled.set(true)
}
}

Expand Down

0 comments on commit 2dfd431

Please sign in to comment.