forked from unclechu/web-front-end-grunt-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-new-project.sh
executable file
·98 lines (80 loc) · 2.05 KB
/
create-new-project.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
#!/bin/bash
#
# Author: Viacheslav Lotsmanov
# License: GNU/GPLv3 (https://github.com/unclechu/web-front-end-grunt-template/blob/master/LICENSE)
#
REPO_URL="https://github.com/unclechu/grunt-project-templates/"
REPO_LOCAL_DIR="web-front-end-grunt-template"
REPO_BRANCH="master"
THIS_FILENAME="$(basename "$0")"
clr_info='\e[0;36m'
clr_ok='\e[0;32m'
clr_ask='\e[0;34m'
clr_err='\e[0;31m'
clr_end='\e[0m'
function ok {
echo -e "${clr_ok}[ OK ]${clr_end}"
return 0
}
function err {
echo -e "${clr_err}[ ERR ]${clr_end}" 1>&2
exit 1
}
function run {
echo -e "${clr_info}${@}${clr_end} ... "
"$@" && ok || err
return 0
}
function ask {
echo -en "${clr_ask}${@}${clr_end} [Y/n] "
read answer
if echo "$answer" | grep -i '^y\(es\)\?$' &>/dev/null; then
return 0
elif echo "$answer" | grep -i '^n\(o\)\?$' &>/dev/null; then
return 1
else
echo -e "${clr_err}Incorrect answer!${clr_end}" 1>&2
exit 1
fi
}
# get first element in array (by delimiter)
function get_delim_first {
echo "$1" | sed -e 's/^\([^:]\+\).*$/\1/'
}
function remove_delim_first {
item=$(echo "$1" | sed -e 's/^\([^:]\+\).*$/\1/')
echo "${1:$[${#item}+1]}"
}
if [ -z "$1" ]; then
exit $?
else
REPO_BRANCH="$1"
fi
run git init
run git clone -b "$REPO_BRANCH" "$REPO_URL" "$REPO_LOCAL_DIR"
arr=$(ls "./$REPO_LOCAL_DIR/" \
| grep -iv README \
| grep -iv node_modules \
| grep -iv LICENSE \
| grep -ivF "$THIS_FILENAME" \
| tr '\n' ':')
while true; do
[ -z "$arr" ] && break
item=$(get_delim_first "$arr")
arr=$(remove_delim_first "$arr")
if [ -d "./$REPO_LOCAL_DIR/$item" ]; then
run cp -R "./$REPO_LOCAL_DIR/$item/" ./
else
run cp "./$REPO_LOCAL_DIR/$item" ./
fi
done
run cp "./$REPO_LOCAL_DIR/.gitignore" ./
run npm install
echo -e "${clr_ok}Success! Project created by template \"$REPO_BRANCH\".${clr_end}"
if [ -f "$THIS_FILENAME" ]; then
ask "Remove \"${clr_info}${THIS_FILENAME}${clr_ask}\" script?" && rm "$THIS_FILENAME"
fi
if [ -d "$REPO_LOCAL_DIR" ]; then
ask "Remove temporary directory \"${clr_info}${REPO_LOCAL_DIR}${clr_ask}\"?" && rm -rf "$REPO_LOCAL_DIR"
fi
exit 0