Skip to content

Commit

Permalink
Add logic to get and apply bundle path to PlayPublishTask
Browse files Browse the repository at this point in the history
  • Loading branch information
rinekri authored and slmlt committed Apr 16, 2024
1 parent de05342 commit 5bab5f3
Show file tree
Hide file tree
Showing 15 changed files with 383 additions and 531 deletions.
4 changes: 2 additions & 2 deletions plugin-build/plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ gradlePlugin {
id = "ru.kode.android.build-publish"
displayName = "Configure project with Firebase App Distribution and changelogs"
implementationClass = "ru.kode.android.build.publish.plugin.BuildPublishPlugin"
version = "1.3.2"
version = project.version
description = "Android plugin to publish bundles and apks to Firebase App Distribution with changelogs"
tags.set(listOf("firebase", "publish", "changelog", "build"))
}
Expand All @@ -45,7 +45,7 @@ publishing {
create<MavenPublication>("maven") {
groupId = project.group.toString()
artifactId = "ru.kode.android.build-publish".removePrefix("$groupId.")
version = "1.3.2"
version = project.version.toString()

from(components["java"])
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

package ru.kode.android.build.publish.plugin

import com.android.build.api.artifact.SingleArtifact
import com.android.build.api.variant.ApplicationAndroidComponentsExtension
import com.android.build.api.variant.impl.VariantOutputImpl
import com.android.build.gradle.AppExtension
import com.android.build.gradle.AppPlugin
import com.android.build.gradle.internal.tasks.PackageBundleTask
import com.android.build.gradle.tasks.PackageAndroidArtifact
import com.android.builder.model.Version.ANDROID_GRADLE_PLUGIN_VERSION
import com.google.firebase.appdistribution.gradle.AppDistributionExtension
Expand All @@ -19,7 +19,6 @@ import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.file.RegularFile
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.logging.Logging
import org.gradle.api.provider.Provider
import org.gradle.api.provider.SetProperty
import org.gradle.api.tasks.StopExecutionException
Expand Down Expand Up @@ -51,7 +50,6 @@ import ru.kode.android.build.publish.plugin.util.capitalizedName
import java.io.File
import java.time.LocalDate
import java.time.format.DateTimeFormatter
import java.util.logging.Logger

internal const val SEND_SLACK_CHANGELOG_TASK_PREFIX = "sendSlackChangelog"
internal const val SEND_TELEGRAM_CHANGELOG_TASK_PREFIX = "sendTelegramChangelog"
Expand Down Expand Up @@ -93,27 +91,26 @@ abstract class BuildPublishPlugin : Plugin<Project> {

androidExtension.onVariants(
callback = { variant ->
Logging.getLogger(this::class.java).info("FOUND VARIANT: $variant WITH OUTPUT NAME ${variant.outputs
.find { it is VariantOutputImpl && it.fullName == variant.name }
as? VariantOutputImpl}")

val output =
variant.outputs
.find { it is VariantOutputImpl && it.fullName == variant.name }
as? VariantOutputImpl
if (output != null) {
val buildVariant = BuildVariant(variant.name, variant.flavorName, variant.buildType)
val outputFileName = output.outputFileName.get()
val apkOutputFileName = output.outputFileName.get()

val bundleFile = variant.artifacts.get(SingleArtifact.BUNDLE)
val outputProviders =
project.registerVariantTasks(
buildPublishExtension,
buildVariant,
changelogFile,
outputFileName,
apkOutputFileName,
bundleFile,
grgitService,
)
output.versionCode.set(outputProviders.versionCode)
output.outputFileName.set(outputProviders.outputFileName)
output.outputFileName.set(outputProviders.apkOutputFileName)
output.versionName.set(outputProviders.versionName)
}
},
Expand All @@ -138,7 +135,8 @@ abstract class BuildPublishPlugin : Plugin<Project> {
buildPublishExtension: BuildPublishExtension,
buildVariant: BuildVariant,
changelogFile: Provider<RegularFile>,
outputFileName: String,
apkOutputFileName: String,
bundleFile: Provider<RegularFile>,
grgitService: Provider<GrgitService>,
): OutputProviders {
val outputConfig =
Expand All @@ -160,15 +158,15 @@ abstract class BuildPublishPlugin : Plugin<Project> {
project.provider { DEFAULT_VERSION_CODE }
}
}
val outputFileNameProvider =
val apkOutputFileNameProvider =
useVersionsFromTagProvider.flatMap { useVersionsFromTag ->
if (useVersionsFromTag) {
outputConfig.baseFileName.zip(tagBuildProvider) { baseFileName, tagBuildFile ->
mapToOutputFileName(tagBuildFile, outputFileName, baseFileName)
mapToOutputApkFileName(tagBuildFile, apkOutputFileName, baseFileName)
}
} else {
outputConfig.baseFileName.map { baseFileName ->
createDefaultOutputFileName(baseFileName, outputFileName)
createDefaultOutputFileName(baseFileName, apkOutputFileName)
}
}
}
Expand All @@ -182,9 +180,9 @@ abstract class BuildPublishPlugin : Plugin<Project> {
project.provider { DEFAULT_VERSION_NAME }
}
}
val outputFileProvider =
outputFileNameProvider.flatMap { fileName ->
mapToOutputFile(buildVariant, fileName)
val apkOutputFileProvider =
apkOutputFileNameProvider.flatMap { fileName ->
mapToOutputApkFile(buildVariant, fileName)
}
tasks.registerPrintLastIncreasedTagTask(
buildVariant,
Expand Down Expand Up @@ -230,7 +228,7 @@ abstract class BuildPublishPlugin : Plugin<Project> {
buildVariant,
generateChangelogFileProvider,
tagBuildProvider,
outputFileProvider,
apkOutputFileProvider,
)
}
val appCenterDistributionConfig =
Expand All @@ -243,7 +241,7 @@ abstract class BuildPublishPlugin : Plugin<Project> {
config = appCenterDistributionConfig,
buildVariant = buildVariant,
changelogFileProvider = generateChangelogFileProvider,
buildVariantOutputFileProvider = outputFileProvider,
apkOutputFileProvider = apkOutputFileProvider,
tagBuildProvider = tagBuildProvider,
outputConfig = outputConfig,
)
Expand All @@ -258,7 +256,7 @@ abstract class BuildPublishPlugin : Plugin<Project> {
PlayTaskParams(
config = playConfig,
buildVariant = buildVariant,
buildVariantOutputFileProvider = outputFileProvider,
bundleOutputFileProvider = bundleFile,
tagBuildProvider = tagBuildProvider,
outputConfig = outputConfig,
)
Expand All @@ -281,7 +279,7 @@ abstract class BuildPublishPlugin : Plugin<Project> {
return OutputProviders(
versionName = versionNameProvider,
versionCode = versionCodeProvider,
outputFileName = outputFileNameProvider,
apkOutputFileName = apkOutputFileNameProvider,
)
}

Expand Down Expand Up @@ -323,7 +321,7 @@ abstract class BuildPublishPlugin : Plugin<Project> {
buildVariant: BuildVariant,
generateChangelogFileProvider: Provider<RegularFile>,
tagBuildProvider: Provider<RegularFile>,
outputFileProvider: Provider<RegularFile>,
apkOutputFileProvider: Provider<RegularFile>,
) {
registerSendSlackChangelogTask(
outputConfig,
Expand All @@ -341,7 +339,7 @@ abstract class BuildPublishPlugin : Plugin<Project> {
slackConfig.uploadApiTokenFile,
slackConfig.uploadChannels,
buildVariant,
outputFileProvider,
apkOutputFileProvider,
)
}
}
Expand All @@ -350,13 +348,13 @@ abstract class BuildPublishPlugin : Plugin<Project> {
apiTokenFile: RegularFileProperty,
channels: SetProperty<String>,
buildVariant: BuildVariant,
outputFileProvider: Provider<RegularFile>,
apkOutputFileProvider: Provider<RegularFile>,
) {
register(
"$SLACK_DISTRIBUTION_UPLOAD_TASK_PREFIX${buildVariant.capitalizedName()}",
SlackDistributionTask::class.java,
) {
it.buildVariantOutputFile.set(outputFileProvider)
it.buildVariantOutputFile.set(apkOutputFileProvider)
it.apiTokenFile.set(apiTokenFile)
it.channels.set(channels)
}
Expand Down Expand Up @@ -474,7 +472,7 @@ abstract class BuildPublishPlugin : Plugin<Project> {
AppCenterDistributionTask::class.java,
) {
it.tagBuildFile.set(params.tagBuildProvider)
it.buildVariantOutputFile.set(params.buildVariantOutputFileProvider)
it.buildVariantOutputFile.set(params.apkOutputFileProvider)
it.changelogFile.set(params.changelogFileProvider)
it.apiTokenFile.set(config.apiTokenFile)
it.ownerName.set(config.ownerName)
Expand All @@ -487,9 +485,7 @@ abstract class BuildPublishPlugin : Plugin<Project> {
}
}

private fun TaskContainer.registerPlayTask(
params: PlayTaskParams,
): TaskProvider<PlayDistributionTask> {
private fun TaskContainer.registerPlayTask(params: PlayTaskParams): TaskProvider<PlayDistributionTask> {
val buildVariant = params.buildVariant
val config = params.config

Expand All @@ -498,7 +494,7 @@ abstract class BuildPublishPlugin : Plugin<Project> {
PlayDistributionTask::class.java,
) {
it.tagBuildFile.set(params.tagBuildProvider)
it.buildVariantOutputFile.set(params.buildVariantOutputFileProvider)
it.buildVariantOutputFile.set(params.bundleOutputFileProvider)
it.apiTokenFile.set(config.apiTokenFile)
it.appId.set(config.appId)
it.trackId.set(config.trackId)
Expand Down Expand Up @@ -584,23 +580,22 @@ private fun AppDistributionExtension.configure(
private data class OutputProviders(
val versionName: Provider<String>,
val versionCode: Provider<Int>,
val outputFileName: Provider<String>,
val apkOutputFileName: Provider<String>,
)

private data class AppCenterDistributionTaskParams(
val config: AppCenterDistributionConfig,
val buildVariant: BuildVariant,
val changelogFileProvider: Provider<RegularFile>,
val buildVariantOutputFileProvider: Provider<RegularFile>,
val apkOutputFileProvider: Provider<RegularFile>,
val tagBuildProvider: Provider<RegularFile>,
val outputConfig: OutputConfig,
)


private data class PlayTaskParams(
val config: PlayConfig,
val buildVariant: BuildVariant,
val buildVariantOutputFileProvider: Provider<RegularFile>,
val bundleOutputFileProvider: Provider<RegularFile>,
val tagBuildProvider: Provider<RegularFile>,
val outputConfig: OutputConfig,
)
Expand All @@ -614,7 +609,7 @@ private fun mapToVersionCode(tagBuildFile: RegularFile): Int {
}
}

private fun mapToOutputFileName(
private fun mapToOutputApkFileName(
tagBuildFile: RegularFile,
outputFileName: String,
baseFileName: String?,
Expand All @@ -626,15 +621,8 @@ private fun mapToOutputFileName(
val versionName = tagBuild.buildVariant
val versionCode = tagBuild.buildNumber
"$baseFileName-$versionName-vc$versionCode-$formattedDate.apk"
} else if (file.exists() && outputFileName.endsWith(".aab")) {
val tagBuild = fromJson(file)
val versionName = tagBuild.buildVariant
val versionCode = tagBuild.buildNumber
"$baseFileName-$versionName-vc$versionCode-$formattedDate.aab"
} else if (!file.exists() && outputFileName.endsWith(".apk")) {
"$baseFileName-$formattedDate.apk"
} else if (!file.exists() && outputFileName.endsWith(".aab")) {
"$baseFileName-$formattedDate.aab"
} else {
createDefaultOutputFileName(baseFileName, outputFileName)
}
Expand All @@ -660,20 +648,13 @@ private fun mapToVersionName(
}
}

private fun Project.mapToOutputFile(
private fun Project.mapToOutputApkFile(
buildVariant: BuildVariant,
fileName: String,
): Provider<RegularFile> {
return if (fileName.endsWith("aab")) {
project.tasks.withType(PackageBundleTask::class.java)
.firstOrNull { it.variantName == buildVariant.name }
?.bundleFile
?: throw GradleException("no output for variant ${buildVariant.name}")
} else {
project.tasks.withType(PackageAndroidArtifact::class.java)
.firstOrNull { it.variantName == buildVariant.name }
?.outputDirectory
?.map { directory -> directory.file(fileName) }
?: throw GradleException("no output for variant ${buildVariant.name}")
}
return project.tasks.withType(PackageAndroidArtifact::class.java)
.firstOrNull { it.variantName == buildVariant.name }
?.outputDirectory
?.map { directory -> directory.file(fileName) }
?: throw GradleException("no output for variant ${buildVariant.name}")
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import ru.kode.android.build.publish.plugin.extension.config.OutputConfig
import ru.kode.android.build.publish.plugin.extension.config.PlayConfig
import ru.kode.android.build.publish.plugin.extension.config.SlackConfig
import ru.kode.android.build.publish.plugin.extension.config.TelegramConfig
import ru.kode.android.build.publish.plugin.task.play.PlayDistributionTask
import javax.inject.Inject

const val EXTENSION_NAME = "buildPublish"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package ru.kode.android.build.publish.plugin.extension.config

import org.gradle.api.file.RegularFileProperty
import org.gradle.api.provider.Property
import org.gradle.api.provider.SetProperty
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.InputFile

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import org.gradle.api.GradleException
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.plugins.BasePlugin
import org.gradle.api.provider.Property
import org.gradle.api.provider.SetProperty
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.InputFile
import org.gradle.api.tasks.Optional
Expand All @@ -14,13 +13,14 @@ import org.gradle.api.tasks.options.Option
import org.gradle.workers.WorkQueue
import org.gradle.workers.WorkerExecutor
import ru.kode.android.build.publish.plugin.enity.mapper.fromJson
import ru.kode.android.build.publish.plugin.extension.config.MAX_REQUEST_COUNT
import ru.kode.android.build.publish.plugin.extension.config.MAX_REQUEST_DELAY_MS
import ru.kode.android.build.publish.plugin.task.appcenter.work.AppCenterUploadWork
import ru.kode.android.build.publish.plugin.task.play.work.PlayUploadWork
import ru.kode.android.build.publish.plugin.util.capitalized
import javax.inject.Inject

/**
* Task to publish app at given release track in google play console with set priority
* Contains basic functionality, for the extensions reference to original implementation:
* https://github.com/Triple-T/gradle-play-publisher
*/
abstract class PlayDistributionTask
@Inject
constructor(
Expand Down Expand Up @@ -56,7 +56,6 @@ abstract class PlayDistributionTask
)
abstract val appId: Property<String>


@get:Input
@get:Option(
option = "trackId",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ interface PlayPublisher {
* More docs are available
* [here](https://developers.google.com/android-publisher/api-ref/edits/commit).
*/
fun commitEdit(id: String, sendChangesForReview: Boolean = true): CommitResponse
fun commitEdit(
id: String,
sendChangesForReview: Boolean = true,
): CommitResponse

/**
* Validates an edit with the given [id].
Expand Down
Loading

0 comments on commit 5bab5f3

Please sign in to comment.