Replace 'replace' w/ 'wag' script for ag search & replace
This commit is contained in:
parent
9230471d7d
commit
623956f74b
59
bin/replace
59
bin/replace
|
@ -1,59 +0,0 @@
|
||||||
#!/usr/bin/env bash
|
|
||||||
|
|
||||||
# A simple script that uses ag
|
|
||||||
# <https://github.com/ggreer/the_silver_searcher> and perl to perform
|
|
||||||
# a project-wide search and replace; it shows you the results and asks
|
|
||||||
# for confirmation before committing the changes.
|
|
||||||
#
|
|
||||||
# Usage: replace [pattern] [replace] [options for ag]
|
|
||||||
|
|
||||||
TMP_FILE="/tmp/ag-repl"
|
|
||||||
RED=`printf "\033[31m"`
|
|
||||||
NC=`printf "\033[0m"`
|
|
||||||
|
|
||||||
sanitize() {
|
|
||||||
# For text that we interpolate into perl search/replace regex
|
|
||||||
echo "$1" | sed -e "s_/_\\\/_g"
|
|
||||||
}
|
|
||||||
|
|
||||||
# Begin
|
|
||||||
input=$1
|
|
||||||
search=`sanitize "$1"`
|
|
||||||
replace=`sanitize "$2"`
|
|
||||||
|
|
||||||
shift
|
|
||||||
shift
|
|
||||||
|
|
||||||
# Show the search/replace results
|
|
||||||
if ag $input $* -s --group --color --break --stats \
|
|
||||||
| perl -p -e "s/(?<=.)$search/$NC$RED$replace$NC/g" \
|
|
||||||
| perl -p -e "s/^([0-9]+ matches$)/\n\$1/g" \
|
|
||||||
> "$TMP_FILE"; then
|
|
||||||
|
|
||||||
# Show the results
|
|
||||||
less -XF "$TMP_FILE"
|
|
||||||
|
|
||||||
# Calculate if there were results
|
|
||||||
lines=`wc -l $TMP_FILE | awk '{print $1}'`
|
|
||||||
rm -rf "$TMP_FILE"
|
|
||||||
[ "$lines" -lt 6 ] && exit 2
|
|
||||||
|
|
||||||
# Ask for confirmation
|
|
||||||
echo
|
|
||||||
echo -n "Apply changes? (y/n) "
|
|
||||||
read -n1 REPLY
|
|
||||||
echo
|
|
||||||
|
|
||||||
if [[ $REPLY =~ ^[Yy]$ ]]; then
|
|
||||||
# Now actually do the search replace! This is destructive. No, no
|
|
||||||
# backups are made. You better have read the earlier prompt!
|
|
||||||
ag -l $input $* | xargs perl -pi -e "s/$search/$replace/g"
|
|
||||||
echo "Done!"
|
|
||||||
exit
|
|
||||||
else
|
|
||||||
echo $RED"Aborted!"$NC
|
|
||||||
exit 3
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
|
|
||||||
exit 1
|
|
71
bin/wag
Executable file
71
bin/wag
Executable file
|
@ -0,0 +1,71 @@
|
||||||
|
#!/usr/bin/env ruby
|
||||||
|
|
||||||
|
# A hacky script that allows you to search with ag
|
||||||
|
# <https://github.com/ggreer/the_silver_searcher> and edit the results in
|
||||||
|
# $EDITOR. The changes are propagated to all listed files, if any.
|
||||||
|
#
|
||||||
|
# Usage: wag [pattern]
|
||||||
|
#
|
||||||
|
# TODO Also rename files whose names have been changed
|
||||||
|
|
||||||
|
if ARGV.empty?
|
||||||
|
puts "You didn't enter in anything!"
|
||||||
|
exit 1
|
||||||
|
end
|
||||||
|
|
||||||
|
def quit
|
||||||
|
t.unlink
|
||||||
|
exit 1
|
||||||
|
end
|
||||||
|
|
||||||
|
require 'tempfile'
|
||||||
|
require 'shellwords'
|
||||||
|
|
||||||
|
t = Tempfile.new('.ag-results')
|
||||||
|
out = system("ag #{ARGV.join(' ')} --nogroup --nocolor > #{t.path}")
|
||||||
|
if [false, nil].include? out
|
||||||
|
puts "No matches" if out === false
|
||||||
|
|
||||||
|
t.unlink
|
||||||
|
exit 1
|
||||||
|
end
|
||||||
|
|
||||||
|
old_lines = File.open(t.path).readlines
|
||||||
|
system("#{ENV['EDITOR']} #{t.path}")
|
||||||
|
lines = File.open(t.path).readlines
|
||||||
|
t.unlink
|
||||||
|
|
||||||
|
if old_lines.length != lines.length
|
||||||
|
puts "There was a line-number mismatch. Abort! Abort! Abort!"
|
||||||
|
exit 2
|
||||||
|
end
|
||||||
|
|
||||||
|
i = nil
|
||||||
|
lines.each_with_index do |line, i|
|
||||||
|
next if line.empty? || line.eql?(old_lines[i])
|
||||||
|
|
||||||
|
l = line.split(":", 3)
|
||||||
|
unless l.length == 3
|
||||||
|
puts "Line #{i} is malformed as was skipped"
|
||||||
|
next
|
||||||
|
end
|
||||||
|
|
||||||
|
filename = Shellwords.escape(l[0])
|
||||||
|
lineno = Shellwords.escape(l[1])
|
||||||
|
content = l[2].gsub("'", "\\'")
|
||||||
|
|
||||||
|
if system("sed -i .bak '#{lineno}c\\
|
||||||
|
#{content}' #{filename}")
|
||||||
|
puts "Replaced line #{lineno} in #{filename}"
|
||||||
|
else
|
||||||
|
puts "Failed to replace line #{lineno} in #{filename}"
|
||||||
|
end
|
||||||
|
|
||||||
|
system("rm -f #{filename}.bak") if File.exists?("#{filename}.bak")
|
||||||
|
end
|
||||||
|
|
||||||
|
if i === nil
|
||||||
|
puts "Didn't do nuthin'"
|
||||||
|
else
|
||||||
|
puts "Done!"
|
||||||
|
end
|
Loading…
Reference in a new issue