Skip to content

Commit

Permalink
Merge pull request #93 from DeployGate/supportAab
Browse files Browse the repository at this point in the history
Support uploading app bundle files
  • Loading branch information
jmatsu authored Dec 18, 2019
2 parents 364fd44 + 1013849 commit 890ce12
Show file tree
Hide file tree
Showing 34 changed files with 1,553 additions and 431 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,12 @@ class DeployGatePlugin implements Plugin<Project> {
processor.registerDeclarationAwareUploadApkTask(variantOrCustomName)

if (AndroidGradlePlugin.isAppBundleSupported()) {
// TODO create tasks for app bundle
processor.registerDeclarationAwareUploadAabTask(variantOrCustomName)
}
}

processor.registerAggregatedDeclarationAwareUploadApkTask(processor.declaredNames)
processor.registerAggregatedDeclarationAwareUploadAabTask(processor.declaredNames)

if (!processor.canProcessVariantAware()) {
project.logger.warn("DeployGate Gradle Plugin is stopped because Android Gradle Plugin must be applied before.")
Expand All @@ -85,7 +86,7 @@ class DeployGatePlugin implements Plugin<Project> {
processor.registerVariantAwareUploadApkTask(variantProxy)

if (AndroidGradlePlugin.isAppBundleSupported()) {
// TODO create tasks for app bundle
processor.registerVariantAwareUploadAabTask(variantProxy)
}
}
}
Expand Down
47 changes: 39 additions & 8 deletions src/main/groovy/com/deploygate/gradle/plugins/Processor.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,16 @@ class Processor {
private final LogoutTaskFactory logoutTaskFactory

@Nonnull
private final UploadApkTaskFactory<IApplicationVariant> applicationVariantBasedUploadApkTaskFactory
private final UploadArtifactTaskFactory<IApplicationVariant> applicationVariantBasedUploadApkTaskFactory

@Nonnull
private final UploadApkTaskFactory<String> stringBasedUploadApkTaskFactory
private final UploadArtifactTaskFactory<IApplicationVariant> applicationVariantBasedUploadAabTaskFactory

@Nonnull
private final UploadArtifactTaskFactory<String> stringBasedUploadApkTaskFactory

@Nonnull
private final UploadArtifactTaskFactory<String> stringBasedUploadAabTaskFactory

def declaredNames = new HashSet<String>()

Expand All @@ -38,7 +44,9 @@ class Processor {
new LoginTaskFactoryImpl(project),
new LogoutTaskFactoryImpl(project),
new AGPBasedUploadApkTaskFactory(project),
new DSLBasedUploadApkTaskFactory(project)
new AGPBasedUploadAabTaskFactory(project),
new DSLBasedUploadApkTaskFactory(project),
new DSLBasedUploadAabTaskFactory(project)
)
}

Expand All @@ -47,14 +55,18 @@ class Processor {
@Nonnull Project project,
@Nonnull LoginTaskFactory loginTaskFactory,
@Nonnull LogoutTaskFactory logoutTaskFactory,
@Nonnull UploadApkTaskFactory<IApplicationVariant> applicationVariantBasedUploadApkTaskFactory,
@Nonnull UploadApkTaskFactory<String> stringBasedUploadApkTaskFactory
@Nonnull UploadArtifactTaskFactory<IApplicationVariant> applicationVariantBasedUploadApkTaskFactory,
@Nonnull UploadArtifactTaskFactory<IApplicationVariant> applicationVariantBasedUploadAabTaskFactory,
@Nonnull UploadArtifactTaskFactory<String> stringBasedUploadApkTaskFactory,
@Nonnull UploadArtifactTaskFactory<String> stringBasedUploadAabTaskFactory
) {
this.project = project
this.loginTaskFactory = loginTaskFactory
this.logoutTaskFactory = logoutTaskFactory
this.applicationVariantBasedUploadApkTaskFactory = applicationVariantBasedUploadApkTaskFactory
this.applicationVariantBasedUploadAabTaskFactory = applicationVariantBasedUploadAabTaskFactory
this.stringBasedUploadApkTaskFactory = stringBasedUploadApkTaskFactory
this.stringBasedUploadAabTaskFactory = stringBasedUploadAabTaskFactory
}

boolean canProcessVariantAware() {
Expand All @@ -79,11 +91,21 @@ class Processor {
}

def registerDeclarationAwareUploadApkTask(String variantOrCustomName) {
stringBasedUploadApkTaskFactory.registerUploadApkTask(variantOrCustomName, *dependencyAncestorOfUploadTaskNames)
stringBasedUploadApkTaskFactory.registerUploadArtifactTask(variantOrCustomName, *dependencyAncestorOfUploadTaskNames)
}

def registerDeclarationAwareUploadAabTask(String variantOrCustomName) {
stringBasedUploadAabTaskFactory.registerUploadArtifactTask(variantOrCustomName, *dependencyAncestorOfUploadTaskNames)
}

def registerAggregatedDeclarationAwareUploadApkTask(Collection<String> variantOrCustomNames) {
stringBasedUploadApkTaskFactory.registerAggregatedUploadApkTask(variantOrCustomNames.collect {
stringBasedUploadApkTaskFactory.registerAggregatedUploadArtifactTask(variantOrCustomNames.collect {
DeployGateTaskFactory.uploadApkTaskName(it)
})
}

def registerAggregatedDeclarationAwareUploadAabTask(Collection<String> variantOrCustomNames) {
stringBasedUploadAabTaskFactory.registerAggregatedUploadArtifactTask(variantOrCustomNames.collect {
DeployGateTaskFactory.uploadApkTaskName(it)
})
}
Expand All @@ -94,7 +116,16 @@ class Processor {
return
}

applicationVariantBasedUploadApkTaskFactory.registerUploadApkTask(variant, *dependencyAncestorOfUploadTaskNames)
applicationVariantBasedUploadApkTaskFactory.registerUploadArtifactTask(variant, *dependencyAncestorOfUploadTaskNames)
}

def registerVariantAwareUploadAabTask(@Nonnull IApplicationVariant variant) {
if (!canProcessVariantAware()) {
project.logger.error("android gradle plugin not found but tried to create android-specific tasks. Ignored...")
return
}

applicationVariantBasedUploadAabTaskFactory.registerUploadArtifactTask(variant, *dependencyAncestorOfUploadTaskNames)
}

@VisibleForTesting
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.deploygate.gradle.plugins.artifacts

import javax.annotation.Nonnull
import javax.annotation.Nullable

interface AabInfo {
@Nonnull
String getVariantName()

@Nullable
File getAabFile()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.deploygate.gradle.plugins.artifacts

import javax.annotation.Nonnull

class DefaultPresetAabInfo extends DirectAabInfo {
DefaultPresetAabInfo(@Nonnull String variantName) {
super(variantName, null)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.deploygate.gradle.plugins.artifacts

import com.google.common.annotations.VisibleForTesting

import javax.annotation.Nonnull
import javax.annotation.Nullable

@VisibleForTesting
class DirectAabInfo implements AabInfo {
@Nonnull
private final String variantName
@Nullable
private final File aabFile

DirectAabInfo(@Nonnull String variantName, @Nullable File aabFile) {
this.variantName = variantName
this.aabFile = aabFile

if (!variantName) {
throw new IllegalArgumentException("variantName must not be null or empty")
}
}

@Override
@Nonnull
String getVariantName() {
return variantName
}

@Override
@Nullable
File getAabFile() {
return aabFile
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.deploygate.gradle.plugins.artifacts


import com.deploygate.gradle.plugins.internal.agp.AndroidGradlePlugin
import groovy.transform.PackageScope
import org.gradle.api.Project

import javax.annotation.Nonnull

Expand All @@ -10,8 +12,7 @@ class PackageAppTaskCompat {
}

@Nonnull
static ApkInfo getApkInfo(@Nonnull /* PackageApplication */ packageAppTask) {
String variantName = packageAppTask.name
static ApkInfo getApkInfo(@Nonnull /* PackageApplication */ packageAppTask, @Nonnull String variantName) {
// outputScope is retrieved by the reflection
Collection<String> apkNames = packageAppTask.outputScope.apkDatas*.outputFileName
File outputDir = packageAppTask.outputDirectory
Expand All @@ -26,6 +27,26 @@ class PackageAppTaskCompat {
)
}

@Nonnull
static AabInfo getAabInfo(@Nonnull /* PackageApplication */ packageAppTask, @Nonnull String variantName, @Nonnull Project project) {
final String aabName

if (AndroidGradlePlugin.isAppBundleArchiveNameChanged()) {
// outputScope is retrieved by the reflection
Collection<String> apkNames = packageAppTask.outputScope.apkDatas*.outputFileName
aabName = ((String) apkNames[0]).replaceFirst("\\.apk\$", ".aab")
} else {
aabName = "${project.properties["archivesBaseName"] as String}.aab"
}

def outputDir = new File(project.buildDir, "outputs/bundle/${variantName}")

return new DirectAabInfo(
variantName,
new File(outputDir, aabName),
)
}

@PackageScope
static boolean hasSigningConfig(packageAppTask) {
if (!AndroidGradlePlugin.isSigningConfigCollectionSupported()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.deploygate.gradle.plugins.internal.agp


import com.deploygate.gradle.plugins.internal.VersionString
import org.gradle.api.Plugin
import org.gradle.api.Project
Expand Down Expand Up @@ -56,11 +57,20 @@ class AndroidGradlePlugin {
return getVersion().major >= 4 || getVersion().major == 3 && getVersion().minor > 2
}

static boolean isAppBundleArchiveNameChanged() {
return getVersion().major >= 4 || getVersion().major == 3 && getVersion().minor >= 5
}

@Nonnull
static String androidAssembleTaskName(@Nonnull String variantName) {
return "assemble${variantName.capitalize()}"
}

@Nonnull
static String androidBundleTaskName(@Nonnull String variantName) {
return "bundle${variantName.capitalize()}"
}

/**
* Get the AGP version from a classloader because `plugins` block will separate class loaders
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,33 @@ class TaskFactory {
}

@Nullable
final <T extends Task> LazyConfigurableTask<T> register(@Nonnull String taskName, @Nonnull Class<T> klass, boolean allowExisting = true) {
def existingTask = project.tasks.findByName(taskName)
final <T extends Task> LazyConfigurableTask<T> register(@Nonnull String taskName, @Nonnull Class<T> klass) {
def existingTask = findByName(taskName)

if (existingTask) {
if (allowExisting) {
return new SingleTask(existingTask as T)
} else {
return null
}
return null
}

return GradleCompat.newLazyConfigurableTask(project, taskName, klass)
}

@Nullable
final <T extends Task> LazyConfigurableTask<T> registerOrFindBy(@Nonnull String taskName, @Nonnull Class<T> klass) {
def existingTask = findByName(taskName)

if (existingTask) {
return new SingleTask(existingTask as T)
}

return GradleCompat.newLazyConfigurableTask(project, taskName, klass)
}

final boolean exists(@Nonnull String taskName) {
return findByName(taskName)
}

@Nullable
private Task findByName(@Nonnull String taskName) {
return project.tasks.findByName(taskName)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.deploygate.gradle.plugins.tasks

import com.deploygate.gradle.plugins.artifacts.AabInfo
import com.deploygate.gradle.plugins.dsl.NamedDeployment
import com.deploygate.gradle.plugins.tasks.factory.DeployGateTaskFactory
import org.gradle.api.tasks.TaskAction

import javax.annotation.Nonnull

class UploadAabTask extends UploadArtifactTask {
static Configuration createConfiguration(@Nonnull NamedDeployment deployment, @Nonnull AabInfo aabInfo) {
return new Configuration(
artifactFile: deployment.sourceFile ?: aabInfo.aabFile,
isSigningReady: false,
isUniversalApk: false,
uploadParams: createUploadParams(deployment)
)
}

@TaskAction
void doUpload() {
super.doUpload()
}

@Override
void applyTaskProfile() {
setDescription("Deploy bundled $variantName to DeployGate")

group = DeployGateTaskFactory.GROUP_NAME
}

@Override
void runArtifactSpecificVerification() {
}
}
Loading

0 comments on commit 890ce12

Please sign in to comment.