-
Notifications
You must be signed in to change notification settings - Fork 1
/
headstart-core.bash
174 lines (146 loc) · 5.11 KB
/
headstart-core.bash
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
#!/usr/bin/env bash
# TODO add set -ueo pipefail until stack trace kicks in
# The code below is not in a function as variables, functions, etc defined here
# and in the sourced files need to be accessible in the global scope.
# Path to the project's root directory
declare -gx PROJECT_DIR
# The `SHELL` env variable is not supported by every shell and bash sets it
# only if it is not set already. To be sure it exists, it is set here.
[[ ! -v BASH ]] || export SHELL=$BASH
# Used for testing
if [[ -v PROJECT_DIR ]]; then
PROJECT_DIR="$PROJECT_DIR"
else
declare -gx PROJECT_DIR="$(cd "$(dirname "$0")" && pwd)"
fi
if [[ ! -v _HEADSTART_SCRIPT_NAME ]]; then # tests may set this directly
declare -gx _HEADSTART_SCRIPT_NAME="${0##*/}"
fi
# Path to bash-headstart's directory
if [[ "${BASH_SOURCE[0]:0:1}" != '/' ]]; then
cd "$PWD/${BASH_SOURCE[0]%/*}" || exit "$_HEADSTART_EC_GENERR"
else
cd "${BASH_SOURCE[0]%/*}" || exit "$_HEADSTART_EC_GENERR"
fi
declare -r -x _HEADSTART_CORE_DIR="$PWD"
cd "$PROJECT_DIR"
# if [[ -v HEADSTART_INSTALLATION_DIR ]]; then
# TODO make sure this variable is during 'headstart env'
# pushd "$HEADSTART_INSTALLATION_DIR" >/dev/null || exit 1
# fi
# TODO see if this needed once I turn everything into git modules
# . "$_HEADSTART_CORE_DIR/lib/installation"
# if [[ ! -v HEADSTART_INSTALLATION_DIR ]]; then
# installation_install >&2
# <<-CODE_NOTE We are redirecting output to stderr because when running
# `eval "$(./headstart env -)"` when first installing the project,
# the above script will run and output from it will cause eval to
# print errors that the commands like 'Downloading' and 'Download'
# do not exist. By redirecting to stderr, we do not have this
# problem.
# fi
. "$_HEADSTART_CORE_DIR/headstart-load-libs.bash" "$@" \
"${_HEADSTART_CORE_DIR#$PROJECT_DIR/}/"{commands,}
declare -gx _HEADSTART_CMD="${_GO_CMD##*/}"
declare -gx _HEADSTART_PROJECT_CONFIG="${HEADSTART_PROJECT_CONFIG-data/config/project.conf}"
declare -gx _HEADSTART_CORE_LOCK="${HEADSTART_CORE_LOCK-data/config/.core.lock}"
declare -x _GO_HELP_HIJACK=true
declare -x GO_TAB_COMPLETIONS_PATTERN=''
. "$_GO_USE_MODULES" 'core'
core_get_installed_version
function headstart_bootstrap() {
tmp_dir="${_HEADSTART_SCRIPT_NAME~~}_TMP_DIR"
declare -gx _HEADSTART_TMP_DIR="${!tmp_dir}"
config_dir="${_HEADSTART_SCRIPT_NAME~~}_CONFIG_DIR"
declare -gx _HEADSTART_CONFIG_DIR="${!config_dir}"
declare -gx _HEADSTART_VENDOR_DIR="${_HEADSTART_CORE_DIR}/vendor"
declare -gx HEADSTART_RESOURCES_DIR="${HEADSTART_RESOURCES_DIR-resources}"
core_parse_project_config
}
function headstart() {
local trace=false
local debug=false
local verbosity=''
local -a rest
local go_early=false
local print_version=false
local print_help=false
while [[ "$#" -gt 0 ]]; do
case "$1" in
-d | --trace)
trace=true
;;
-dd | --debug)
debug=true
;;
-v*)
verbosity="${1#-}"
;;
--version | -V)
print_version=true
;;
complete | env)
# <<-CODE_NOTE: This is matched when autocompletion occurs or when using
# `eval "$(./headstart env -)"`
rest+=("$1")
;;
*)
rest+=("$1")
;;
esac
shift
done
# This was originally in headstart-load-libs (as all uses of `import`) but
# was moved here in order to put the trace behind the `-d` cli option.
if [[ "$trace" == 'true' && "${_GO_BATS_DIR-unset}" == 'unset' ]]; then
import util/exception
fi
# When we are testing the project's code, we want for instance to check that
# a function returns with a specific code in case of an error. In these cases,
# we do not want this error code to trigger the stack trace that the
# `util/exception` code prints as it would pollute the test output. So, we
# only import this module when not testing. To check if we are in test mode or
# not, we check the existence of a variable that is set only when testing. We
# chose randomly `_GO_BATS_DIR` that is set in `devel/test`.
. "$_GO_USE_MODULES" 'installation' 'aliases'
case "${rest[0]}" in
'env')
@go "${rest[@]}"
return
;;
'complete')
if [[ "$(get_installation_status)" == 'bootstrapped' ]]; then
@go "${rest[@]}"
fi
return
;;
esac
. "$_GO_USE_MODULES" 'system'
set_standard_outputs
set_trace "$debug"
set_debug_levels "$verbosity"
unset verbosity
unset debug
if [[ "$print_version" == 'true' ]]; then
printf "$_HEADSTART_CMD version: "
eval "echo \$${_HEADSTART_SCRIPT_NAME~~}_VERSION"
return
fi
if ! installation_check_status; then
if [[ "${rest[*]}" == 'help core bootstrap' ]]; then
@go "${rest[@]}"
return
elif [[ "${rest[*]}" =~ ^core\ bootstrap\ ?.*$ && "$print_help" == 'true' ]]; then
@go help "${rest[@]}"
return
elif [[ "${rest[*]}" =~ ^core\ bootstrap\ ?.*$ ]]; then
@go "${rest[@]}"
return
else
abort "project needs to be bootstrapped first: $_HEADSTART_CMD core bootstrap"
return
fi
fi
core_check_upgrades
@go "${rest[@]}"
}