Last active
September 15, 2016 01:18
-
-
Save julianh2o/df6419051f15f80038d43f443a7dceaf to your computer and use it in GitHub Desktop.
Simple bash refactoring function (depends on SilverSearcher)
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
# Usage: | |
# First, search files recursively for text using ag (SilverSearcher). This could be refactored to use grep probably | |
# $ refactor gen 'function' > lines.txt | |
# Lines.txt will contain the file paths, line numbers, and line contents like so: (one entry per line) | |
# index.js:52: handler: function onNext() { | |
# Now, using your favorite editor, edit the lines.txt file with any changes you want to make to those lines (using something search replace, a text editor, or a bash script) | |
# Save the file and now run the script again to update all the files accordingly: | |
# refactor run lines.txt | |
# Note: You can also use `refactor dry lines.txt` to show the changes that will be made | |
# Note2: Make sure that you run the script from the same directory and that you don't make line-numbering changes between gen and run | |
function refactor { | |
case $1 in | |
# This will search using ag (SilverSearcher) for lines that contain the regex provided | |
gen) | |
PATTERN=$2 | |
ag --noheading $PATTERN | awk NF | |
;; | |
dry) | |
CHANGE_FILE=$2 | |
cat $CHANGE_FILE | while read line | |
do | |
# The relative file path | |
F=${line%%:*} | |
# The line number | |
LINE=${line#*:} | |
LINE=${LINE%%:*} | |
# The replacement line from the lines file | |
REPL=${line#*:} | |
REPL=${line#*:} | |
# Pull the existing line from the file | |
EXISTING=`cat $F | head -n $LINE | tail -n 1` | |
echo $F:$LINE | |
echo -e '\t\e[31m'$EXISTING | |
echo -e '\t\e[32m'$REPL | |
echo -e '\e[39m' | |
done; | |
;; | |
run) | |
CHANGE_FILE=$2 | |
cat $CHANGE_FILE | while read line | |
do | |
# The relative file path | |
F=${line%%:*} | |
# The line number | |
LINE=${line#*:} | |
LINE=${LINE%%:*} | |
# The replacement line | |
REPL=${line#*:} | |
REPL=${REPL#*:} | |
# Hack the text before and after the line off and splice in the changed line. Output to a tempfile | |
{ cat $F | head -n $((LINE-1)); echo $REPL; cat $F | tail -n +$((LINE+1)); } > /tmp/refactortmp.txt | |
cat /tmp/refactortmp.txt > $F | |
rm /tmp/refactortmp.txt | |
done; | |
;; | |
*) | |
echo "Usage:" | |
echo | |
echo "First, search files recursively for text using ag (SilverSearcher). This could" | |
echo "be refactored to use grep probably" | |
echo " $ refactor gen 'function' > lines.txt" | |
echo | |
echo "Lines.txt will contain the file paths, line numbers, and line contents like so:" | |
echo "(one entry per line)" | |
echo " index.js:52: handler: function onNext() {" | |
echo | |
echo "Now, using your favorite editor, edit the lines.txt file with any changes you" | |
echo "want to make to those lines (using something search replace, a text editor, or" | |
echo "a bash script)" | |
echo | |
echo "Save the file and now run the script again to update all the files accordingly:" | |
echo " refactor run lines.txt" | |
echo | |
echo "Note: You can also use 'refactor dry lines.txt' to show the changes that" | |
echo "will be made" | |
echo | |
echo "Note2: Make sure that you run the script from the same directory and that" | |
echo "you don't make line-numbering changes between gen and run" | |
;; | |
esac | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment