-
Notifications
You must be signed in to change notification settings - Fork 0
/
bundle-script.sh
executable file
·98 lines (86 loc) · 3.37 KB
/
bundle-script.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
#!/usr/bin/env bash
DESCRIPTION="Bundles a script with it's dependencies (meant to be used for scripts from https://github.com/theCalcaholic/bash-utils)"
USAGE="bundle-script.sh [OPTIONS] input output [dependency [dependency [...]]]
input path to the original script
output path to save the bundled script at.
dependency path to a dependency to bundle
Options:
--check, -c If provided, the bundled script will be called with the given arguments to
check if it works (i.e. returns with exit code 0).
--gzip, -z Use additional gzip compression for bundled scripts
--exit, -e The expected exit code if the script is working (requires --check)"
. "$(dirname "$BASH_SOURCE")/lib/parse_args.sh"
REQUIRED=("input" "output")
KEYWORDS=("--check" "-c" "--exit" "-e" "--gzip;bool" "-z;bool")
set_trap 1
parse_args __USAGE "$USAGE" __DESCRIPTION "$DESCRIPTION" "$@"
input_script="${NAMED_ARGS["input"]}"
output_script="${NAMED_ARGS["output"]}"
if [[ "$(realpath $input_script)" == "$(realpath "$output_script")" ]]
then
echo "ERROR: input and output scripts can't be equal!" >&2
exit 1
fi
shopt -s extglob
echo "" > "$output_script"
line_no=1
while IFS= read -r line
do
if [[ "$line" =~ ^" "*("source "|". ").* ]]
then
source_path="${line##source+([[:space:]])}"
source_path="${line##.+([[:space:]])}"
source_path="$(eval "echo ${source_path%%*([[:space:]])}")"
source_path="$(realpath "$source_path")"
[[ "$?" -eq 0 ]] && [[ -f "${source_path}" ]] || {
echo "ERROR: Something wen't wrong while analyzing source path in line ${line_no} (does the sourced file exist?):" >&2
echo "> $line"
}
is_replaced="false"
for dependency in "${ARGS[@]}"
do
if [[ "$(realpath ${dependency})" == "$source_path" ]]
then
is_replaced="true"
echo "replacing dependency '${source_path}'"
{
source_file="$(basename "${source_path}")"
echo "##### ${source_file} #####"
echo -n "source <(echo '"
if [[ -z "${KW_ARGS['--gzip']-"${KW_ARGS['-z']}"}" ]]
then
base64 -w 0 "$source_path"
else
gzip -cq "$source_path" | base64 -w 0
fi
echo -n "' | base64 -d"
[[ -z "${KW_ARGS['--gzip']-"${KW_ARGS['-z']}"}" ]] || echo -n " | gunzip -cq"
echo ")"
echo -n "######"
for n in $(seq ${#source_file}); do echo -n "#"; done
echo "######"
} >> "$output_script"
fi
done
if [[ "$is_replaced" != "true" ]]
then
echo "$line" >> "$output_script"
fi
else
echo "$line" >> "$output_script"
fi
line_no="$(($line_no + 1))"
done < "$input_script"
check_args="${KW_ARGS['--check']-${KW_ARGS['-c']}}"
if [[ -n "$check_args" ]]
then
rc=0
expected="${KW_ARGS['-e']:-0}"
expected="${KW_ARGS['--exit']-$expected}"
bash "$output_script" $check_args > /dev/null 2>&1 || rc=$?
if [[ "$rc" -ne "$expected" ]]
then
echo "ERROR: The bundled script doesn't seem to work ('bash \"$output_script\" $check_args' terminated with exit code $rc not $expected)!" >&2
exit 2
fi
fi