-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuilder.sh
executable file
·264 lines (200 loc) · 7.43 KB
/
builder.sh
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#!/usr/bin/env bash
# Usage:
# `install arm64 python3.10` (arm64 native on macOS)
# `install x86_64 python3.10` (x86_64 native on macOS/Linux)
# `install x86_64 python3.10-intel64` (building x86_64 on arm64 on macOS)
#
# `buildSpec arm64`
# Modify spec file as needed
# `build arm64`
install() {(set -e
# Prepare virtualenv for given architecture
arch=$1 # architecture name, `arm64` | `x86_64`
pythonExec=$2 # python executable name, e.g. `python3.10` or `python3.10-intel64`
_validateArchArg "$arch"
if [ -z "$pythonExec" ]; then
_logError 'Python executable must be provided as argument (e.g. "python3.10" or "python3.10-intel64")'
exit 1
fi
_validateExecutableArchitecture "$arch" "$pythonExec"
os=$(_getOsName)
venvPath=".venv-$os-$arch"
# ${var:?} needed to safeguard against deleting /* if variable is empty
rm -rf "${venvPath:?}/*"
_log "Installing virtualenv to \"$venvPath\""
# --clear - Delete the contents of the environment directory if it already exists, before environment creation
# --copies - Try to use copies rather than symlinks, even when symlinks are the default for the platform.
$pythonExec -m venv "$venvPath" --clear --copies
source "$venvPath/bin/activate"
pip install wheel
pip install -r "requirements_$os.txt"
if [[ "$os" == 'linux' ]]; then
_installLinux "$venvPath"
fi
_log "Successfully installed virtualenv to \"$venvPath\""
)}
_installLinux() {(set -e
# $1 - virtualenv path
venvPath=$1
_log "Installing clipnotify"
clipnotifyPath="binaries/clipnotify"
rm -rf "$clipnotifyPath"
git clone https://github.com/cdown/clipnotify.git "$clipnotifyPath"
make -C "$clipnotifyPath"
_log "Successfully installed clipnotify"
# TODO solve this somehow better. Installing PyGObject directly into virtualenv seems not possible
cp -r /usr/lib/python3/dist-packages/gi "$venvPath/lib/python3.10/site-packages/gi"
_log "Successfully copied gi"
)}
buildSpec() {(set -e
arch=$1 # architecture name, `arm64` | `x86_64`
os=$(_getOsName)
_validateArchArg "$arch"
_log "Generating spec for $os-$arch"
venvPath=".venv-$os-$arch"
specFilePath="build/spec-$os-$arch.spec"
rm -f "$specFilePath"
source "$venvPath/bin/activate"
if [ "$os" == 'macos' ]; then
"$venvPath/bin/pyi-makespec" \
--name "Statusbar Converter" \
--onedir \
--windowed \
--add-data '../assets:assets' \
--add-data '../config:config' \
--add-data '../version:.' \
--icon '../assets/icon_macos.png' \
--target-arch "$arch" \
--osx-bundle-identifier 'com.mindaugasw.statusbar_converter' \
--specpath 'build' \
start.py
else
"$venvPath/bin/pyi-makespec" \
--name "Statusbar Converter" \
--onefile \
--add-data '../assets:assets' \
--add-data '../config:config' \
--add-data '../version:.' \
--add-binary '../binaries/clipnotify/clipnotify:binaries/clipnotify' \
--icon '../assets/icon_linux.png' \
--specpath 'build' \
start.py
fi
mv "build/Statusbar Converter.spec" "$specFilePath"
_log "Successfully generated spec at $specFilePath"
_log "Build the executable by running \`./builder.sh build $arch\`"
)}
build() {(set -e
arch=$1 # architecture name, `arm64` | `x86_64`
os=$(_getOsName)
_validateArchArg "$arch"
venvPath=".venv-$os-$arch"
distPath="build/dist-$os-$arch"
source "$venvPath/bin/activate"
rm -rf "./$distPath"
_log "Starting build for $os-$arch"
"$venvPath/bin/pyinstaller" \
--clean \
--distpath "$distPath" \
--workpath "$distPath/build" \
"build/spec-$os-$arch.spec"
_log "Successfully built in $distPath"
_createZip "$arch" "$os"
if [ "$os" == 'macos' ]; then
_createDmg "$arch"
fi
)}
_createZip() {(set -e
# Compress app to .zip archive.
# Previously there was also a script for packing into .dmg image for macOS (see git history).
# But .dmg triggers additional security measures: because of missing app signature,
# Chrome warns about unsafe app when downloading. So .zip is more convenient.
arch=$1 # architecture name, `arm64` | `x86_64`
os=$2 # OS name, `linux` | `macos`
version=$(cat 'version' | xargs)
cd "build/dist-$os-$arch"
if [ "$os" == 'macos' ]; then
compressContent='Statusbar Converter.app'
if [ "$arch" == 'arm64' ]; then
fileName=$(printf 'Statusbar_Converter_v%s_macOS_AppleSilicon_%s.app.zip' "$version" "$arch")
else
fileName=$(printf 'Statusbar_Converter_v%s_macOS_intel_%s.app.zip' "$version" "$arch")
fi
else
compressContent='Statusbar Converter'
fileName=$(printf 'Statusbar_Converter_v%s_linux_%s.zip' "$version" "$arch")
fi
_log "Compressing into zip for $os-$arch"
zip -qr "$fileName" "$compressContent"
_log "Successfully compressed into \"$fileName\""
)}
_createDmg() {(set -e
# `create-dmg` in path is required
# `brew install create-dmg`
arch=$1 # architecture name, `arm64` | `x86_64`
version=$(cat 'version' | xargs)
_log "Packing into dmg image for $arch"
cd "build/dist-macos-$arch"
if [ "$arch" == 'arm64' ]; then
fileName=$(printf 'Statusbar_Converter_v%s_macOS_AppleSilicon_%s.dmg' "$version" "$arch")
else
fileName=$(printf 'Statusbar_Converter_v%s_macOS_intel_%s.dmg' "$version" "$arch")
fi
# create-dmg includes all files in the directory. So we copy only the needed stuff to a new directory
rm -rf dmg/ ./*.dmg
mkdir dmg
cp -r 'Statusbar Converter.app' 'dmg/Statusbar Converter.app'
create-dmg \
--volname 'Statusbar Converter' \
--icon-size 80 \
--text-size 14 \
--icon 'Statusbar Converter.app' 190 0 \
--app-drop-link 0 0 \
--hide-extension 'Statusbar Converter.app' \
$fileName \
dmg/
rm -rf dmg/
_log "Successfully packed into dmg image \"$fileName\""
)}
_validateExecutableArchitecture() {(set -e
arch=$1 # architecture name, `arm64` | `x86_64`
pythonExec=$2 # python executable name, e.g. `python3.10` or `python3.10-intel64`
platform=$($pythonExec -c 'import platform; print(platform.platform())')
if [[ "$platform" != *"$arch"* ]]; then
_logError "Requested architecture ($arch) does not match provided python executable ($platform)"
exit 1
else
_log "Requested architecture ($arch) matches provided python executable ($platform)"
fi
)}
_validateArchArg() {(set -e
# $1 - architecture name
if [ "$1" != 'arm64' ] && [ "$1" != 'x86_64' ]; then
_logError 'Argument must be either "arm64" or "x86_64"'
exit 1
fi
)}
_getOsName() {(set -e
name=$(uname)
if [ "$name" == 'Linux' ]; then
echo 'linux'
elif [ "$name" == 'Darwin' ]; then
echo 'macos'
else
_logError "Unsupported OS type: $name"
exit 1
fi
)}
_log() {(set -e
yellowCode='\033[0;33m'
resetCode='\033[0m'
echo -e "$yellowCode> $1$resetCode"
)}
_logError() {(set -e
redCode='\033[0;31m'
resetCode='\033[0m'
echo -e "$redCode> ERROR: $1$resetCode"
)}
# This will call function name from the first argument
# See: https://stackoverflow.com/a/16159057/4110469
"$@"