[402] | 1 | #!/bin/bash
|
---|
| 2 |
|
---|
| 3 | function info () {
|
---|
| 4 | echo -e "\033[32m$1\033[0m"
|
---|
| 5 | }
|
---|
| 6 |
|
---|
| 7 | function warn () {
|
---|
| 8 | echo -e "\033[33m$1\033[0m"
|
---|
| 9 | }
|
---|
| 10 |
|
---|
| 11 | function err () {
|
---|
| 12 | echo -e "\033[31m$1\033[0m"
|
---|
| 13 | exit 1
|
---|
| 14 | }
|
---|
| 15 |
|
---|
| 16 | #arg 1 cmake file
|
---|
| 17 | #arg 2 old project name
|
---|
| 18 | #arg 3 new project name
|
---|
| 19 | function parse_cmakefile () {
|
---|
| 20 | echo
|
---|
| 21 | info "parsing ${1}"
|
---|
| 22 | dir=$(dirname ${1})
|
---|
| 23 |
|
---|
| 24 | #rename cpp and h files from cmakelist
|
---|
| 25 | cat ${1} | grep cpp | grep $2 | while read file; do
|
---|
| 26 | #remove \r
|
---|
| 27 | file=$(echo $file | sed 's/\r//')
|
---|
| 28 | new_file=$(echo ${file/$2/$3})
|
---|
| 29 |
|
---|
| 30 | mv ${dir}/${file} ${dir}/${new_file}
|
---|
| 31 | echo renamed $file to $new_file
|
---|
| 32 |
|
---|
| 33 | file=$(echo ${file/cpp/h})
|
---|
| 34 | if [ -f ${dir}/${file} ]; then
|
---|
| 35 | new_file=$(echo ${file/$2/$3})
|
---|
| 36 | mv ${dir}/${file} ${dir}/${new_file}
|
---|
| 37 | echo renamed $file to $new_file
|
---|
| 38 | fi
|
---|
| 39 |
|
---|
| 40 | done
|
---|
| 41 |
|
---|
| 42 | #replace old project refeerence to new project un cmakelists
|
---|
| 43 | sed -i "s/${2}/${3}/g" ${1}
|
---|
| 44 | echo "changed reference of $2 to $3"
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | #arg 1 cpp file
|
---|
| 48 | #arg 2 old project name
|
---|
| 49 | #arg 3 new project name
|
---|
| 50 | function parse_source_file () {
|
---|
| 51 | echo
|
---|
| 52 | info "parsing ${1}"
|
---|
| 53 | sed -i "s/${2}/${3}/g" ${1}
|
---|
| 54 | echo "changed reference of $2 to $3"
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | if [ "${2}" = "" ]; then
|
---|
| 58 | err "usage clone_demo.sh path_to_source_demo destination_demo_name"
|
---|
| 59 | fi
|
---|
| 60 |
|
---|
| 61 | if ! [ -f ${1}/CMakeLists.txt ]; then
|
---|
| 62 | err "${1} directory does not contain CMakeLists.txt"
|
---|
| 63 | fi
|
---|
| 64 |
|
---|
[405] | 65 | if [ -d ${2} ];then
|
---|
| 66 | err "output ${2} directory already exists"
|
---|
| 67 | fi
|
---|
| 68 |
|
---|
[402] | 69 | echo cloning ${1} to ${2}
|
---|
| 70 | cp -r ${1} ./${2}
|
---|
| 71 | cd ./${2}
|
---|
| 72 |
|
---|
| 73 | #get old project name from top cmakelists
|
---|
| 74 | old_project=$(cat ./CMakeLists.txt | grep -i project\( | cut -d "(" -f2 | cut -d ")" -f1)
|
---|
| 75 |
|
---|
| 76 | echo "old project name is" $old_project
|
---|
| 77 |
|
---|
| 78 | #search all CMakeLists.txt, sort by depth and loop
|
---|
| 79 | find -name "CMakeLists.txt" -printf '%h\0%d\0%p\n' | sort -t '\0' -n | awk -F '\0' '{print $3}' | while read cmakefile; do
|
---|
| 80 | parse_cmakefile $cmakefile ${old_project} ${2}
|
---|
| 81 | done
|
---|
| 82 |
|
---|
| 83 |
|
---|
| 84 | #find all cpp files
|
---|
| 85 | find ${dir} -name "*.cpp" | while read file; do
|
---|
| 86 | parse_source_file $file ${old_project} ${2}
|
---|
| 87 | done
|
---|
| 88 |
|
---|
| 89 | #find all h files
|
---|
| 90 | find ${dir} -name "*.h" | while read file; do
|
---|
| 91 | parse_source_file $file ${old_project} ${2}
|
---|
| 92 | done
|
---|
| 93 |
|
---|
| 94 | #find all sh files
|
---|
| 95 | find ${dir} -name "*.sh" | while read file; do
|
---|
| 96 | parse_source_file $file ${old_project} ${2}
|
---|
| 97 | done
|
---|