Skip to content

[MOB-11999] Use RN Internal Sourcemap Script #942

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Mar 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ jobs:
command: ls scripts/*.sh | xargs bash -n
- run:
name: Validate iOS Script
command: bash -n ios/upload_sourcemap.sh
command: bash -n ios/sourcemaps.sh

# Make sure that files like yarn.lock and project.pbxproj
# are in sync with the latest changes in package.json and
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
### Changed

- 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)).
- Improve debug variant's build time on iOS, by disabling automatic source map file uploads ([#942](https://github.com/Instabug/Instabug-React-Native/pull/942)).

## [11.9.1](https://github.com/Instabug/Instabug-React-Native/compare/v11.9.0...v11.9.1) (March 01, 2023)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@
name = "[CP-User] [instabug-reactnative] Upload Sourcemap";
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "#!/bin/sh\ncd ${PROJECT_DIR}\ncd ..\nif [ -s \"$HOME/.nvm/nvm.sh\" ]; then\n. \"$HOME/.nvm/nvm.sh\"\nelif [ -x \"$(command -v brew)\" ] && [ -s \"$(brew --prefix nvm)/nvm.sh\" ]; then\n. \"$(brew --prefix nvm)/nvm.sh\"\nfi\nexport NODE_BINARY=node\n\nINSTABUG_SOURCEMAPS_UPLOAD_DISABLE=$(echo $INSTABUG_SOURCEMAPS_UPLOAD_DISABLE | tr 'A-Z' 'a-z')\nif [ \"${INSTABUG_SOURCEMAPS_UPLOAD_DISABLE}\" = \"true\" ]; then\n echo \"Instabug: Environment variable INSTABUG_SOURCEMAPS_UPLOAD_DISABLE was set to true, skipping sourcemap upload\"\n exit 0\nfi\n\nif [ ! \"$INFOPLIST_FILE\" ] || [ -z \"$INFOPLIST_FILE\" ]; then\n echo \"Instabug: INFOPLIST_FILE not found in Xcode build settings, skipping sourcemap upload\"\n exit 0\nfi\n\nif [ ! \"${PROJECT_DIR}\" ] || [ -z \"${PROJECT_DIR}\" ]; then\n echo \"Instabug: PROJECT_DIR not found in Xcode build settings, skipping sourcemap upload\"\n exit 0\nfi\n\nPLIST_ABS_PATH=\"$PROJECT_DIR/$INFOPLIST_FILE\"\necho \"Instabug: PLIST_ABS_PATH: $PLIST_ABS_PATH\"\n\nif [ ! \"${INSTABUG_APP_TOKEN}\" ] || [ -z \"${INSTABUG_APP_TOKEN}\" ]; then\n echo \"Instabug: Looking for Token...\"\n INSTABUG_APP_TOKEN=$(grep -r -A 6 --exclude-dir={node_modules,ios,android} --exclude='*.json' \"Instabug.init({\" ./ -m1 | grep \"token:[[:space:]]*[\\\"\\'][0-9a-zA-Z]*[\\\"\\']\" | grep -o \"[\\\"\\'][0-9a-zA-Z]*[\\\"\\']\" | cut -d \"\\\"\" -f 2 | cut -d \"'\" -f 2)\n if [ -z \"${INSTABUG_APP_TOKEN}\" ]; then\n INSTABUG_APP_TOKEN=$(grep -r --exclude-dir={node_modules,ios,android} --exclude='*.json' \"Instabug.start[WithToken]*([\\\"\\'][0-9a-zA-Z]*[\\\"\\']\" ./ -m 1 | grep -o \"[\\\"\\'][0-9a-zA-Z]*[\\\"\\']\" | cut -d \"\\\"\" -f 2 | cut -d \"'\" -f 2)\n fi\nfi\n\nif [ ! \"${INSTABUG_APP_TOKEN}\" ] || [ -z \"${INSTABUG_APP_TOKEN}\" ]; then\n echo \"Instabug: INSTABUG_APP_TOKEN not found. Make sure you've added the SDK initialization line Instabug.init Or added the environment variable INSTABUG_APP_TOKEN\"\n exit 0\nelse\n if [ ! \"${INSTABUG_APP_VERSION_CODE}\" ] || [ -z \"${INSTABUG_APP_VERSION_CODE}\" ]; then\n INSTABUG_APP_VERSION_CODE=$(/usr/libexec/PlistBuddy -c 'print CFBundleVersion' ${PLIST_ABS_PATH} )\n if [ ! \"${INSTABUG_APP_VERSION_CODE}\" ] || [ -z \"${INSTABUG_APP_VERSION_CODE}\" ]; then\n echo \"CFBundleVersion could not be found, please upload the sourcemap files manually\"\n exit 0\n fi\n INSTABUG_APP_VERSION_CODE_ENV=$(grep -o '$(.*)' <<< $INSTABUG_APP_VERSION_CODE | cut -d \"(\" -f2 | cut -d \")\" -f1)\n if !([ ! \"${INSTABUG_APP_VERSION_CODE_ENV}\" ] || [ -z \"${INSTABUG_APP_VERSION_CODE_ENV}\" ]); then\n INSTABUG_APP_VERSION_CODE=${!INSTABUG_APP_VERSION_CODE_ENV}\n if [ ! \"${INSTABUG_APP_VERSION_CODE}\" ] || [ -z \"${INSTABUG_APP_VERSION_CODE}\" ]; then\n echo \"Environment variable $INSTABUG_APP_VERSION_CODE_ENV was specified inside Info.plist but was not found, please upload the sourcemap files manually\" \n exit 0\n fi\n fi\n fi\n if [ ! \"${INSTABUG_APP_VERSION_NAME}\" ] || [ -z \"${INSTABUG_APP_VERSION_NAME}\" ]; then\n INSTABUG_APP_VERSION_NAME=$(/usr/libexec/PlistBuddy -c 'print CFBundleShortVersionString' ${PLIST_ABS_PATH} )\n if [ ! \"${INSTABUG_APP_VERSION_NAME}\" ] || [ -z \"${INSTABUG_APP_VERSION_NAME}\" ]; then\n echo \"CFBundleShortVersionString could not be found, please upload the sourcemap files manually\"\n exit 0\n fi\n INSTABUG_APP_VERSION_NAME_ENV=$(grep -o '$(.*)' <<< $INSTABUG_APP_VERSION_NAME | cut -d \"(\" -f2 | cut -d \")\" -f1)\n if !([ ! \"${INSTABUG_APP_VERSION_NAME_ENV}\" ] || [ -z \"${INSTABUG_APP_VERSION_NAME_ENV}\" ]); then\n INSTABUG_APP_VERSION_NAME=${!INSTABUG_APP_VERSION_NAME_ENV}\n if [ ! \"${INSTABUG_APP_VERSION_NAME}\" ] || [ -z \"${INSTABUG_APP_VERSION_NAME}\" ]; then\n echo \"Environment variable $INSTABUG_APP_VERSION_NAME_ENV was specified inside Info.plist but was not found, please upload the sourcemap files manually\" \n exit 0\n fi\n fi\n fi\n if [ -z \"${INSTABUG_ENTRY_FILE}\" ]; then \n ENTRY_FILE='index.js'\n else \n ENTRY_FILE=${INSTABUG_ENTRY_FILE}\n fi\n if [ ! -f $ENTRY_FILE ]; then\n echo \"Instabug: err: entry file not found. Make sure\" \"\\\"${ENTRY_FILE}\\\"\" \"exists in your projects root directory. Or add the environment variable INSTABUG_ENTRY_FILE with the name of your entry file\"\n exit 0\n fi\n VERSION='{\"code\":\"'\"$INSTABUG_APP_VERSION_CODE\"'\",\"name\":\"'\"$INSTABUG_APP_VERSION_NAME\"'\"}'\n echo \"Instabug: Token:\" \"\\\"\"${INSTABUG_APP_TOKEN}\"\\\"\"\n echo \"Instabug: VERSION: $VERSION\"\n echo \"Instabug: Entry file found\" \"\\\"\"${ENTRY_FILE}\"\\\"\"\n echo \"Instabug: Generating sourcemap files...\"\n HERMES_ENABLED=$(grep hermes-engine ./ios/Podfile.lock -m 1)\n INSTALLED_RN_VERSION_MAJOR=$(node -p \"require('react-native/package.json').version\" | cut -d \".\" -f2)\n if [[ ! -z \"$HERMES_ENABLED\" && \"$INSTALLED_RN_VERSION_MAJOR\" -ge 65 && \"$INSTALLED_RN_VERSION_MAJOR\" -lt 70 ]]; then\n EXTRA_ARGS=\"--minify false\"\n fi\n #Generate ios sourcemap\n npx react-native bundle --platform ios \\\n --reset-cache \\\n --entry-file $ENTRY_FILE \\\n --dev false \\\n --bundle-output ./ios/main.jsbundle \\\n --sourcemap-output ./ios-sourcemap.json \\\n $EXTRA_ARGS\n \n echo \"Instabug: Uploading files...\"\n #Upload ios sourcemap\n curl -X POST 'https://api.instabug.com/api/sdk/v3/symbols_files' -F \"app_version=${VERSION}\" -F \"symbols_file=@./ios-sourcemap.json\" -F \"application_token=${INSTABUG_APP_TOKEN}\" -F \"platform=react_native\" -F \"os=ios\" \n rm -rf ios-sourcemap.json\n echo \nfi\n";
shellScript = "#!/bin/sh\n\nmain() {\n if [[ \"$INSTABUG_SOURCEMAPS_UPLOAD_DISABLE\" = true ]]; then\n echo \"[Info] \\`INSTABUG_SOURCEMAPS_UPLOAD_DISABLE\\` was set to true, skipping sourcemaps upload...\"\n exit 0\n fi\n\n if [[ -z \"$INFOPLIST_FILE\" ]] || [[ -z \"$PROJECT_DIR\" ]]; then\n echo \"[Error] Instabug sourcemaps script must be invoked by Xcode\"\n exit 1\n fi\n\n local source_map_file=$(generate_sourcemaps | tail -n 1)\n\n local js_project_dir=\"$PROJECT_DIR/..\"\n local instabug_dir=$(dirname $(node -p \"require.resolve('instabug-reactnative/package.json')\"))\n local inferred_token=$(cd $js_project_dir && source $instabug_dir/scripts/find-token.sh)\n local app_token=$(resolve_var \"App Token\" \"INSTABUG_APP_TOKEN\" \"$inferred_token\" | tail -n 1)\n\n local inferred_name=$(/usr/libexec/PlistBuddy -c 'print CFBundleShortVersionString' \"$PROJECT_DIR/$INFOPLIST_FILE\")\n local version_name=$(resolve_var \"Version Name\" \"INSTABUG_APP_VERSION_NAME\" \"$inferred_name\" | tail -n 1)\n\n local inferred_code=$(/usr/libexec/PlistBuddy -c 'print CFBundleVersion' \"$PROJECT_DIR/$INFOPLIST_FILE\")\n local version_code=$(resolve_var \"Version Code\" \"INSTABUG_APP_VERSION_CODE\" \"$inferred_code\" | tail -n 1)\n\n npx instabug upload-sourcemaps \\\n --platform ios \\\n --file $source_map_file \\\n --token $app_token \\\n --name $version_name \\\n --code $version_code\n}\n\ngenerate_sourcemaps() {\n local react_native_dir=$(dirname $(node -p \"require.resolve('react-native/package.json')\"))\n\n # Fixes an issue with react-native prior to v0.67.0\n # For more info: https://github.com/facebook/react-native/issues/32168\n export RN_DIR=$react_native_dir \n\n # Used withing `react-native-xcode.sh` to generate sourcemap file\n export SOURCEMAP_FILE=\"$(pwd)/main.jsbundle.map\";\n\n source \"$react_native_dir/scripts/react-native-xcode.sh\"\n\n if [[ ! -f \"$SOURCEMAP_FILE\" ]]; then\n echo \"[Error] Unable to find source map file at: $SOURCEMAP_FILE\"\n exit 1\n fi\n\n echo $SOURCEMAP_FILE\n}\n\nresolve_var() {\n local name=$1\n local env_key=$2\n local default_value=$3\n\n local env_value=\"${!env_key}\"\n\n if [[ -n \"$env_value\" ]] && [[ \"$env_value\" != default_value ]]; then\n echo \"[Warning] Environment variable \\`$env_key\\` might have incorrect value, make sure this was intentional:\"\n echo \" Environment Value: $env_value\"\n echo \" Default Value: $default_value\"\n fi\n\n local value=\"${env_value:-$default_value}\"\n\n if [[ -z \"$value\" ]]; then\n echo \"[Error] Unable to find $name! Set the environment variable \\`$env_key\\` and try again.\"\n exit 1\n fi\n\n echo $value\n}\n\nmain \"$@\"; exit\n";
};
DD1AA4DBFEAB75A854826F0C /* [CP] Embed Pods Frameworks */ = {
isa = PBXShellScriptBuildPhase;
Expand Down
78 changes: 78 additions & 0 deletions ios/sourcemaps.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#!/bin/sh

main() {
if [[ "$INSTABUG_SOURCEMAPS_UPLOAD_DISABLE" = true ]]; then
echo "[Info] \`INSTABUG_SOURCEMAPS_UPLOAD_DISABLE\` was set to true, skipping sourcemaps upload..."
exit 0
fi

if [[ -z "$INFOPLIST_FILE" ]] || [[ -z "$PROJECT_DIR" ]]; then
echo "[Error] Instabug sourcemaps script must be invoked by Xcode"
exit 1
fi

local source_map_file=$(generate_sourcemaps | tail -n 1)

local js_project_dir="$PROJECT_DIR/.."
local instabug_dir=$(dirname $(node -p "require.resolve('instabug-reactnative/package.json')"))
local inferred_token=$(cd $js_project_dir && source $instabug_dir/scripts/find-token.sh)
local app_token=$(resolve_var "App Token" "INSTABUG_APP_TOKEN" "$inferred_token" | tail -n 1)

local inferred_name=$(/usr/libexec/PlistBuddy -c 'print CFBundleShortVersionString' "$PROJECT_DIR/$INFOPLIST_FILE")
local version_name=$(resolve_var "Version Name" "INSTABUG_APP_VERSION_NAME" "$inferred_name" | tail -n 1)

local inferred_code=$(/usr/libexec/PlistBuddy -c 'print CFBundleVersion' "$PROJECT_DIR/$INFOPLIST_FILE")
local version_code=$(resolve_var "Version Code" "INSTABUG_APP_VERSION_CODE" "$inferred_code" | tail -n 1)

npx instabug upload-sourcemaps \
--platform ios \
--file $source_map_file \
--token $app_token \
--name $version_name \
--code $version_code
}

generate_sourcemaps() {
local react_native_dir=$(dirname $(node -p "require.resolve('react-native/package.json')"))

# Fixes an issue with react-native prior to v0.67.0
# For more info: https://github.com/facebook/react-native/issues/32168
export RN_DIR=$react_native_dir

# Used withing `react-native-xcode.sh` to generate sourcemap file
export SOURCEMAP_FILE="$(pwd)/main.jsbundle.map";

source "$react_native_dir/scripts/react-native-xcode.sh"

if [[ ! -f "$SOURCEMAP_FILE" ]]; then
echo "[Error] Unable to find source map file at: $SOURCEMAP_FILE"
exit 1
fi

echo $SOURCEMAP_FILE
}

resolve_var() {
local name=$1
local env_key=$2
local default_value=$3

local env_value="${!env_key}"

if [[ -n "$env_value" ]] && [[ "$env_value" != default_value ]]; then
echo "[Warning] Environment variable \`$env_key\` might have incorrect value, make sure this was intentional:"
echo " Environment Value: $env_value"
echo " Default Value: $default_value"
fi

local value="${env_value:-$default_value}"

if [[ -z "$value" ]]; then
echo "[Error] Unable to find $name! Set the environment variable \`$env_key\` and try again."
exit 1
fi

echo $value
}

main "$@"; exit
105 changes: 0 additions & 105 deletions ios/upload_sourcemap.sh

This file was deleted.

2 changes: 1 addition & 1 deletion react-native.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module.exports = {
scriptPhases: [
{
name: '[instabug-reactnative] Upload Sourcemap',
path: './ios/upload_sourcemap.sh',
path: './ios/sourcemaps.sh',
execution_position: 'after_compile',
},
],
Expand Down