-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[distribution] Add Gradle plugin for distribution
- Loading branch information
Showing
8 changed files
with
253 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
...rc/main/kotlin/com/emergetools/android/gradle/tasks/distribution/DistributionPreflight.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package com.emergetools.android.gradle.tasks.distribution | ||
|
||
import com.emergetools.android.gradle.tasks.base.BasePreflightTask | ||
import com.emergetools.android.gradle.util.preflight.Preflight | ||
import com.emergetools.android.gradle.util.preflight.PreflightFailure | ||
import org.gradle.api.provider.Property | ||
import org.gradle.api.tasks.Input | ||
import org.gradle.api.tasks.Optional | ||
import org.gradle.api.tasks.TaskAction | ||
|
||
abstract class DistributionPreflight : BasePreflightTask() { | ||
|
||
@get:Input | ||
@get:Optional | ||
abstract val hasEmergeApiToken: Property<Boolean> | ||
|
||
@get:Input | ||
@get:Optional | ||
abstract val distributionApiKey: Property<String> | ||
|
||
@get:Input | ||
@get:Optional | ||
abstract val distributionTag: Property<String> | ||
|
||
@get:Input | ||
@get:Optional | ||
abstract val distributionEnabled: Property<Boolean> | ||
|
||
@get:Input | ||
abstract val variantName: Property<String> | ||
|
||
@get:Input | ||
abstract val hasDistributionImplementationDependency: Property<Boolean> | ||
|
||
@TaskAction | ||
fun execute() { | ||
val preflight = Preflight("Reaper preflight check") | ||
|
||
val hasEmergeApiToken = hasEmergeApiToken.getOrElse(false) | ||
preflight.add("Emerge API token set") { | ||
if (!hasEmergeApiToken) { | ||
throw PreflightFailure("Emerge API token not set. See https://docs.emergetools.com/docs/uploading-basics#obtain-an-api-key") | ||
} | ||
} | ||
|
||
val variantName = variantName.get() | ||
preflight.add("enabled for variant: $variantName") { | ||
if (!distributionEnabled.getOrElse(false)) { | ||
throw PreflightFailure("Distribution not enabled for variant $variantName. Make sure \"${variantName}\" is included in `distribution.enabledVariants`") | ||
} | ||
} | ||
|
||
preflight.add("apiKey set") { | ||
val key = distributionApiKey.orNull | ||
if (key == null) { | ||
throw PreflightFailure("apiKey not set. See https://docs.emergetools.com/docs/distribution-setup-android#configure-the-sdk") | ||
} | ||
if (key == "") { | ||
throw PreflightFailure("apiKey must not be empty. See https://docs.emergetools.com/docs/distribution-setup-android#configure-the-sdk") | ||
} | ||
} | ||
|
||
preflight.add("Runtime SDK added") { | ||
if (!hasDistributionImplementationDependency.getOrElse(false)) { | ||
throw PreflightFailure("Distribution runtime SDK missing as an implementation dependency. See https://docs.emergetools.com/docs/distribution-setup-android#install-the-sdk") | ||
} | ||
} | ||
|
||
preflight.addSubPreflight(buildVcsPreflight()) | ||
preflight.logOutput(logger) | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
...ugin/plugin/src/main/kotlin/com/emergetools/android/gradle/tasks/distribution/Register.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package com.emergetools.android.gradle.tasks.distribution | ||
|
||
import com.android.build.api.variant.Variant | ||
import com.emergetools.android.gradle.EmergePlugin.Companion.EMERGE_TASK_PREFIX | ||
import com.emergetools.android.gradle.EmergePluginExtension | ||
import com.emergetools.android.gradle.tasks.base.BasePreflightTask.Companion.setPreflightTaskInputs | ||
import com.emergetools.android.gradle.util.capitalize | ||
import com.emergetools.android.gradle.util.hasDependency | ||
import org.gradle.api.Project | ||
|
||
private const val EMERGE_DISTRIBUTION_TASK_GROUP = "Emerge Distribution" | ||
private const val DISTRIBUTION_DEP_GROUP = "com.emergetools.distribution" | ||
private const val DISTRIBUTION_DEP_NAME = "distribution" | ||
|
||
private const val PLACEHOLDER_API_KEY = "emerge.distribution.apiKey" | ||
private const val PLACEHOLDER_TAG = "emerge.distribution.tag" | ||
|
||
fun registerDistributionTasks( | ||
appProject: Project, | ||
extension: EmergePluginExtension, | ||
variant: Variant, | ||
) { | ||
appProject.logger.debug( | ||
"Registering Distribution tasks for variant ${variant.name} in project ${appProject.path}" | ||
) | ||
|
||
registerDistributionPreflightTask(appProject, extension, variant) | ||
|
||
// Set the build tag: | ||
variant.manifestPlaceholders.put( | ||
PLACEHOLDER_TAG, | ||
extension.distributionOptions.tag.getOrElse("release") | ||
) | ||
|
||
// API key is special in that we only want to put it in the manifest if distribution is actually | ||
// enabled: | ||
val enabledVariants = extension.distributionOptions.enabledVariants.getOrElse(emptyList()) | ||
if (enabledVariants.contains(variant.name)) { | ||
appProject.logger.debug("Distribution enabled for variant ${variant.name}") | ||
val apiKey = extension.distributionOptions.publishableApiKey.getOrElse("") | ||
variant.manifestPlaceholders.put(PLACEHOLDER_API_KEY, apiKey) | ||
} else { | ||
variant.manifestPlaceholders.put(PLACEHOLDER_API_KEY, "") | ||
} | ||
} | ||
|
||
private fun registerDistributionPreflightTask( | ||
appProject: Project, | ||
extension: EmergePluginExtension, | ||
variant: Variant, | ||
) { | ||
val preflightTaskName = "${EMERGE_TASK_PREFIX}DistributionPreflight${variant.name.capitalize()}" | ||
appProject.tasks.register(preflightTaskName, DistributionPreflight::class.java) { | ||
it.group = EMERGE_DISTRIBUTION_TASK_GROUP | ||
it.description = "Validate Distribution is properly set up for variant ${variant.name}" | ||
it.variantName.set(variant.name) | ||
it.hasEmergeApiToken.set(!extension.apiToken.orNull.isNullOrBlank()) | ||
it.distributionEnabled.set( | ||
extension.distributionOptions.enabledVariants.getOrElse(emptyList()).contains(variant.name) | ||
) | ||
it.distributionApiKey.set(extension.distributionOptions.publishableApiKey) | ||
it.distributionApiKey.set(extension.distributionOptions.tag) | ||
it.hasDistributionImplementationDependency.set( | ||
hasDependency(appProject, variant, DISTRIBUTION_DEP_GROUP, DISTRIBUTION_DEP_NAME) | ||
) | ||
it.setPreflightTaskInputs(extension) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters