Skip to content

Commit 658dde6

Browse files
committed
Add command-line arguments
This improvement adds command line arguments and help pages to the build script, extending its functionality. Arguments can now be passed through to the flatpak, to gain access to bottles-cli at runtime. Additionally, the script now has help pages: The help page can be accessed by running `./build.sh --help` or `./build.sh -h`. From there help on actions and options can be viewed by running `./build.sh <action> --help. NOTE: This script is almost generic enough to be used for any flatpak builder project, but it is still tailored to bottles as the package name is hardcoded in the script. It does not fully support all arguments of flatpak itself (yet), but it is a good start.
1 parent b9f9b15 commit 658dde6

File tree

1 file changed

+202
-11
lines changed

1 file changed

+202
-11
lines changed

build.sh

+202-11
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,27 @@
22
# @name FlatPakBuildScript
33
# @brief This script provides functionality to build and run this project as a flatpak package, from either a host, or a container environment.
44
# @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
616

717
set -eo pipefail
818

19+
BUILD=false
20+
RUN=false
21+
USERMODE=false
22+
AARGS=false
23+
BARGS=()
24+
COMMAND=""
25+
926
# @description Colored output for error messages
1027
# @arg $1 string Message
1128
# @exitcode 0
@@ -68,6 +85,168 @@ function error_trap() {
6885
}
6986
trap 'error_trap ${LINENO} $?' ERR
7087

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+
71250
# @description Check if a command exists
72251
# @arg $1 string Command name
73252
# @exitcode 0 If the command exists
@@ -138,11 +317,13 @@ function check_flatpak_repository() {
138317
err "No repository URI provided"
139318
return 1
140319
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
142322
warning "Flatpak repository ${repository} does not exist"
143323
return 1
144324
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
146327
warning "Flatpak repository ${repository} URI does not match"
147328
return 1
148329
fi
@@ -162,7 +343,8 @@ function check_flatpak_package() {
162343
err "No package name provided"
163344
return 1
164345
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
166348
warning "Flatpak package ${package} is not installed"
167349
return 1
168350
fi
@@ -193,7 +375,8 @@ function install_flatpak_repository() {
193375
return 0
194376
fi
195377
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
197380
err "Failed to add flatpak repository ${repository}"
198381
return 1
199382
else
@@ -218,7 +401,8 @@ function install_flatpak_package() {
218401
return 0
219402
fi
220403
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
222406
err "Failed to install flatpak package ${package}"
223407
return 1
224408
else
@@ -253,7 +437,8 @@ function build_flatpak() {
253437
install_flatpak_repository "flathub" "https://flathub.org/repo/flathub.flatpakrepo"
254438
install_flatpak_package "org.flatpak.Builder"
255439
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
257442
err "Failed to build flatpak package"
258443
return 1
259444
else
@@ -277,14 +462,20 @@ function run_flatpak() {
277462
if ! check_flatpak_package "${package_name}"; then
278463
return 1
279464
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
281467
err "Failed to test-run flatpak package"
282468
return 1
283469
else
284-
success "Flatpak package test-ran successfully"
470+
success "Flatpak package ran successfully"
285471
return 0
286472
fi
287473
}
288474

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

Comments
 (0)