From acc448ce740b3756c622daf35d7be8372e76cebb Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Wed, 23 Feb 2022 14:25:03 +0530 Subject: Use explicit Gradle tasks to handle build artifact collection (#1745) --- .../src/main/kotlin/artifacts/CollectApksTask.kt | 39 ++++++++++++++++++++++ .../src/main/kotlin/artifacts/CollectBundleTask.kt | 30 +++++++++++++++++ ...d-password-store.android-application.gradle.kts | 1 + ...ndroid-password-store.android-common.gradle.kts | 1 + ...roid-password-store.rename-artifacts.gradle.kts | 22 ++++++++++++ .../src/main/kotlin/crowdin/CrowdinPlugin.kt | 1 + .../src/main/kotlin/psl/PublicSuffixListPlugin.kt | 1 + 7 files changed, 95 insertions(+) create mode 100644 build-logic/android-plugins/src/main/kotlin/artifacts/CollectApksTask.kt create mode 100644 build-logic/android-plugins/src/main/kotlin/artifacts/CollectBundleTask.kt create mode 100644 build-logic/android-plugins/src/main/kotlin/com.github.android-password-store.rename-artifacts.gradle.kts (limited to 'build-logic') diff --git a/build-logic/android-plugins/src/main/kotlin/artifacts/CollectApksTask.kt b/build-logic/android-plugins/src/main/kotlin/artifacts/CollectApksTask.kt new file mode 100644 index 00000000..23f94f22 --- /dev/null +++ b/build-logic/android-plugins/src/main/kotlin/artifacts/CollectApksTask.kt @@ -0,0 +1,39 @@ +package artifacts + +import com.android.build.api.variant.BuiltArtifactsLoader +import java.io.File +import org.gradle.api.DefaultTask +import org.gradle.api.file.DirectoryProperty +import org.gradle.api.provider.Property +import org.gradle.api.tasks.CacheableTask +import org.gradle.api.tasks.Input +import org.gradle.api.tasks.InputFiles +import org.gradle.api.tasks.Internal +import org.gradle.api.tasks.OutputDirectory +import org.gradle.api.tasks.PathSensitive +import org.gradle.api.tasks.PathSensitivity +import org.gradle.api.tasks.TaskAction + +/** Task to collect APKs in a given [outputDirectory]. */ +@CacheableTask +abstract class CollectApksTask : DefaultTask() { + @get:InputFiles @get:PathSensitive(PathSensitivity.NONE) abstract val apkFolder: DirectoryProperty + + @get:Input abstract val variantName: Property + + @get:Internal abstract val builtArtifactsLoader: Property + + @get:OutputDirectory abstract val outputDirectory: DirectoryProperty + + @TaskAction + fun run() { + val outputDir = outputDirectory.asFile.get() + outputDir.mkdirs() + val builtArtifacts = + builtArtifactsLoader.get().load(apkFolder.get()) ?: throw RuntimeException("Cannot load APKs") + builtArtifacts.elements.forEach { artifact -> + File(artifact.outputFile) + .renameTo(outputDir.resolve("APS-${variantName.get()}-${artifact.versionName}.apk")) + } + } +} diff --git a/build-logic/android-plugins/src/main/kotlin/artifacts/CollectBundleTask.kt b/build-logic/android-plugins/src/main/kotlin/artifacts/CollectBundleTask.kt new file mode 100644 index 00000000..a735fb7b --- /dev/null +++ b/build-logic/android-plugins/src/main/kotlin/artifacts/CollectBundleTask.kt @@ -0,0 +1,30 @@ +package artifacts + +import org.gradle.api.DefaultTask +import org.gradle.api.file.DirectoryProperty +import org.gradle.api.file.RegularFileProperty +import org.gradle.api.provider.Property +import org.gradle.api.tasks.Input +import org.gradle.api.tasks.InputFile +import org.gradle.api.tasks.OutputDirectory +import org.gradle.api.tasks.TaskAction + +abstract class CollectBundleTask : DefaultTask() { + @get:InputFile abstract val bundleFile: RegularFileProperty + + @get:Input abstract val variantName: Property + + @get:Input abstract val versionName: Property + + @get:OutputDirectory abstract val outputDirectory: DirectoryProperty + + @TaskAction + fun taskAction() { + val outputDir = outputDirectory.asFile.get() + outputDir.mkdirs() + bundleFile + .get() + .asFile + .renameTo(outputDir.resolve("APS-${variantName.get()}-${versionName.get()}.aab")) + } +} diff --git a/build-logic/android-plugins/src/main/kotlin/com.github.android-password-store.android-application.gradle.kts b/build-logic/android-plugins/src/main/kotlin/com.github.android-password-store.android-application.gradle.kts index 859203a9..e56d400b 100644 --- a/build-logic/android-plugins/src/main/kotlin/com.github.android-password-store.android-application.gradle.kts +++ b/build-logic/android-plugins/src/main/kotlin/com.github.android-password-store.android-application.gradle.kts @@ -2,6 +2,7 @@ * Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved. * SPDX-License-Identifier: GPL-3.0-only */ +@file:Suppress("UnstableApiUsage") import flavors.FlavorDimensions import flavors.ProductFlavors diff --git a/build-logic/android-plugins/src/main/kotlin/com.github.android-password-store.android-common.gradle.kts b/build-logic/android-plugins/src/main/kotlin/com.github.android-password-store.android-common.gradle.kts index 4f76ba19..9400acc2 100644 --- a/build-logic/android-plugins/src/main/kotlin/com.github.android-password-store.android-common.gradle.kts +++ b/build-logic/android-plugins/src/main/kotlin/com.github.android-password-store.android-common.gradle.kts @@ -2,6 +2,7 @@ * Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved. * SPDX-License-Identifier: GPL-3.0-only */ +@file:Suppress("UnstableApiUsage") import com.android.build.gradle.TestedExtension import flavors.configureSlimTests diff --git a/build-logic/android-plugins/src/main/kotlin/com.github.android-password-store.rename-artifacts.gradle.kts b/build-logic/android-plugins/src/main/kotlin/com.github.android-password-store.rename-artifacts.gradle.kts new file mode 100644 index 00000000..9c74c360 --- /dev/null +++ b/build-logic/android-plugins/src/main/kotlin/com.github.android-password-store.rename-artifacts.gradle.kts @@ -0,0 +1,22 @@ +import artifacts.CollectApksTask +import artifacts.CollectBundleTask +import com.android.build.api.artifact.SingleArtifact + +plugins { id("com.android.application") } + +androidComponents { + onVariants { variant -> + project.tasks.register("collect${variant.name.capitalize()}Apks") { + variantName.set(variant.name) + apkFolder.set(variant.artifacts.get(SingleArtifact.APK)) + builtArtifactsLoader.set(variant.artifacts.getBuiltArtifactsLoader()) + outputDirectory.set(project.layout.projectDirectory.dir("outputs")) + } + project.tasks.register("collect${variant.name.capitalize()}Bundle") { + variantName.set(variant.name) + versionName.set(android.defaultConfig.versionName) + bundleFile.set(variant.artifacts.get(SingleArtifact.BUNDLE)) + outputDirectory.set(project.layout.projectDirectory.dir("outputs")) + } + } +} diff --git a/build-logic/automation-plugins/src/main/kotlin/crowdin/CrowdinPlugin.kt b/build-logic/automation-plugins/src/main/kotlin/crowdin/CrowdinPlugin.kt index 9152aff4..e147b42c 100644 --- a/build-logic/automation-plugins/src/main/kotlin/crowdin/CrowdinPlugin.kt +++ b/build-logic/automation-plugins/src/main/kotlin/crowdin/CrowdinPlugin.kt @@ -24,6 +24,7 @@ private const val EXCEPTION_MESSAGE = private const val CROWDIN_BUILD_API_URL = "https://api.crowdin.com/api/project/%s/export?login=%s&account-key=%s" +@Suppress("Unused") class CrowdinDownloadPlugin : Plugin { override fun apply(project: Project) { diff --git a/build-logic/automation-plugins/src/main/kotlin/psl/PublicSuffixListPlugin.kt b/build-logic/automation-plugins/src/main/kotlin/psl/PublicSuffixListPlugin.kt index bc503fde..9d4c3f63 100644 --- a/build-logic/automation-plugins/src/main/kotlin/psl/PublicSuffixListPlugin.kt +++ b/build-logic/automation-plugins/src/main/kotlin/psl/PublicSuffixListPlugin.kt @@ -20,6 +20,7 @@ import org.gradle.kotlin.dsl.register * Base on PublicSuffixListGenerator from OkHttp: * https://github.com/square/okhttp/blob/master/okhttp/src/test/java/okhttp3/internal/publicsuffix/PublicSuffixListGenerator.java */ +@Suppress("Unused") class PublicSuffixListPlugin : Plugin { override fun apply(project: Project) { project.tasks.register("updatePSL") { -- cgit v1.2.3