Created
January 8, 2022 22:52
-
-
Save geoffjay/2351a0b29e4ea7bc7807a8b81f07c1d4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Simple file refactoring for a c project, not intended for use, just | |
# preserving something I've used a couple of times. | |
function __refactor { | |
dir=$(echo $1 | sed 's/\/$//') | |
files=$(find $dir -type f -regex ".*\.[ch]") | |
for file in $files; do | |
new=$(basename $file | sed "s/$2/$3/") | |
echo "refactoring: \"$file\" -> \"$dir/$new\"" | |
__fix_references $(basename $file) $new | |
git mv $file $dir/$new | |
done | |
} | |
function __fix_references { | |
references=$(git grep "['\"/]$1['\"]" | sed 's/:.*$//' | uniq) | |
for reference in $references; do | |
# this is specific to macos/bsd sed | |
sed -i "" "s/$1/$2/" $reference &>/dev/null | |
if [[ $? -ne 0 ]]; then | |
echo "failed to update references in " $reference | |
fi | |
done | |
echo -e | |
} | |
function __usage { | |
cat <<=EOF | |
usage: $0 <path> <from> <to> | |
=EOF | |
} | |
if [[ "$#" -ne 3 ]]; then | |
__usage "$@" | |
exit 1 | |
fi | |
__refactor "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment