-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenameTemplate.sh
executable file
·87 lines (68 loc) · 1.9 KB
/
renameTemplate.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
#!/bin/sh
ackInstalled() {
if ! [ -x "$(command -v ack)" ]; then
echo >&2 "error: Ack is not installed, install 'ack' via brew."
exit 1;
fi
}
renameInstalled() {
if ! [ -x "$(command -v rename)" ]; then
echo >&2 "error: Rename is not installed, install 'rename' via brew."
exit 1;
fi
}
copyTemplateToNewPath() {
cp -rf $1 $2
}
renameFiles() {
# need to do multiple passes. first directories will get hit, then filenames, at various levels.
while true; do
echo "Renaming files ..."
str=$(find $1/. -name "$2*" -print0 | xargs -0 rename --subst-all "$2" "$3" 2>&1)
if [ "$str" = "" ]; then
break
fi
done
}
replaceOccurrences() {
ack --literal --files-with-matches "$1" --print0 "$3" | xargs -0 sed -i "" "s/$1/$2/g"
}
OLDNAME=$1
NEWNAME=$2
WORKDIR=$3
NEWPATH=${WORKDIR%%/}${WORKDIR:+/}${NEWNAME}
# remove bad characters
OLDNAME=`echo "${OLDNAME}" | sed -e "s/[^a-zA-Z0-9_ -]//g"`
NEWNAME=`echo "${NEWNAME}" | sed -e "s/[^a-zA-Z0-9_ -]//g"`
if [ "$OLDNAME" = "" -o "$NEWNAME" = "" ]; then
echo "usage: $0 <template> <new_name> <directory>"
exit 2
fi
if [ "$WORKDIR" = "" ]; then
echo "usage: $0 <template> <new_name> <directory>"
exit 2
fi
echo "${NEWNAME}" | grep "${OLDNAME}" > /dev/null
if [ $? -eq 0 ]; then
echo "${NEWNAME}, ${OLDNAME}"
echo "error: new project name cannot contain the template name."
exit 2
fi
if [ ! -d "${OLDNAME}" ]; then
echo "error: \"${OLDNAME}\" must be a directory"
exit 2
fi
# set new project directory
if [ -d "${NEWPATH}" ]; then
echo "error: project directory \"${NEWPATH}\" exists. Terminating."
exit 2
fi
ackInstalled
renameInstalled
echo "\nCopying $OLDNAME to $NEWPATH"
copyTemplateToNewPath $OLDNAME $NEWPATH
echo "Renaming files ..."
renameFiles $NEWPATH $OLDNAME $NEWNAME
echo "Replacing occurences of $OLDNAME with $NEWNAME ..."
replaceOccurrences $OLDNAME $NEWNAME $NEWPATH
echo "\nDone. The new project now lives here: $NEWPATH"