-
Notifications
You must be signed in to change notification settings - Fork 17
/
init-plugin.sh
executable file
·160 lines (129 loc) · 4.07 KB
/
init-plugin.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
#!/bin/bash
# Usage: ./wp-foo-bar/init-plugin.sh
# Check for valid plugin name.
function valid_name () {
valid="^[A-Z][A-Za-z0-9]*( [A-Z][A-Za-z0-9]*)*$"
if [[ ! "$1" =~ $valid ]]; then
return 1
fi
return 0
}
echo
echo "Hello, "$USER"."
echo
echo "This script will automatically generate a new plugin based on the scaffolding."
echo "The way it works is you enter a plugin name like 'Hello World' and the script "
echo "will create a directory 'hello-world' in the current working directory, or one "
echo "directory up if called from the plugin root, all while performing substitutions "
echo "on the 'wp-foo-bar' scaffolding plugin."
echo
echo -n "Enter your plugin name and press [ENTER]: "
read name
# Validate plugin name.
if ! valid_name "$name"; then
echo "Malformed name '$name'. Please use title case words separated by spaces. No hyphens. For example, 'Hello World'."
echo
echo -n "Enter a valid plugin name and press [ENTER]: "
read name
if ! valid_name "$name"; then
echo
echo "The name you entered is invalid, rage quitting."
exit 1
fi
fi
slug="$( echo "$name" | tr '[:upper:]' '[:lower:]' | sed 's/ /-/g' )"
prefix="$( echo "$name" | tr '[:upper:]' '[:lower:]' | sed 's/ /_/g' )"
namespace="$( echo "$name" | sed 's/ //g' )"
class="$( echo "$name" | sed 's/ /_/g' )"
repo="$slug"
echo
echo "The Organization name will be converted to lowercase for use in the repository "
echo "path (i.e. XWP becomes xwp)."
echo -n "Enter your GitHub organization name, and press [ENTER]: "
read org
org_lower="$( echo "$org" | tr '[:upper:]' '[:lower:]' )"
echo
echo -n "Do you want to prepend 'wp-' to your repository name? [Y/N]: "
read prepend
if [[ "$prepend" != Y ]] && [[ "$prepend" != y ]]; then
echo
echo -n "Do you want to append '-wp' to your repository name? [Y/N]: "
read append
if [[ "$append" == Y ]] || [[ "$append" == y ]]; then
repo="${slug}-wp"
fi
else
repo="wp-${slug}"
fi
echo
echo -n "Do you want to make the initial commit? [Y/N]: "
read commit
if [[ "$commit" == Y ]] || [[ "$commit" == y ]]; then
echo
echo -n "Do you want to push the plugin to your GitHub repository? [Y/N]: "
read push
fi
echo
echo -n "Do you want to install the dependencies in the new plugin? [Y/N]: "
read deps
echo
cwd="$(pwd)"
cd "$(dirname "$0")"
src_repo_path="$(pwd)"
cd "$cwd"
if [[ -e $( basename "$0" ) ]]; then
echo
echo "Moving up one directory outside of 'wp-foo-bar'"
cd ..
fi
if [[ -e "$slug" ]]; then
echo
echo "The $slug directory already exists"
exit 1
fi
echo
git clone "$src_repo_path" "$repo"
cd "$repo"
git mv foo-bar.php "$slug.php"
git mv tests/phpunit/class-test-foo-bar.php "tests/phpunit/class-test-$slug.php"
git grep -lz "xwp%2Fwp-foo-bar" | xargs -0 sed -i '' -e "s|xwp%2Fwp-foo-bar|$org_lower%2F$repo|g"
git grep -lz "xwp/wp-foo-bar" | xargs -0 sed -i '' -e "s|xwp/wp-foo-bar|$org_lower/$repo|g"
git grep -lz "wp-foo-bar" | xargs -0 sed -i '' -e "s/wp-foo-bar/$repo/g"
git grep -lz "Foo Bar" | xargs -0 sed -i '' -e "s/Foo Bar/$name/g"
git grep -lz "foo-bar" | xargs -0 sed -i '' -e "s/foo-bar/$slug/g"
git grep -lz "foo_bar" | xargs -0 sed -i '' -e "s/foo_bar/$prefix/g"
git grep -lz "FooBar" | xargs -0 sed -i '' -e "s/FooBar/$namespace/g"
git grep -lz "Foo_Bar" | xargs -0 sed -i '' -e "s/Foo_Bar/$class/g"
# Clean slate.
rm -rf .git
rm -rf node_modules
rm -rf vendor
rm -f init-plugin.sh
rm -f composer.lock
rm -f package-lock.json
# Setup Git.
git init
git add .
git remote add origin "[email protected]:$org_lower/$repo.git"
# Install dependencies.
if [[ "$deps" == Y ]] || [[ "$deps" == y ]]; then
npm install
fi
# Commit and push change.
if [[ "$commit" == Y ]] || [[ "$commit" == y ]]; then
git commit -m "Initial commit"
if [[ "$push" == Y ]] || [[ "$push" == y ]]; then
git push -u origin master
else
echo
echo "Push changes to GitHub with the following command:"
echo "cd $(pwd) && git push -u origin master"
fi
else
echo
echo "Commit and push changes to GitHub with the following command:"
echo "cd $(pwd) && git commit -m \"Initial commit\" && git push -u origin master"
fi
echo
echo "Plugin is located at:"
pwd