This repository has been archived by the owner on Jul 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathinit.sh
executable file
·135 lines (106 loc) · 3.22 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
#!/bin/bash
# Usage: ./block-scaffolding-wp/init.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, while "
echo "performing substitutions on the 'block-scaffolding' 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' )"
namespace="$( echo "$name" | sed 's/ //g' )"
repo="$slug"
echo
echo "The Organization name will be used as the Namespace prefix as-entered and "
echo "converted to lowercase for use in the repository 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 push the plugin to your GitHub repository? [Y/N]: "
read push
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 'block-scaffolding-wp'"
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 block-scaffolding.php "$slug.php"
git grep -lz "xwp" | xargs -0 sed -i '' -e "s/xwp/$org_lower/g"
git grep -lz "block-scaffolding-wp" | xargs -0 sed -i '' -e "s/block-scaffolding-wp/$repo/g"
git grep -lz "XWP" | xargs -0 sed -i '' -e "s/XWP/$org/g"
git grep -lz "Block Scaffolding" | xargs -0 sed -i '' -e "s/Block Scaffolding/$name/g"
git grep -lz "BlockScaffolding" | xargs -0 sed -i '' -e "s/BlockScaffolding/$namespace/g"
git grep -lz "block-scaffolding" | xargs -0 sed -i '' -e "s/block-scaffolding/$slug/g"
# Clean slate.
rm -rf .git
rm -rf node_modules
rm -rf vendor
rm -f init.sh
rm -f composer.lock
rm -f package-lock.json
# Install dependencies.
npm install
# Setup Git.
git init
git add .
git commit -m "Initial commit"
git remote add origin "[email protected]:$org_lower/$repo.git"
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
echo
echo "Plugin is located at:"
pwd