This repository has been archived by the owner on Feb 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
studiow
executable file
·235 lines (199 loc) · 7.13 KB
/
studiow
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#!/bin/bash
set -e
set -m
# This is a wrapper script that runs the specific version of Android Studio that is recommended for developing in this repository.
# (This serves a similar purpose to gradlew)
#
# NOTE: if you are changing this file, consider applying the changes to ../studiow, used for the main AndroidX project.
function getPrebuiltStudioZipPath() {
version="$(grep "studio_version=" ${propertiesFile} | sed 's/[^=]*=//')"
ideaMajorVersion="$(grep "idea_major_version=" ${propertiesFile} | sed 's/[^=]*=//')"
buildNumber="$(grep "studio_build_number=" ${propertiesFile} | sed 's/[^=]*=//')"
osName="$1"
if [ "${osName}" == "linux" ]; then
extension="tar.gz"
else
extension="zip"
fi
echo "${scriptDir}/androidx_prebuilts/androidx/studio/android-studio-ide-${ideaMajorVersion}.${buildNumber}-${osName}.${extension}"
}
acceptsLicenseAgreement="$1"
scriptDir="$(cd $(dirname $0) && pwd)"
projectDir=$scriptDir
tempDir="${scriptDir}/studio"
function getOsName() {
unameOutput="$(uname)"
osName=""
if [ "${unameOutput}" == "Linux" ]; then
osName="linux"
else
osName="mac"
fi
echo "${osName}"
}
osName="$(getOsName)"
propertiesFile="${scriptDir}/studio_versions.properties"
prebuiltStudioZipPath="$(getPrebuiltStudioZipPath $osName)"
studioDestName="$(basename ${prebuiltStudioZipPath})"
studioUnzippedPath="${tempDir}/${studioDestName}"
studioUnzippedPath="$(echo ${studioUnzippedPath} | sed 's/\.zip$//' | sed 's/\.tar.gz$//')"
# The version of AGP we should override with - may be empty if we should just use the default
# version specified in AndroidX. See ui/studio_versions.properties.
agp_override="$(grep "agp_override=" ${propertiesFile} | sed 's/[^=]*=//')"
# Set the env variable if we have a valid version to override with, else noop
GRADLE_PLUGIN_VERSION_OVERRIDE=""
[ ! -z "$agp_override" ] && GRADLE_PLUGIN_VERSION_OVERRIDE="GRADLE_PLUGIN_VERSION=${agp_override}"
function error_exit {
echo "$1" >&2 ## Send message to stderr.
exit 1
}
function downloadFile() {
fromUrl="$1"
destPath="$2"
tempPath="${destPath}.tmp"
if [ -f "${destPath}" ]; then
read -r -n 1 -p "File already exists. Do you want to delete and re-download? [Y/n]? " reply
if [ ! -z "${reply}" ]; then
# Fix missing newline
echo
fi
case "${reply}" in
[yY]|"")
rm "${destPath}"
;;
*)
esac
fi
if [ -f "${destPath}" ]; then
echo "Using existing file from ${destPath}"
else
echo "Downloading ${fromUrl} to ${destPath}"
curl "${fromUrl}" > "${tempPath}"
mv "${tempPath}" "${destPath}"
fi
}
function findStudioMacAppPath() {
echo "$(find "${studioUnzippedPath}" -type d -depth 1 -name "Android Studio*.app")"
}
function getLicensePath() {
if [ "${osName}" == "mac" ]; then
appPath="$(findStudioMacAppPath)"
echo "${appPath}/Contents/Resources/LICENSE.txt"
else
echo "${studioUnzippedPath}/android-studio/LICENSE.txt"
fi
}
function checkLicenseAgreement() {
# TODO: Is there a more official way to check that the user accepts the license?
licenseAcceptedPath="${studioUnzippedPath}/STUDIOW_LICENSE_ACCEPTED"
if [ ! -f "${licenseAcceptedPath}" ]; then
if [ "${acceptsLicenseAgreement}" == "-y" ]; then
touch "${licenseAcceptedPath}"
else
read -r -n 1 -p "Do you accept the license agreement at $(getLicensePath) [Y/n]? " reply
if [ ! -z "${reply}" ]; then
# Fix missing newline
echo
fi
case "${reply}" in
[yY]|"")
touch "${licenseAcceptedPath}"
;;
*)
exit 1
;;
esac
fi
fi
}
# Temporary fix. Remove this after fixing b/135183535
function updateJvmHeapSize() {
if [ "${osName}" == "mac" ]; then
sed -i '' 's/-Xmx.*/-Xmx8g/' "$(findStudioMacAppPath)/Contents/bin/studio.vmoptions"
else
sed -i 's/-Xmx.*/-Xmx8g/' "${studioUnzippedPath}/android-studio/bin/studio64.vmoptions"
sed -i 's/-Xmx.*/-Xmx4g/' "${studioUnzippedPath}/android-studio/bin/studio.vmoptions"
fi
}
function updateStudio() {
# skip if already up-to-date
if stat "${studioUnzippedPath}" >/dev/null 2>/dev/null; then
# already up-to-date
return
fi
mkdir -p "${tempDir}"
echo "Removing previous installations"
ls "${tempDir}" | grep -v "^${studioDestName}\$" | sed "s|^|${tempDir}/|" | xargs rm -rf
echo "Unzipping"
if [ "${osName}" == "linux" ]; then
mkdir $studioUnzippedPath
tar -xzf "${prebuiltStudioZipPath}" --directory "${studioUnzippedPath}"
else
unzip "${prebuiltStudioZipPath}" -d "${studioUnzippedPath}"
fi
}
# Copies the built compose-ide-plugin to the plugin directory of the studio installation. This
# ensures that the compose plugin is used at startup
function updateComposeIdePlugin() {
if [ "${osName}" == "mac" ]; then
composeIdePluginDirectory="$(findStudioMacAppPath)/Contents/plugins/compose-ide-plugin/"
else
composeIdePluginDirectory="${studioUnzippedPath}/android-studio/plugins/"
fi
# this is the idea sandbox directory that the compose ide plugin is located in
composePluginDirectory="${projectDir}/androidx_prebuilts/out/ui/compose/compose-ide-plugin/build/idea-sandbox/plugins/compose-ide-plugin/"
echo ""
echo "Copying compose-ide-plugin to $composeIdePluginDirectory"
echo ""
mkdir -p "${composeIdePluginDirectory}"
cp -f -R "${composePluginDirectory}" "${composeIdePluginDirectory}"
}
function ensureCompose() {
# This gradle command will prepare a sandbox directory that we can point studio to that will
# have all of the correct structure to load the compose plugin
${scriptDir}/gradlew -p ${projectDir} :compose:compose-ide-plugin:prepareSandbox
}
# ANDROID_LINT_NULLNESS_IGNORE_DEPRECATED environment variable prevents Studio from showing IDE
# inspection warnings for nullability issues, if the context is deprecated
# This environment variable is consumed by InteroperabilityDetector.kt
function runStudioLinux() {
studioPath="${studioUnzippedPath}/android-studio/bin/studio.sh"
updateComposeIdePlugin
echo ""
echo "Launching Studio"
# Override AGP version overridden because compose studio is behind androidx studio
env STUDIO_PROPERTIES="${projectDir}/idea.properties" \
STUDIO_VM_OPTIONS="${projectDir}/../development/studio/studio.vmoptions" \
ANDROID_LINT_NULLNESS_IGNORE_DEPRECATED="true" \
KOTLIN_OVERRIDE="1.3.50-compose-20190806" \
$GRADLE_PLUGIN_VERSION_OVERRIDE \
"${studioPath}" "${projectDir}" &
}
function runStudioMac() {
appPath="$(findStudioMacAppPath)"
updateComposeIdePlugin
echo ""
echo "Launching Studio"
# Override AGP version overridden because compose studio is behind androidx studio
env STUDIO_PROPERTIES="${projectDir}/idea.properties" \
STUDIO_VM_OPTIONS="${projectDir}/../development/studio/studio.vmoptions" \
ANDROID_LINT_NULLNESS_IGNORE_DEPRECATED="true" \
KOTLIN_OVERRIDE="1.3.50-compose-20190806" \
$GRADLE_PLUGIN_VERSION_OVERRIDE \
open -a "${appPath}" "${projectDir}"
}
function runStudio() {
updateJvmHeapSize
if [ "${osName}" == "mac" ]; then
runStudioMac
else
runStudioLinux
fi
}
function main() {
updateStudio
ensureCompose
checkLicenseAgreement
runStudio
}
main