Repository navigation
Expand file tree
/
Copy pathproject.sh
More file actions
executable file
·218 lines (176 loc) · 8.08 KB
/
Copy pathproject.sh
File metadata and controls
executable file
·218 lines (176 loc) · 8.08 KB
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
#!/bin/bash
set -euo pipefail
if [[ "$OSTYPE" != "linux-gnu"* ]]; then
echo "This script works only on GNU/Linux systems" >&2
exit 1
fi
## Variables
#PROJECT_NAME=`echo ${PWD##*/}` ## PROJECT_NAME = parent directory
PROJECT_UID=$(id -u)
PROJECT_GID=$(id -g)
PROJECT_GROUPS=$({ id -G; stat -c %g /dev/dri/* /dev/kvm /dev/snd/* || true; } 2>/dev/null | tr ' ' '\n' | sort -un \
| sed "/^0\?$/d; /^$PROJECT_GID$/d" | sed 's/.*/\\n\\1 - "&"/' | tr -d '\n') ## your groups and those of the GPU, KVM and sound devices, as group_add lines for composehack
AMD_GPU=$(grep -l 0x1002 /sys/class/drm/renderD*/device/vendor 2>/dev/null | head -n 1 || true)
AMD_PRIME=${AMD_GPU:+pci-$(basename "$(readlink -f "${AMD_GPU%/vendor}")" | tr :. __)}
DRI_DEVICE=$(ls -d /dev/dri 2>/dev/null || true)
SND_DEVICE=$(ls -d /dev/snd 2>/dev/null || true)
MACHINE_ID=$(find /etc/machine-id /var/lib/dbus/machine-id -maxdepth 0 2>/dev/null | head -n 1 || true)
XAUTH=$(ls "${XAUTHORITY:-$HOME/.Xauthority}" 2>/dev/null || true) ## mounted only if it exists, as Docker would create a directory in its place
# shellcheck disable=SC2016 ## left to docker compose, which reads XAUTHORITY and HOME whenever it starts the container
XAUTH_PATH='${XAUTHORITY:-$HOME/.Xauthority}'
NVIDIA_GPU=$(ls /dev/nvidiactl 2>/dev/null || true)
NVIDIA_TOOLKIT=$(command -v nvidia-container-runtime-hook || true)
NVIDIA=${NVIDIA_GPU:+${NVIDIA_TOOLKIT:+yes}}
USER=android
CMDLINE_TOOLS_VERSION=13114758
GRADLE_VERSION=9.0.0 ## keep in sync with GRADLE_HOME and PATH in Dockerfile
KOTLIN_VERSION=2.2.21
## sdkmanager only installs the newest of these, so they are downloaded instead
EMULATOR_ARCHIVE=emulator-linux_x64-16970787.zip ## 37.3.10
PLATFORM_TOOLS_ARCHIVE=platform-tools_r37.0.3-linux.zip
SYSTEM_IMAGE_ARCHIVE=x86_64-36_r07.zip
SYSTEM_IMAGE="system-images;android-36;google_apis;x86_64"
## Functions
generated() {
find . -mindepth 1 -maxdepth 1 \
| sed "
/Dockerfile/d;
/project.sh/d;
/README.md/d;
"
}
confirm() {
if [[ ! project.sh -ef $0 ]]; then
echo "clean removes the files of the current directory, so run it in the one of project.sh." >&2
return 1
fi
[[ $1 == --force ]] && return
echo "clean removes the containers, images and volumes (with the databases) of this environment,"
echo "and these files in $PWD:"
generated | sort
if [[ ! -t 0 ]]; then
echo "Nothing removed: run ./project.sh clean --force to remove them without being asked." >&2
return 1
fi
read -r -p "Type yes to remove them: "
[[ $REPLY == yes ]] || { echo "Nothing removed." >&2; return 1; }
}
clean() {
confirm "${1:-}" || exit 1
if [[ -f docker-compose.yml ]]; then
docker compose down -v --rmi all --remove-orphans
fi
generated | xargs -I {} rm -rf {}
}
compose() {
if [[ ! -f docker-compose.yml ]]; then
if [[ -n $NVIDIA_GPU && -z $NVIDIA_TOOLKIT ]]; then
echo "NVIDIA GPU found, but NVIDIA Container Toolkit is not installed. The container will not use the NVIDIA GPU." >&2
fi
mkdir -p .cache
cat << EOF > docker-compose.yml
services:
android:
build: .
working_dir: /home/$USER
volumes:
- .:/home/$USER
- /tmp/.X11-unix:/tmp/.X11-unix
- /run/user/$PROJECT_UID:/run/user/$PROJECT_UID
${MACHINE_ID:+ - $MACHINE_ID:/etc/machine-id:ro
- $MACHINE_ID:/var/lib/dbus/machine-id:ro
}${XAUTH:+ - $XAUTH_PATH:$XAUTH_PATH:ro
} environment:
DISPLAY: \${DISPLAY:-:0}
XDG_RUNTIME_DIR: /run/user/$PROJECT_UID
${XAUTH:+ XAUTHORITY: $XAUTH_PATH
}${AMD_PRIME:+ DRI_PRIME: $AMD_PRIME
}${NVIDIA:+ NVIDIA_DRIVER_CAPABILITIES: all
__NV_PRIME_RENDER_OFFLOAD: 1
__GLX_VENDOR_LIBRARY_NAME: nvidia
__VK_LAYER_NV_optimus: NVIDIA_only
} devices:
${DRI_DEVICE:+ - /dev/dri:/dev/dri
} - /dev/kvm:/dev/kvm
${SND_DEVICE:+ - /dev/snd:/dev/snd
}${NVIDIA:+ deploy:
resources:
reservations:
devices:
- driver: nvidia
count: all
capabilities: [gpu]
} network_mode: host
EOF
fi
}
composehack() {
if ! grep -q "user\:" "docker-compose.yml"; then
echo "Adding user configuration line to docker-compose.yml for GNU/Linux users."
sed -i "/working_dir\:/{s@^\( \+\)@\1user\: $PROJECT_UID\:$PROJECT_GID${PROJECT_GROUPS:+\n\1group_add\:$PROJECT_GROUPS}\n\1@}" docker-compose.yml
fi
}
sdk() {
if [[ ! -d sdk/cmdline-tools/latest ]]; then
mkdir -p sdk/{cmdline-tools,emulator}
## avdmanager only knows packages with this file, which sdkmanager would write
cat << EOF > sdk/emulator/package.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<sdk:repository xmlns:sdk="http://schemas.android.com/repository/android/common/02" xmlns:generic="http://schemas.android.com/repository/android/generic/02" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><localPackage path="emulator"><type-details xsi:type="generic:genericDetailsType"/><revision><major>37</major><minor>3</minor><micro>10</micro></revision><display-name>Android Emulator</display-name></localPackage></sdk:repository>
EOF
docker compose run --rm android sh -c " \
wget https://dl.google.com/android/repository/commandlinetools-linux-${CMDLINE_TOOLS_VERSION}_latest.zip \
&& unzip commandlinetools-linux-${CMDLINE_TOOLS_VERSION}_latest.zip 'cmdline-tools/*' -d sdk/cmdline-tools \
&& mv sdk/cmdline-tools/cmdline-tools sdk/cmdline-tools/latest \
&& rm commandlinetools-linux-${CMDLINE_TOOLS_VERSION}_latest.zip \
&& wget https://services.gradle.org/distributions/gradle-${GRADLE_VERSION}-bin.zip \
&& unzip gradle-${GRADLE_VERSION}-bin.zip -d sdk \
&& rm gradle-${GRADLE_VERSION}-bin.zip \
&& wget https://github.com/JetBrains/kotlin/releases/download/v${KOTLIN_VERSION}/kotlin-compiler-${KOTLIN_VERSION}.zip \
&& unzip kotlin-compiler-${KOTLIN_VERSION}.zip -d sdk \
&& rm kotlin-compiler-${KOTLIN_VERSION}.zip \
&& wget https://dl.google.com/android/repository/${EMULATOR_ARCHIVE} \
&& unzip ${EMULATOR_ARCHIVE} -d sdk \
&& rm ${EMULATOR_ARCHIVE} \
&& wget https://dl.google.com/android/repository/${PLATFORM_TOOLS_ARCHIVE} \
&& unzip ${PLATFORM_TOOLS_ARCHIVE} -d sdk \
&& rm ${PLATFORM_TOOLS_ARCHIVE} \
&& wget https://dl.google.com/android/repository/sys-img/google_apis/${SYSTEM_IMAGE_ARCHIVE} \
&& mkdir -p sdk/system-images/android-36/google_apis \
&& unzip ${SYSTEM_IMAGE_ARCHIVE} -d sdk/system-images/android-36/google_apis \
&& rm ${SYSTEM_IMAGE_ARCHIVE} \
&& yes | sdkmanager --licenses \
&& sdkmanager \
'build-tools;36.0.0' \
'cmake;3.22.1' \
'ndk;27.1.12297006' \
'platforms;android-36' \
&& echo 'no' | avdmanager create avd -n 1 -k '$SYSTEM_IMAGE'"
fi
}
reactnative() {
if ! command -v mate-terminal > /dev/null; then
echo "Install mate-terminal to be able to start this development environment!" >&2
exit 1
fi
if [[ ! -f app/package.json ]]; then
mate-terminal -- sh -c "docker compose run --rm android sh -c 'yes | npx @react-native-community/cli@21.0.0 init app --version 0.88.1'"
sleep 60
fi
mate-terminal -- sh -c "docker compose run --rm android sh -c 'emulator -avd 1'"
sleep 60
mate-terminal -- sh -c "docker compose run --rm android sh -c 'cd app && node_modules/.bin/react-native start'"
sleep 60
mate-terminal -- sh -c "docker compose run --rm android sh -c 'cd app && npx react-native run-android'"
}
start() {
compose
composehack
sdk
reactnative
}
if ! declare -F "${1:-}" > /dev/null; then
echo "Usage: $0 {start|clean}" >&2
exit 1
fi
"$1" "${@:2}"