|
| 1 | +import org.apache.tools.ant.taskdefs.condition.Os |
| 2 | + |
| 3 | +def appProject = project(":app") |
| 4 | + |
| 5 | +gradle.projectsEvaluated { |
| 6 | + // Works for both `bundleReleaseJsAndAssets` and `createBundleReleaseJsAndAssets` |
| 7 | + def bundleTask = appProject.tasks.find { |
| 8 | + task -> task.name.endsWithIgnoreCase('bundleReleaseJsAndAssets') |
| 9 | + } |
| 10 | + |
| 11 | + bundleTask.finalizedBy uploadSourcemaps |
| 12 | +} |
| 13 | + |
| 14 | +task uploadSourcemaps() { |
| 15 | + group 'instabug' |
| 16 | + description 'Uploads sourcemaps file to Instabug server' |
| 17 | + enabled rootProject.hasProperty('instabugUploadEnable') |
| 18 | + ? new Boolean(rootProject.property('instabugUploadEnable')) |
| 19 | + : true |
| 20 | + |
| 21 | + doLast { |
| 22 | + try { |
| 23 | + def appDir = appProject.projectDir |
| 24 | + def sourceMapDest = 'build/generated/sourcemaps/react/release/index.android.bundle.map' |
| 25 | + def sourceMapFile = new File(appDir, sourceMapDest) |
| 26 | + |
| 27 | + if (!sourceMapFile.exists()) { |
| 28 | + throw new InvalidUserDataException("Unable to find source map file at: ${sourceMapFile.absolutePath}") |
| 29 | + } |
| 30 | + |
| 31 | + def jsProjectDir = rootDir.parentFile |
| 32 | + def instabugDir = new File(['node', '-p', 'require.resolve("instabug-reactnative/package.json")'].execute(null, rootDir).text.trim()).getParentFile() |
| 33 | + |
| 34 | + def tokenShellFile = new File(instabugDir, 'scripts/find-token.sh') |
| 35 | + def inferredToken = executeShellScript(tokenShellFile, jsProjectDir) |
| 36 | + def appToken = resolveVar('App Token', 'INSTABUG_APP_TOKEN', inferredToken) |
| 37 | + |
| 38 | + def projectConfig = appProject.android.defaultConfig |
| 39 | + def versionName = resolveVar('Version Name', 'INSTABUG_VERSION_NAME', "${projectConfig.versionName}") |
| 40 | + def versionCode = resolveVar('Version Code', 'INSTABUG_VERSION_CODE', "${projectConfig.versionCode}") |
| 41 | + |
| 42 | + exec { |
| 43 | + def osCompatibility = Os.isFamily(Os.FAMILY_WINDOWS) ? ['cmd', '/c'] : [] |
| 44 | + def args = [ |
| 45 | + 'npx', 'instabug', 'upload-sourcemaps', |
| 46 | + '--platform', 'android', |
| 47 | + '--file', sourceMapFile.absolutePath, |
| 48 | + '--token', appToken, |
| 49 | + '--name', versionName, |
| 50 | + '--code', versionCode |
| 51 | + ] |
| 52 | + |
| 53 | + commandLine(*osCompatibility, *args) |
| 54 | + } |
| 55 | + } catch (exception) { |
| 56 | + project.logger.error "Failed to upload source map file.\n" + |
| 57 | + "Reason: ${exception.message}" |
| 58 | + } |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +String resolveVar(String name, String envKey, String defaultValue) { |
| 63 | + def env = System.getenv() |
| 64 | + def envValue = env.get(envKey) |
| 65 | + |
| 66 | + if (envValue != null && envValue != defaultValue) { |
| 67 | + project.logger.warn "Environment variable `${envKey}` might have incorrect value, " + |
| 68 | + "make sure this was intentional:\n" + |
| 69 | + " Environment Value: ${envValue}\n" + |
| 70 | + " Default Value: ${defaultValue}" |
| 71 | + } |
| 72 | + |
| 73 | + def value = envValue ?: defaultValue |
| 74 | + |
| 75 | + if (value == null) { |
| 76 | + throw new InvalidUserDataException("Unable to find ${name}! " + |
| 77 | + "Set the environment variable `${envKey}` and try again.") |
| 78 | + } |
| 79 | + |
| 80 | + return value |
| 81 | +} |
| 82 | + |
| 83 | +static String executeShellScript(File script, File workingDir) { |
| 84 | + if (Os.isFamily(Os.FAMILY_WINDOWS)) { |
| 85 | + return null |
| 86 | + } |
| 87 | + |
| 88 | + def output = new StringBuffer() |
| 89 | + def process = ['sh', script.getAbsolutePath()].execute(null, workingDir) |
| 90 | + process?.waitForProcessOutput(output, new StringBuffer()) |
| 91 | + |
| 92 | + return process?.exitValue() == 0 ? output.toString().trim() : null |
| 93 | +} |
0 commit comments