forked from Expensify/App
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup-mapbox-sdk.sh
executable file
·140 lines (120 loc) · 5.3 KB
/
setup-mapbox-sdk.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/bin/bash
# Mapbox SDK Configuration Script for iOS and Android
# ===================================================
#
# Purpose:
# --------
# This script configures the development environment to download Mapbox SDKs
# for both iOS and Android builds. We use Mapbox to display maps in the App. As Mapbox SDKs
# are closed-sourced, we need to authenticate with Mapbox during the download.
#
# Background:
# -----------
# Engineers are required to obtain a secret token from Mapbox and store it on
# their development machine. This allows tools like CocoaPods for iOS or Gradle for Android
# to access the Mapbox SDK during the build process.
#
# The `.netrc` file for iOS Configuration:
# ----------------------------------------
# The token for iOS is stored in the `.netrc` file located in the user's home directory.
# This file is used in Unix-like systems to store credentials for remote machine access.
#
# The `gradle.properties` file for Android Configuration:
# -------------------------------------------------------
# The token for Android is stored in the `gradle.properties` file located in the .gradle directory
# in the user's home. This is accessed by the Android build system during the SDK download.
#
# How this script helps:
# ----------------------
# This script streamlines the process of adding the credential to both the `.netrc` and
# `gradle.properties` files. When executed, it prompts the user for the secret token and
# then saves it to the respective files along with other necessary information.\n
#
# Usage:
# ------
# To run this script, pass the secret Mapbox access token as a command-line argument:
# ./scriptname.sh YOUR_MAPBOX_ACCESS_TOKEN
# Use functions and variables from the utils script
source scripts/shellUtils.sh
NETRC_PATH="$HOME/.netrc"
GRADLE_PROPERTIES_PATH="$HOME/.gradle/gradle.properties"
# This function provides a user-friendly error message when the script encounters an error.
# It informs the user about probable permission issues and suggests commands to resolve them.
handleError() {
echo -e "\n"
error "The script failed."
echo "The most probable reason is permissions."
echo -e "Please ensure you have read/write permissions for the following:\n"
echo -e "1. \033[1m$NETRC_PATH\033[0m"
echo -e "2. \033[1m$GRADLE_PROPERTIES_PATH\033[0m"
echo -e "\nYou can grant permissions using the commands:"
echo -e "\033[1mchmod u+rw $NETRC_PATH\033[0m"
echo -e "\033[1mchmod u+rw $GRADLE_PROPERTIES_PATH\033[0m"
echo -e "\n"
exit 1
}
# Set a trap to call the handleError function when any of the commands fail
trap handleError ERR
# Take the token as a command-line argument
TOKEN="$1"
# Check if the token was provided
if [ -z "$TOKEN" ]; then
echo "Usage: $0 <YOUR_MAPBOX_ACCESS_TOKEN>"
echo "No token provided. Exiting."
exit 1
fi
# -----------------------------------------------
# iOS Configuration for .netrc
# -----------------------------------------------
info "Configuring $NETRC_PATH for Mapbox iOS SDK download"
# Check for existing Mapbox entries in .netrc
if grep -q "api.mapbox.com" "$NETRC_PATH"; then
# Extract the current token from .netrc
CURRENT_TOKEN=$(grep -A2 "api.mapbox.com" "$NETRC_PATH" | grep "password" | awk '{print $2}')
# Compare the current token to the entered token
if [ "$CURRENT_TOKEN" == "$TOKEN" ]; then
echo -e "\nThe entered token matches the existing token in $NETRC_PATH. No changes made."
else
# Use sed to replace the old token with the new one
sed -i.bak "/api.mapbox.com/,+2s/password $CURRENT_TOKEN/password $TOKEN/" "$NETRC_PATH"
echo -e "\nToken updated in $NETRC_PATH"
fi
else
# If no existing entry, append the new credentials
{
echo "machine api.mapbox.com"
echo "login mapbox"
echo "password $TOKEN"
} >> "$NETRC_PATH"
# Set the permissions of the .netrc file to ensure it's kept private
chmod 600 "$NETRC_PATH"
echo -e "\n$NETRC_PATH has been updated with new credentials"
fi
# -----------------------------------------------
# Android Configuration for gradle.properties
# -----------------------------------------------
echo -e "\n"
info "Configuring $GRADLE_PROPERTIES_PATH for Mapbox Android SDK download"
# Ensure the .gradle directory exists
if [ ! -d "$HOME/.gradle" ]; then
mkdir "$HOME/.gradle"
fi
# Check if gradle.properties exists. If not, create one.
if [ ! -f "$GRADLE_PROPERTIES_PATH" ]; then
touch "$GRADLE_PROPERTIES_PATH"
fi
# Check if MAPBOX_DOWNLOADS_TOKEN already exists in the file
if grep -q "MAPBOX_DOWNLOADS_TOKEN" "$GRADLE_PROPERTIES_PATH"; then
# Extract the current token from gradle.properties
CURRENT_ANDROID_TOKEN=$(grep "MAPBOX_DOWNLOADS_TOKEN" "$GRADLE_PROPERTIES_PATH" | cut -d'=' -f2)
# Compare the current token to the entered token
if [ "$CURRENT_ANDROID_TOKEN" == "$TOKEN" ]; then
echo -e "\nThe entered token matches the existing token in $GRADLE_PROPERTIES_PATH. No changes made."
else
sed -i.bak "s/MAPBOX_DOWNLOADS_TOKEN=$CURRENT_ANDROID_TOKEN/MAPBOX_DOWNLOADS_TOKEN=$TOKEN/" "$GRADLE_PROPERTIES_PATH"
echo -e "\nToken updated in $GRADLE_PROPERTIES_PATH"
fi
else
echo "MAPBOX_DOWNLOADS_TOKEN=$TOKEN" >> "$GRADLE_PROPERTIES_PATH"
echo -e "\n$GRADLE_PROPERTIES_PATH has been updated with new credentials"
fi