-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinit
executable file
·77 lines (62 loc) · 2.6 KB
/
init
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
#!/usr/bin/env bash
ask_for() {
if [[ "${!1}" == "" ]] && [[ "${!1}" != 'abort' ]]; then
echo -n " ${2:?'A prompt text must be specified'}: "
read -r "${1:?'A prompt variable name must be specified'}"
echo
fi
}
ask_continue() {
echo -n "Press [ENTER] to continue..."
# shellcheck disable=SC2034
read -r proceed
}
init() {
echo 'Initializing new cli'
echo
local -r default_name="$(basename "${PWD:?}")"
local -r default_owner="$(git config --global --get github.user 2>/dev/null)"
local -r default_fullname="$(git config --global --get user.name 2>/dev/null)"
local -r default_homebrew_tap_name='homebrew-tap'
local -r default_license='Apache License 2.0'
ask_for template_name "name (default: ${default_name:?})"
template_name="${template_name:-${default_name:?}}"
[[ "${template_name:?'name is required'}" ]]
ask_for template_description 'description (optional)'
ask_for template_owner "owner (eg. github.com/<owner>, default: ${default_owner:-})"
template_owner="${template_owner:-${default_owner:-}}"
[[ "${template_owner:?'owner is required'}" ]]
ask_for template_fullname "full name (default: ${default_fullname:-})"
template_fullname="${template_fullname:-${default_fullname:-}}"
[[ "${template_fullname:?'full name is required'}" ]]
ask_for template_homebrew_tap_name "homebrew tap name (eg. github.com/${template_owner:?}/${default_homebrew_tap_name:?})"
template_homebrew_tap_name="${template_homebrew_tap_name:-${default_homebrew_tap_name:?}}"
[[ "${template_homebrew_tap_name:?'homebrew tap name is required'}" ]]
ask_for template_license "license (default: ${default_license:?})"
template_license="${template_license:-${default_license:?}}"
[[ "${template_license:?'license is required'}" ]]
ask_continue
for p in ./.* ./** ./**/*; do
if [[ -f "${p:?}" ]]; then
case "${p:?}" in
'./init' | "./${template_name:?}")
continue
;;
esac
echo "processing file ${p:?}..."
sed -i.backup "s/cli_template_name/${template_name:?}/g" "${p:?}"
sed -i.backup "s/cli_template_description/${template_description:-}/g" "${p:?}"
sed -i.backup "s/cli_template_owner/${template_owner:?}/g" "${p:?}"
sed -i.backup "s/cli_template_fullname/${template_fullname:?}/g" "${p:?}"
sed -i.backup "s/cli_template_homebrew_tap_name/${template_homebrew_tap_name:?}/g" "${p:?}"
sed -i.backup "s/cli_template_license/${template_license:?}/g" "${p:?}"
fi
done
rm -f ./.*.backup
rm -f ./*.backup
rm -f ./**/*.backup
echo
git add .
git commit -m "Initialized new cli ${template_name:?}."
}
init "$@"