|
4 | 4 |
|
5 | 5 | script_name=$(basename "$0")
|
6 | 6 |
|
7 |
| -ask_question(){ |
| 7 | +ask_question() { |
8 | 8 | # ask_question <question> <default>
|
9 | 9 | local ANSWER
|
10 | 10 | read -r -p "$1 ($2): " ANSWER
|
11 | 11 | echo "${ANSWER:-$2}"
|
12 | 12 | }
|
13 | 13 |
|
14 |
| -confirm(){ |
| 14 | +confirm() { |
15 | 15 | # confirm <question> (default = N)
|
16 | 16 | local ANSWER
|
17 | 17 | read -r -p "$1 (y/N): " -n 1 ANSWER
|
18 | 18 | echo " "
|
19 | 19 | [[ "$ANSWER" =~ ^[Yy]$ ]]
|
20 | 20 | }
|
21 | 21 |
|
| 22 | +slugify() { |
| 23 | + # slugify <input> <separator> |
| 24 | + # Jack, Jill & Clémence LTD => jack-jill-clemence-ltd |
| 25 | + # inspiration: https://github.com/pforret/bashew/blob/master/template/normal.sh |
| 26 | + separator="$2" |
| 27 | + [[ -z "$separator" ]] && separator="-" |
| 28 | + # shellcheck disable=SC2020 |
| 29 | + echo "$1" | |
| 30 | + tr '[:upper:]' '[:lower:]' | |
| 31 | + tr 'àáâäæãåāçćčèéêëēėęîïííīįìłñńôöòóœøōõßśšûüùúūÿžźż' 'aaaaaaaaccceeeeeeeiiiiiiilnnoooooooosssuuuuuyzzz' | |
| 32 | + awk '{ |
| 33 | + gsub(/[\[\]@#$%^&*;,.:()<>!?\/+=_]/," ",$0); |
| 34 | + gsub(/^ */,"",$0); |
| 35 | + gsub(/ *$/,"",$0); |
| 36 | + gsub(/ */,"-",$0); |
| 37 | + gsub(/[^a-z0-9\-]/,""); |
| 38 | + print; |
| 39 | + }' | |
| 40 | + sed "s/-/$separator/g" |
| 41 | +} |
| 42 | + |
| 43 | +titlecase() { |
| 44 | + # titlecase <input> <separator> |
| 45 | + # Jack, Jill & Clémence LTD => JackJillClemenceLtd |
| 46 | + separator="${2:-}" |
| 47 | + echo "$1" | |
| 48 | + tr '[:upper:]' '[:lower:]' | |
| 49 | + tr 'àáâäæãåāçćčèéêëēėęîïííīįìłñńôöòóœøōõßśšûüùúūÿžźż' 'aaaaaaaaccceeeeeeeiiiiiiilnnoooooooosssuuuuuyzzz' | |
| 50 | + awk '{ gsub(/[\[\]@#$%^&*;,.:()<>!?\/+=_-]/," ",$0); print $0; }' | |
| 51 | + awk '{ |
| 52 | + for (i=1; i<=NF; ++i) { |
| 53 | + $i = toupper(substr($i,1,1)) tolower(substr($i,2)) |
| 54 | + }; |
| 55 | + print $0; |
| 56 | + }' | |
| 57 | + sed "s/ /$separator/g" |
| 58 | +} |
| 59 | + |
22 | 60 | git_name=$(git config user.name)
|
23 | 61 | author_name=$(ask_question "Author name" "$git_name")
|
24 | 62 |
|
25 | 63 | git_email=$(git config user.email)
|
26 | 64 | author_email=$(ask_question "Author email" "$git_email")
|
27 | 65 |
|
28 |
| -homepage_temp=$(echo $author_email | awk -F '@' '{print $NF}') |
29 |
| -homepage_guess=${homepage_temp//[[:blank:]]/} |
30 |
| -author_homepage=$(ask_question "Author homepage" "$homepage_guess") |
31 |
| - |
32 |
| -username_guess=${author_name//[[:blank:]]/} |
| 66 | +username_guess=$(git config remote.origin.url | cut -d: -f2-) |
| 67 | +username_guess=$(dirname "$username_guess") |
| 68 | +username_guess=$(basename "$username_guess") |
33 | 69 | author_username=$(ask_question "Author username" "$username_guess")
|
34 | 70 |
|
| 71 | +vendor_name=$(ask_question "Vendor name" "$author_username") |
| 72 | +vendor_slug=$(slugify "$vendor_name") |
| 73 | +VendorName=$(titlecase "$vendor_name" "") |
| 74 | + |
35 | 75 | current_directory=$(pwd)
|
36 | 76 | folder_name=$(basename "$current_directory")
|
37 | 77 |
|
38 |
| -vendor_name_unsanitized=$(ask_question "Vendor name" "$author_name") |
39 | 78 | package_name=$(ask_question "Package name" "$folder_name")
|
40 |
| -package_description=$(ask_question "Package description" "$package_name") |
| 79 | +package_slug=$(slugify "$package_name" "_") |
41 | 80 |
|
42 |
| -# convert my-class-title to MyClassTitle - RODO: use to subsctitute ./src/* |
43 |
| -class_name=$(echo "$package_name" | sed 's/[-_]/ /g' | awk '{for(j=1;j<=NF;j++){ $j=toupper(substr($j,1,1)) substr($j,2) }}1' | sed 's/[[:space:]]//g') |
| 81 | +ClassName=$(titlecase "$package_name") |
| 82 | +ClassName=$(ask_question "Class Name" "$ClassName") |
| 83 | +package_description=$(ask_question "Package description" "This is my package $ClassName") |
44 | 84 |
|
45 |
| -vendor_name_pascal_case=`echo "$vendor_name_unsanitized" | sed -r 's/(^|-)(\w)/\U\2/g'` |
46 |
| -vendor_name_lower_case=`echo "$vendor_name_unsanitized" | tr '[:upper:]' '[:lower:]'` |
47 |
| -vendor_name="$(tr '[:lower:]' '[:upper:]' <<< ${vendor_name_lower_case:0:1})${vendor_name_lower_case:1}" |
| 85 | +echo -e "------" |
| 86 | +echo -e "Author : $author_name ($author_username, $author_email)" |
| 87 | +echo -e "Vendor : $vendor_name ($vendor_slug)" |
| 88 | +echo -e "Package : $package_slug <$package_description>" |
| 89 | +echo -e "Namespace : $VendorName\\$ClassName" |
| 90 | +echo -e "ClassName : $ClassName" |
| 91 | +echo -e "------" |
48 | 92 |
|
49 |
| -echo |
50 |
| -echo -e "Author: $author_name ($author_username, $author_email) - $author_homepage" |
51 |
| -echo -e "Package: $package_name <$package_description>" |
| 93 | +files= $(grep -E -r -l -i ":author|:vendor|:package|:short|spatie|[email protected]|vendor_slug|skeleton|Skeleton" --exclude-dir=vendor ./* ./.github/* | grep -v "$script_name") |
52 | 94 |
|
53 |
| -echo |
54 |
| -echo "This script will replace the above values in all files in the project directory." |
55 |
| -if ! confirm "Modify files?" ; then |
| 95 | +echo "This script will replace the above values in all relevant files in the project directory." |
| 96 | + |
| 97 | +if ! confirm "Modify files?"; then |
56 | 98 | $safe_exit 1
|
57 | 99 | fi
|
58 | 100 |
|
59 |
| -echo |
60 |
| -files=$(grep -E -r -l -i ":author|:package|spatie|skeleton|:vendor_name" --exclude-dir=vendor ./* ./.github/* | grep -v "$script_name") |
61 |
| - |
62 |
| -for file in $files ; do |
63 |
| - echo "Customising file $file" |
64 |
| - temp_file="$file.temp" |
65 |
| - < "$file" \ |
66 |
| - sed "s/:author_name/$author_name/g" \ |
67 |
| - | sed "s/:author_username/$author_username/g" \ |
68 |
| - | sed "s/:author_email/$author_email/g" \ |
69 |
| - | sed "s/:author_homepage/$author_homepage/g" \ |
70 |
| - | sed "s/:package_name/$package_name/g" \ |
71 |
| - | sed "s/Spatie/$vendor_name_pascal_case/g" \ |
72 |
| - | sed "s/Skeleton/$class_name/g" \ |
73 |
| - | sed "s/:vendor_name/$vendor_name_lower_case/g" \ |
74 |
| - | sed "s/:package_description/$package_description/g" \ |
75 |
| - | sed "/^\*\*Note:\*\* Run/d" \ |
76 |
| - > "$temp_file" |
77 |
| - rm -f "$file" |
78 |
| - new_file=`echo $file | sed -e "s/Skeleton/${class_name}/g"` |
79 |
| - mv "$temp_file" "$new_file" |
| 101 | +files_regex= ":author|:vendor|:package|:short|spatie|[email protected]|vendor_slug|skeleton|Skeleton" |
| 102 | +grep -E -r -l -i "$files_regex" --exclude-dir=vendor ./* ./.github/* \ |
| 103 | +| grep -v "$script_name" \ |
| 104 | +| while read -r file ; do |
| 105 | + new_file="$file" |
| 106 | + new_file="${new_file//Skeleton/$ClassName}" |
| 107 | + new_file="${new_file//skeleton/$package_slug}" |
| 108 | + new_file="${new_file//laravel_/}" |
| 109 | + echo "adapting file $file -> $new_file" |
| 110 | + temp_file="$file.temp" |
| 111 | + < "$file" \ |
| 112 | + sed "s/:author_name/$author_name/g" \ |
| 113 | + | sed "s/:author_username/$author_username/g" \ |
| 114 | + | sed "s/[email protected]/$author_email/g" \ |
| 115 | + | sed "s/:vendor_name/$vendor_name/g" \ |
| 116 | + | sed "s/vendor_slug/$vendor_slug/g" \ |
| 117 | + | sed "s/VendorName/$VendorName/g" \ |
| 118 | + | sed "s/:package_name/$package_name/g" \ |
| 119 | + | sed "s/package_slug/$package_slug/g" \ |
| 120 | + | sed "s/skeleton/$package_slug/g" \ |
| 121 | + | sed "s/Skeleton/$ClassName/g" \ |
| 122 | + | sed "s/:package_description/$package_description/g" \ |
| 123 | + | sed "/^\[\]\(delete\) /d" \ |
| 124 | + > "$temp_file" |
| 125 | + rm -f "$file" |
| 126 | + mv "$temp_file" "$new_file" |
80 | 127 | done
|
81 | 128 |
|
82 |
| -echo "Replaced all values and Renamed the files" |
83 |
| - |
84 |
| -if confirm "Execute composer install and phpunit test" ; then |
| 129 | +if confirm "Execute composer install and phpunit test"; then |
85 | 130 | composer install && ./vendor/bin/phpunit
|
86 | 131 | fi
|
87 | 132 |
|
88 |
| -if confirm 'Let this script delete itself (since you only need it once)?' ; then |
| 133 | +if confirm 'Let this script delete itself (since you only need it once)?'; then |
89 | 134 | echo "Delete $0 !"
|
90 |
| - rm -- "$0" |
| 135 | + sleep 1 && rm -- "$0" |
91 | 136 | fi
|
0 commit comments