-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathinit.sh
More file actions
executable file
·1463 lines (1249 loc) · 46.3 KB
/
Copy pathinit.sh
File metadata and controls
executable file
·1463 lines (1249 loc) · 46.3 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
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
##
# Adjust project repository based on user input.
#
# This script initializes a new project from the Scaffold template by:
# - Replacing placeholder values (namespace, project name, author)
# - Removing unwanted components (PHP, NodeJS, Shell, etc.)
# - Cleaning up template-specific files
# - Configuring selected features (release drafter, renovate, docs)
#
# The script can run interactively (with prompts) or non-interactively (with
# options). Passing any option switches to non-interactive mode: prompts are
# skipped, unspecified choices use their defaults, and --namespace, --name and
# --author are required.
#
# @usage:
# Interactive prompt:
# ./init.sh
#
# Interactive one-liner (downloads Scaffold, then prompts):
# curl -fsSL https://getscaffold.dev/init.sh | bash
#
# Non-interactive:
# ./init.sh --namespace=AcmeApp --name=acme-app --author="Jane Doe"
#
# Non-interactive one-liner (for AI agents and automation):
# curl -fsSL https://getscaffold.dev/init.sh | \
# bash -s -- --namespace=AcmeApp --name=acme-app --author="Jane Doe"
#
# When run without the template present (for example piped from curl in an
# empty directory), the script downloads the Scaffold into the current
# directory and then initializes it. The directory must be empty. Piping with
# no options prompts interactively; pass options to run unattended.
#
# Run "./init.sh --help" for the full list of options.
#
# shellcheck disable=SC2162,SC2015
set -euo pipefail
[ "${SCRIPT_DEBUG-}" = "1" ] && set -x
# Scaffold version. Rewritten to the release tag when the script is published
# for download from getscaffold.dev; stays "dev" in a plain checkout.
SCAFFOLD_VERSION="dev"
# Identity values. Populated from options or interactive prompts.
namespace=""
project=""
author=""
project_pascalcase=""
# Feature selections. An empty value means "not set on the command line"; it is
# resolved later from an interactive prompt or a non-interactive default.
use_php=""
use_php_command=""
php_command_name=""
use_php_command_build=""
use_php_script=""
php_script_name=""
use_nodejs=""
use_shell=""
shell_command_name=""
use_docker=""
docker_image_name=""
use_release_drafter=""
use_pr_autoassign=""
use_funding=""
use_pr_template=""
use_renovate=""
use_docs=""
use_test_actions=""
use_schedule=""
use_ai=""
use_ai_arch_docs=""
remove_self=""
# Ref (tag, branch, or commit) to bootstrap the template from when the script is
# run without the template present. Empty means "use the latest release".
archive_ref=""
# Whether to run interactively. Disabled as soon as any option is passed.
interactive=1
#-------------------------------------------------------------------------------
# STRING UTILITIES
#-------------------------------------------------------------------------------
##
# Convert a string to various naming conventions.
#
# @param $1 string Input string to convert
# @param $2 string Conversion type
#
# Conversion types:
# file_name : lowercase with underscores (my_file_name)
# route_path : lowercase with underscores (my_route_path)
# deployment_id : lowercase with underscores (my_deployment_id)
# domain_name : lowercase, underscores, no hyphens (mydomain_name)
# package_namespace: lowercase, underscores, no hyphens (mypackage_namespace)
# namespace : PascalCase, no spaces/hyphens (MyNamespace)
# class_name : PascalCase, no spaces/hyphens (MyClassName)
# package_name : lowercase with hyphens (my-package-name)
# function_name : lowercase with underscores (my_function_name)
# ui_id : lowercase with underscores (my_ui_id)
# cli_command : lowercase with underscores (my_cli_command)
# log_entry : unchanged (My Log Entry)
# code_comment_title: unchanged (My Comment Title)
#
# @return string Converted string
#
convert_string() {
local input_string="${1}"
local 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 "Error: Invalid conversion type '${conversion_type}'" >&2
return 1
;;
esac
}
to_lowercase() {
echo "${1}" | tr '[:upper:]' '[:lower:]'
}
to_pascalcase() { echo "${1}" | awk -F'[-_ ]' '{for(i=1;i<=NF;i++) $i=toupper(substr($i,1,1)) substr($i,2); } 1' OFS=''; }
#-------------------------------------------------------------------------------
# FILE OPERATION FUNCTIONS
#-------------------------------------------------------------------------------
##
# List the files containing a needle, restricted to template-owned paths.
#
# '.claude' holds both template-owned and consumer-owned files, so it is
# excluded from the recursive search and its shipped paths are searched
# explicitly.
#
# @param $1 string Needle (string to find)
# @param $2 string Path relative to the project root to search instead of the
# whole tree (optional)
#
sweep_files() {
local needle="${1}"
local target="${2:-}"
if [ -n "${target}" ]; then
grep -rIl "${needle}" "$(pwd)/${target}" 2>/dev/null || true
return 0
fi
grep -rIl --exclude-dir=".git" --exclude-dir=".idea" --exclude-dir=".claude" --exclude-dir="vendor" --exclude-dir="node_modules" "${needle}" "$(pwd)" || true
grep -rIl "${needle}" "$(pwd)/.claude/settings.json" "$(pwd)/.claude/skills/update-architecture-docs" 2>/dev/null || true
}
##
# Replace all occurrences of a string in files.
#
# @param $1 string Needle (string to find)
# @param $2 string Replacement (string to replace with)
#
replace_string_content() {
local needle="${1}"
local replacement="${2}"
local sed_opts
sed_opts=(-i) && [ "$(uname)" = "Darwin" ] && sed_opts=(-i '')
sweep_files "${needle}" | xargs sed "${sed_opts[@]}" "s!${needle}!${replacement}!g" || true
}
remove_string_content() {
local token="${1}"
local sed_opts
sed_opts=(-i) && [ "$(uname)" = "Darwin" ] && sed_opts=(-i '')
sweep_files "${token}" | 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 '')
sweep_files "${token}" "${target}" | LC_ALL=C.UTF-8 xargs sed "${sed_opts[@]}" -e "/${token}/d" || true
}
##
# Drop a workflow's suppression entries from the zizmor configuration.
#
# 'zizmor.yml' is absent when Actions linting is not selected, and grepping a
# missing file errors.
#
# @param $1 string Workflow file name
#
remove_zizmor_entry() {
[ -f zizmor.yml ] || return 0
remove_string_content_line "${1}" "zizmor.yml"
}
##
# Remove tokens and their enclosed content from files.
# Tokens use format: #;< TOKEN ... #;> TOKEN
#
# @param $1 string Token name (without #;< or #;> prefixes)
#
# Example:
# #;< PHP
# Some PHP-specific content
# #;> PHP
# This entire block will be removed
#
remove_tokens_with_content() {
local token="${1}"
local sed_opts
sed_opts=(-i) && [ "$(uname)" = "Darwin" ] && sed_opts=(-i '')
sweep_files "#;> $token" | 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 '')
sweep_files "${token}" | LC_ALL=C.UTF-8 xargs sed "${sed_opts[@]}" -e "/${token}/d" || true
}
#-------------------------------------------------------------------------------
# USER INTERACTION FUNCTIONS
#-------------------------------------------------------------------------------
##
# Prompt user for input with optional default value.
#
# @param $1 string Prompt text
# @param $2 string Default value (optional)
# @return string User input or default value
#
ask() {
local label="${1}"
local prompt="${1}"
local default="${2-}"
local result=""
if [ -n "${default}" ]; then
prompt="${prompt} [${default}]: "
else
prompt="${prompt}: "
fi
while [ -z "${result}" ]; do
if ! read -p "${prompt}" result; then
# Stdin reached EOF with no value. This happens when the script is piped
# (e.g. 'curl ... | bash') without options, so there is nothing to read.
if [ -n "${default}" ]; then
result="${default}"
break
fi
echo "Error: No input available for '${label}'. Pass options (run with --help) or run interactively." >&2
exit 1
fi
if [ -n "${default}" ] && [ -z "${result}" ]; then
result="${default}"
fi
done
echo "${result}"
}
##
# Prompt user for yes/no answer.
#
# @param $1 string Prompt text
# @param $2 string Default value (Y or N, default: Y)
# @return string 'y' or 'n'
#
ask_yesno() {
local prompt="${1}"
local default="${2:-Y}"
local result
read -p "${prompt} [$([ "${default}" = "Y" ] && echo "Y/n" || echo "y/N")]: " result
result="$(to_lowercase "${result:-${default}}")"
echo "${result}"
}
##
# Prompt user to choose one of the allowed values.
#
# @param $1 string Prompt text
# @param $2 string Default value
# @param $3 string Space-separated allowed values
# @return string Chosen value
#
ask_choice() {
local label="${1}"
local default="${2}"
local options="${3}"
local choices
local result
choices="$(echo "${options}" | tr ' ' '/')"
while :; do
if ! read -p "${label} (${choices}) [${default}]: " result; then
# Stdin reached EOF with no value. This happens when the script is piped
# (e.g. 'curl ... | bash') without options, so there is nothing to read.
if [ -n "${default}" ]; then
result="${default}"
break
fi
echo "Error: No input available for '${label}'. Pass options (run with --help) or run interactively." >&2
exit 1
fi
result="$(to_lowercase "${result:-${default}}")"
[[ ${result} != *" "* ]] && [[ " ${options} " == *" ${result} "* ]] && break
done
echo "${result}"
}
#-------------------------------------------------------------------------------
# INPUT VALIDATION FUNCTIONS
#-------------------------------------------------------------------------------
check_dependencies() {
local missing=() required=("sed" "grep" "tr" "awk" "curl")
for cmd in "${required[@]}"; do command -v "${cmd}" >/dev/null 2>&1 || missing+=("${cmd}"); done
[ ${#missing[@]} -gt 0 ] && echo "Error: Missing required commands: ${missing[*]}" >&2 && return 1
return 0
}
validate_namespace() {
[[ ${1} =~ ^[A-Z][a-zA-Z0-9]*$ ]] || {
echo "Error: Namespace must be PascalCase (e.g., MyNamespace)" >&2
return 1
}
}
validate_project_name() {
[[ ${1} =~ ^[a-z0-9-]+$ ]] && [[ ! ${1} =~ ^- ]] && [[ ! ${1} =~ -$ ]] || {
echo "Error: Project name must be lowercase with hyphens (e.g., my-project)" >&2
return 1
}
}
validate_author() {
[ -n "${1}" ] || {
echo "Error: Author name cannot be empty" >&2
return 1
}
}
validate_php_mode() {
if [ "${use_php_command}" = "y" ] && [ "${use_php_script}" = "y" ]; then
echo "Error: --php-command and --php-script cannot be used together." >&2
return 1
fi
}
#-------------------------------------------------------------------------------
# COMPONENT REMOVAL FUNCTIONS
#-------------------------------------------------------------------------------
remove_php() {
remove_php_command
remove_php_command_build
remove_php_script
remove_php_classes
remove_php_library
remove_php_entrypoint
remove_php_release
rm -f composer.json || true
rm -f composer.lock || true
rm -Rf vendor || true
rm -Rf tests/phpunit || true
rm -f phpcs.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 "\/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
remove_tokens_with_content "PHP"
# Remove "composer" from renovate matchManagers and php language rule.
if [ -f renovate.json ]; then
local sed_opts
sed_opts=(-i) && [ "$(uname)" = "Darwin" ] && sed_opts=(-i '')
sed "${sed_opts[@]}" 's/\["npm", "composer"\]/["npm"]/' renovate.json
sed "${sed_opts[@]}" 's/\["composer"\]/[]/' renovate.json
local line
line=$(grep -n '"matchDepNames": \["php"\]' renovate.json | cut -d: -f1)
if [ -n "${line}" ]; then
local start=$((line - 1))
local end=$((line + 3))
sed "${sed_opts[@]}" "${start},${end}d" renovate.json
fi
fi
}
remove_php_command() {
rm -f php-command || true
rm -Rf src/Command || true
rm -f tests/phpunit/Functional/ApplicationFunctionalTestCase.php || true
rm -f tests/phpunit/Functional/JokeCommandTest.php || true
rm -f 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 -f box.json || true
rm -f docs/content/ci/php-packaging.mdx || true
remove_tokens_with_content "PHP_PHAR"
}
##
# Remove the release workflow, which exists only to attach an asset to a tag.
#
remove_php_release() {
rm -f .github/workflows/release-php.yml || true
remove_zizmor_entry "release-php.yml"
remove_tokens_with_content "PHP_RELEASE"
}
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/Functional/ScriptFunctionalTestCase.php || true
rm -f tests/phpunit/Functional/ExampleScriptFunctionalTest.php || true
remove_tokens_with_content "PHP_SCRIPT"
remove_string_content_line '"php-script"'
replace_string_content '"'"${new_name}"'",' '"'"${new_name}"'"'
}
##
# Remove the class source directory and the tooling references to it.
#
remove_php_classes() {
rm -Rf src || true
remove_tokens_with_content "!PHP_SCRIPT"
}
##
# Remove the class-library example and notes from a project with an entry point.
#
remove_php_library() {
rm -f src/Example.php || true
rm -f tests/phpunit/Unit/ExampleUnitTest.php || true
remove_tokens_with_content "PHP_LIBRARY"
}
##
# Remove the declaration and the documentation of an executable entry point.
#
remove_php_entrypoint() {
remove_tokens_with_content "!PHP_LIBRARY"
[ -f composer.json ] || return 0
local sed_opts
sed_opts=(-i) && [ "$(uname)" = "Darwin" ] && sed_opts=(-i '')
# An empty "bin" array passes 'composer validate' but fails the
# 'composer normalize' check the shipped workflow runs. Both ends of the
# range are anchored so an unexpected layout removes nothing instead of
# swallowing the keys that follow.
sed "${sed_opts[@]}" -e '/^ *"bin": \[$/,/^ *\],$/d' composer.json
}
remove_nodejs() {
rm -f package.json || true
rm -f package-lock.json || true
rm -f yarn.lock || true
rm -Rf node_modules || true
rm -f nodejs-script || true
rm -f eslint.config.js || true
rm -f .c8rc.json || true
rm -Rf tests/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_zizmor_entry "release-nodejs.yml"
remove_tokens_with_content "NODEJS"
# Remove "npm" from renovate matchManagers and node/yarn language rule.
if [ -f renovate.json ]; then
local sed_opts
sed_opts=(-i) && [ "$(uname)" = "Darwin" ] && sed_opts=(-i '')
sed "${sed_opts[@]}" 's/\["npm", "composer"\]/["composer"]/' renovate.json
sed "${sed_opts[@]}" 's/\["npm"\]/[]/' renovate.json
local line
line=$(grep -n '"matchDepNames": \["node", "yarn"\]' renovate.json | cut -d: -f1)
if [ -n "${line}" ]; then
local start=$((line - 1))
local end=$((line + 3))
sed "${sed_opts[@]}" "${start},${end}d" renovate.json
fi
fi
}
remove_shell() {
rm -f shell-command.sh || true
rm -Rf tests/bats || true
rm -f .github/workflows/test-shell.yml || true
remove_tokens_with_content "SHELL"
}
remove_docker() {
rm -f Dockerfile || true
rm -f entrypoint.sh || true
rm -f .github/workflows/test-docker.yml || true
rm -f .github/workflows/release-docker.yml || true
remove_tokens_with_content "DOCKER"
}
remove_release_drafter() {
rm -f .github/workflows/draft-release-notes.yml || true
rm -f .github/release-drafter.yml || true
remove_tokens_with_content "RELEASEDRAFTER"
}
remove_pr_autoassign() {
rm -f .github/workflows/assign-author.yml || 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
remove_tokens_with_content "RENOVATE"
}
remove_renovate_managers() {
if [ -f renovate.json ] && grep -q '"matchManagers": \[\]' renovate.json; then
local sed_opts
sed_opts=(-i) && [ "$(uname)" = "Darwin" ] && sed_opts=(-i '')
local line
line=$(grep -n '"matchManagers": \[\]' renovate.json | cut -d: -f1)
if [ -n "${line}" ]; then
local start=$((line - 1))
local end=$((line + 3))
sed "${sed_opts[@]}" "${start},${end}d" renovate.json
fi
fi
}
remove_ai() {
rm -f CLAUDE.md || true
rm -f AGENTS.md || true
rm -Rf .claude || true
remove_tokens_with_content "AI"
}
remove_ai_arch_docs() {
rm -Rf .claude/skills/update-architecture-docs || true
rm -Rf docs/content/architecture || true
# Prefix match also removes AI_ARCH_DOCS_MERMAID and AI_ARCH_DOCS_PLANTUML.
remove_tokens_with_content "AI_ARCH_DOCS"
}
remove_docs() {
if [ -d docs/content/architecture ]; then
# Keep the AI architecture docs: relocate them from the site content
# tree to docs/architecture and rewrite path references to match.
# 'mv' would nest into a pre-existing destination directory.
rm -Rf .architecture-preserve-tmp || true
mv docs/content/architecture .architecture-preserve-tmp
rm -Rf docs || true
mkdir -p docs
mv .architecture-preserve-tmp docs/architecture
replace_string_content "docs/content/architecture" "docs/architecture"
else
rm -Rf docs || true
remove_string_content_line "\/docs" ".gitattributes"
fi
rm -f .github/workflows/test-docs.yml || true
rm -f .github/workflows/release-docs.yml || true
}
remove_test_actions() {
rm -f .github/workflows/test-actions.yml || true
rm -f .github/.yamllint-for-gha.yml || true
rm -f zizmor.yml || true
}
remove_schedule() {
remove_tokens_with_content "SCHEDULE"
}
#-------------------------------------------------------------------------------
# PROCESSING FUNCTIONS
#-------------------------------------------------------------------------------
##
# Reduce the PHP stack to a class library with no entry point.
#
process_php_library() {
remove_php_command
remove_php_command_build
remove_php_script
remove_php_entrypoint
remove_php_release
}
##
# Keep only the selected diagram format in the AI architecture docs.
#
process_ai_arch_docs() {
case "${use_ai_arch_docs:-none}" in
mermaid) remove_tokens_with_content "AI_ARCH_DOCS_PLANTUML" ;;
plantuml) remove_tokens_with_content "AI_ARCH_DOCS_MERMAID" ;;
none) ;;
esac
}
##
# Trim Claude Code permission rules to match the selected features.
#
# The template ships '.claude/settings.json' with the full command allow-list.
# Rules for features that were not selected are removed so that a generated
# project only pre-approves commands it can actually run.
#
process_claude_settings() {
local file=".claude/settings.json"
[ -f "${file}" ] || return 0
local sed_opts
sed_opts=(-i) && [ "$(uname)" = "Darwin" ] && sed_opts=(-i '')
[ "${use_php:-n}" != "y" ] && sed "${sed_opts[@]}" -e '/composer:/d' -e '/phpcs:/d' -e '/phpcbf:/d' -e '/phpstan:/d' -e '/rector:/d' -e '/phpunit:/d' "${file}"
[ "${use_shell:-n}" != "y" ] && sed "${sed_opts[@]}" -e '/bats:/d' "${file}"
[ "${use_docker:-n}" != "y" ] && sed "${sed_opts[@]}" -e '/docker build:/d' -e '/docker run:/d' "${file}"
[ "${use_ai_arch_docs:-none}" != "plantuml" ] && sed "${sed_opts[@]}" -e '/plantuml:/d' "${file}"
# 'npm' is shared by the NodeJS feature and the documentation site, so keep it
# while either is present.
if [ "${use_nodejs:-n}" != "y" ] && [ "${use_docs:-n}" != "y" ]; then
sed "${sed_opts[@]}" -e '/npm:/d' "${file}"
fi
# Drop a trailing comma left on the final array element after any removals.
awk 'NR>1{if($0~/^ *]/)sub(/,$/,"",prev);print prev}{prev=$0}END{if(NR>0)print prev}' "${file}" >"${file}.tmp" && mv "${file}.tmp" "${file}"
}
process_readme() {
local project="${1}"
mv README.dist.md "README.md" >/dev/null 2>&1 || true
# Update placeholder image URL and alt text in README.md
replace_string_content 'text=Yourproject' "text=${project// /+}"
replace_string_content 'alt="Yourproject logo"' "alt=\"${project} logo\""
curl "https://placehold.jp/000000/ffffff/200x200.png?text=${project// /+}&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 -f logo.tmp.png || true
}
##
# Promote the distributable CONTRIBUTING guide to its final name.
#
# Scaffold keeps its own CONTRIBUTING.md; CONTRIBUTING.dist.md is the guide that
# ships to generated projects, mirroring the README.dist.md handling.
#
process_contributing() {
mv CONTRIBUTING.dist.md "CONTRIBUTING.md" >/dev/null 2>&1 || true
}
##
# Replace the self-update skill references with placeholder tokens.
#
# The initialised project must keep instructions that point at the upstream
# template repository rather than at the project's own namespace. These
# references are hidden behind tokens before the bulk replacements run and
# put back afterwards by restore_skill_references().
#
protect_skill_references() {
replace_string_content "https://raw.githubusercontent.com/AlexSkrypnyk/scaffold/main/.scaffold/skills/update-consumer-scaffold/SKILL.md" "__SCAFFOLD_SKILL_URL__"
replace_string_content "update-consumer-scaffold" "__SCAFFOLD_SKILL_NAME__"
replace_string_content "update scaffold" "__SCAFFOLD_SKILL_TRIGGER__"
}
##
# Restore the self-update skill references hidden by protect_skill_references().
#
restore_skill_references() {
replace_string_content "__SCAFFOLD_SKILL_URL__" "https://raw.githubusercontent.com/AlexSkrypnyk/scaffold/main/.scaffold/skills/update-consumer-scaffold/SKILL.md"
replace_string_content "__SCAFFOLD_SKILL_NAME__" "update-consumer-scaffold"
replace_string_content "__SCAFFOLD_SKILL_TRIGGER__" "update scaffold"
}
process_internal() {
local namespace="${1}"
local project="${2}"
local author="${3}"
local project_pascalcase="${4}"
local namespace_lowercase
namespace_lowercase="$(to_lowercase "${namespace}")"
rm -f SECURITY.md || true
rm -Rf ".scaffold" || true
rm -f ".github/workflows/scaffold-test.yml" || true
rm -f ".github/workflows/scaffold-release-docs.yml" || true
remove_zizmor_entry "scaffold-release-docs.yml"
protect_skill_references
# Replace the real repository name with the placeholder token used in the
# replacements below.
replace_string_content "AlexSkrypnyk/scaffold" "yournamespace/yourproject"
replace_string_content "YourNamespace" "${namespace}"
replace_string_content "yournamespace" "${namespace_lowercase}"
replace_string_content "yourproject" "${project}"
replace_string_content "Yourproject" "${project_pascalcase}"
replace_string_content "Your Name" "${author}"
replace_string_content "Alex Skrypnyk" "${author}"
remove_string_content "Generic project scaffold template"
restore_skill_references
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" "\/logo.png"
uncomment_line ".gitattributes" "\/tests"
uncomment_line ".gitattributes" "\/phpcs.xml"
uncomment_line ".gitattributes" "\/phpstan.neon"
uncomment_line ".gitattributes" "\/phpunit.xml"
uncomment_line ".gitattributes" "\/rector.php"
uncomment_line ".gitattributes" "\/.npmignore"
uncomment_line ".gitattributes" "\/renovate.json"
remove_tokens_with_content "META"
remove_special_comments
}
#-------------------------------------------------------------------------------
# ARGUMENT PARSING
#-------------------------------------------------------------------------------
usage() {
cat <<'USAGE'
Usage: ./init.sh [OPTIONS]
Initialise a new project from the Scaffold template.
With no options the script runs interactively and prompts for every choice.
Passing any option switches to non-interactive mode: prompts are skipped,
unspecified choices use their defaults, and --namespace, --name and --author
are required.
One-liner (interactive - downloads Scaffold, then prompts):
curl -fsSL https://getscaffold.dev/init.sh | bash
One-liner (non-interactive):
curl -fsSL https://getscaffold.dev/init.sh | \
bash -s -- --namespace=AcmeApp --name=acme-app --author="Jane Doe"
When run without the template present (e.g. piped from curl in an empty
directory), the script downloads the Scaffold into the current directory first,
then initialises it. The directory must be empty. Piping with no options prompts
interactively. Set SCAFFOLD_ARCHIVE_URL to bootstrap from a specific archive URL.
Identity (required in non-interactive mode):
--namespace=VALUE Project namespace in PascalCase (e.g. AcmeApp).
--name=VALUE Project name in kebab-case (e.g. acme-app).
--author=VALUE Author name (e.g. "Jane Doe").
Features (enabled by default unless noted; use --no-<name> to disable):
--php, --no-php PHP support.
--php-command Use the Symfony CLI command app (default).
--no-php-command Do not use the Symfony CLI command app.
--php-script Use a single-file script instead of the app.
--no-php-script Do not use a single-file script. Combined with
--no-php-command, scaffolds a class library
with the PHP tooling and no entry point.
--php-command-name=VALUE CLI command file name (default: project name).
--php-script-name=VALUE Script file name (default: project name).
--phar, --no-phar Build a PHAR (default: on).
--nodejs, --no-nodejs NodeJS support.
--shell, --no-shell Shell script support.
--shell-command-name=VALUE Shell script name (default: project name).
--docker, --no-docker Docker support (default: off).
--docker-image-name=VALUE Docker image name (default: namespace/project).
--release-drafter, --no-release-drafter GitHub Release Drafter.
--pr-autoassign, --no-pr-autoassign GitHub PR author auto-assign.
--funding, --no-funding GitHub funding configuration.
--pr-template, --no-pr-template GitHub pull request template.
--renovate, --no-renovate Renovate configuration.
--docs, --no-docs Documentation site.
--test-actions, --no-test-actions GitHub Actions linting (default: off).
--schedule, --no-schedule Daily scheduled build (default: on).
--ai, --no-ai AI agents configuration (default: on).
--ai-arch-docs[=VALUE] AI architecture docs: mermaid, plantuml or
none (default: mermaid).
--no-ai-arch-docs Disable AI architecture docs.
--keep Keep this init script (default: removed).
Other:
--ref=VALUE Bootstrap from a tag, branch or commit when the
template is absent (default: latest release).
--yes, -y Run non-interactively using defaults.
--help, -h Show this help and exit.
--version Print the version ("dev" unless released).
USAGE
}
##
# Parse command-line options.
#
# Any recognised option disables interactive mode. Unknown options and the
# --help flag terminate the script.
#
parse_args() {
if [ "$#" -gt 0 ]; then
interactive=0
fi
while [ "$#" -gt 0 ]; do
case "${1}" in
--namespace=*) namespace="${1#*=}" ;;
--name=*) project="${1#*=}" ;;
--author=*) author="${1#*=}" ;;
--php) use_php="y" ;;
--no-php) use_php="n" ;;
--php-command) use_php_command="y" ;;
--no-php-command) use_php_command="n" ;;
--php-script) use_php_script="y" ;;
--no-php-script) use_php_script="n" ;;
--php-command-name=*)
php_command_name="${1#*=}"
use_php_command="y"
;;
--php-script-name=*)
php_script_name="${1#*=}"
use_php_script="y"
;;
--phar) use_php_command_build="y" ;;
--no-phar) use_php_command_build="n" ;;
--nodejs) use_nodejs="y" ;;
--no-nodejs) use_nodejs="n" ;;
--shell) use_shell="y" ;;
--no-shell) use_shell="n" ;;
--shell-command-name=*)
shell_command_name="${1#*=}"
use_shell="y"
;;
--docker) use_docker="y" ;;
--no-docker) use_docker="n" ;;
--docker-image-name=*)
docker_image_name="${1#*=}"
use_docker="y"
;;
--release-drafter) use_release_drafter="y" ;;
--no-release-drafter) use_release_drafter="n" ;;
--pr-autoassign) use_pr_autoassign="y" ;;
--no-pr-autoassign) use_pr_autoassign="n" ;;
--funding) use_funding="y" ;;
--no-funding) use_funding="n" ;;
--pr-template) use_pr_template="y" ;;
--no-pr-template) use_pr_template="n" ;;
--renovate) use_renovate="y" ;;
--no-renovate) use_renovate="n" ;;
--docs) use_docs="y" ;;
--no-docs) use_docs="n" ;;
--test-actions) use_test_actions="y" ;;
--no-test-actions) use_test_actions="n" ;;
--schedule) use_schedule="y" ;;
--no-schedule) use_schedule="n" ;;
--ai) use_ai="y" ;;
--no-ai) use_ai="n" ;;
--ai-arch-docs) use_ai_arch_docs="mermaid" ;;
--ai-arch-docs=*)
use_ai_arch_docs="${1#*=}"
case "${use_ai_arch_docs}" in
mermaid | plantuml | none) ;;
*)
echo "Error: Invalid value for --ai-arch-docs: ${use_ai_arch_docs}. Allowed values: mermaid, plantuml, none." >&2
echo "Run with --help for usage." >&2
exit 1
;;
esac
;;
--no-ai-arch-docs) use_ai_arch_docs="none" ;;
--keep) remove_self="n" ;;
--ref=*) archive_ref="${1#*=}" ;;
--yes | -y) interactive=0 ;;
--help | -h)
usage
exit 0
;;
--version)
echo "${SCAFFOLD_VERSION}"
exit 0
;;
*)
echo "Error: Unknown option: ${1}" >&2
echo "Run with --help for usage." >&2
exit 1
;;
esac
shift
done
validate_php_mode || exit 1
}
#-------------------------------------------------------------------------------
# INPUT COLLECTION
#-------------------------------------------------------------------------------
require_identity() {
local missing=()
[ -z "${namespace}" ] && missing+=("--namespace")
[ -z "${project}" ] && missing+=("--name")
[ -z "${author}" ] && missing+=("--author")
if [ "${#missing[@]}" -gt 0 ]; then
echo "Error: Missing required option(s) in non-interactive mode: ${missing[*]}" >&2
echo "Run with --help for usage." >&2