-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathnew.sh
More file actions
executable file
·194 lines (167 loc) · 4.57 KB
/
new.sh
File metadata and controls
executable file
·194 lines (167 loc) · 4.57 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
#!/bin/bash
set -e
CATALYST_BASE="https://github.com/kosatnkn/catalyst"
CATALYST_REF="<ph_ref>"
CATALYST_MODULE="github.com/kosatnkn/catalyst/v3"
show_help() {
echo -e "
Usage: $0 --module=<go_module_name> [--dir=<working_dir>]
Options:
--module (Required) Go module path (e.g., example.com/sampler)
--dir (Optional) Target directory (default: current directory)
-y, --yes Answer 'yes' to confirmation prompt
-h, --help Display this help message
Example:
$0 --module example.com/dummyuser/sampler --dir ./projects"
}
# messages
msg_info() {
echo -e "⦁ $1"
}
msg_err() {
echo -e "⨉ $1"
}
msg_success() {
echo -e "✔ $1"
}
msg_ongoing() {
echo -e "⨠ $1"
}
msg_done() {
echo -e "✔ Done"
}
msg_complete() {
echo -e "✔✔ Complete!"
}
prompt_yes_no() {
local msg="∆ Are you sure?"
local decline="⦸ Declined, Stopped!"
local proceed="⨠ Proceeding..."
while true; do
read -r -p "${msg} [Y/n]: " input
case ${input} in
[yY][eE][sS] | [yY])
echo -e "${proceed}"
return 0
;;
[nN][oO] | [nN])
echo -e "${decline}"
exit 1
;;
esac
done
}
# parse args
CONFIRM=false
while [[ $# -gt 0 ]]; do
case "$1" in
--module)
MODULE="$2"
shift 2
;;
--module=*)
MODULE="${1#*=}"
shift
;;
--dir)
WORKING_DIR="$2"
shift 2
;;
--dir=*)
WORKING_DIR="${1#*=}"
shift
;;
-y|--yes)
CONFIRM=true
shift
;;
-h|--help)
show_help
exit 0
;;
*)
msg_err "Unknown option '$1'"
show_help
exit 1
;;
esac
done
# validate
if [[ -z "${MODULE}" ]]; then
msg_err "'--module' is required"
show_help
exit 1
fi
# set defaults
if [[ -z "${WORKING_DIR}" ]]; then
WORKING_DIR="$(pwd)"
fi
# normalize path (remove trailing slash)
WORKING_DIR="${WORKING_DIR%/}"
#infer project dir from go module
PROJECT_DIR=$(basename "$MODULE")
# If it ends with /vN (where N is a number), remove that suffix
if [[ "$PROJECT_DIR" =~ ^v[0-9]+$ ]]; then
# If last part is version, get the previous segment
PROJECT_DIR=$(basename "$(dirname "$MODULE")")
fi
# confirm
msg_info "Go module: ${MODULE}"
msg_info "Working directory: ${WORKING_DIR}"
msg_info "Project directory: ${PROJECT_DIR}"
if [[ ${CONFIRM} = false ]]; then
prompt_yes_no
fi
msg_ongoing "Switching to working directory '${WORKING_DIR}'..."
cd ${WORKING_DIR}
msg_done
msg_ongoing "Creating project directory '${PROJECT_DIR}' in '${WORKING_DIR}'..."
if [[ -d "${PROJECT_DIR}" ]]; then
msg_err "A directory with the name '${PROJECT_DIR}' already exists in '${WORKING_DIR}'"
exit 1
fi
mkdir ${PROJECT_DIR}
msg_done
# NOTE: it is very important that you change dir to project dir
# safety gates are implemented to halt operation if it detects that you are not in the correct dir
cd ${PROJECT_DIR}
# get source
msg_ongoing "Copying files from '${CATALYST_BASE}' to '$(pwd)'..."
[[ $(basename "$(pwd)") == "${PROJECT_DIR}" ]] || { msg_err "Not in '${PROJECT_DIR}', exiting!"; exit 1; } # NOTE: safety gate
curl --silent --show-error --location "${CATALYST_BASE}/archive/${CATALYST_REF}.tar.gz" | tar --extract --gzip --strip-components=1
msg_done
# remove dirs and files
msg_ongoing "Cleaning up..."
[[ $(basename "$(pwd)") == "${PROJECT_DIR}" ]] || { msg_err "Not in '${PROJECT_DIR}', exiting!"; exit 1; } # NOTE: safety gate
REMOVE_LIST=(
".github"
"docs/img/*.drawio.*"
"LICENSE"
"README.md"
"NOTES.md"
"new.sh"
)
for item in "${REMOVE_LIST[@]}"; do
rm -rf ${item} 2>/dev/null || true
msg_success "Removed, '${item}'"
done
# replace following
# github.com/kosatnkn/catalyst/v3 with <new_module_name>
msg_ongoing "Updating go module and references..."
[[ $(basename "$(pwd)") == "${PROJECT_DIR}" ]] || { msg_err "Not in '${PROJECT_DIR}', exiting!"; exit 1; } # NOTE: safety gate
file -i $(find . -type f ! -path "*/.git/*") | grep "text/" | cut -d: -f1 | while read -r file; do # NOTE: checks whether the file contains text
before=$(md5sum ${file})
sed -i -e "s#${CATALYST_MODULE}#${MODULE}#g" "${file}"
after=$(md5sum ${file})
[[ "${before}" != "${after}" ]] && msg_success "Updated, '${file}'"
done
# create necessary files
msg_ongoing "Creating 'README.md'..."
[[ $(basename "$(pwd)") == "${PROJECT_DIR}" ]] || { msg_err "Not in '${PROJECT_DIR}', exiting!"; exit 1; } # NOTE: safety gate
echo -e "# ${PROJECT_DIR}
TODO: Add content
---
<img src="docs/img/catalyst_logo.svg" alt="catalyst" width="14"/> Powered by [kosatnkn/catalyst](https://github.com/kosatnkn/catalyst) (${CATALYST_REF})
" > README.md
msg_done
msg_complete