-
Notifications
You must be signed in to change notification settings - Fork 2
/
setup.bash
executable file
·314 lines (248 loc) · 9.44 KB
/
setup.bash
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
#!/usr/bin/env bash
set -euo pipefail
HELP="
Usage:
bash [--github | --gitlab] $0 PLUGIN_NAME TOOL_TEST GH_USER AUTHOR_NAME TOOL_GH TOOL_PAGE LICENSE
All arguments are optional and will be interactively prompted when not given.
PLUGIN_NAME.
A name for your new plugin always starting with \`asdf-\` prefix.
TOOL_TEST.
A shell command used to test correct installation.
Normally this command is something taking \`--version\` or \`--help\`.
GH_USER.
Your GitHub/GitLab username.
AUTHOR_NAME.
Your name, used for licensing.
TOOL_GH.
The tool's GitHub homepage. Default installation process will try to use
this to access GitHub releases.
TOOL_PAGE.
Documentation site for tool usage, mostly informative for users.
LICENSE.
A license keyword.
https://help.github.com/en/github/creating-cloning-and-archiving-repositories/licensing-a-repository#searching-github-by-license-type
"
HELP_PLUGIN_NAME="Name for your plugin, starting with \`asdf-\`, eg. \`asdf-foo\`"
HELP_TOOL_CHECK="Shell command for testing correct tool installation. eg. \`foo --version\` or \`foo --help\`"
HELP_TOOL_REPO="The tool's GitHub homepage."
HELP_TOOL_HOMEPAGE="The tool's documentation homepage if necessary."
ask_for() {
local prompt="$1"
local default_value="${2:-}"
local alternatives="${3:-"[$default_value]"}"
local value=""
while [ -z "$value" ]; do
echo "$prompt" >&2
if [ "[]" != "$alternatives" ]; then
echo -n "$alternatives " >&2
fi
echo -n "> " >&2
read -r value
echo >&2
if [ -z "$value" ] && [ -n "$default_value" ]; then
value="$default_value"
fi
done
printf "%s\n" "$value"
}
download_license() {
local keyword file
keyword="$1"
file="$2"
curl -qsL "https://raw.githubusercontent.com/github/choosealicense.com/gh-pages/_licenses/${keyword}.txt" |
extract_license >"$file"
}
extract_license() {
awk '/^---/{f=1+f} f==2 && /^$/ {f=3} f==3'
}
test_url() {
curl -fqsL -I "$1" | head -n 1 | grep 200 >/dev/null
}
ask_license() {
local license keyword
printf "%s\n" "Please choose a LICENSE keyword." >&2
printf "%s\n" "See available license keywords at" >&2
printf "%s\n" "https://help.github.com/en/github/creating-cloning-and-archiving-repositories/licensing-a-repository#searching-github-by-license-type" >&2
while true; do
license="$(ask_for "License keyword:" "apache-2.0" "mit/[apache-2.0]/agpl-3.0/unlicense")"
keyword=$(echo "$license" | tr '[:upper:]' '[:lower:]')
url="https://choosealicense.com/licenses/$keyword/"
if test_url "$url"; then
break
else
printf "Invalid license keyword: %s\n" "$license"
fi
done
printf "%s\n" "$keyword"
}
set_placeholder() {
local name value out file tmpfile
name="$1"
value="$2"
out="$3"
git grep -P -l -F --untracked "$name" -- "$out" |
while IFS=$'\n' read -r file; do
tmpfile="$file.sed"
sed "s#$name#$value#g" "$file" >"$tmpfile" && mv "$tmpfile" "$file"
done
}
setup_github() {
local cwd out tool_name tool_repo check_command author_name github_username tool_homepage ok primary_branch
cwd="$PWD"
out="$cwd/out"
# ask for arguments not given via CLI
tool_name="${1:-$(ask_for "$HELP_PLUGIN_NAME")}"
tool_name="${tool_name/asdf-/}"
check_command="${2:-$(ask_for "$HELP_TOOL_CHECK" "$tool_name --help")}"
github_username="${3:-$(ask_for "Your GitHub username")}"
author_name="${4:-$(ask_for "Your name" "$(git config user.name 2>/dev/null)")}"
tool_repo="${5:-$(ask_for "$HELP_TOOL_REPO" "https://github.com/$github_username/$tool_name")}"
tool_homepage="${6:-$(ask_for "$HELP_TOOL_HOMEPAGE" "$tool_repo")}"
license_keyword="${7:-$(ask_license)}"
license_keyword="$(echo "$license_keyword" | tr '[:upper:]' '[:lower:]')"
primary_branch="main"
cat <<-EOF
Setting up plugin: asdf-$tool_name
author: $author_name
plugin repo: https://github.com/$github_username/asdf-$tool_name
license: https://choosealicense.com/licenses/$license_keyword/
$tool_name github: $tool_repo
$tool_name docs: $tool_homepage
$tool_name test: \`$check_command\`
After confirmation, the \`$primary_branch\` will be replaced with the generated
template using the above information. Please ensure all seems correct.
EOF
ok="${8:-$(ask_for "Type \`yes\` if you want to continue.")}"
if [ "yes" != "$ok" ]; then
printf "Nothing done.\n"
else
(
set -e
# previous cleanup to ensure we can run this program many times
git branch template 2>/dev/null || true
git checkout -f template
git worktree remove -f out 2>/dev/null || true
git branch -D out 2>/dev/null || true
# checkout a new worktree and replace placeholders there
git worktree add --detach out
cd "$out"
git checkout --orphan out
git rm -rf "$out" >/dev/null
git read-tree --prefix="" -u template:template/
download_license "$license_keyword" "$out/LICENSE"
sed -i '1s;^;TODO: INSERT YOUR NAME & COPYRIGHT YEAR\n;g' "$out/LICENSE"
set_placeholder "<YOUR TOOL>" "$tool_name" "$out"
set_placeholder "<TOOL HOMEPAGE>" "$tool_homepage" "$out"
set_placeholder "<TOOL REPO>" "$tool_repo" "$out"
set_placeholder "<TOOL CHECK>" "$check_command" "$out"
set_placeholder "<YOUR NAME>" "$author_name" "$out"
set_placeholder "<YOUR GITHUB USERNAME>" "$github_username" "$out"
set_placeholder "<PRIMARY BRANCH>" "$primary_branch" "$out"
git add "$out"
# remove GitLab specific files
git rm -rf "$out/.gitlab" "$out/.gitlab-ci.yml" "$out/README-gitlab.md" "$out/contributing-gitlab.md"
# rename GitHub specific files to final filenames
git mv "$out/README-github.md" "$out/README.md"
git mv "$out/contributing-github.md" "$out/contributing.md"
git commit -m "Generate asdf-$tool_name plugin from template."
cd "$cwd"
git branch -M out "$primary_branch"
git worktree remove -f out
git checkout -f "$primary_branch"
printf "All done.\n"
printf "Your %s branch has been reset to an initial commit.\n" "$primary_branch"
printf "Push to origin/%s with \`git push --force-with-lease\`\n" "$primary_branch"
printf "Review these TODO items:\n"
git grep -P -n -C 3 "TODO"
) || cd "$cwd"
fi
}
setup_gitlab() {
local cwd out tool_name tool_repo check_command author_name github_username gitlab_username tool_homepage ok primary_branch
cwd="$PWD"
out="$cwd/out"
# ask for arguments not given via CLI
tool_name="${1:-$(ask_for "$HELP_PLUGIN_NAME")}"
tool_name="${tool_name/asdf-/}"
check_command="${2:-$(ask_for "$HELP_TOOL_CHECK" "$tool_name --help")}"
gitlab_username="$(ask_for "Your GitLab username")"
author_name="${4:-$(ask_for "Your name" "$(git config user.name 2>/dev/null)")}"
github_username="${3:-$(ask_for "Tool GitHub username")}"
tool_repo="${5:-$(ask_for "$HELP_TOOL_REPO" "https://github.com/$github_username/$tool_name")}"
tool_homepage="${6:-$(ask_for "$HELP_TOOL_HOMEPAGE" "$tool_repo")}"
license_keyword="${7:-$(ask_license)}"
license_keyword="$(echo "$license_keyword" | tr '[:upper:]' '[:lower:]')"
primary_branch="main"
cat <<-EOF
Setting up plugin: asdf-$tool_name
author: $author_name
plugin repo: https://gitlab.com/$gitlab_username/asdf-$tool_name
license: https://choosealicense.com/licenses/$license_keyword/
$tool_name github: $tool_repo
$tool_name docs: $tool_homepage
$tool_name test: \`$check_command\`
After confirmation, the \`$primary_branch\` will be replaced with the generated
template using the above information. Please ensure all seems correct.
EOF
ok="${8:-$(ask_for "Type \`yes\` if you want to continue.")}"
if [ "yes" != "$ok" ]; then
printf "Nothing done.\n"
else
(
set -e
# previous cleanup to ensure we can run this program many times
git branch template 2>/dev/null || true
git checkout -f template
git worktree remove -f out 2>/dev/null || true
git branch -D out 2>/dev/null || true
# checkout a new worktree and replace placeholders there
git worktree add --detach out
cd "$out"
git checkout --orphan out
git rm -rf "$out" >/dev/null
git read-tree --prefix=/ -u template:template/
download_license "$license_keyword" "$out/LICENSE"
set_placeholder "<YOUR TOOL>" "$tool_name" "$out"
set_placeholder "<TOOL HOMEPAGE>" "$tool_homepage" "$out"
set_placeholder "<TOOL REPO>" "$tool_repo" "$out"
set_placeholder "<TOOL CHECK>" "$check_command" "$out"
set_placeholder "<YOUR NAME>" "$author_name" "$out"
set_placeholder "<YOUR GITHUB USERNAME>" "$github_username" "$out"
set_placeholder "<YOUR GITLAB USERNAME>" "$gitlab_username" "$out"
set_placeholder "<PRIMARY BRANCH>" "$primary_branch" "$out"
git add "$out"
# remove GitHub specific files
git rm -rf "$out/.github" "$out/README-github.md" "$out/contributing-github.md"
# rename GitLab specific files to final filenames
git mv "$out/README-gitlab.md" "$out/README.md"
git mv "$out/contributing-gitlab.md" "$out/contributing.md"
git commit -m "Generate asdf-$tool_name plugin from template."
cd "$cwd"
git branch -M out "$primary_branch"
git worktree remove -f out
git checkout -f "$primary_branch"
printf "All done.\n"
printf "Your %s branch has been reset to an initial commit.\n" "$primary_branch"
printf "You might want to push using \`--force-with-lease\` to origin/%s\n" "$primary_branch"
printf "Showing pending TODO tags that you might want to review\n"
git grep -P -n -C 3 "TODO"
) || cd "$cwd"
fi
}
case "${1:-}" in
"-h" | "--help" | "help")
printf "%s\n" "$HELP"
exit 0
;;
"--gitlab")
shift
setup_gitlab "$@"
;;
"--github")
shift
setup_github "$@"
;;
*)
setup_github "$@"
;;
esac