Jeff Gaston | 259a905 | 2018-10-19 14:55:19 -0400 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | set -e |
| 3 | |
| 4 | # This is a wrapper script that runs the specific version of Android Studio that is recommended for developing in this repository. |
| 5 | # (This serves a similar purpose to gradlew) |
| 6 | |
| 7 | |
| 8 | function getStudioUrl() { |
Oussama Ben Abdelbaki | d0265c1 | 2019-02-08 16:06:33 -0500 | [diff] [blame] | 9 | version="3.4.0.12" |
Louis Pullen-Freilich | c89eff2 | 2019-02-01 17:30:17 +0000 | [diff] [blame] | 10 | ideaMajorVersion="183" |
Oussama Ben Abdelbaki | d0265c1 | 2019-02-08 16:06:33 -0500 | [diff] [blame] | 11 | buildNumber="5256591" |
Jeff Gaston | 259a905 | 2018-10-19 14:55:19 -0400 | [diff] [blame] | 12 | osName="$1" |
Alan Viverette | 6ed162a | 2018-11-30 14:35:55 -0500 | [diff] [blame] | 13 | studioUrl="https://dl.google.com/dl/android/studio/ide-zips/${version}/android-studio-ide-${ideaMajorVersion}.${buildNumber}-${osName}.zip" |
Jeff Gaston | 259a905 | 2018-10-19 14:55:19 -0400 | [diff] [blame] | 14 | echo "${studioUrl}" |
| 15 | } |
| 16 | |
| 17 | acceptsLicenseAgreement="$1" |
| 18 | scriptDir="$(cd $(dirname $0) && pwd)" |
Alan Viverette | 2fa738f | 2018-11-28 16:38:53 -0500 | [diff] [blame] | 19 | projectDir=$scriptDir |
Jeff Gaston | 259a905 | 2018-10-19 14:55:19 -0400 | [diff] [blame] | 20 | tempDir="${scriptDir}/studio" |
| 21 | function getOsName() { |
| 22 | unameOutput="$(uname)" |
| 23 | osName="" |
| 24 | if [ "${unameOutput}" == "Linux" ]; then |
| 25 | osName="linux" |
| 26 | else |
| 27 | osName="mac" |
| 28 | fi |
| 29 | echo "${osName}" |
| 30 | } |
| 31 | osName="$(getOsName)" |
| 32 | studioUrl="$(getStudioUrl $osName)" |
| 33 | studioDestName="$(basename ${studioUrl})" |
| 34 | studioZipPath="${tempDir}/${studioDestName}" |
| 35 | studioUnzippedPath="$(echo ${studioZipPath} | sed 's/\.zip$//')" |
| 36 | |
| 37 | function downloadFile() { |
| 38 | fromUrl="$1" |
| 39 | destPath="$2" |
| 40 | tempPath="${destPath}.tmp" |
| 41 | echo "Downloading ${fromUrl} to ${destPath}" |
| 42 | curl "${fromUrl}" > "${tempPath}" |
| 43 | mv "${tempPath}" "${destPath}" |
| 44 | } |
| 45 | |
Ashley Rose | 37f957b | 2019-02-12 16:07:39 -0500 | [diff] [blame] | 46 | function findStudioMacAppPath() { |
| 47 | echo "$(find "${studioUnzippedPath}" -type d -depth 1 -name "Android Studio*.app")" |
| 48 | } |
| 49 | |
| 50 | function getLicensePath() { |
| 51 | if [ "${osName}" == "mac" ]; then |
| 52 | appPath="$(findStudioMacAppPath)" |
| 53 | echo "${appPath}/Contents/Resources/LICENSE.txt" |
| 54 | else |
| 55 | echo "${studioUnzippedPath}/android-studio/LICENSE.txt" |
| 56 | fi |
| 57 | } |
| 58 | |
Jeff Gaston | 259a905 | 2018-10-19 14:55:19 -0400 | [diff] [blame] | 59 | function checkLicenseAgreement() { |
| 60 | # 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] | 61 | |
| 62 | licenseAcceptedPath="${studioUnzippedPath}/STUDIOW_LICENSE_ACCEPTED" |
| 63 | |
| 64 | if [ ! -f "${licenseAcceptedPath}" ]; then |
| 65 | if [ "${acceptsLicenseAgreement}" == "-y" ]; then |
| 66 | touch "${licenseAcceptedPath}" |
| 67 | else |
Ashley Rose | 7d9c410 | 2019-02-19 11:52:14 -0500 | [diff] [blame] | 68 | read -r -n 1 -p "Do you accept the license agreement at $(getLicensePath) [Y/n]? " reply |
| 69 | |
| 70 | if [ ! -z "${reply}" ]; then |
| 71 | # Fix missing newline |
| 72 | echo |
| 73 | fi |
| 74 | |
| 75 | case "${reply}" in |
| 76 | [yY]|"") |
| 77 | touch "${licenseAcceptedPath}" |
| 78 | ;; |
| 79 | *) |
| 80 | exit 1 |
| 81 | ;; |
| 82 | esac |
Ashley Rose | 09aa4d0 | 2019-02-12 16:40:54 -0500 | [diff] [blame] | 83 | fi |
Jeff Gaston | 259a905 | 2018-10-19 14:55:19 -0400 | [diff] [blame] | 84 | fi |
| 85 | } |
| 86 | |
| 87 | function updateStudio() { |
| 88 | # skip if already up-to-date |
| 89 | if stat "${studioZipPath}" >/dev/null 2>/dev/null; then |
| 90 | # already up-to-date |
| 91 | return |
| 92 | fi |
| 93 | |
| 94 | mkdir -p "${tempDir}" |
| 95 | downloadFile "${studioUrl}" "${studioZipPath}" |
| 96 | echo |
| 97 | |
| 98 | echo "Removing previous installations" |
| 99 | ls "${tempDir}" | grep -v "^${studioDestName}\$" | sed "s|^|${tempDir}/|" | xargs rm -rf |
| 100 | |
| 101 | echo "Unzipping" |
| 102 | unzip "${studioZipPath}" -d "${studioUnzippedPath}" |
| 103 | } |
| 104 | |
| 105 | function runStudioLinux() { |
| 106 | studioPath="${studioUnzippedPath}/android-studio/bin/studio.sh" |
| 107 | echo "$studioPath &" |
Alan Viverette | 6ed162a | 2018-11-30 14:35:55 -0500 | [diff] [blame] | 108 | env STUDIO_PROPERTIES="${projectDir}/development/studio/idea.properties" \ |
| 109 | STUDIO_VM_OPTIONS="${projectDir}/development/studio/studio.vmoptions" \ |
| 110 | "${studioPath}" "${projectDir}" & |
Jeff Gaston | 259a905 | 2018-10-19 14:55:19 -0400 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | function runStudioMac() { |
Ashley Rose | 37f957b | 2019-02-12 16:07:39 -0500 | [diff] [blame] | 114 | appPath="$(findStudioMacAppPath)" |
| 115 | echo "open ${appPath}" |
Alan Viverette | 6ed162a | 2018-11-30 14:35:55 -0500 | [diff] [blame] | 116 | env STUDIO_PROPERTIES="${projectDir}/development/studio/idea.properties" \ |
| 117 | STUDIO_VM_OPTIONS="${projectDir}/development/studio/studio.vmoptions" \ |
Ashley Rose | 37f957b | 2019-02-12 16:07:39 -0500 | [diff] [blame] | 118 | open -a "${appPath}" "${projectDir}" |
Jeff Gaston | 259a905 | 2018-10-19 14:55:19 -0400 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | function runStudio() { |
Chris Craik | b84f61b | 2018-10-31 14:18:06 -0700 | [diff] [blame] | 122 | if [ "${osName}" == "mac" ]; then |
Jeff Gaston | 259a905 | 2018-10-19 14:55:19 -0400 | [diff] [blame] | 123 | runStudioMac |
Chris Craik | b84f61b | 2018-10-31 14:18:06 -0700 | [diff] [blame] | 124 | else |
| 125 | runStudioLinux |
Jeff Gaston | 259a905 | 2018-10-19 14:55:19 -0400 | [diff] [blame] | 126 | fi |
| 127 | } |
| 128 | |
| 129 | function main() { |
| 130 | updateStudio |
| 131 | checkLicenseAgreement |
| 132 | runStudio |
| 133 | } |
| 134 | |
| 135 | main |