|
| 1 | +// capture-sdk - bitdrift's client SDK |
| 2 | +// Copyright Bitdrift, Inc. All rights reserved. |
| 3 | +// |
| 4 | +// Use of this source code is governed by a source available license that can be found in the |
| 5 | +// LICENSE file or at: |
| 6 | +// https://polyformproject.org/wp-content/uploads/2020/06/PolyForm-Shield-1.0.0.txt |
| 7 | + |
| 8 | +package io.bitdrift.capture.task |
| 9 | + |
| 10 | +import org.gradle.api.DefaultTask |
| 11 | +import org.gradle.api.tasks.Internal |
| 12 | +import org.gradle.api.tasks.TaskAction |
| 13 | +import org.w3c.dom.Document |
| 14 | +import java.io.File |
| 15 | +import java.io.IOException |
| 16 | +import java.net.URI |
| 17 | +import javax.xml.parsers.DocumentBuilderFactory |
| 18 | +import org.gradle.api.file.Directory |
| 19 | + |
| 20 | +abstract class CLIUploadMappingTask : CLITask() { |
| 21 | + @TaskAction |
| 22 | + fun action() { |
| 23 | + // e.g. build/intermediates/packaged_manifests/release/processReleaseManifestForPackage/AndroidManifest.xml |
| 24 | + val androidManifestXmlFile = buildDir.asFile.mostRecentSubfileNamed("AndroidManifest.xml") |
| 25 | + // e.g. build/outputs/mapping/release/mapping.txt |
| 26 | + val mappingTxtFile = buildDir.asFile.mostRecentSubfileNamed("mapping.txt") |
| 27 | + |
| 28 | + val manifest = androidManifestXmlFile.asXmlDocument().documentElement |
| 29 | + val appId = manifest.getAttribute("package") |
| 30 | + val versionCode = manifest.getAttribute("android:versionCode") |
| 31 | + val versionName = manifest.getAttribute("android:versionName") |
| 32 | + |
| 33 | + runBDCLI(listOf( |
| 34 | + "debug-files", "upload-proguard", |
| 35 | + "--app-id", appId, |
| 36 | + "--app-version", versionName, |
| 37 | + "--version-code", versionCode, |
| 38 | + mappingTxtFile.absolutePath)) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +abstract class CLIUploadSymbolsTask : CLITask() { |
| 43 | + @TaskAction |
| 44 | + fun action() { |
| 45 | + // e.g. build/intermediates/merged_native_libs/release/mergeReleaseNativeLibs |
| 46 | + val nativeLibsDir = buildDir.asFile.mostRecentSubfileMatching(".*merge.*NativeLibs".toRegex()) |
| 47 | + runBDCLI(listOf("debug-files", "upload", nativeLibsDir.absolutePath)) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +abstract class CLITask : DefaultTask() { |
| 52 | + @Internal |
| 53 | + val buildDir: Directory = project.layout.buildDirectory.get() |
| 54 | + @Internal |
| 55 | + val bdcliFile: File = buildDir.dir("bin").file("bd").asFile |
| 56 | + @Internal |
| 57 | + val downloader = BDCLIDownloader(bdcliFile) |
| 58 | + |
| 59 | + fun runBDCLI(args: List<String>) { |
| 60 | + checkEnvironment() |
| 61 | + downloader.downloadIfNeeded() |
| 62 | + runCommand(listOf(bdcliFile.absolutePath) + args) |
| 63 | + } |
| 64 | + |
| 65 | + fun runCommand(command: List<String>) { |
| 66 | + val process = ProcessBuilder(command) |
| 67 | + .redirectErrorStream(true) |
| 68 | + .start() |
| 69 | + process.inputStream.transferTo(System.out) |
| 70 | + if (process.waitFor() != 0) { |
| 71 | + throw RuntimeException("Command $command failed") |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + private fun checkEnvironment() { |
| 76 | + val apiKeyEnvName = "API_KEY" |
| 77 | + if(System.getenv(apiKeyEnvName) == null) { |
| 78 | + throw IllegalStateException("Environment variable $apiKeyEnvName must be set to your Bitdrift API key before running this task") |
| 79 | + } |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +class BDCLIDownloader(val executableFilePath: File) { |
| 84 | + val bdcliVersion = "0.1.33-rc.1" |
| 85 | + val bdcliDownloadLoc: URI = URI.create("https://dl.bitdrift.io/bd-cli/${bdcliVersion}/${downloadFilename()}/bd") |
| 86 | + |
| 87 | + private enum class OSType { |
| 88 | + MacIntel, |
| 89 | + MacArm, |
| 90 | + LinuxIntel, |
| 91 | + } |
| 92 | + |
| 93 | + private fun osType(): OSType { |
| 94 | + val osName = System.getProperty("os.name") |
| 95 | + val arch = System.getProperty("os.arch") |
| 96 | + return when(osName) { |
| 97 | + "Mac OS X" -> when(arch) { |
| 98 | + "aarch64" -> OSType.MacArm |
| 99 | + else -> OSType.MacIntel |
| 100 | + } |
| 101 | + "Linux" -> OSType.LinuxIntel |
| 102 | + else -> throw IllegalStateException("Could not determine running system (got $osName, $arch). Only Mac (Intel, Arm) and linux (Intel) are currently supported") |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + private fun downloadFilename(): String { |
| 107 | + return when(osType()) { |
| 108 | + OSType.MacArm -> "bd-cli-mac-arm64.tar.gz" |
| 109 | + OSType.MacIntel -> "bd-cli-mac-x86_64.tar.gz" |
| 110 | + OSType.LinuxIntel -> "bd-cli-linux-x86_64.tar.gz" |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + fun downloadIfNeeded() { |
| 115 | + if(executableFilePath.exists()) { |
| 116 | + return |
| 117 | + } |
| 118 | + val parentDir = executableFilePath.parentFile |
| 119 | + if(!parentDir.exists() && !parentDir.mkdirs()) { |
| 120 | + throw IOException("Could not create path '${parentDir.absolutePath}' to contain the downloaded binary") |
| 121 | + } |
| 122 | + try { |
| 123 | + executableFilePath.writeBytes(bdcliDownloadLoc.toURL().readBytes()) |
| 124 | + } catch(e: Exception) { |
| 125 | + throw IOException("Failed to download bd cli tool from $bdcliDownloadLoc", e) |
| 126 | + } |
| 127 | + if(!executableFilePath.setExecutable(true)) { |
| 128 | + throw IOException("Could not mark ${executableFilePath.absolutePath} as executable") |
| 129 | + } |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +fun File.asXmlDocument(): Document { |
| 134 | + try { |
| 135 | + return DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(this) |
| 136 | + } catch(e: Exception) { |
| 137 | + throw IOException("Could not parse XML file $this", e) |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +fun File.mostRecentSubfileNamed(name: String): File { |
| 142 | + try { |
| 143 | + return this.subfilesNamed(name).mostRecent() |
| 144 | + } catch(e: Exception) { |
| 145 | + throw IOException("Could not find any file named '${name}' in path or subpath of '${this}", e) |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +fun File.subfilesNamed(name: String): Sequence<File> { |
| 150 | + return this.walkTopDown().filter { it.name == name } |
| 151 | +} |
| 152 | + |
| 153 | +fun File.mostRecentSubfileMatching(regex: Regex): File { |
| 154 | + try { |
| 155 | + return this.subfilesMatching(regex).mostRecent() |
| 156 | + } catch(e: Exception) { |
| 157 | + throw IOException("Could not find any file matching regex '${regex}' in path or subpath of '${this}", e) |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +fun File.subfilesMatching(regex: Regex): Sequence<File> { |
| 162 | + return this.walkTopDown().filter { regex.matches(it.name) } |
| 163 | +} |
| 164 | + |
| 165 | +fun Sequence<File>.mostRecent(): File { |
| 166 | + return this.sortedBy { it.lastModified() }.last() |
| 167 | +} |
0 commit comments