Skip to content

Commit a32edd5

Browse files
committed
Merge kernel lib to bootstrap.
1 parent f05e164 commit a32edd5

2 files changed

Lines changed: 136 additions & 130 deletions

File tree

bin/dev-bootstrap.bash

Lines changed: 136 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,34 @@
22
# Use of this source code is governed by a BSD-style
33
# license that can be found in the LICENSE file.
44

5+
# dev_kernel_<xxx>: framework functions;
6+
# cmd_<xxx>: command functions;
7+
# <project>_<xxx>: library functions;
8+
9+
# dev_global_<xxx>: global variables;
10+
# dev_conf_<xxx>: configuration variables;
11+
512
# trace ERR through pipes
613
set -o pipefail
714

815
# set -e : exit the script if any statement returns a non-true return value
916
set -o errexit
1017

18+
# The dev package version
19+
declare -gr dev_global_version="2.1.0"
20+
21+
# The dev execution file path
22+
declare -gr dev_global_self="$(realpath $0)"
23+
24+
# The dev execution base path
25+
declare -gr dev_global_base="$(dirname $(dirname $dev_global_self))"
26+
27+
# Disable completion default
28+
declare -g dev_global_completion="0"
29+
30+
# Cache for imports
31+
declare -gA dev_global_imports=()
32+
1133
# Optional arguments
1234
dev_kernel_optional_arguments() {
1335
declare carry=""
@@ -43,36 +65,127 @@ dev_kernel_optional_arguments() {
4365
[[ ! -z "$carry" ]] && dev_global_carry="$dev_global_carry $carry" || true
4466
}
4567

46-
# The dev package version
47-
declare -gr dev_global_version="2.1.0"
68+
# Display a error message in the output
69+
dev_kernel_error() {
70+
printf "\e[35m" >&2 && echo "$@" >&2 && printf "\e[m" >&2
71+
exit 1
72+
}
4873

49-
# The dev execution file path
50-
declare -gr dev_global_self="$(realpath $0)"
74+
# Display a normal message in the output
75+
dev_kernel_info() {
76+
echo "$@" >&2
77+
}
5178

52-
# The dev execution base path
53-
declare -gr dev_global_base="$(dirname $(dirname $dev_global_self))"
79+
dev_kernel_completion() {
80+
dev_kernel_info "$@"
81+
[[ "$dev_global_completion" -eq "1" ]] && echo "$@" | tr '\n' ' '
82+
}
5483

55-
# Disable completion default
56-
declare -g dev_global_completion="0"
84+
dev_kernel_command_file() {
85+
if [[ $# -le 0 ]]; then
86+
dev_kernel_info "These are the all aviliable command files:"
87+
dev_kernel_completion "$(dev_run dev completion dev_kernel_command_files $dev_global_wkdir/cmd)"
88+
exit 1
89+
fi
5790

58-
# Load kernel functions
59-
source "$dev_global_base/lib/kernel.bash"
91+
# Init command file
92+
declare -gr dev_global_command_file="$dev_global_wkdir/cmd/$1.bash"
6093

61-
# Init project
62-
if [[ $# -le 0 ]]; then
63-
dev_kernel_info "These are the all aviliable projects:"
64-
dev_kernel_info "$(dev_run dev completion dev_kernel_projects)"
65-
exit 1
66-
fi
94+
if [[ ! -f "$dev_global_command_file" ]]; then
95+
dev_kernel_error "Command file not found: $dev_global_command_file"
96+
fi
97+
98+
source "$dev_global_command_file"
99+
}
100+
101+
dev_kernel_command_function() {
102+
if [[ $# -le 0 ]]; then
103+
dev_kernel_info "These are the all aviliable command functions:"
104+
dev_kernel_completion "$(dev_run dev completion dev_kernel_command_functions)"
105+
exit 1
106+
fi
107+
108+
if [[ "$dev_global_completion" -eq "1" ]]; then
109+
exit 0
110+
fi
111+
112+
# Init command function
113+
declare -gr dev_global_command_function="cmd_$1"
114+
115+
if [[ "$(declare -F $dev_global_command_function)" != "$dev_global_command_function" ]]; then
116+
dev_kernel_error "Command function not found: $dev_global_command_function"
117+
fi
118+
}
119+
120+
dev_kernel_project() {
121+
# Load configuration file
122+
declare -g dev_global_conf="$dev_global_base/etc/dev.conf"
123+
if [[ -f "$dev_global_conf" ]]; then
124+
source "$dev_global_conf"
125+
else
126+
dev_kernel_error "Configuration file not found: $dev_global_conf"
127+
fi
128+
129+
if [[ $# -le 0 ]]; then
130+
dev_kernel_info "These are the all aviliable projects:"
131+
dev_kernel_info "$(dev_run dev completion dev_kernel_projects)"
132+
exit 1
133+
fi
134+
135+
# Init project
136+
declare -gr dev_global_project="$1"
137+
138+
if [[ ! -n "${dev_conf_projects[$dev_global_project]+1}" ]]; then
139+
dev_kernel_error "Project not exists: $dev_global_project"
140+
fi
141+
}
142+
143+
# Importing an entire command file in a particular project
144+
# To import all functions contained in a particular command file
145+
# Usage: dev_import <project> <command-file>
146+
dev_import() {
147+
if [[ $# -lt 2 ]]; then
148+
dev_kernel_error "Incorrect import syntax"
149+
fi
150+
151+
declare import_key="$1/$2"
152+
if [[ -n "${dev_global_imports[$import_key]+1}" ]]; then
153+
return 0
154+
fi
67155

68-
declare -gr dev_global_project="$1"
156+
if [[ ! -n "${dev_conf_projects[$1]+1}" ]]; then
157+
dev_kernel_error "Import error, project does not exist: $1"
158+
fi
159+
160+
declare wkdir="${dev_conf_projects[$1]}"
161+
declare command_path="$wkdir/lib/$2.bash"
162+
163+
if [[ ! -f "$command_path" ]]; then
164+
dev_kernel_error "Import error, library file not found: $command_path"
165+
fi
166+
167+
source "$command_path"
168+
169+
dev_global_imports[$import_key]="1"
170+
}
171+
172+
# Load the function from the lib directory and run it.
173+
# Usage: dev_run <project> <file> <function> [arguments]
174+
dev_run() {
175+
if [[ $# -lt 3 ]]; then
176+
dev_kernel_error "Incorrect run syntax"
177+
fi
178+
179+
declare func="$3"
180+
dev_import $1 $2
181+
shift 3
182+
$func $@
183+
}
184+
185+
dev_kernel_project $@
69186
shift
70187

71188
# Init project working directory
72-
if [[ ! -n "${dev_conf_projects[$dev_global_project]+1}" ]]; then
73-
dev_kernel_error "Project not exists: $dev_global_project"
74-
fi
75-
76189
declare -gr dev_global_wkdir="${dev_conf_projects[$dev_global_project]}"
77190

78191
# Load working project configuration file
@@ -95,40 +208,12 @@ if [[ "$dev_global_sudo" -eq "1" ]]; then
95208
declare -g dev_global_cmd="$dev_global_self $dev_global_project $dev_global_carry $@"
96209
sudo $dev_global_cmd
97210
else
98-
# Init command file
99-
if [[ $# -le 0 ]]; then
100-
dev_kernel_info "These are the all aviliable command files:"
101-
dev_kernel_completion "$(dev_run dev completion dev_kernel_command_files $dev_global_wkdir/cmd)"
102-
exit 1
103-
fi
104-
105-
declare -gr dev_global_command_file="$dev_global_wkdir/cmd/$1.bash"
211+
dev_kernel_command_file $@
106212
shift
107213

108-
if [[ ! -f "$dev_global_command_file" ]]; then
109-
dev_kernel_error "Command file not found: $dev_global_command_file"
110-
fi
111-
112-
source "$dev_global_command_file"
113-
114-
# Init command function
115-
if [[ $# -le 0 ]]; then
116-
dev_kernel_info "These are the all aviliable command functions:"
117-
dev_kernel_completion "$(dev_run dev completion dev_kernel_command_functions)"
118-
exit 1
119-
fi
120-
121-
if [[ "$dev_global_completion" -eq "1" ]]; then
122-
exit 0
123-
fi
124-
125-
declare -gr dev_global_command_function="cmd_$1"
214+
dev_kernel_command_function $@
126215
shift
127216

128-
if [[ "$(declare -F $dev_global_command_function)" != "$dev_global_command_function" ]]; then
129-
dev_kernel_error "Command function not found: $dev_global_command_function"
130-
fi
131-
132217
# Run the function
133218
$dev_global_command_function $@
134219
fi

lib/kernel.bash

Lines changed: 0 additions & 79 deletions
This file was deleted.

0 commit comments

Comments
 (0)