-
Notifications
You must be signed in to change notification settings - Fork 1
/
init.sh
executable file
·451 lines (373 loc) · 14.3 KB
/
init.sh
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
#!/usr/bin/env bash
##
# Adjust project repository based on user input.
#
# @usage:
# Interactive prompt:
# ./init.sh
#
# Silent:
# ./init.sh yournamespace yourproject "Your Name"
#
# shellcheck disable=SC2162,SC2015
set -euo pipefail
[ "${SCRIPT_DEBUG-}" = "1" ] && set -x
namespace=${1-}
project=${2-}
author=${3-}
#-------------------------------------------------------------------------------
convert_string() {
input_string="$1"
conversion_type="$2"
case "${conversion_type}" in
"file_name" | "route_path" | "deployment_id")
echo "${input_string}" | tr ' ' '_' | tr '[:upper:]' '[:lower:]'
;;
"domain_name" | "package_namespace")
echo "${input_string}" | tr ' ' '_' | tr '[:upper:]' '[:lower:]' | tr -d '-'
;;
"namespace" | "class_name")
echo "${input_string}" | awk -F" " '{for(i=1;i<=NF;i++) $i=toupper(substr($i,1,1)) substr($i,2); } 1' | tr -d ' -'
;;
"package_name")
echo "${input_string}" | tr ' ' '-' | tr '[:upper:]' '[:lower:]'
;;
"function_name" | "ui_id" | "cli_command")
echo "${input_string}" | tr ' ' '_' | tr '[:upper:]' '[:lower:]'
;;
"log_entry" | "code_comment_title")
echo "${input_string}"
;;
*)
echo "Invalid conversion type"
;;
esac
}
replace_string_content() {
local needle="${1}"
local replacement="${2}"
local sed_opts
sed_opts=(-i) && [ "$(uname)" = "Darwin" ] && sed_opts=(-i '')
set +e
grep -rI --exclude-dir=".git" --exclude-dir=".idea" --exclude-dir="vendor" --exclude-dir="node_modules" -l "${needle}" "$(pwd)" | xargs sed "${sed_opts[@]}" "s!$needle!$replacement!g" || true
set -e
}
to_lowercase() {
echo "${1}" | tr '[:upper:]' '[:lower:]'
}
remove_string_content() {
local token="${1}"
local sed_opts
sed_opts=(-i) && [ "$(uname)" == "Darwin" ] && sed_opts=(-i '')
grep -rI --exclude-dir=".git" --exclude-dir=".idea" --exclude-dir="vendor" --exclude-dir="node_modules" -l "${token}" "$(pwd)" | LC_ALL=C.UTF-8 xargs sed "${sed_opts[@]}" -e "/^${token}/d" || true
}
remove_string_content_line() {
local token="${1}"
local target="${2:-.}"
local sed_opts
sed_opts=(-i) && [ "$(uname)" == "Darwin" ] && sed_opts=(-i '')
grep -rI --exclude-dir=".git" --exclude-dir=".idea" --exclude-dir="vendor" --exclude-dir="node_modules" -l "${token}" "$(pwd)/${target}" | LC_ALL=C.UTF-8 xargs sed "${sed_opts[@]}" -e "/${token}/d" || true
}
remove_tokens_with_content() {
local token="${1}"
local sed_opts
sed_opts=(-i) && [ "$(uname)" == "Darwin" ] && sed_opts=(-i '')
grep -rI --include=".*" --include="*" --exclude-dir=".git" --exclude-dir=".idea" --exclude-dir="vendor" --exclude-dir="node_modules" -l "#;> $token" "$(pwd)" | LC_ALL=C.UTF-8 xargs sed "${sed_opts[@]}" -e "/#;< $token/,/#;> $token/d" || true
}
uncomment_line() {
local file_name="${1}"
local start_string="${2}"
local sed_opts
sed_opts=(-i) && [ "$(uname)" == "Darwin" ] && sed_opts=(-i '')
LC_ALL=C.UTF-8 sed "${sed_opts[@]}" -e "s/^# ${start_string}/${start_string}/" "${file_name}"
}
remove_special_comments() {
local token="#;"
local sed_opts
sed_opts=(-i) && [ "$(uname)" == "Darwin" ] && sed_opts=(-i '')
grep -rI --exclude-dir=".git" --exclude-dir=".idea" --exclude-dir="vendor" --exclude-dir="node_modules" -l "${token}" "$(pwd)" | LC_ALL=C.UTF-8 xargs sed "${sed_opts[@]}" -e "/${token}/d" || true
}
ask() {
local prompt="$1"
local default="${2-}"
local result=""
if [[ -n $default ]]; then
prompt="${prompt} [${default}]: "
else
prompt="${prompt}: "
fi
while [[ -z ${result} ]]; do
read -p "${prompt}" result
if [[ -n $default && -z ${result} ]]; then
result="${default}"
fi
done
echo "${result}"
}
ask_yesno() {
local prompt="${1}"
local default="${2:-Y}"
local result
read -p "${prompt} [$([ "${default}" = "Y" ] && echo "Y/n" || echo "y/N")]: " result
result="$(echo "${result:-${default}}" | tr '[:upper:]' '[:lower:]')"
echo "${result}"
}
#-------------------------------------------------------------------------------
remove_php() {
remove_php_command
remove_php_command_build
remove_php_script
rm -f composer.json >/dev/null || true
rm -f composer.lock >/dev/null || true
rm -Rf vendor >/dev/null || true
rm -Rf tests/phpunit || true
rm -f phpcs.xml || true
rm -f phpmd.xml || true
rm -f phpstan.neon || true
rm -f phpunit.xml || true
rm -f rector.php || true
rm -Rf docs/php || true
remove_string_content_line "\/tests" ".gitattributes"
remove_string_content_line "\/phpcs.xml" ".gitattributes"
remove_string_content_line "\/phpmd.xml" ".gitattributes"
remove_string_content_line "\/phpstan.neon" ".gitattributes"
remove_string_content_line "\/phpunit.xml" ".gitattributes"
remove_string_content_line "\/rector.php" ".gitattributes"
rm -f .github/workflows/test-php.yml || true
rm -f .github/workflows/release-php.yml || true
remove_tokens_with_content "PHP"
}
remove_php_command() {
rm -Rf php-command || true
rm -Rf src || true
rm -Rf tests/phpunit/Functional/ApplicationFunctionalTestCase.php || true
rm -Rf tests/phpunit/Functional/JokeCommandTest.php || true
rm -Rf tests/phpunit/Functional/SayHelloCommandTest.php || true
rm -f docs/content/php/php-command.mdx || true
remove_tokens_with_content "PHP_COMMAND"
remove_string_content_line '"php-command"'
remove_string_content_line '"php-command",'
}
remove_php_command_build() {
rm -Rf box.json || true
rm -Rf docs/content/ci/php-packaging.mdx || true
remove_tokens_with_content "PHP_PHAR"
}
remove_php_script() {
local new_name="${1:-php-script}"
rm -f php-script || true
rm -f tests/phpunit/Unit/ExampleScriptUnitTest.php || true
rm -f tests/phpunit/Unit/ScriptUnitTestCase.php || true
rm -f tests/phpunit/Unit/ExampleScriptUnitTest.php || true
rm -f tests/phpunit/Functional/ScriptFunctionalTestCase.php || true
rm -f tests/phpunit/Functional/ExampleScriptFunctionalTest.php || true
rm -f docs/content/php/php-script.mdx || true
remove_tokens_with_content "!PHP_COMMAND"
remove_tokens_with_content "!PHP_PHAR"
replace_string_content 'cp php-script php-script.php && phpcs; rm php-script.php' 'phpcs'
replace_string_content 'cp php-script php-script.php && phpcbf; rm php-script.php' 'phpcbf'
remove_string_content_line '"php-script"'
replace_string_content '"'"${new_name}"'",' '"'"${new_name}"'"'
}
remove_nodejs() {
rm -f package.json >/dev/null || true
rm -f package.lock >/dev/null || true
rm -f yarn.lock >/dev/null || true
rm -Rf node_modules >/dev/null || true
rm -Rf docs/nodejs || true
remove_string_content_line "\/.npmignore" ".gitattributes"
rm -f .github/workflows/test-nodejs.yml || true
rm -f .github/workflows/release-nodejs.yml || true
remove_tokens_with_content "NODEJS"
}
remove_shell() {
rm -f shell-command.sh >/dev/null || true
rm -Rf tests/bats || true
rm -Rf docs/shell || true
rm -f .github/workflows/test-shell.yml || true
remove_tokens_with_content "SHELL"
}
remove_release_drafter() {
rm -f .github/workflows/draft-release-notes.yml || true
rm -f .github/release-drafter.yml
remove_tokens_with_content "RELEASEDRAFTER"
rm -Rf docs/content/ci/release-drafter.mdx || true
}
remove_pr_autoassign() {
rm -f .github/workflows/assign-author.yml || true
rm -Rf docs/content/ci/auto-assign-pr.mdx || true
}
remove_funding() {
rm -f .github/FUNDING.yml || true
}
remove_pr_template() {
rm -f .github/PULL_REQUEST_TEMPLATE.md || true
}
remove_renovate() {
rm -f renovate.json || true
rm -Rf docs/content/ci/renovate.mdx || true
remove_tokens_with_content "RENOVATE"
}
remove_docs() {
rm -Rf docs || true
rm -f .github/workflows/test-docs.yml || true
rm -f .github/workflows/release-docs.yml || true
remove_string_content_line "\/docs" ".gitattributes"
}
process_readme() {
mv README.dist.md "README.md" >/dev/null 2>&1 || true
curl "https://placehold.jp/000000/ffffff/200x200.png?text=${1// /+}&css=%7B%22border-radius%22%3A%22%20100px%22%7D" >logo.tmp.png || true
if [ -s "logo.tmp.png" ]; then
mv logo.tmp.png "logo.png" >/dev/null 2>&1 || true
fi
rm logo.tmp.png >/dev/null 2>&1 || true
}
process_internal() {
local namespace="${1}"
local project="${2}"
local author="${3}"
local namespace_lowercase
namespace_lowercase="$(to_lowercase "${namespace}")"
replace_string_content "YourNamespace" "${namespace}"
replace_string_content "AlexSkrypnyk" "${namespace}"
replace_string_content "yournamespace" "${namespace_lowercase}"
replace_string_content "alexskrypnyk" "${namespace_lowercase}"
replace_string_content "yourproject" "${project}"
replace_string_content "Your Name" "${author}"
replace_string_content "Alex Skrypnyk" "${author}"
remove_string_content "Generic project scaffold template"
replace_string_content "https://getscaffold.dev/ project scaffold template" "__ATTRIBUTION__"
replace_string_content "Scaffold" "${project}"
replace_string_content "scaffold" "${project}"
replace_string_content "__ATTRIBUTION__" "https://getscaffold.dev/ project scaffold template"
remove_string_content "# Uncomment the lines below"
uncomment_line ".gitattributes" "\/.editorconfig"
uncomment_line ".gitattributes" "\/.gitattributes"
uncomment_line ".gitattributes" "\/.github"
uncomment_line ".gitattributes" "\/.gitignore"
uncomment_line ".gitattributes" "\/docs"
uncomment_line ".gitattributes" "\/tests"
uncomment_line ".gitattributes" "\/phpcs.xml"
uncomment_line ".gitattributes" "\/phpmd.xml"
uncomment_line ".gitattributes" "\/phpstan.neon"
uncomment_line ".gitattributes" "\/phpunit.xml"
uncomment_line ".gitattributes" "\/rector.php"
uncomment_line ".gitattributes" "\/.npmignore"
rm -f LICENSE >/dev/null || true
rm -Rf "tests/scaffold" >/dev/null || true
rm -f ".github/workflows/test-scaffold.yml" >/dev/null || true
rm -f "docs/static/img/init.gif" >/dev/null || true
remove_tokens_with_content "META"
remove_special_comments
}
#-------------------------------------------------------------------------------
main() {
echo "Please follow the prompts to adjust your project configuration"
echo
[ -z "${namespace}" ] && namespace="$(ask "Namespace (PascalCase)")"
[ -z "${project}" ] && project="$(ask "Project")"
[ -z "${author}" ] && author="$(ask "Author")"
# Make sure the input become valid value.
project="$(convert_string "${project}" "package_name")"
namespace="$(convert_string "${namespace}" "namespace")"
use_php="$(ask_yesno "Use PHP")"
use_php_command="y"
use_php_command_build="y"
use_php_script="n"
php_command_name="<unset>"
if [ "${use_php}" = "y" ]; then
use_php_command="$(ask_yesno " Use CLI command app")"
if [ "${use_php_command}" = "y" ]; then
php_command_name=$(ask " CLI command name" "${project}")
use_php_command_build="$(ask_yesno " Build PHAR")"
else
use_php_script="$(ask_yesno " Use simple script")"
php_command_name=$(ask " CLI command name" "${project}")
fi
fi
use_nodejs="$(ask_yesno "Use NodeJS")"
use_shell="$(ask_yesno "Use Shell")"
if [ "${use_shell}" = "y" ]; then
shell_command_name=$(ask " CLI command name" "${project}")
fi
use_release_drafter="$(ask_yesno "Use GitHub release drafter")"
use_pr_autoassign="$(ask_yesno "Use GitHub PR author auto-assign")"
use_funding="$(ask_yesno "Use GitHub funding")"
use_pr_template="$(ask_yesno "Use GitHub PR template")"
use_renovate="$(ask_yesno "Use Renovate")"
use_docs="$(ask_yesno "Use docs")"
remove_self="$(ask_yesno "Remove this script")"
echo
echo " Summary"
echo "---------------------------------"
echo "Namespace : ${namespace}"
echo "Project : ${project}"
echo "Author : ${author}"
echo "Use PHP : ${use_php}"
echo " Use CLI command app : ${use_php_command}"
echo " CLI command name : ${php_command_name}"
echo " Build PHAR : ${use_php_command_build}"
echo " Use simple script : ${use_php_script}"
echo "Use NodeJS : ${use_nodejs}"
echo "Use Shell : ${use_shell}"
echo "Use GitHub release drafter : ${use_release_drafter}"
echo "Use GitHub PR author auto-assign : ${use_pr_autoassign}"
echo "Use GitHub funding : ${use_funding}"
echo "Use GitHub PR template : ${use_pr_template}"
echo "Use Renovate : ${use_renovate}"
echo "Use Docs : ${use_docs}"
echo "Remove this script : ${remove_self}"
echo "---------------------------------"
echo
should_proceed="$(ask_yesno "Proceed with project init")"
if [ "${should_proceed}" != "y" ]; then
echo
echo "Aborting."
exit 1
fi
#
# Processing.
#
: "${namespace:?Namespace is required}"
: "${project:?Project is required}"
: "${author:?Author is required}"
if [ "${use_php}" = "y" ]; then
if [ "${use_php_command}" = "y" ]; then
[ "${use_php_command_build:-n}" != "y" ] && remove_php_command_build
replace_string_content "php-command" "${php_command_name}"
mv "php-command" "${php_command_name}" >/dev/null 2>&1 || true
else
remove_php_command "${php_command_name}"
remove_php_command_build
fi
if [ "${use_php_script:-n}" = "y" ]; then
replace_string_content "php-script" "${php_command_name}"
mv "php-script" "${php_command_name}" >/dev/null 2>&1 || true
else
remove_php_script "${php_command_name}"
fi
else
remove_php
fi
[ "${use_nodejs}" != "y" ] && remove_nodejs
if [ "${use_shell}" = "y" ]; then
replace_string_content "shell-command.sh" "${shell_command_name}.sh"
mv "shell-command.sh" "${shell_command_name}.sh" >/dev/null 2>&1 || true
else
remove_shell
fi
[ "${use_release_drafter}" != "y" ] && remove_release_drafter
[ "${use_pr_autoassign}" != "y" ] && remove_pr_autoassign
[ "${use_funding}" != "y" ] && remove_funding
[ "${use_pr_template}" != "y" ] && remove_pr_template
[ "${use_renovate}" != "y" ] && remove_renovate
[ "${use_docs}" != "y" ] && remove_docs
process_readme "${project}"
process_internal "${namespace}" "${project}" "${author}"
[ "${remove_self}" != "n" ] && rm -- "$0" || true
echo
echo "Initialization complete."
}
if [ "$0" = "${BASH_SOURCE[0]}" ]; then
main "$@"
fi