Jeff Gaston | 259a905 | 2018-10-19 14:55:19 -0400 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | set -e |
Dustin Lam | b8289c8 | 2019-06-25 15:48:18 -0700 | [diff] [blame] | 3 | set -m |
Jeff Gaston | 259a905 | 2018-10-19 14:55:19 -0400 | [diff] [blame] | 4 | |
| 5 | # This is a wrapper script that runs the specific version of Android Studio that is recommended for developing in this repository. |
| 6 | # (This serves a similar purpose to gradlew) |
| 7 | |
| 8 | |
| 9 | function getStudioUrl() { |
Jeff Gaston | 149350d | 2019-03-08 20:32:29 -0500 | [diff] [blame] | 10 | propertiesFile="${scriptDir}/buildSrc/studio_versions.properties" |
| 11 | version="$(grep "studio_version=" ${propertiesFile} | sed 's/[^=]*=//')" |
| 12 | ideaMajorVersion="$(grep "idea_major_version=" ${propertiesFile} | sed 's/[^=]*=//')" |
| 13 | buildNumber="$(grep "studio_build_number=" ${propertiesFile} | sed 's/[^=]*=//')" |
Jeff Gaston | 259a905 | 2018-10-19 14:55:19 -0400 | [diff] [blame] | 14 | osName="$1" |
Louis Pullen-Freilich | c5750d8 | 2019-04-23 14:23:03 +0100 | [diff] [blame] | 15 | extension="" |
| 16 | if [ "${osName}" == "linux" ]; then |
| 17 | extension="tar.gz" |
| 18 | else |
| 19 | extension="zip" |
| 20 | fi |
| 21 | studioUrl="https://dl.google.com/dl/android/studio/ide-zips/${version}/android-studio-ide-${ideaMajorVersion}.${buildNumber}-${osName}.${extension}" |
Jeff Gaston | 259a905 | 2018-10-19 14:55:19 -0400 | [diff] [blame] | 22 | echo "${studioUrl}" |
| 23 | } |
| 24 | |
Ashley Rose | bf3111f | 2019-07-02 17:20:26 -0400 | [diff] [blame] | 25 | acceptsLicenseAgreement="false" |
Ashley Rose | b77fc62 | 2019-07-03 14:35:32 -0400 | [diff] [blame] | 26 | runStudio="true" |
Jeff Gaston | 259a905 | 2018-10-19 14:55:19 -0400 | [diff] [blame] | 27 | scriptDir="$(cd $(dirname $0) && pwd)" |
Alan Viverette | 2fa738f | 2018-11-28 16:38:53 -0500 | [diff] [blame] | 28 | projectDir=$scriptDir |
Jeff Gaston | 259a905 | 2018-10-19 14:55:19 -0400 | [diff] [blame] | 29 | tempDir="${scriptDir}/studio" |
| 30 | function getOsName() { |
| 31 | unameOutput="$(uname)" |
| 32 | osName="" |
| 33 | if [ "${unameOutput}" == "Linux" ]; then |
| 34 | osName="linux" |
| 35 | else |
| 36 | osName="mac" |
| 37 | fi |
| 38 | echo "${osName}" |
| 39 | } |
| 40 | osName="$(getOsName)" |
| 41 | studioUrl="$(getStudioUrl $osName)" |
| 42 | studioDestName="$(basename ${studioUrl})" |
| 43 | studioZipPath="${tempDir}/${studioDestName}" |
Louis Pullen-Freilich | c5750d8 | 2019-04-23 14:23:03 +0100 | [diff] [blame] | 44 | studioUnzippedPath="$(echo ${studioZipPath} | sed 's/\.zip$//; s/\.tar.gz$//')" |
Jeff Gaston | 259a905 | 2018-10-19 14:55:19 -0400 | [diff] [blame] | 45 | |
Ashley Rose | bf3111f | 2019-07-02 17:20:26 -0400 | [diff] [blame] | 46 | function parseOptions() { |
| 47 | while :; do |
| 48 | case "$1" in |
| 49 | -y|--accept-license-agreement) |
| 50 | acceptsLicenseAgreement="true" |
| 51 | ;; |
| 52 | --update-only) |
Ashley Rose | b77fc62 | 2019-07-03 14:35:32 -0400 | [diff] [blame] | 53 | runStudio="false" |
Ashley Rose | bf3111f | 2019-07-02 17:20:26 -0400 | [diff] [blame] | 54 | ;; |
| 55 | *) |
| 56 | break |
| 57 | esac |
| 58 | |
| 59 | shift |
| 60 | done |
| 61 | } |
| 62 | |
Ashley Rose | b77fc62 | 2019-07-03 14:35:32 -0400 | [diff] [blame] | 63 | |
Jeff Gaston | 259a905 | 2018-10-19 14:55:19 -0400 | [diff] [blame] | 64 | function downloadFile() { |
| 65 | fromUrl="$1" |
| 66 | destPath="$2" |
| 67 | tempPath="${destPath}.tmp" |
Louis Pullen-Freilich | c5750d8 | 2019-04-23 14:23:03 +0100 | [diff] [blame] | 68 | if [ -f "${destPath}" ]; then |
| 69 | read -r -n 1 -p "File already exists. Do you want to delete and re-download? [Y/n]? " reply |
| 70 | |
| 71 | if [ ! -z "${reply}" ]; then |
| 72 | # Fix missing newline |
| 73 | echo |
| 74 | fi |
| 75 | |
| 76 | case "${reply}" in |
| 77 | [yY]|"") |
| 78 | rm "${destPath}" |
| 79 | ;; |
| 80 | *) |
| 81 | esac |
| 82 | fi |
| 83 | |
| 84 | if [ -f "${destPath}" ]; then |
| 85 | echo "Using existing file from ${destPath}" |
| 86 | else |
| 87 | echo "Downloading ${fromUrl} to ${destPath}" |
| 88 | curl "${fromUrl}" > "${tempPath}" |
| 89 | mv "${tempPath}" "${destPath}" |
| 90 | fi |
Jeff Gaston | 259a905 | 2018-10-19 14:55:19 -0400 | [diff] [blame] | 91 | } |
| 92 | |
Ashley Rose | 37f957b | 2019-02-12 16:07:39 -0500 | [diff] [blame] | 93 | function findStudioMacAppPath() { |
| 94 | echo "$(find "${studioUnzippedPath}" -type d -depth 1 -name "Android Studio*.app")" |
| 95 | } |
| 96 | |
| 97 | function getLicensePath() { |
| 98 | if [ "${osName}" == "mac" ]; then |
| 99 | appPath="$(findStudioMacAppPath)" |
| 100 | echo "${appPath}/Contents/Resources/LICENSE.txt" |
| 101 | else |
| 102 | echo "${studioUnzippedPath}/android-studio/LICENSE.txt" |
| 103 | fi |
| 104 | } |
| 105 | |
Jeff Gaston | 259a905 | 2018-10-19 14:55:19 -0400 | [diff] [blame] | 106 | function checkLicenseAgreement() { |
| 107 | # TODO: Is there a more official way to check that the user accepts the license? |
Ashley Rose | 09aa4d0 | 2019-02-12 16:40:54 -0500 | [diff] [blame] | 108 | |
| 109 | licenseAcceptedPath="${studioUnzippedPath}/STUDIOW_LICENSE_ACCEPTED" |
| 110 | |
| 111 | if [ ! -f "${licenseAcceptedPath}" ]; then |
Ashley Rose | bf3111f | 2019-07-02 17:20:26 -0400 | [diff] [blame] | 112 | if [ "${acceptsLicenseAgreement}" == "true" ]; then |
Ashley Rose | 09aa4d0 | 2019-02-12 16:40:54 -0500 | [diff] [blame] | 113 | touch "${licenseAcceptedPath}" |
| 114 | else |
Ashley Rose | 7d9c410 | 2019-02-19 11:52:14 -0500 | [diff] [blame] | 115 | read -r -n 1 -p "Do you accept the license agreement at $(getLicensePath) [Y/n]? " reply |
| 116 | |
| 117 | if [ ! -z "${reply}" ]; then |
Ashley Rose | b77fc62 | 2019-07-03 14:35:32 -0400 | [diff] [blame] | 118 | # Fix missing newline |
Ashley Rose | 7d9c410 | 2019-02-19 11:52:14 -0500 | [diff] [blame] | 119 | echo |
| 120 | fi |
| 121 | |
| 122 | case "${reply}" in |
| 123 | [yY]|"") |
| 124 | touch "${licenseAcceptedPath}" |
| 125 | ;; |
| 126 | *) |
| 127 | exit 1 |
| 128 | ;; |
| 129 | esac |
Ashley Rose | 09aa4d0 | 2019-02-12 16:40:54 -0500 | [diff] [blame] | 130 | fi |
Jeff Gaston | 259a905 | 2018-10-19 14:55:19 -0400 | [diff] [blame] | 131 | fi |
| 132 | } |
| 133 | |
Sam Gilbert | c547ea8 | 2019-06-14 14:58:35 -0400 | [diff] [blame] | 134 | # Temporary fix. Remove this after fixing b/135183535 |
| 135 | function updateJvmHeapSize() { |
| 136 | if [ "${osName}" == "mac" ]; then |
| 137 | sed -i '' 's/-Xmx.*/-Xmx8g/' "$(findStudioMacAppPath)/Contents/bin/studio.vmoptions" |
| 138 | else |
| 139 | sed -i 's/-Xmx.*/-Xmx8g/' "${studioUnzippedPath}/android-studio/bin/studio64.vmoptions" |
| 140 | sed -i 's/-Xmx.*/-Xmx4g/' "${studioUnzippedPath}/android-studio/bin/studio.vmoptions" |
| 141 | fi |
| 142 | } |
| 143 | |
Jeff Gaston | 259a905 | 2018-10-19 14:55:19 -0400 | [diff] [blame] | 144 | function updateStudio() { |
| 145 | # skip if already up-to-date |
Louis Pullen-Freilich | c5750d8 | 2019-04-23 14:23:03 +0100 | [diff] [blame] | 146 | if stat "${studioUnzippedPath}" >/dev/null 2>/dev/null; then |
Jeff Gaston | 259a905 | 2018-10-19 14:55:19 -0400 | [diff] [blame] | 147 | # already up-to-date |
| 148 | return |
| 149 | fi |
| 150 | |
| 151 | mkdir -p "${tempDir}" |
| 152 | downloadFile "${studioUrl}" "${studioZipPath}" |
| 153 | echo |
| 154 | |
| 155 | echo "Removing previous installations" |
| 156 | ls "${tempDir}" | grep -v "^${studioDestName}\$" | sed "s|^|${tempDir}/|" | xargs rm -rf |
| 157 | |
| 158 | echo "Unzipping" |
Louis Pullen-Freilich | c5750d8 | 2019-04-23 14:23:03 +0100 | [diff] [blame] | 159 | if [ ${studioZipPath: -7} == ".tar.gz" ]; then |
| 160 | mkdir "${studioUnzippedPath}" |
| 161 | tar -xvf "${studioZipPath}" -C "${studioUnzippedPath}" |
| 162 | else |
| 163 | unzip "${studioZipPath}" -d "${studioUnzippedPath}" |
| 164 | fi |
Jeff Gaston | 259a905 | 2018-10-19 14:55:19 -0400 | [diff] [blame] | 165 | } |
| 166 | |
Jeff Gaston | 57575ca | 2019-05-03 13:50:07 -0400 | [diff] [blame] | 167 | function ensureLocalPropertiesUpdated() { |
| 168 | testPath="${projectDir}/local.properties" |
| 169 | populaterCommand="./gradlew help" |
| 170 | if [ ! -f "${testPath}" ]; then |
| 171 | cd "$scriptDir" |
| 172 | echo "Creating $testPath by running '$populaterCommand'" |
| 173 | eval $populaterCommand |
| 174 | fi |
| 175 | } |
| 176 | |
Louis Pullen-Freilich | 1b57674a | 2019-06-13 19:51:30 +0100 | [diff] [blame] | 177 | # ANDROID_LINT_NULLNESS_IGNORE_DEPRECATED environment variable prevents Studio from showing IDE |
| 178 | # inspection warnings for nullability issues, if the context is deprecated |
| 179 | # This environment variable is consumed by InteroperabilityDetector.kt |
| 180 | |
Jeff Gaston | 259a905 | 2018-10-19 14:55:19 -0400 | [diff] [blame] | 181 | function runStudioLinux() { |
| 182 | studioPath="${studioUnzippedPath}/android-studio/bin/studio.sh" |
| 183 | echo "$studioPath &" |
Alan Viverette | 6ed162a | 2018-11-30 14:35:55 -0500 | [diff] [blame] | 184 | env STUDIO_PROPERTIES="${projectDir}/development/studio/idea.properties" \ |
| 185 | STUDIO_VM_OPTIONS="${projectDir}/development/studio/studio.vmoptions" \ |
Louis Pullen-Freilich | 1b57674a | 2019-06-13 19:51:30 +0100 | [diff] [blame] | 186 | ANDROID_LINT_NULLNESS_IGNORE_DEPRECATED="true" \ |
Alan Viverette | 6ed162a | 2018-11-30 14:35:55 -0500 | [diff] [blame] | 187 | "${studioPath}" "${projectDir}" & |
Jeff Gaston | 259a905 | 2018-10-19 14:55:19 -0400 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | function runStudioMac() { |
Ashley Rose | 37f957b | 2019-02-12 16:07:39 -0500 | [diff] [blame] | 191 | appPath="$(findStudioMacAppPath)" |
| 192 | echo "open ${appPath}" |
Alan Viverette | 6ed162a | 2018-11-30 14:35:55 -0500 | [diff] [blame] | 193 | env STUDIO_PROPERTIES="${projectDir}/development/studio/idea.properties" \ |
| 194 | STUDIO_VM_OPTIONS="${projectDir}/development/studio/studio.vmoptions" \ |
Louis Pullen-Freilich | 1b57674a | 2019-06-13 19:51:30 +0100 | [diff] [blame] | 195 | ANDROID_LINT_NULLNESS_IGNORE_DEPRECATED="true" \ |
Ashley Rose | 37f957b | 2019-02-12 16:07:39 -0500 | [diff] [blame] | 196 | open -a "${appPath}" "${projectDir}" |
Jeff Gaston | 259a905 | 2018-10-19 14:55:19 -0400 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | function runStudio() { |
Sam Gilbert | c547ea8 | 2019-06-14 14:58:35 -0400 | [diff] [blame] | 200 | updateJvmHeapSize |
Chris Craik | b84f61b | 2018-10-31 14:18:06 -0700 | [diff] [blame] | 201 | if [ "${osName}" == "mac" ]; then |
Jeff Gaston | 259a905 | 2018-10-19 14:55:19 -0400 | [diff] [blame] | 202 | runStudioMac |
Chris Craik | b84f61b | 2018-10-31 14:18:06 -0700 | [diff] [blame] | 203 | else |
| 204 | runStudioLinux |
Jeff Gaston | 259a905 | 2018-10-19 14:55:19 -0400 | [diff] [blame] | 205 | fi |
| 206 | } |
| 207 | |
| 208 | function main() { |
Ashley Rose | b77fc62 | 2019-07-03 14:35:32 -0400 | [diff] [blame] | 209 | parseOptions "$@" |
Jeff Gaston | 259a905 | 2018-10-19 14:55:19 -0400 | [diff] [blame] | 210 | updateStudio |
Ashley Rose | b77fc62 | 2019-07-03 14:35:32 -0400 | [diff] [blame] | 211 | if [ "${runStudio}" == "true" ]; then |
Ashley Rose | bf3111f | 2019-07-02 17:20:26 -0400 | [diff] [blame] | 212 | checkLicenseAgreement |
| 213 | ensureLocalPropertiesUpdated |
| 214 | runStudio |
| 215 | fi |
Jeff Gaston | 259a905 | 2018-10-19 14:55:19 -0400 | [diff] [blame] | 216 | } |
| 217 | |
Ashley Rose | b77fc62 | 2019-07-03 14:35:32 -0400 | [diff] [blame] | 218 | main "$@" |