blob: 2ba8f5927df5c197920cf2e59846ff95624d1a6e [file] [log] [blame]
Jeff Gaston259a9052018-10-19 14:55:19 -04001#!/bin/bash
2set -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
8function getStudioUrl() {
Oussama Ben Abdelbakid0265c12019-02-08 16:06:33 -05009 version="3.4.0.12"
Louis Pullen-Freilichc89eff22019-02-01 17:30:17 +000010 ideaMajorVersion="183"
Oussama Ben Abdelbakid0265c12019-02-08 16:06:33 -050011 buildNumber="5256591"
Jeff Gaston259a9052018-10-19 14:55:19 -040012 osName="$1"
Alan Viverette6ed162a2018-11-30 14:35:55 -050013 studioUrl="https://dl.google.com/dl/android/studio/ide-zips/${version}/android-studio-ide-${ideaMajorVersion}.${buildNumber}-${osName}.zip"
Jeff Gaston259a9052018-10-19 14:55:19 -040014 echo "${studioUrl}"
15}
16
17acceptsLicenseAgreement="$1"
18scriptDir="$(cd $(dirname $0) && pwd)"
Alan Viverette2fa738f2018-11-28 16:38:53 -050019projectDir=$scriptDir
Jeff Gaston259a9052018-10-19 14:55:19 -040020tempDir="${scriptDir}/studio"
21function 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}
31osName="$(getOsName)"
32studioUrl="$(getStudioUrl $osName)"
33studioDestName="$(basename ${studioUrl})"
34studioZipPath="${tempDir}/${studioDestName}"
35studioUnzippedPath="$(echo ${studioZipPath} | sed 's/\.zip$//')"
36
37function 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 Rose37f957b2019-02-12 16:07:39 -050046function findStudioMacAppPath() {
47 echo "$(find "${studioUnzippedPath}" -type d -depth 1 -name "Android Studio*.app")"
48}
49
50function 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 Gaston259a9052018-10-19 14:55:19 -040059function checkLicenseAgreement() {
60 # TODO: Is there a more official way to check that the user accepts the license?
Ashley Rose09aa4d02019-02-12 16:40:54 -050061
62 licenseAcceptedPath="${studioUnzippedPath}/STUDIOW_LICENSE_ACCEPTED"
63
64 if [ ! -f "${licenseAcceptedPath}" ]; then
65 if [ "${acceptsLicenseAgreement}" == "-y" ]; then
66 touch "${licenseAcceptedPath}"
67 else
Ashley Rose7d9c4102019-02-19 11:52:14 -050068 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 Rose09aa4d02019-02-12 16:40:54 -050083 fi
Jeff Gaston259a9052018-10-19 14:55:19 -040084 fi
85}
86
87function 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
105function runStudioLinux() {
106 studioPath="${studioUnzippedPath}/android-studio/bin/studio.sh"
107 echo "$studioPath &"
Alan Viverette6ed162a2018-11-30 14:35:55 -0500108 env STUDIO_PROPERTIES="${projectDir}/development/studio/idea.properties" \
109 STUDIO_VM_OPTIONS="${projectDir}/development/studio/studio.vmoptions" \
110 "${studioPath}" "${projectDir}" &
Jeff Gaston259a9052018-10-19 14:55:19 -0400111}
112
113function runStudioMac() {
Ashley Rose37f957b2019-02-12 16:07:39 -0500114 appPath="$(findStudioMacAppPath)"
115 echo "open ${appPath}"
Alan Viverette6ed162a2018-11-30 14:35:55 -0500116 env STUDIO_PROPERTIES="${projectDir}/development/studio/idea.properties" \
117 STUDIO_VM_OPTIONS="${projectDir}/development/studio/studio.vmoptions" \
Ashley Rose37f957b2019-02-12 16:07:39 -0500118 open -a "${appPath}" "${projectDir}"
Jeff Gaston259a9052018-10-19 14:55:19 -0400119}
120
121function runStudio() {
Chris Craikb84f61b2018-10-31 14:18:06 -0700122 if [ "${osName}" == "mac" ]; then
Jeff Gaston259a9052018-10-19 14:55:19 -0400123 runStudioMac
Chris Craikb84f61b2018-10-31 14:18:06 -0700124 else
125 runStudioLinux
Jeff Gaston259a9052018-10-19 14:55:19 -0400126 fi
127}
128
129function main() {
130 updateStudio
131 checkLicenseAgreement
132 runStudio
133}
134
135main