Use script to speed file move in code refactory
When do code refactor, we will need to move files from lib to lib, when it comes to move a very common used file, we will need to change a lot of file and check all of them out, this is boring. We could use script to speed the process and make life a little easier.
For example, when we want to move "libAAA/SomeHeaderFile.h" to "libBBB/SomeHeaderFile.h", after we had done the moving, we need to do following things to make it can compile through:
- Check out all the files included "libAAA/SomeHeaderFile.h". (P4 in linux could not auto checkout files when we modified them).
- Change all the "libAAA/SomeHeaderFile.h" to "libBBB/SomeHeaderFile.h"
First, we need to find out which file include "libAAA/SomeHeaderFile.h", use find may not be the best way, but it's OK when we limit the search within the project .
find .|xargs grep -ri "libAAA/SomeHeaderFile.h" -l | sort | uniq >> myFile.txt
Second, we can use script to checkout all the files in myFile.txt, the command it's like below:
./bunchCheckOut.sh myFile.txt <change-list-id>
And the script bunchCheckOut.sh:
#!/bin/bash
FILENAME="$1 "
CHANGELIST="$2"
while read line
do
p4 edit -c $CHANGELIST $line
done < $FILENAME
echo "done"
The third step is to change all the related files.(This step should be very careful!)
sed -i "s/libAAA\/SomeHeaderFile.h/libBBB\/SomeHeaderFile.h/g" `grep libAAA/SomeHeaderFile.h -rl ~/gui_10/SQL`
The last step is to compile and check if there is some file missed to checkout or modified.
We can also combine the above script into one file, but I think we should always check the result of the command before we carry on.