2
2
# @name FlatPakBuildScript
3
3
# @brief This script provides functionality to build and run this project as a flatpak package, from either a host, or a container environment.
4
4
# @example
5
- # ./build.sh
5
+ # 1. This command will build, and run the flatpak package as the current user, and display the help message for the bottles-cli command:
6
+ #
7
+ # ./build.sh build -r -u -c bottles-cli -a --help
8
+ #
9
+ # 2. This command will run the flatpak package as the current user, and run bottles in GUI-interactive mode:
10
+ #
11
+ # ./build.sh run -r -u -c bottles
12
+ #
13
+ # 3. This command will display the help message for the build action:
14
+ #
15
+ # ./build.sh build -h
6
16
7
17
set -eo pipefail
8
18
19
+ BUILD=false
20
+ RUN=false
21
+ USERMODE=false
22
+ AARGS=false
23
+ BARGS=()
24
+ COMMAND=" "
25
+
9
26
# @description Colored output for error messages
10
27
# @arg $1 string Message
11
28
# @exitcode 0
@@ -68,6 +85,168 @@ function error_trap() {
68
85
}
69
86
trap ' error_trap ${LINENO} $?' ERR
70
87
88
+ # @description Display usage information
89
+ # @arg $1 string Action
90
+ # @exitcode 0
91
+ # @stdout Usage information
92
+ function usage() {
93
+ local action
94
+ action=" ${1} "
95
+ action=$( echo " ${action} " | tr ' [:lower:]' ' [:upper:]' )
96
+ echo " Bottles Flatpak Build Script"
97
+ case " ${action} " in
98
+ BUILD)
99
+ echo " Usage: $0 ACTION [OPTIONS] [ARGS]"
100
+ echo -e " \nHelp for build action:\n"
101
+ echo " This action builds the flatpak package"
102
+ echo -e " \nOptions:\n"
103
+ echo " -h, -help: Display this help message"
104
+ echo " -u, --user: Build the flatpak package as the current user"
105
+ echo " -r, --run: Run the flatpak package after building"
106
+ echo " -c, --command [COMMAND]: Pass a command to the run action"
107
+ echo " -a, --args [ARGS]: Pass additional arguments to the run action"
108
+ ;;
109
+ RUN)
110
+ echo " Usage: $0 ACTION [OPTIONS] [ARGS]"
111
+ echo -e " \nHelp for run action:\n"
112
+ echo " This action runs the flatpak package"
113
+ echo -e " \nOptions:\n"
114
+ echo " -h, --help: Display this help message"
115
+ echo " -u, --user: Run the flatpak package as the current user"
116
+ echo " -c, --command [COMMAND]: Pass a command to the run action"
117
+ echo " -a, --args [ARGS]: Pass additional arguments to the run action"
118
+ ;;
119
+ * )
120
+ echo " Usage: $0 ACTION [OPTIONS]"
121
+ echo -e " \nActions:\n"
122
+ echo " build: Build the flatpak package"
123
+ echo " run: Run the flatpak package"
124
+ echo -e " \nOptions:\n"
125
+ echo " -h, --help: Display this help message"
126
+ echo -e " \nYou can get help for a specific action by running: $0 ACTION -h|--help\n"
127
+ ;;
128
+ esac
129
+ exit 0
130
+ }
131
+
132
+ # @description Parse command line arguments
133
+ # @arg $@ string Command line arguments
134
+ # @exitcode 0 If the arguments are parsed successfully
135
+ # @stdout: Usage if the command line arguments fail to parse
136
+ function parse_args() {
137
+ if [ $# -eq 0 ]; then
138
+ usage
139
+ fi
140
+ if [ $# -lt 1 ]; then
141
+ error " No action provided"
142
+ usage
143
+ fi
144
+ local action
145
+ action=" ${1} "
146
+ if [ " ${action} " = " -h" ] || [ " ${action} " = " --help" ]; then
147
+ usage
148
+ fi
149
+ action=$( echo " ${action} " | tr ' [:lower:]' ' [:upper:]' )
150
+ shift 1
151
+ case " ${action} " in
152
+ BUILD)
153
+ BUILD=true
154
+ while [ $# -gt 0 ]; do
155
+ if [ " $AARGS " = true ]; then
156
+ BARGS+=(" $1 " )
157
+ else
158
+ case " $1 " in
159
+ -h|--help)
160
+ usage " BUILD"
161
+ ;;
162
+ -r|--run)
163
+ RUN=true
164
+ ;;
165
+ -u|--user)
166
+ USERMODE=true
167
+ ;;
168
+ -c|--command)
169
+ COMMAND=" $2 "
170
+ shift 1
171
+ ;;
172
+ -a|--args)
173
+ AARGS=true
174
+ ;;
175
+ * )
176
+ error " Invalid argument: $1 "
177
+ usage " BUILD"
178
+ ;;
179
+ esac
180
+ fi
181
+ shift 1
182
+ done
183
+ ;;
184
+ RUN)
185
+ RUN=true
186
+ while [ $# -gt 0 ]; do
187
+ if [ " $AARGS " = true ]; then
188
+ BARGS+=(" $1 " )
189
+ else
190
+ case " $1 " in
191
+ -h|--help)
192
+ usage " RUN"
193
+ ;;
194
+ -r|--run)
195
+ RUN=true
196
+ ;;
197
+ -u|--user)
198
+ USERMODE=true
199
+ ;;
200
+ -c|--command)
201
+ COMMAND=" $2 "
202
+ shift 1
203
+ ;;
204
+ -a | --args)
205
+ AARGS=true
206
+ ;;
207
+ * )
208
+ error " Invalid argument: $1 "
209
+ usage " RUN"
210
+ ;;
211
+ esac
212
+ fi
213
+ shift 1
214
+ done
215
+ ;;
216
+ * )
217
+ error " Invalid action: ${action} "
218
+ usage
219
+ ;;
220
+ esac
221
+ }
222
+
223
+ # @description Get the user mode flag
224
+ # @exitcode 0
225
+ # @stdout User mode flag
226
+ function __user_mode() {
227
+ if [ " ${USERMODE} " = true ]; then
228
+ echo -ne " --user "
229
+ fi
230
+ }
231
+
232
+ # @description Get the passed-through command line arguments
233
+ # @exitcode 0
234
+ # @stdout Command line arguments
235
+ function __bargs() {
236
+ if [ ${# BARGS[@]} -gt 0 ]; then
237
+ echo -ne " ${BARGS[*]} "
238
+ fi
239
+ }
240
+
241
+ # @description Get the passed-through command
242
+ # @exitcode 0
243
+ # @stdout Command
244
+ function __command() {
245
+ if [ -n " ${COMMAND} " ]; then
246
+ echo -ne " --command=${COMMAND} "
247
+ fi
248
+ }
249
+
71
250
# @description Check if a command exists
72
251
# @arg $1 string Command name
73
252
# @exitcode 0 If the command exists
@@ -138,11 +317,13 @@ function check_flatpak_repository() {
138
317
err " No repository URI provided"
139
318
return 1
140
319
fi
141
- if ! exec_flatpak --user remote-list | grep -qi " ${repository} " ; then
320
+ # shellcheck disable=SC2046
321
+ if ! exec_flatpak $( __user_mode) remote-list | grep -qi " ${repository} " ; then
142
322
warning " Flatpak repository ${repository} does not exist"
143
323
return 1
144
324
fi
145
- if ! exec_flatpak --user remote-list | grep -qi " ${uri} " ; then
325
+ # shellcheck disable=SC2046
326
+ if ! exec_flatpak $( __user_mode) remote-list | grep -qi " ${uri} " ; then
146
327
warning " Flatpak repository ${repository} URI does not match"
147
328
return 1
148
329
fi
@@ -162,7 +343,8 @@ function check_flatpak_package() {
162
343
err " No package name provided"
163
344
return 1
164
345
fi
165
- if ! exec_flatpak --user list | grep -qi " ${package} " ; then
346
+ # shellcheck disable=SC2046
347
+ if ! exec_flatpak $( __user_mode) list | grep -qi " ${package} " ; then
166
348
warning " Flatpak package ${package} is not installed"
167
349
return 1
168
350
fi
@@ -193,7 +375,8 @@ function install_flatpak_repository() {
193
375
return 0
194
376
fi
195
377
info " Adding flatpak repository: ${repository} "
196
- if ! exec_flatpak --user remote-add --if-not-exists ${repository} ${uri} ; then
378
+ # shellcheck disable=SC2046
379
+ if ! exec_flatpak $( __user_mode) remote-add --if-not-exists ${repository} ${uri} ; then
197
380
err " Failed to add flatpak repository ${repository} "
198
381
return 1
199
382
else
@@ -218,7 +401,8 @@ function install_flatpak_package() {
218
401
return 0
219
402
fi
220
403
info " Installing flatpak package: ${package} "
221
- if ! exec_flatpak install --user flathub " ${package} " -y; then
404
+ # shellcheck disable=SC2046
405
+ if ! exec_flatpak install $( __user_mode) flathub " ${package} " -y; then
222
406
err " Failed to install flatpak package ${package} "
223
407
return 1
224
408
else
@@ -253,7 +437,8 @@ function build_flatpak() {
253
437
install_flatpak_repository " flathub" " https://flathub.org/repo/flathub.flatpakrepo"
254
438
install_flatpak_package " org.flatpak.Builder"
255
439
info " Building flatpak package using configuration: ${package_config} ..."
256
- if ! exec_flatpak run org.flatpak.Builder --install --install-deps-from=flathub --default-branch=master --force-clean --user build-dir ${package_config} ; then
440
+ # shellcheck disable=SC2046
441
+ if ! exec_flatpak run org.flatpak.Builder --install --install-deps-from=flathub --default-branch=master --force-clean $( __user_mode) build-dir ${package_config} ; then
257
442
err " Failed to build flatpak package"
258
443
return 1
259
444
else
@@ -277,14 +462,20 @@ function run_flatpak() {
277
462
if ! check_flatpak_package " ${package_name} " ; then
278
463
return 1
279
464
fi
280
- if ! exec_flatpak run " ${package_name} " ; then
465
+ # shellcheck disable=SC2046
466
+ if ! exec_flatpak run $( __command) $( __user_mode) " ${package_name} " $( __bargs) ; then
281
467
err " Failed to test-run flatpak package"
282
468
return 1
283
469
else
284
- success " Flatpak package test- ran successfully"
470
+ success " Flatpak package ran successfully"
285
471
return 0
286
472
fi
287
473
}
288
474
289
- build_flatpak " com.usebottles.bottles.yml"
290
- run_flatpak " com.usebottles.bottles"
475
+ parse_args " $@ "
476
+ if [ " ${BUILD} " = true ]; then
477
+ build_flatpak " com.usebottles.bottles.yml"
478
+ fi
479
+ if [ " ${RUN} " = true ]; then
480
+ run_flatpak " com.usebottles.bottles"
481
+ fi
0 commit comments