-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnew_proj
executable file
·73 lines (62 loc) · 1.46 KB
/
new_proj
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
#!/bin/bash -e
shopt -s dotglob extglob
function ask_yn() {
local ans
printf '%s (N/y) ' "$1" >&2
read -r ans
case "${ans,,}" in
y*) return 0 ;;
*) return 1 ;;
esac
}
function usage() {
printf '%s\n' \
'USAGE' \
' ./new_proj name [repo]' \
'name: the name of the project and the folder' \
'repo: the URL to the repo'
exit 1
}
if [ -t 1 ]; then
BOLD=$'\e[1m'
NORMAL=$'\e[0m'
fi
SRC="$(dirname "$0")/repo"
NAME="$1"
REPO="$2"
case "$NAME" in
'-h'|'--help'|'') usage ;;
*) ;;
esac
read -ra FILES < <(echo "$NAME"/!(.|..|.git))
if [ -n "${FILES[*]}" ]; then
if ask_yn "$NAME exists. Replace?"; then
echo "${BOLD}REMOVE${NORMAL}"
rm -rfv "${FILES[@]}"
else
exit
fi
fi
echo "${BOLD}COPY${NORMAL}"
mkdir -pv "$NAME"
cp -rfv "$SRC"/* "$NAME"
YEAR="$(date '+%Y')"
echo "${BOLD}REPLACING PROJECT NAME${NORMAL}"
find "$NAME" -type f ! -path "$NAME/.git/*" -print -exec sed -i \
-e "s/{{PROJ}}/$NAME/g" \
-e "s/{{PROJ_UP}}/${NAME^^}/g" \
-e "s/{{YEAR}}/$YEAR/g" \
'{}' +
if [ -n "$REPO" ]; then
echo "${BOLD}INIT GIT${NORMAL}"
(cd "$NAME"
git init
git remote remove origin 2> /dev/null || true
git remote add origin "$REPO"
git branch -m main
git add .
git commit -m 'Initial commit'
git branch --set-upstream-to=origin/main main
)
fi
echo "${BOLD}DONE${NORMAL}"