-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·143 lines (119 loc) · 4.8 KB
/
Copy pathsetup.sh
File metadata and controls
executable file
·143 lines (119 loc) · 4.8 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
#!/bin/bash
# 'return' when run as "source <script>" or ". <script>", 'exit' otherwise
[[ "$0" != "${BASH_SOURCE[0]}" ]] && safe_exit="return" || safe_exit="exit"
script_name=$(basename "$0")
ask_question() {
# ask_question <question> <default>
local ANSWER
read -r -p "$1 ($2): " ANSWER
echo "${ANSWER:-$2}"
}
confirm() {
# confirm <question> (default = N)
local ANSWER
read -r -p "$1 (y/N): " -n 1 ANSWER
echo " "
[[ "$ANSWER" =~ ^[Yy]$ ]]
}
slugify() {
# slugify <input> <separator>
# Jack, Jill & Clémence LTD => jack-jill-clemence-ltd
# inspiration: https://github.com/pforret/bashew/blob/master/template/normal.sh
separator="$2"
[[ -z "$separator" ]] && separator="-"
# shellcheck disable=SC2020
echo "$1" |
tr '[:upper:]' '[:lower:]' |
tr 'àáâäæãåāçćčèéêëēėęîïííīįìłñńôöòóœøōõßśšûüùúūÿžźż' 'aaaaaaaaccceeeeeeeiiiiiiilnnoooooooosssuuuuuyzzz' |
awk '{
gsub(/[\[\]@#$%^&*;,.:()<>!?\/+=_]/," ",$0);
gsub(/^ */,"",$0);
gsub(/ *$/,"",$0);
gsub(/ */,"-",$0);
gsub(/[^a-z0-9\-]/,"");
print;
}' |
sed "s/-/$separator/g"
}
titlecase() {
# titlecase <input> <separator>
# Jack, Jill & Clémence LTD => JackJillClemenceLtd
separator="${2:-}"
echo "$1" |
tr '[:upper:]' '[:lower:]' |
tr 'àáâäæãåāçćčèéêëēėęîïííīįìłñńôöòóœøōõßśšûüùúūÿžźż' 'aaaaaaaaccceeeeeeeiiiiiiilnnoooooooosssuuuuuyzzz' |
awk '{ gsub(/[\[\]@#$%^&*;,.:()<>!?\/+=_-]/," ",$0); print $0; }' |
awk '{
for (i=1; i<=NF; ++i) {
$i = toupper(substr($i,1,1)) tolower(substr($i,2))
};
print $0;
}' |
sed "s/ /$separator/g"
}
git_name=$(git config user.name)
author_name=$(ask_question "Author name" "$git_name")
git_email=$(git config user.email)
author_email=$(ask_question "Author email" "$git_email")
username_guess=$(git config remote.origin.url | cut -d: -f2-)
username_guess=$(dirname "$username_guess")
username_guess=$(basename "$username_guess")
author_username=$(ask_question "Author username" "$username_guess")
vendor_name=$(ask_question "Vendor name" "$author_username")
vendor_slug=$(slugify "$vendor_name")
vendor_url=$(ask_question "Vendor URL" "https://$vendor_slug.com")
current_directory=$(pwd)
folder_name=$(basename "$current_directory")
package_name=$(ask_question "Package name" "$folder_name")
package_slug=$(slugify "$package_name" "-")
package_url_guess="https://$package_slug.com"
package_description=$(ask_question "Package description" "The $package_url_guess website")
echo -e "------"
echo -e "Author : $author_name ($author_username, $author_email)"
echo -e "Vendor : $vendor_name ($vendor_slug, $vendor_url)"
echo -e "Package : $package_slug <$package_description>"
echo -e "------"
echo "This script will replace the above values in all relevant files in the project directory."
if ! confirm "Modify files?"; then
$safe_exit 1
fi
if [[ -d "public/themes/wp-starter" && "$package_slug" != "wp-starter" ]]; then
mv "public/themes/wp-starter" "public/themes/$package_slug"
fi
grep -E -r -l -i ":author|:vendor|:package|vendor_name|vendor_slug|package_slug|[email protected]" --exclude-dir=vendor --exclude-dir=node_modules ./* ./.??* \
| grep -v "$script_name" \
| while read -r file ; do
new_file="$file"
echo "adapting file $file -> $new_file"
temp_file="$file.temp"
< "$file" \
sed "s#:author_name#$author_name#g" \
| sed "s#:author_username#$author_username#g" \
| sed "s#[email protected]#$author_email#g" \
| sed "s#:author_url#$vendor_url#g" \
| sed "s#:vendor_name#$vendor_name#g" \
| sed "s#vendor_slug#$vendor_slug#g" \
| sed "s#:package_name#$package_name#g" \
| sed "s#package_slug#$package_slug#g" \
| sed "s#wp-starter#$package_slug#g" \
| sed "s#:package_description#$package_description#g" \
| sed "#^\[\]\(delete\) #d" \
> "$temp_file"
rm -f "$file"
mv "$temp_file" "$new_file"
done
if confirm "Execute composer install"; then
composer install
fi
if confirm "Install WordPress"; then
site_url=$(ask_question "Site URL" "http://$package_slug.com")
site_title=$(ask_question "Site title" "$package_name")
admin_user=$(ask_question "Admin user" "admin")
admin_email=$(ask_question "Admin email" "admin@$package_slug.com")
admin_password=$(ask_question "Admin password" "secret")
wp core install --url="$site_url" --title="$site_title" --admin_user="$admin_user" --admin_email="$admin_email" --admin_password="$admin_password"
fi
if confirm 'Let this script delete itself (since you only need it once)?'; then
echo "Delete $0 !"
sleep 1 && rm -- "$0"
fi