-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathclap.sh
More file actions
217 lines (188 loc) · 6.04 KB
/
clap.sh
File metadata and controls
217 lines (188 loc) · 6.04 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
function clap() {
# THE DEFAULTS INITIALIZATION - POSITIONALS
# _positionals=();
# THE DEFAULTS INITIALIZATION - OPTIONALS
_arg_debug="off";
_arg_release="off";
_arg_run="off";
parse_commandline()
{
_positionals_count=0;
while test $# -gt 0; do {
_key="$1"
case "$_key" in
--debug)
_arg_debug="on";
;;
--release)
_arg_release="on";
;;
--run)
_arg_run="on";
;;
--help)
print_help && exit 0;
;;
--) # Do not parse anymore if _run_target_args are found.
return 0;
;;
*)
_last_positional="$1";
_positionals+=("$_last_positional");
_positionals_count=$((_positionals_count + 1));
;;
esac
shift
} done
}
# handle_passed_args_count()
# {
# local _required_args_string="'path'";
# test "${_positionals_count}" -ge 1 || log::error "Not enough positional arguments - we require exactly 1 (namely: $_required_args_string), but got only ${_positionals_count}." 1 || exit;
# test "${_positionals_count}" -le 1 || log::error "There were spurious positional arguments --- we expect exactly 1 (namely: $_required_args_string), but got ${_positionals_count} (the last one was: '${_last_positional}')." 1 || exit;
# }
# assign_positional_args()
# {
# local _positional_name _shift_for=$1;
# _positional_names="_arg_path ";
# shift "$_shift_for";
# for _positional_name in ${_positional_names}; do {
# test $# -gt 0 || break;
# eval "$_positional_name=\${1}" || log::error "Error during argument parsing, possibly an Argbash bug." 1 || exit;
# shift;
# } done
# }
parse_runargs()
{
for _arg in "${@}"; do {
if test "$_arg" != '--'; then {
shift;
} else {
shift; # Escapes the `--` itself.
_run_target_args=("$@");
break;
} fi
} done
}
parse_commandline "$@";
# Parse _run_target_args
parse_runargs "$@";
# handle_passed_args_count
# assign_positional_args 1 "${_positionals[@]}";
function gettop() {
# Taken from AOSP build/envsetup.sh with slight modifications
local TOPFILE="$_bashbox_meta_name";
local TOPDIR="$_src_dir_name";
local TOP=;
local T;
if [ -n "$TOP" ] && [ -f "$TOP/$TOPFILE" ] && [ -d "$TOPFILE" ]; then {
# The following circumlocution ensures we remove symlinks from TOP.
cd "$TOP";
echo "$PWD";
} elif [ -f "$TOPFILE" ] && [ -d "$TOPDIR" ]; then {
# The following circumlocution (repeated below as well) ensures
# that we record the true directory name and not one that is
# faked up with symlink names.
echo "$PWD";
cd "$PWD";
} else {
local HERE="$PWD";
while [ \( ! \( -f "$TOPFILE" -a "$TOPDIR" \) \) -a \( "$PWD" != "/" \) ]; do {
cd ..;
T="$(cd -- "$PWD" && pwd)";
} done
cd "$HERE";
if [ -f "$T/$TOPFILE" ] && [ -d "$T/$TOPDIR" ]; then {
echo "$T";
} fi
} fi
}
: "${_arg_path:="$PWD"}";
_arg_path="$(cd -- "$_arg_path" && pwd)"; # Pull full path
if test ! -d "$_arg_path/$_src_dir_name" || test ! -e "$_arg_path/$_bashbox_meta_name"; then {
_top="$(gettop)";
if test -n "$_top"; then {
_arg_path="$_top";
unset _top;
} else {
log::error "$_arg_path is not a valid bashbox project" 1 || exit;
} fi
} fi
_src_dir="$_arg_path/$_src_dir_name";
_target_dir="$_arg_path/target";
_target_debug_dir="$_target_dir/debug";
_bashbox_meta="$_arg_path/$_bashbox_meta_name";
_target_release_dir="$_target_dir/release";
case "${FUNCNAME[1]}" in
"subcommand::build" | "subcommand::run")
# Detect the build variant
_build_variant="$(
if test "$_arg_release" == "on"; then {
echo "${_target_release_dir##*/}";
} else {
echo "${_target_debug_dir##*/}";
} fi
)"; # TODO: Need to add more cases depending on args.
_target_workdir="$_target_dir/$_build_variant";
_used_symbols_statfile="$_target_workdir/.used_symbols";
_compiled_mod_bundle="$_target_workdir/.lib.compiled.mod";
_local_build_registrydir="$_target_workdir/.registry";
# TODO: Decide whether to keep ignoring already loaded modules.
# Start with creating the placeholder target dirs
local _dirs_to_create=(
"$_target_debug_dir"
"$_target_release_dir"
"$_local_build_registrydir"
)
mkdir -p "${_dirs_to_create[@]}";
# Check newline on meta
io::file::check_newline "$_bashbox_meta";
# Merge old-new files
local _old_new_file_ignore=(
-not -path "${_local_build_registrydir}/*"
)
cp -r "$_src_dir/". "$_target_workdir/";
local _dest_file && while read -r _dest_file; do {
if test ! -e "$_src_dir/${_dest_file##"$_target_workdir"}"; then {
rm -r "$_dest_file" || rm -rf "$_dest_file";
} fi
} done < <(find "$_target_workdir" "${_old_new_file_ignore[@]}" -type f)
unset _old_new_file_ignore
# rsync -a --delete "$_src_dir/" "$_target_workdir";
printf '' > "$_used_symbols_statfile";
# Load metadata
source "$_bashbox_meta";
# Check compatibility with bashbox
if test -v "$_bashbox_compat_var_name"; then {
local MIN MAX VAR_REF;
VAR_REF="${!_bashbox_compat_var_name}";
IFS='~' read -r MIN MAX <<<"${VAR_REF}" || true;
# Check mandetory MIN version
if test -n "$MIN"; then {
if ! (( $(awk '{print ($1 <= $2)}' <<<"$MIN $___self_VERSION") )); then {
log::error "$CODENAME requires at least bashbox $MIN" 1 || exit;
} fi
} else {
log::error "MIN version is missing from $_bashbox_compat_var_name in $_bashbox_meta_name" 1 || exit;
} fi
# Check optional MAX version
if test -n "$MAX"; then {
if ! (( $(awk '{print ($1 >= $2)}' <<<"$MAX $___self_VERSION") )); then {
log::error "$CODENAME supports bashbox upto $MAX" 1 || exit;
} fi
} fi
unset MIN MAX VAR_REF;
} else {
log::error "$_bashbox_compat_var_name metadata is missing in $_bashbox_meta_name" 1 || exit;
} fi
# Resolve dependencies
EXPORT_USEMOL="true" subcommand::install "${DEPENDENCIES[@]}";
_target_workfile="$_target_workdir/$CODENAME";
# _usemols_meta="$_target_workdir/$_usemols_meta_name";
# # Now lets load the usemols in RAM
# set -a;
# source "$_usemols_meta";
# set +a;
;;
esac
}