-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshinit
executable file
·306 lines (247 loc) · 6.24 KB
/
shinit
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
#!/bin/sh
# shinit - initialize Bourne shell project.
# Copyright 2023 Maciej Żok
# SPDX-License-Identifier: MIT
# <https://github.com/macie/shinit>
SHINIT_VERSION="24.02"
# DEFAULTS
SHINIT_GIT_BIN=$(git -v)
if [ -n "${SHINIT_GIT_BIN}" ]; then
SHINIT_AUTHOR_DEFAULT="$(git config user.name)"
else
SHINIT_AUTHOR_DEFAULT="$(id -u -n)"
fi
SHINIT_LICENSE_DEFAULT="MIT"
SHINIT_NAME_DEFAULT="sh-$(hexdump -v -n4 -e'"%02x"' /dev/random)"
SHINIT_DIR="."
# Obtain project parameters from user.
shinit_prompt() {
printf 'Choose:\n'
if [ -n "${SHINIT_GIT_BIN}" ]; then
printf ' - repo root path (directory name or URL) [%s]: ' "${SHINIT_NAME_DEFAULT}"
read -r SHINIT_REPO_PATH
if [ -z "${SHINIT_REPO_PATH}" ]; then
SHINIT_NAME="${SHINIT_NAME_DEFAULT}"
else
SHINIT_NAME=$(echo "${SHINIT_REPO_PATH}" | xargs -E "" -I {} basename {} .git)
fi
else
# Git is not installed
printf ' - project name [%s]: ' "${SHINIT_NAME_DEFAULT}"
read -r SHINIT_NAME
if [ -z "${SHINIT_NAME}" ]; then
SHINIT_NAME="${SHINIT_NAME_DEFAULT}"
fi
fi
printf ' - author [%s]: ' "${SHINIT_AUTHOR_DEFAULT}"
read -r SHINIT_AUTHOR
if [ -z "${SHINIT_AUTHOR}" ]; then
SHINIT_AUTHOR="${SHINIT_AUTHOR_DEFAULT}"
fi
while ! echo "${SHINIT_LICENSE}" | grep -q -E '^MIT|GPL-3.0-only|MIT-0'; do
printf ' - license (MIT, GPL-3.0-only, MIT-0) [%s]: ' "${SHINIT_LICENSE_DEFAULT}"
read -r SHINIT_LICENSE
if [ -z "${SHINIT_LICENSE}" ]; then
SHINIT_LICENSE="${SHINIT_LICENSE_DEFAULT}"
fi
done
}
# Generate project files.
shinit_generate() {
printf '\nCreating:\n'
printf ' - project directory: %s\n' "${SHINIT_DIR}/${SHINIT_NAME}"
mkdir "${SHINIT_DIR}/${SHINIT_NAME}"
cd "${SHINIT_DIR}/${SHINIT_NAME}" || exit 1
shinit_git
shinit_markdown
shinit_makefile
shinit_sh
}
# Initialize Git repository.
shinit_git() {
if [ -z "${SHINIT_GIT_BIN}" ]; then
printf ' - Git configuration skipped (cannot find Git binary)\n'
return 0
fi
printf ' - Git repository\n'
git init
if [ -n "${SHINIT_REPO_PATH}" ] && echo "${SHINIT_REPO_PATH}" | grep -q -E '^https:\/\/|[^@]+@[^:]+:'; then
printf ' - link to remote repository: origin at %s\n' "${SHINIT_REPO_PATH}"
git remote add origin "${SHINIT_REPO_PATH}"
fi
printf ' - .gitignore\n'
cat >.gitignore <<-EOF
# Whitelist for sh
# Ignore everything
*
# But not these files...
!/.gitignore
!${SHINIT_NAME}
!*.sh
!README.md
!LICENSE
!Makefile
# ...even if they are in subdirectories
!*/
EOF
}
# Prepare shell script.
shinit_sh() {
printf ' - %s\n' "${SHINIT_NAME}"
cat >"${SHINIT_NAME}" <<-EOF
#!/bin/sh
# ${SHINIT_NAME}
# Copyright $(date +%Y) ${SHINIT_AUTHOR}
# SPDX-License-Identifier: ${SHINIT_LICENSE}
# <${SHINIT_REPO_PATH}>
printf 'ERROR: not implemented' >&2
exit 64 # EX_USAGE
EOF
printf ' - add execution permission to %s\n' "${SHINIT_NAME}"
chmod +x "${SHINIT_NAME}"
}
# Prepare Makefile.
shinit_makefile() {
printf ' - Makefile\n'
cat >Makefile <<-EOF
.POSIX:
.SUFFIXES:
#
# PUBLIC MACROS
#
CLI = ${SHINIT_NAME}
DESTDIR = ./dist
LINT = shellcheck
TEST = ./unittest
#
# INTERNAL MACROS
#
TEST_SRC=https://github.com/macie/unittest.sh/releases/latest/download/unittest
EOF
cat >>Makefile <<'EOF'
#
# DEVELOPMENT TASKS
#
.PHONY: all
all: test check
.PHONY: clean
clean:
@echo '# Delete test runner:' >&2
rm -f $(TEST)
@echo '# Delete bulid directory' >&2
rm -rf $(DESTDIR)
.PHONY: info
info:
@printf '# OS info: '
@uname -rsv;
@echo '# Development dependencies:'
@echo; $(LINT) -V || true
@echo; $(TEST) -v || true
@echo '# Environment variables:'
@env || true
.PHONY: check
check: $(LINT)
@printf '# Static analysis: $(LINT) $(CLI) ./tests/*.sh' >&2
@$(LINT) $(CLI) ./tests/*.sh
.PHONY: test
test: $(TEST)
@echo '# Unit tests: $(TEST)' >&2
@$(TEST)
.PHONY: install
install:
@echo '# Install in /usr/local/bin' >&2
@mkdir -p /usr/local/bin; cp $(CLI) /usr/local/bin/
.PHONY: dist
dist:
@echo '# Copy CLI executable to $(DESTDIR)/$(CLI)' >&2
@mkdir -p $(DESTDIR); cp $(CLI) $(DESTDIR)/
@echo '# Add executable checksum to: $(DESTDIR)/$(CLI).sha256sum' >&2
@cd $(DESTDIR); sha256sum $(CLI) >> $(CLI).sha256sum
#
# DEPENDENCIES
#
$(LINT):
@printf '# $@ installation path: ' >&2
@command -v $@ >&2 || { echo "ERROR: Cannot find $@" >&2; exit 1; }
$(TEST):
@echo '# Prepare $@:' >&2
@if [ "$$(uname -s)" = "OpenBSD" ]; then \
ftp -V $(TEST_SRC); \
ftp -V $(TEST_SRC).sha256sum; \
sha256 -c [email protected]; \
else \
curl -fLO $(TEST_SRC); \
curl -fLO $(TEST_SRC).sha256sum; \
sha256sum -c [email protected]; \
fi
chmod +x $@
EOF
}
# Prepare documentation files.
shinit_markdown() {
# shellcheck disable=SC2018,SC2019
LICENSE_SRC='https://raw.githubusercontent.com/github/choosealicense.com/gh-pages/_licenses/'$(echo "${SHINIT_LICENSE}" | tr 'A-Z' 'a-z' | sed 's/-only//g')'.txt'
printf ' - LICENSE (from %s)\n' "${LICENSE_SRC}"
curl -fL "${LICENSE_SRC}" | sed -e '/---/,/---/d' -e '/./,$!d' -e "s/\[year\]/$(date +%Y)/g" -e "s/\[fullname\]/$SHINIT_AUTHOR/g" >LICENSE
printf ' - README.md\n'
cat >README.md <<-EOF
# ${SHINIT_NAME}
EOF
cat >>README.md <<-'EOF'
## Development
Use `make`:
- `make` - run checks
- `make test` - run test
- `make check` - perform static code analysis
- `make install` - install in `/usr/local/bin`
- `make dist` - prepare for distributing
- `make clean` - remove distribution artifacts
- `make info` - print system info (useful for debugging).
EOF
cat >>README.md <<-EOF
## License
[${SHINIT_LICENSE}](./LICENSE)
EOF
}
# MAIN ROUTINE
{
case $# in
0) # default
;;
1)
case $1 in
-h)
cat >&2 <<-'EOF'
shinit - initialize Bourne shell project inside current (or given) directory
Usage:
shinit [PARENT_DIR]
shinit [-h] [-v]
Options:
-h Show this help and exit.
-v Show version number and exit.
EOF
exit 0
;;
-v)
printf 'shinit %s' "${SHINIT_VERSION}" >&2
exit 0
;;
*)
if [ -d "$1" ]; then
SHINIT_DIR="$1"
else
printf 'ERROR: invalid usage. Try: %s -h\n' "$0" >&2
exit 64 # EX_USAGE
fi
;;
esac
;;
*)
printf 'ERROR: invalid usage. Try: %s -h\n' "$0" >&2
exit 64 # EX_USAGE
;;
esac
shinit_prompt
shinit_generate
exit 0
}