-
Notifications
You must be signed in to change notification settings - Fork 2
/
skel.sh
393 lines (316 loc) · 11.3 KB
/
skel.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
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
#!/bin/bash
set -e
# Export those variables in your .bashrc to ease the deployment of new packages
#
# export VENDOR_NAME=pragmarx
# export PACKAGE_AUTHOR_NAME=Antonio Carlos Ribeiro
# export PACKAGE_AUTHOR_EMAIL=acr@antoniocarlosribeiro.com
# export PACKAGE_AUTHOR_WEBSITE=https://antoniocarlosribeiro.com
# export PACKAGE_AUTHOR_USERNAME=antonioribeiro
# export PACKAGE_DESCRIPTION=
#
# export SKELETON_VENDOR_NAME=pragmarx
# export SKELETON_VENDOR_NAME_CAPITAL=PragmaRX
# export SKELETON_PACKAGE_NAME=skeleton
# export SKELETON_VENDOR_NAME_CAPITAL=Skeleton
# export SKELETON_PACKAGE_REPOSITORY=https://github.com/antonioribeiro/skel.git
# export SKELETON_VCS_USER=antonioribeiro
# export SKELETON_VCS_SERVICE=github.com
# export SKELETON_PACKAGE_BRANCH=league
function main()
{
clear
displayAppName
generateDefaultData
askForData
createPackage
}
function generateDefaultData()
{
if [[ "$SKELETON_VCS_SERVICE" == "" ]]; then
SKELETON_VCS_SERVICE=github.com
fi
if [[ "$SKELETON_VENDOR_NAME" == "" ]]; then
SKELETON_VENDOR_NAME=:vendor_name
fi
if [[ "$SKELETON_VENDOR_NAME_CAPITAL" == "" ]]; then
SKELETON_VENDOR_NAME_CAPITAL=:VendorName
fi
if [[ "$SKELETON_PACKAGE_NAME" == "" ]]; then
SKELETON_PACKAGE_NAME=:package_name
fi
if [[ "$SKELETON_PACKAGE_NAME_CAPITAL" == "" ]]; then
SKELETON_PACKAGE_NAME_CAPITAL=:PackageName
fi
if [[ "$SKELETON_PACKAGE_REPOSITORY" == "" ]]; then
SKELETON_PACKAGE_REPOSITORY=https://github.com/antonioribeiro/skel.git
fi
if [[ "$SKELETON_PACKAGE_BRANCH" == "" ]]; then
SKELETON_PACKAGE_BRANCH=league
fi
}
function searchAndReplace()
{
find "$1" -name "*" -exec sed -i "s/$2/$3/g" {} \; &> /dev/null
}
function createPackage()
{
message Creating package "$VENDOR_NAME/$PACKAGE_NAME"
message From repository: "$SKELETON_REPOSITORY" branch "$SKELETON_PACKAGE_BRANCH"
git clone --branch "$SKELETON_PACKAGE_BRANCH" "$SKELETON_REPOSITORY" "$DESTINATION_FOLDER"
searchAndReplace "$DESTINATION_FOLDER" "$SKELETON_PACKAGE_NAME" "$PACKAGE_NAME"
searchAndReplace "$DESTINATION_FOLDER" "$SKELETON_PACKAGE_NAME_CAPITAL" "$PACKAGE_NAME_CAPITAL"
searchAndReplace "$DESTINATION_FOLDER" "$SKELETON_VENDOR_NAME" "$VENDOR_NAME"
searchAndReplace "$DESTINATION_FOLDER" "$SKELETON_VENDOR_NAME_CAPITAL" "$VENDOR_NAME_CAPITAL"
searchAndReplace "$DESTINATION_FOLDER" "$SKELETON_AUTHOR_NAME" "$PACKAGE_AUTHOR_NAME"
searchAndReplace "$DESTINATION_FOLDER" "$SKELETON_AUTHOR_EMAIL" "$PACKAGE_AUTHOR_EMAIL"
searchAndReplace "$DESTINATION_FOLDER" "$SKELETON_AUTHOR_WEBSITE" "$PACKAGE_AUTHOR_WEBSITE"
searchAndReplace "$DESTINATION_FOLDER" "$SKELETON_AUTHOR_USERNAME" "$PACKAGE_AUTHOR_USERNAME"
searchAndReplace "$DESTINATION_FOLDER" "$SKELETON_DESCRIPTION" "$PACKAGE_DESCRIPTION"
renameAll "$DESTINATION_FOLDER" "$SKELETON_PACKAGE_NAME" "$PACKAGE_NAME"
renameAll "$DESTINATION_FOLDER" "$SKELETON_PACKAGE_NAME_CAPITAL" "$PACKAGE_NAME_CAPITAL"
renameAll "$DESTINATION_FOLDER" "$SKELETON_VENDOR_NAME" "$VENDOR_NAME"
renameAll "$DESTINATION_FOLDER" "$SKELETON_VENDOR_NAME_CAPITAL" "$VENDOR_NAME_CAPITAL"
mv "$DESTINATION_FOLDER/src/PackageNameClass.php" "$DESTINATION_FOLDER/src/$PACKAGE_NAME_CAPITAL.php"
mv "$DESTINATION_FOLDER/src/PackageNameServiceProvider.php" "$DESTINATION_FOLDER/src/${PACKAGE_NAME_CAPITAL}ServiceProvider.php"
rm -rf "$DESTINATION_FOLDER/.git/"
cd "$DESTINATION_FOLDER"
git init
git add -A
git commit -m "first commit"
if [[ "$PACKAGE_REPOSITORY" != "" ]]; then
git remote add origin "$PACKAGE_REPOSITORY"
git push origin master
displayInstructions
fi
}
function askForData()
{
DESTINATION_FOLDER=$(pwd)/
inquireText "Package destination folder:" "$DESTINATION_FOLDER"
DESTINATION_FOLDER=$answer
if [[ -d "$DESTINATION_FOLDER" ]]; then
message
message "Destination folder already exists. Aborting..."
message
exit 1
fi
message
VENDOR_NAME=$(echo "$VENDOR_NAME" | awk '{print tolower($0)}')
inquireText "Vendor name for your new package (lowercase):" "$VENDOR_NAME"
VENDOR_NAME=$answer
VENDOR_NAME_CAPITAL=${VENDOR_NAME^}
inquireText "Vendor name for your new package (Capitalized):" "$VENDOR_NAME_CAPITAL"
VENDOR_NAME_CAPITAL=$answer
message
if [[ "$SKELETON_VCS_USER" != "" ]]; then
VCS_USER=$SKELETON_VCS_USER
else
VCS_USER=$VENDOR_NAME
fi
inquireText "Your VCS username:" "$VCS_USER"
VCS_USER=$answer
message
PACKAGE_NAME=$(echo "$(basename "$DESTINATION_FOLDER")" | awk '{print tolower($0)}')
inquireText "Your new package name (lowercase):" "$PACKAGE_NAME"
PACKAGE_NAME=$answer
PACKAGE_NAME_CAPITAL=${PACKAGE_NAME^}
inquireText "Your new package name (Capitalized):" "$PACKAGE_NAME_CAPITAL"
PACKAGE_NAME_CAPITAL=$answer
if [[ "$PACKAGE_AUTHOR_NAME" == "" ]]; then
PACKAGE_AUTHOR_NAME=$VENDOR_NAME_CAPITAL
fi
inquireText "Author name:" "$VENDOR_NAME_CAPITAL"
PACKAGE_AUTHOR_NAME=$answer
inquireText "Author email:" "$PACKAGE_AUTHOR_EMAIL"
PACKAGE_AUTHOR_EMAIL=$answer
inquireText "Author website:" "$PACKAGE_AUTHOR_WEBSITE"
PACKAGE_AUTHOR_WEBSITE=$answer
inquireText "Author username:" "$VCS_USER"
PACKAGE_AUTHOR_USERNAME=$answer
inquireText "Description:" "$PACKAGE_DESCRIPTION"
PACKAGE_DESCRIPTION=$answer
PACKAGE_REPOSITORY=https://$SKELETON_VCS_SERVICE/$VCS_USER/$PACKAGE_NAME.git
inquireText "Your new package repository link (create a package first or leave it blank):" "$PACKAGE_REPOSITORY"
PACKAGE_REPOSITORY=$answer
message
message --- Skeleton replacements
message
SKELETON_VENDOR_NAME=$(echo "$SKELETON_VENDOR_NAME" | awk '{print tolower($0)}')
inquireText "Skeleton Vendor name (lowercase):" "$SKELETON_VENDOR_NAME"
SKELETON_VENDOR_NAME=$answer
if [[ "$SKELETON_VENDOR_NAME" != ":vendor_name" ]]; then
SKELETON_VENDOR_NAME_CAPITAL=$SKELETON_VENDOR_NAME
fi
SKELETON_VENDOR_NAME_CAPITAL=${SKELETON_VENDOR_NAME_CAPITAL^}
inquireText "Skeleton Vendor name (Capitalized):" "$SKELETON_VENDOR_NAME_CAPITAL"
SKELETON_VENDOR_NAME_CAPITAL=$answer
message
SKELETON_PACKAGE_NAME=$(echo "$(basename "$SKELETON_PACKAGE_NAME")" | awk '{print tolower($0)}')
inquireText "Skeleton package name (lowercase):" "$SKELETON_PACKAGE_NAME"
SKELETON_PACKAGE_NAME=$answer
if [[ "$SKELETON_PACKAGE_NAME_CAPITAL" != ":PackageName" ]]; then
SKELETON_PACKAGE_NAME_CAPITAL=$SKELETON_PACKAGE_NAME
fi
SKELETON_PACKAGE_NAME_CAPITAL=${SKELETON_PACKAGE_NAME_CAPITAL^}
inquireText "Skeleton package name (Capitalized):" "$SKELETON_PACKAGE_NAME_CAPITAL"
SKELETON_PACKAGE_NAME_CAPITAL=$answer
inquireText "Skeleton author name:" :author_name
SKELETON_AUTHOR_NAME=$answer
inquireText "Skeleton author email:" :author_email
SKELETON_AUTHOR_EMAIL=$answer
inquireText "Skeleton author website:" :author_website
SKELETON_AUTHOR_WEBSITE=$answer
inquireText "Skeleton author username:" :author_username
SKELETON_AUTHOR_USERNAME=$answer
inquireText "Skeleton package description:" :package_description
SKELETON_DESCRIPTION=$answer
if [[ "$SKELETON_PACKAGE_NAME_CAPITAL" == ":PackageName" ]]; then
selectSkeletonRepository
SKELETON_REPOSITORY=$repository
SKELETON_PACKAGE_BRANCH=$branch
else
SKELETON_REPOSITORY=https://$SKELETON_VCS_SERVICE/$SKELETON_VENDOR_NAME/$SKELETON_PACKAGE_NAME.git
fi
if [[ "$repository" == "" ]]; then
repo="$SKELETON_VENDOR_NAME/skel.git"
if [[ "$SKELETON_VENDOR_NAME" == ":vendor_name" ]]; then
repo=
fi
SKELETON_REPOSITORY=https://$SKELETON_VCS_SERVICE/$repo
inquireText "Skeleton repository link:" "$SKELETON_REPOSITORY"
SKELETON_REPOSITORY=$answer
inquireText "Skeleton branch:" master
SKELETON_PACKAGE_BRANCH=$answer
fi
}
function selectSkeletonRepository()
{
repository=https://github.com/antonioribeiro/skel.git
PS3='Please enter your choice: '
options=("League" "Laravel 5" "Laravel 4" "PragmRX" "Use your own skeleton")
select opt in "${options[@]}"
do
case $opt in
"League")
branch=league
break
;;
"Laravel 5")
branch=laravel5
break
;;
"Laravel 4")
branch=laravel4
break
;;
"PragmaRX")
branch=pragmarx
break
;;
"Use your own skeleton")
repository=
branch=
break
;;
*) echo invalid option;;
esac
done
}
function renameAll()
{
renameFiles "$1" "$2" "$3"
renameDirectories "$1" "$2" "$3"
}
function renameFiles()
{
FOLDER=$1
OLD=$2
NEW=$3
find "$FOLDER" -type f -print0 | while IFS= read -r -d $'\0' file; do
dir=$(dirname "$file")
filename=$(basename "$file")
new=$dir/$(echo "$filename" | sed -e "s/$OLD/$NEW/g")
if [[ "$file" != "$new" ]]; then
if [[ -f $file ]]; then
mv "$file" "$new"
fi
fi
done
}
function renameDirectories()
{
FOLDER=$1
OLD=$2
NEW=$3
find "$FOLDER" -type d -print0 | while IFS= read -r -d $'\0' file; do
new=$(echo "$file" | sed -e "s/$OLD/$NEW/g")
if [[ "$file" != "$new" ]]; then
if [[ -d $file ]]; then
mv "$file" "$new"
fi
fi
done
}
function inquireText()
{
answer=""
value=$2
if [[ "$value" == "Pragmarx" ]]; then
value=PragmaRX
fi
if [[ ${BASH_VERSION} > "3.9" ]]; then
read -e -p "$1 " -i "$value" answer
else
read -e -p "$1 [hit enter for $value] " answer
fi
}
function displayInstructions()
{
message
message "Congratulations, your new package has been created!"
message
message "------------------------------------------------------------------------------------------------"
message " How to use it"
message "------------------------------------------------------------------------------------------------"
message
message "Add the items below to your composer.json:"
message
message "\"require\": {"
message " \"$VENDOR_NAME/$PACKAGE_NAME\": \"dev-master\","
message "},"
message
message
message "and"
message
message
message "\"repositories\": ["
message " {"
message " \"type\": \"vcs\","
message " \"url\": \"$PACKAGE_REPOSITORY\""
message " },"
message "],"
message
message
message "If this is a Laravel project, don't forget to add a ServiceProvider in your app/config/app.php too, something like:"
message
message " '$VENDOR_NAME_CAPITAL\\$PACKAGE_NAME_CAPITAL\\ServiceProvider'",
message
message
}
function message()
{
if [[ "$1" != "" ]]; then
command="echo $@"
${command}
else
echo
fi
}
function displayAppName()
{
message "## Skel"
message
message
}
main "$@"