|
1 | 1 | import org.apache.tools.ant.taskdefs.condition.Os |
2 | 2 |
|
3 | | -def appProject = project(":app") |
4 | | - |
5 | 3 | gradle.projectsEvaluated { |
6 | 4 | // Works for both `bundleReleaseJsAndAssets` and `createBundleReleaseJsAndAssets` and product flavors |
7 | | - def bundleTask = appProject.tasks.find { |
8 | | - task -> task.name.endsWith('ReleaseJsAndAssets') |
| 5 | + def suffix = 'ReleaseJsAndAssets' |
| 6 | + def bundleTasks = project(':app').tasks.findAll { |
| 7 | + task -> task.name.endsWith(suffix) |
9 | 8 | } |
10 | 9 |
|
11 | | - bundleTask.finalizedBy uploadSourcemaps |
12 | | -} |
13 | | - |
14 | | -task uploadSourcemaps() { |
15 | | - group 'instabug' |
16 | | - description 'Uploads sourcemaps file to Instabug server' |
17 | | - enabled = isUploadSourcemapsEnabled() |
| 10 | + bundleTasks.forEach { task -> |
| 11 | + def name = task.name |
| 12 | + def prefixes = ['bundle', 'createBundle'] |
| 13 | + def start = name.startsWith(prefixes[0]) ? prefixes[0].length() : prefixes[1].length() |
| 14 | + def end = name.length() - suffix.length() |
| 15 | + def flavor = name.substring(start, end).uncapitalize() |
18 | 16 |
|
19 | | - doLast { |
20 | | - try { |
21 | | - def appDir = appProject.projectDir |
22 | | - def sourceMapDest = 'build/generated/sourcemaps/react/release/index.android.bundle.map' |
23 | | - def sourceMapFile = new File(appDir, sourceMapDest) |
24 | | - |
25 | | - if (!sourceMapFile.exists()) { |
26 | | - throw new InvalidUserDataException("Unable to find source map file at: ${sourceMapFile.absolutePath}") |
27 | | - } |
| 17 | + task.finalizedBy createUploadSourcemapsTask(flavor) |
| 18 | + } |
| 19 | +} |
28 | 20 |
|
29 | | - def jsProjectDir = rootDir.parentFile |
30 | | - def instabugDir = new File(['node', '-p', 'require.resolve("instabug-reactnative/package.json")'].execute(null, rootDir).text.trim()).getParentFile() |
31 | | - |
32 | | - def tokenShellFile = new File(instabugDir, 'scripts/find-token.sh') |
33 | | - def inferredToken = executeShellScript(tokenShellFile, jsProjectDir) |
34 | | - def appToken = resolveVar('App Token', 'INSTABUG_APP_TOKEN', inferredToken) |
35 | | - |
36 | | - def projectConfig = appProject.android.defaultConfig |
37 | | - def versionName = resolveVar('Version Name', 'INSTABUG_VERSION_NAME', "${projectConfig.versionName}") |
38 | | - def versionCode = resolveVar('Version Code', 'INSTABUG_VERSION_CODE', "${projectConfig.versionCode}") |
39 | | - |
40 | | - exec { |
41 | | - def osCompatibility = Os.isFamily(Os.FAMILY_WINDOWS) ? ['cmd', '/c'] : [] |
42 | | - def args = [ |
43 | | - 'npx', 'instabug', 'upload-sourcemaps', |
44 | | - '--platform', 'android', |
45 | | - '--file', sourceMapFile.absolutePath, |
46 | | - '--token', appToken, |
47 | | - '--name', versionName, |
48 | | - '--code', versionCode |
49 | | - ] |
50 | | - |
51 | | - commandLine(*osCompatibility, *args) |
| 21 | +Task createUploadSourcemapsTask(String flavor) { |
| 22 | + def name = 'uploadSourcemaps' + flavor.capitalize() |
| 23 | + def provider = tasks.register(name) { |
| 24 | + group 'instabug' |
| 25 | + description 'Uploads sourcemaps file to Instabug server' |
| 26 | + enabled = isUploadSourcemapsEnabled() |
| 27 | + |
| 28 | + doLast { |
| 29 | + try { |
| 30 | + def appProject = project(':app') |
| 31 | + def appDir = appProject.projectDir |
| 32 | + def flavorPath = flavor + (flavor.empty ? '' : '/') |
| 33 | + def sourceMapDest = "build/generated/sourcemaps/react/${flavorPath}release/index.android.bundle.map" |
| 34 | + def sourceMapFile = new File(appDir, sourceMapDest) |
| 35 | + |
| 36 | + if (!sourceMapFile.exists()) { |
| 37 | + throw new InvalidUserDataException("Unable to find source map file at: ${sourceMapFile.absolutePath}") |
| 38 | + } |
| 39 | + |
| 40 | + def jsProjectDir = rootDir.parentFile |
| 41 | + def instabugDir = new File(['node', '-p', 'require.resolve("instabug-reactnative/package.json")'].execute(null, rootDir).text.trim()).getParentFile() |
| 42 | + |
| 43 | + def tokenShellFile = new File(instabugDir, 'scripts/find-token.sh') |
| 44 | + def inferredToken = executeShellScript(tokenShellFile, jsProjectDir) |
| 45 | + def appToken = resolveVar('App Token', 'INSTABUG_APP_TOKEN', inferredToken) |
| 46 | + |
| 47 | + def projectConfig = appProject.android.defaultConfig |
| 48 | + def versionName = resolveVar('Version Name', 'INSTABUG_VERSION_NAME', "${projectConfig.versionName}") |
| 49 | + def versionCode = resolveVar('Version Code', 'INSTABUG_VERSION_CODE', "${projectConfig.versionCode}") |
| 50 | + |
| 51 | + exec { |
| 52 | + def osCompatibility = Os.isFamily(Os.FAMILY_WINDOWS) ? ['cmd', '/c'] : [] |
| 53 | + def args = [ |
| 54 | + 'npx', 'instabug', 'upload-sourcemaps', |
| 55 | + '--platform', 'android', |
| 56 | + '--file', sourceMapFile.absolutePath, |
| 57 | + '--token', appToken, |
| 58 | + '--name', versionName, |
| 59 | + '--code', versionCode |
| 60 | + ] |
| 61 | + |
| 62 | + commandLine(*osCompatibility, *args) |
| 63 | + } |
| 64 | + } catch (exception) { |
| 65 | + project.logger.error "Failed to upload source map file.\n" + |
| 66 | + "Reason: ${exception.message}" |
52 | 67 | } |
53 | | - } catch (exception) { |
54 | | - project.logger.error "Failed to upload source map file.\n" + |
55 | | - "Reason: ${exception.message}" |
56 | 68 | } |
57 | 69 | } |
| 70 | + |
| 71 | + return provider.get() |
58 | 72 | } |
59 | 73 |
|
60 | 74 | boolean isUploadSourcemapsEnabled() { |
61 | 75 | def envValue = System.getenv('INSTABUG_SOURCEMAPS_UPLOAD_DISABLE')?.toBoolean() |
62 | 76 | def defaultValue = true |
63 | | - |
| 77 | + |
64 | 78 | if (rootProject.hasProperty('instabugUploadEnable')) { |
65 | 79 | project.logger.warn "The property instabugUploadEnable is deprecated and will be removed in an upcoming version. \n" + |
66 | 80 | "You can use INSTABUG_SOURCEMAPS_UPLOAD_DISABLE environment variable instead." |
|
0 commit comments