Skip to content

Commit 3a3d932

Browse files
TheBuggedYRNHeshamMegid
authored andcommitted
[MOB-11998] Use CLI in Sourcemaps Gradle Task (#938)
1 parent 74c40cc commit 3a3d932

File tree

6 files changed

+137
-144
lines changed

6 files changed

+137
-144
lines changed

.circleci/config.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ jobs:
7878
steps:
7979
- advanced-checkout/shallow-checkout
8080
- run:
81-
name: Validate Android Script
82-
command: bash -n android/upload_sourcemap.sh
81+
name: Validate Scripts
82+
command: ls scripts/*.sh | xargs bash -n
8383
- run:
8484
name: Validate iOS Script
8585
command: bash -n ios/upload_sourcemap.sh

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
# Changelog
22

3+
## [Unreleased](https://github.com/Instabug/Instabug-React-Native/compare/v11.9.1...HEAD)
4+
5+
### Added
6+
7+
- Add support for Android automatic source map file upload on Windows; This requires setting the `INSTABUG_APP_TOKEN` environment variable ([#938](https://github.com/Instabug/Instabug-React-Native/pull/938)).
8+
9+
### Changed
10+
11+
- Improve release variant's build time on Android, by using the react-native-generated source map file, instead of generating it within our scripts ([#938](https://github.com/Instabug/Instabug-React-Native/pull/938)).
12+
313
## [11.9.1](https://github.com/Instabug/Instabug-React-Native/compare/v11.9.0...v11.9.1) (March 01, 2023)
414

515
### Changed

android/build.gradle

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ apply plugin: 'com.android.library'
22

33
apply from: './jacoco.gradle'
44
apply from: './native.gradle'
5+
apply from: './sourcemaps.gradle'
56

67
String getExtOrDefault(String name) {
78
if (rootProject.ext.has(name)) {
@@ -40,24 +41,3 @@ dependencies {
4041
testImplementation "org.mockito:mockito-android:3.4.0"
4142
testImplementation 'junit:junit:4.13.2'
4243
}
43-
44-
45-
import org.apache.tools.ant.taskdefs.condition.Os
46-
task upload_sourcemap(type: Exec) {
47-
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
48-
commandLine 'cmd', '/c', 'echo Automatic Upload of sourcemap files is currently not available on windows, please generate the sourcemapfiles and upload them to the dashboard'
49-
project.logger.lifecycle('Automatic Upload of sourcemap files is currently not available on windows, please generate the sourcemapfiles and upload them to the dashboard')
50-
} else {
51-
commandLine 'sh', './upload_sourcemap.sh', rootDir
52-
}
53-
}
54-
55-
tasks.whenTaskAdded { task ->
56-
def isEnabled = rootProject.hasProperty('instabugUploadEnable')
57-
? new Boolean(rootProject.property('instabugUploadEnable'))
58-
: true
59-
60-
if (task.name == 'preReleaseBuild' && isEnabled) {
61-
task.dependsOn upload_sourcemap
62-
}
63-
}

android/sourcemaps.gradle

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+
}

android/upload_sourcemap.sh

Lines changed: 0 additions & 121 deletions
This file was deleted.

scripts/find-token.sh

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/bin/sh
2+
3+
# Searches for app token within source files.
4+
5+
INIT_APP_TOKEN=$(
6+
grep "Instabug.init({" -r -A 6 -m 1 --exclude-dir={node_modules,ios,android} --include=\*.{js,ts,jsx,tsx} ./ |
7+
grep "token:[[:space:]]*[\"\'][0-9a-zA-Z]*[\"\']" |
8+
grep -o "[\"\'][0-9a-zA-Z]*[\"\']" |
9+
cut -d "\"" -f 2 |
10+
cut -d "'" -f 2
11+
)
12+
13+
if [ ! -z "${INIT_APP_TOKEN}" ]; then
14+
echo $INIT_APP_TOKEN
15+
exit 0
16+
fi
17+
18+
START_APP_TOKEN=$(
19+
grep "Instabug.start(" -r -A 1 -m 1 --exclude-dir={node_modules,ios,android} --include=\*.{js,ts,jsx,tsx} ./ |
20+
grep -o "[\"\'][0-9a-zA-Z]*[\"\']" |
21+
cut -d "\"" -f 2 |
22+
cut -d "'" -f 2
23+
)
24+
25+
if [ ! -z "${START_APP_TOKEN}" ]; then
26+
echo $START_APP_TOKEN
27+
exit 0
28+
fi
29+
30+
echo "Couldn't find Instabug's app token"
31+
exit 1

0 commit comments

Comments
 (0)