-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcatkin_rename_package.sh
executable file
·93 lines (78 loc) · 2.3 KB
/
catkin_rename_package.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
#!/bin/bash
set -e -u
shopt -s xpg_echo
# @todo Robustify to spaces in the paths
# @todo Figure out better way to implode flags?
# @todo Robustify `find` with -not -path? -type f?
bin=$(basename $0)
LONG_USAGE="Rename a package from <from> to <to>.
This will first find and replace the package name (whole world) in source files, then\
rename any directories exactly named <from> to <to>.
It is highly recommended that you version control your source code before and after\
these changes!"
OPTIONS_SPEC="\
usage: $bin [options] <dir> <from> <to>
-h, --help Show help
$LONG_USAGE
"
usage()
{
echo "$OPTIONS_SPEC" >&2
exit ${1-0}
}
while [[ $# -gt 0 ]]
do
case "$1" in
-h|--help)
usage
;;
-*)
echo "error: Invalid option: $1"
usage 1
;;
*)
break
;;
esac
shift
done
[[ $# -ne 3 ]] && { echo "error: Invalid arguments: $@" >&2; usage 1; }
dir="$1"
from="$2"
to="$3"
# Ensure that everything is first staged in git (if you are doing this outside of git, then shame on you!)
# What about subprojects? ...
# git add -A :/
# First find all text files (ignoring .git and build/) and replace the package name
# File type / name flags - implementing implode functionality :/
# python -c 'print " -o ".join(map(lambda x: "-name " + x, ["*.cpp", "*.hpp"]))'
patterns='*.cpp *.hpp *.h *.c *.m *.py package.xml CMakeLists.txt *.cmake *.sh'
name_flags=""
for pattern in $patterns
do
flag="-name $pattern"
[[ -z "$name_flags" ]] && name_flags="$flag" || name_flags="$name_flags -o $flag"
done
# Prune flags
prunes='*/build */.git'
prune_flags=""
for prune in $prunes
do
flag="-path $prune -prune"
[[ -z "$prune_flags" ]] && prune_flags="$flag" || prune_flags="$prune_flags -o $flag"
done
echo "[ Rename Package: '$from' -> '$to' ]"
# http://stackoverflow.com/questions/4210042/exclude-directory-from-find-command
# @note This works with -prune because our names are typically files types
# @todo http://stackoverflow.com/questions/8677546/bash-for-in-looping-on-null-delimited-string-variable
files="$(find "$dir" $prune_flags -o \( $name_flags \) -print)"
echo "[ Text Replacement for Patterns: $patterns ]"
sed -i "s#\b$from\b#$to#g" $files
# Next rename directories
echo "[ Rename Directories ]"
srcs="$(find "$dir" -name "$from" -type d | tac)"
for src in $srcs
do
dest="$(dirname $src)/$to"
mv "$src" "$dest"
done