-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrename.sh
executable file
·170 lines (151 loc) · 4.87 KB
/
rename.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
#!/bin/sh
################################################################################
#
# renameXcodeProject.sh
#
# author: Monte Ohrt <monte@ohrt.com>
# date: Jan 27, 2009
# version: 1.0
#
# This script will copy an xcode project to a new project directory name
# and replace/rename all files within to work as expected under the new name.
# Project names that contain characters other than alpha-numeric, spaces or
# underscores MAY not work properly with this script. Use at your own risk!
# Be CERTAIN to backup your project(s) before renaming.
#
# One simple rule:
#
# 1) The old project name cannot contain the new project name, so for instance,
# renaming "MyStuff" to "MyStuff2" will not work. If you really need to do
# this, rename the project to a temp name, then rename again.
#
# I also have instructions for manually renaming an xcode project here:
#
# http://mohrt.blogspot.com/2008/12/renaming-xcode-project.html
#
#
# Installation:
#
# Copy (this) file "renameXcodeProject.sh" to your file system, and invoke:
#
# chmod 755 renameXcodeProject.sh
#
# to make it executable.
#
# usage:
#
# renameXcodeProject.sh <OldProjectName> <NewProjectName>
#
# examples:
#
# ./renameXcodeProject.sh OldName NewName
# ./renameXcodeProject.sh "Old Name" "New Name"
#
################################################################################
OLDNAME=$1
NEWNAME=$2
# 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"`
TMPFILE=/tmp/xcodeRename.$$
if [ "$OLDNAME" = "" -o "$NEWNAME" = "" ]; then
echo "usage: $0 <OldProjectName> <NewProjectName>"
exit
fi
echo "${NEWNAME}" | grep "${OLDNAME}" > /dev/null
if [ $? -eq 0 ]; then
echo "Error: New project name cannot contain old project name. Use a tmp name first. Terminating."
exit
fi
if [ ! -d "${OLDNAME}" ]; then
echo "ERROR: \"${OLDNAME}\" must be a directory"
exit
fi
# set new project directory
if [ -d "${NEWNAME}" ]; then
echo "ERROR: project directory \"${NEWNAME}\" exists. Terminating."
exit
fi
# be sure tmp file is writable
cp /dev/null ${TMPFILE}
if [ $? -ne 0 ]; then
echo "tmp file ${TMPFILE} is not writable. Terminating."
exit
fi
# create project name with unscores for spaces
OLDNAMEUSCORE=`echo "${OLDNAME}" | sed -e "s/ /_/g"`
NEWNAMEUSCORE=`echo "${NEWNAME}" | sed -e "s/ /_/g"`
# copy project directory
echo copying project directory from "${OLDNAME}" to "${NEWNAME}"
cp -rp "${OLDNAME}" "${NEWNAME}"
# remove build directory
echo removing build directory from "${NEWNAME}"
rm -rf "${NEWNAME}/build"
#find text files, replace text
find "${NEWNAME}/." | while read currFile
do
# find files that are of type text
file "${currFile}" | grep "text" > /dev/null
if [ $? -eq 0 ]; then
# see if old proj name with underscores is in the text
grep "${OLDNAMEUSCORE}" "${currFile}" > /dev/null
if [ $? -eq 0 ]; then
# replace the text with new proj name
echo found "${OLDNAMEUSCORE}" in "${currFile}", replacing...
sed -e "s/${OLDNAMEUSCORE}/${NEWNAMEUSCORE}/g" "${currFile}" > ${TMPFILE}
mv ${TMPFILE} "${currFile}"
cp /dev/null ${TMPFILE}
fi
# see if old proj name is in the text
grep "${OLDNAME}" "${currFile}" > /dev/null
if [ $? -eq 0 ]; then
# replace the text with new proj name
echo found "${OLDNAME}" in "${currFile}", replacing...
sed -e "s/${OLDNAME}/${NEWNAME}/g" "${currFile}" > ${TMPFILE}
mv ${TMPFILE} "${currFile}"
cp /dev/null ${TMPFILE}
fi
fi
done
# rename directories with underscores
find "${NEWNAME}/." -type dir | while read currFile
do
echo "${currFile}" | grep "${OLDNAMEUSCORE}" > /dev/null
if [ $? -eq 0 ]; then
MOVETO=`echo "${currFile}" | sed -e "s/${OLDNAMEUSCORE}/${NEWNAMEUSCORE}/g"`
echo renaming "${currFile}" to "${MOVETO}"
mv "${currFile}" "${MOVETO}"
fi
done
# rename directories with spaces
find "${NEWNAME}/." -type dir | while read currFile
do
echo "${currFile}" | grep "${OLDNAME}" > /dev/null
if [ $? -eq 0 ]; then
MOVETO=`echo "${currFile}" | sed -e "s/${OLDNAME}/${NEWNAME}/g"`
echo renaming "${currFile}" to "${MOVETO}"
mv "${currFile}" "${MOVETO}"
fi
done
# rename files with underscores
find "${NEWNAME}/." -type file | while read currFile
do
echo "${currFile}" | grep "${OLDNAMEUSCORE}" > /dev/null
if [ $? -eq 0 ]; then
MOVETO=`echo "${currFile}" | sed -e "s/${OLDNAMEUSCORE}/${NEWNAMEUSCORE}/g"`
echo renaming "${currFile}" to "${MOVETO}"
mv "${currFile}" "${MOVETO}"
fi
done
# rename files with spaces
find "${NEWNAME}/." -type file | while read currFile
do
echo "${currFile}" | grep "${OLDNAME}" > /dev/null
if [ $? -eq 0 ]; then
MOVETO=`echo "${currFile}" | sed -e "s/${OLDNAME}/${NEWNAME}/g"`
echo renaming "${currFile}" to "${MOVETO}"
mv "${currFile}" "${MOVETO}"
fi
done
rm -f ${TMPFILE}
echo finished.