-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add distribution support to gradle-plugin #297
Open
chromy
wants to merge
1
commit into
main
Choose a base branch
from
chromy/2024-10-22-distro-gradle
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+253
−13
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tag doesn't seem to be relevant currently - I'd suggest removing it until we have an upload-related task around distro to avoid confusion
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tag is used to define the release 'channel' (when the SDK checks for an update we send the tag and only consider builds a with matching tag). Possibly we should have a distribution upload task but I was worried that just makes it more confusing.