/
functions
357 lines (300 loc) · 9.68 KB
/
functions
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#!/bin/sh
install_initialize()
{
typeset -ga cleanse_paths directories binaries files aux_paths configs
cleanse_paths=( external completion modules extensions )
directories=( bin )
binaries=( sm sm-ll smp )
files=( LICENSE VERSION README.md local )
aux_paths=( log tmp src )
configs=( user ) # TODO: cleanup, this might be irrelevant
}
perform()
{
paths enter "${sm_path}"
"$*"
}
prepare_for_installation()
{
typeset directory _path
__sm.log.step "Ensuring that installation target path exists (${install_path})"
paths create "${install_path}"
paths enter "${install_path}"
__sm.log.step success
for directory in "${cleanse_paths[@]}"
do
_path="${install_path}/${directory}"
if paths exists "${_path}"
then
[[ "${_path%/}" != "${install_path}" ]] || continue
__sm.log.step "Removing old sm component path (${_path})"\
rm -rf "${_path}"
fi
done
__sm.log.step "Creating install path directories (${directories[*]} ${aux_paths[*]})" \
paths create "${directories[@]}" "${aux_paths[@]}"
}
cleanse_targets()
{
typeset _path _entity _target
_path="$1"
(( $# )) && shift || __sm.log.fail "Usage:\n\n cleanse_targets {target} {paths/files under target}"
for _entity in "$@"
do
[[ -n "${_entity}" ]] || continue
_target="${_path}/${_entity}"
[[ "${_target%/}" != "${install_path}" ]] || continue
rm -rf "${_target}"
done
}
install_core()
{
typeset _dir
__sm.log.step "Installing core directories (${directories[*]})"
cleanse_targets "${install_path}" "${directories[@]}"
paths copy to "${install_path}/" "${directories[@]}"
__sm.log.step success
__sm.log.step "Installing core files (${files[*]})"
cleanse_targets "${install_path}" ${files[@]}
files copy to "${install_path}" ${files[@]}
__sm.log.step success
__sm.log.step "Installing core extension set"
cleanse_targets "${install_path}/" "core"
paths copy to "${install_path}/" "core"
__sm.log.step success
}
install_configuration_files()
{
__sm.log.step "Ensuring configuration files exist (${configs[*]})"
for config_file in "${configs[@]}"
do
files create "${install_path}/config/${config_file}"
done
__sm.log.step success
}
setup_shell_completion()
{
typeset _mode _name completion_prefix
completion_prefix="${etc_path}/bash_completion.d"
_name="sm.bash"
_mode="0644"
if string equals "${DESTROOT:-/}" "/"
then paths exists "${completion_prefix}" || return 0
else paths create "${completion_prefix}"
fi
templates install "install/sm-completion.sh" \
to "${completion_prefix}/${_name}" \
variables \
sm_path "${install_path/${DESTROOT:-\/}/\/}"
files chmod "${_mode}" "${completion_prefix}/${_name}"
}
setup_profile_d()
{
typeset _PATH
_PATH="PATH=\"\${PATH}:${install_path/${DESTROOT:-'/'}/\/}/bin:${install_path/${DESTROOT:-'/'}/\/}/pkg/active/bin:${install_path/${DESTROOT:-'/''}/\/}/pkg/active/sbin\""
if user is root
then
paths create "${profile_d_path}"
if os is darwin
then
if ! files exist "${etc_path}/profile" ||
! files contain string "${profile_d_path}" "${etc_path}/profile"
then
templates install "install/etc_profile" \
to "${etc_path}/profile" \
variables \
profile_d_path "${profile_d_path/${DESTROOT:-\/}/\/}"
fi
fi
if ! files exist "${profile_d_path}/sm.sh" ||
! files contain string "${install_path}/pkg/active/sbin" "${profile_d_path}/sm.sh"
then
files write string "${_PATH}" to "${profile_d_path}/sm.sh"
fi
files chmod 0755 "${profile_d_path}/sm.sh"
else
log "\nNOTE:\n"\
"Since you are installing as a user ($USER), in order to ensure that the 'sm' "\
"command is available to you add the following to your startup profile:\n"\
" export ${_PATH}\n"\
"Then either close the current shell or source the profile file.\n"
# TODO: Provide a way to do this through the cli but outside the install:
#files create "$HOME/.profile" "$HOME/.bashrc" "$HOME/.bash_profile"
#if ! files contain string "$HOME/.profile" "$HOME/.bashrc"
#then
# files write appnd \
# string "if test -s \"$HOME/.profile\" ; then . \"$HOME/.profile\" ; fi"\
# to "$HOME/.bashrc"
#fi
#if ! files contain string "$HOME/.bashrc" "$HOME/.bash_profile"
#then
# files write append \
# string "if test -s \"$HOME/.bashrc\" ; then . \"$HOME/.bashrc\" ; fi"\
# to "$HOME/.bash_profile"
#fi
#if ! files contain string "${profile_d_path}" "$HOME/.profile"
#then
# files write append \
# string "\nif test -d '${profile_d_path}' ; then for profile in \"${profile_d_path}\"/*.sh ; do if test -x \"\$profile\" ; then . \"\$profile\" ; fi ; done ; unset profile ; fi" \
# to "$HOME/.profile"
#fi
fi
}
set_sm_path()
{
files replace regex "^.*sm_path=.*" \
with "sm_path=\"${install_path/${DESTROOT:-\/}/\/}\"" \
"${install_path}/bin/sm"
files replace regex "^sm_path=.*" \
with "sm_path=\"${install_path/${DESTROOT:-\/}/\/}\"" \
"${install_path}/bin/smp"
files replace regex "^saved_sm_path=.*" \
with "saved_sm_path=\"\${sm_path:-${install_path/${DESTROOT:-\/}/\/}}\"" \
"${install_path}/core/sm/shell/core/initialize"
}
make_binaries_executable()
{
typeset _binary
for _binary in "${binaries[@]}"
do
file chmod 0755 "${install_path}/bin/${_binary}"
done
}
set_binaries_shell()
{
typeset _binary
for _binary in local bin/sm bin/smp
do
file replace \
regex "^#!.*$" \
with "#!${sm_shell:-/usr/bin/env zsh}" \
in "${install_path}/${_binary}"
done
}
create_versionedfs_paths()
{
versionedfs create \
path "${install_path}/pkg" strategy fhs method link activate first
versionedfs create \
path "${install_path}/sets" strategy name method copy activate first
versionedfs create \
path "${install_path}/exts" strategy name method copy activate first
}
get_platform()
{
typeset platform
platform="`uname -s`/`uname -m`"
case $platform in
(Darwin/i386|Darwin/x86_64|Linux/i386|Linux/x86_64)
echo "$platform"
;;
(Linux/i[456]86)
echo "Linux/i386"
;;
(Linux/armv[67]l)
echo "Linux/arm"
;;
(*)
echo "Unsupported platform: $platform." >&2
exit 1
;;
esac
}
install_sm_util()
{
typeset _url _util_version _unm _un _file
_un=$(uname | tr '[A-Z]' '[a-z]')
_unm=$(uname -m | sed -e 's/x86_64/amd64/' -e 's/i386|i586|i686/386/')
_util_version=0.1.1
if [[ -s "${install_path}/bin/sm-util" ]]
then
if file is symlink "${install_path}/bin/sm-util" ||
file contains "${install_path}/bin/sm-util" regexp 'perl'
then file remove "${install_path}/bin/sm-util"
fi
fi
if file is executable ./bin/sm-util
then
# TODO: Validate arch
cp ./bin/sm-util ${install_path}/bin/sm-util
chmod a+x ${install_path}/bin/sm-util
fi
if ! file is executable "${install_path}/bin/sm-util"
then
file remove "${install_path}/bin/sm-util"
__sm.log.step "Downloading sm-util."
# TODO: Handle archive extensions based on OS.
_file="sm-util_${_util_version}_${_un}_${_unm}"
_url="https://files.smf.sh/util/${_util_version}/${_file}.tar.gz" || return $?
curl -s ${_url} -o "${install_path}/src/${_file}.tar.gz"
tar zxf "${install_path}/src/${_file}.tar.gz" -C "${install_path}/src/"
cp "${install_path}/src/${_file}/sm-util" "${install_path}/bin/sm-util"
file chmod +x "${install_path}/bin/sm-util"
file link symbolic force from "${install_path}/bin/sm-util" to "${sm_path}/bin/sm-util"
__sm.log.step success
fi
}
install_sm_mustache()
{
typeset _url
if [[ -s "${install_path}/bin/sm-mustache" ]]
then
if file is symlink "${install_path}/bin/sm-mustache" ||
file contains "${install_path}/bin/sm-mustache" regexp 'perl'
then file remove "${install_path}/bin/sm-mustache"
fi
fi
if ! file is executable "${install_path}/bin/sm-mustache"
then
file remove "${install_path}/bin/sm-mustache"
__sm.log.step "Downloading sm-mustache."
_url="https://smf.sh/mustache/$(get_platform)/sm-mustache" || return $?
curl -s ${_url} -o "${install_path}/bin/sm-mustache"
file chmod +x "${install_path}/bin/sm-mustache"
file link symbolic force from "${install_path}/bin/sm-mustache" to "${sm_path}/bin/sm-mustache"
__sm.log.step success
fi
}
install_sm_json()
{
typeset _url
if [[ -s "${install_path}/bin/sm-json" ]]
then
if file is symlink "${install_path}/bin/sm-json" ||
file contains "${install_path}/bin/sm-json" regexp 'perl'
then file remove "${install_path}/bin/sm-json"
fi
fi
if ! file is executable "${install_path}/bin/sm-json"
then
file remove "${install_path}/bin/sm-json"
__sm.log.step "Downloading sm-json"
_url="https://smf.sh/json/$(get_platform)/sm-json" || return $?
curl -s ${_url} -o "${install_path}/bin/sm-json"
file chmod +x "${install_path}/bin/sm-json"
file link symbolic force from "${install_path}/bin/sm-json" to "${sm_path}/bin/sm-json"
__sm.log.step success
fi
}
install_sm()
{
log "S{cripting,ystem,tack} Management (SM) Framework (Core) Installation:"
prepare_for_installation
perform install_core
perform install_sm_util
#perform install_sm_mustache
#perform install_sm_json
perform create_versionedfs_paths
perform set_sm_path
perform install_configuration_files
set_binaries_shell
__sm.log.step "Ensuring that binaries are executable" \
make_binaries_executable
perform setup_profile_d
if user is root
then
__sm.log.step "Setting up sm shell completion (${completion_prefix}/${_name})" \
perform setup_shell_completion
fi
__sm.log.succeed "S{cripting,ystem,tack} Management (SM) Framework (Core) installed."
}