Skip to content

Commit

Permalink
Hook default bundle task rather than having explicit initialize task (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
rbro112 authored Aug 7, 2024
1 parent 9f3f2af commit bbe3dc5
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ 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 org.gradle.api.DefaultTask
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.provider.Property
Expand Down Expand Up @@ -117,7 +116,7 @@ class EmergePlugin : Plugin<Project> {

// Always register the Reaper initialization task even if Reaper is disabled since users use
// it to help get Reaper setup for the first time.
registerInitializeReaperTask(appProject, emergeExtension, variant)
registerReaperTasks(appProject, emergeExtension, variant)

registerReaperTransform(
project = appProject,
Expand Down Expand Up @@ -220,49 +219,6 @@ class EmergePlugin : Plugin<Project> {
}
}

private fun registerInitializeReaperTask(
appProject: Project,
extension: EmergePluginExtension,
variant: Variant,
) {
val preflightTaskName = "${EMERGE_TASK_PREFIX}ValidateReaper${variant.name.capitalize()}"
val uploadAabTaskName = "${EMERGE_TASK_PREFIX}UploadReaperAab${variant.name.capitalize()}"
val initializeTaskName = "${EMERGE_TASK_PREFIX}InitializeReaper${variant.name.capitalize()}"

val preflightTask = appProject.tasks.register(preflightTaskName, PreflightReaper::class.java) {
it.group = EMERGE_TASK_GROUP
it.description = "Validate Reaper is initialized for variant ${variant.name}"
it.reaperEnabled.set(extension.reaperOptions.enabled)
it.reaperPublishableApiKey.set(extension.reaperOptions.publishableApiKey)
it.mergedManifestFile.set(variant.artifacts.get(SingleArtifact.MERGED_MANIFEST))

// We want preflight to happen pretty early so we can detect error conditions and give them
// nice messages. Specifically we need it to occur prior to 'linking' steps which we detect
// that the Reaper added instrumentation calls methods in the SDK to avoid confusing error
// messages.
val bundleTaskName = "minify${variant.name.capitalize()}WithR8"
val bundleTasks = appProject.getTasksByName(bundleTaskName, false)
if (bundleTasks.size != 0) {
bundleTasks.forEach { bundleTask -> bundleTask.dependsOn(it) }
}
}

appProject.tasks.register(uploadAabTaskName, InitializeReaper::class.java) {
it.group = EMERGE_TASK_GROUP
it.description = "Uploads an AAB for variant ${variant.name} to Emerge with Reaper instrumentation added."
it.artifact.set(variant.artifacts.get(SingleArtifact.BUNDLE))
it.setUploadTaskInputs(extension, appProject, variant)
it.setTagFromProductOptions(extension.reaperOptions, variant)
}

appProject.tasks.register(initializeTaskName, DefaultTask::class.java) {
it.group = EMERGE_TASK_GROUP
it.description = "Confirms Reaper is initialized and uploads an AAB for variant ${variant.name} to Emerge."
it.dependsOn(preflightTask)
it.dependsOn(uploadAabTaskName)
}
}

private fun registerPerformanceTasks(
appProject: Project,
performanceProject: Project,
Expand Down Expand Up @@ -470,6 +426,62 @@ class EmergePlugin : Plugin<Project> {
}
}

private fun registerReaperTasks(
appProject: Project,
extension: EmergePluginExtension,
variant: Variant,
) {
registerReaperPreflightTask(appProject, extension, variant)
// Only register upload task if Reaper is enabled
if (extension.reaperOptions.enabled.getOrElse(false)) {
registerReaperUploadTask(appProject, extension, variant)
}
}

private fun registerReaperPreflightTask(
appProject: Project,
extension: EmergePluginExtension,
variant: Variant,
) {
val preflightTaskName = "${EMERGE_TASK_PREFIX}ValidateReaper${variant.name.capitalize()}"
appProject.tasks.register(preflightTaskName, PreflightReaper::class.java) {
it.group = EMERGE_TASK_GROUP
it.description = "Validate Reaper is initialized for variant ${variant.name}"
it.reaperEnabled.set(extension.reaperOptions.enabled)
it.reaperPublishableApiKey.set(extension.reaperOptions.publishableApiKey)
it.mergedManifestFile.set(variant.artifacts.get(SingleArtifact.MERGED_MANIFEST))

// We want preflight to happen pretty early so we can detect error conditions and give them
// nice messages. Specifically we need it to occur prior to 'linking' steps which we detect
// that the Reaper added instrumentation calls methods in the SDK to avoid confusing error
// messages.
val minifyTaskName = "minify${variant.name.capitalize()}WithR8"
val minifyTasks = appProject.getTasksByName(minifyTaskName, false)
if (minifyTasks.size != 0) {
minifyTasks.forEach { minifyTask -> minifyTask.dependsOn(it) }
}
}
}

private fun registerReaperUploadTask(
appProject: Project,
extension: EmergePluginExtension,
variant: Variant,
) {
val uploadReaperAabTaskName = "${EMERGE_TASK_PREFIX}UploadReaperAab${variant.name.capitalize()}"
val uploadReaperAabTask =
appProject.tasks.register(uploadReaperAabTaskName, InitializeReaper::class.java) {
it.artifact.set(variant.artifacts.get(SingleArtifact.BUNDLE))
it.setUploadTaskInputs(extension, appProject, variant)
it.setTagFromProductOptions(extension.reaperOptions, variant)
}
// Hook the bundle tasks to run the reaper upload task after they complete.
appProject.afterEvaluate { project ->
val bundleTask = project.tasks.named("bundle${variant.name.capitalize()}")
bundleTask.configure { it.finalizedBy(uploadReaperAabTask) }
}
}

@Suppress("UnstableApiUsage")
private fun configurePerformanceProject(
performanceProject: Project,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ abstract class InitializeReaper : BaseUploadTask() {
checkNotNull(response) {
"Upload failed, please check your network connection and try again. ${response.toString()}"
}
logger.lifecycle("Reaper initialized. View Reaper status at the url:")
logger.lifecycle("Reaper initialized! View Reaper reports at the url:")
logger.lifecycle("https://emergetools.com/reaper/${response.uploadId}")
logger.lifecycle("Reaper processing can take up to 10 minutes.")
logger.lifecycle("Initial processing can take up to 10 minutes.")
}

companion object {
Expand Down

0 comments on commit bbe3dc5

Please sign in to comment.