Skip to content

Instantly share code, notes, and snippets.

@mikesimons
Created November 4, 2010 12:55
Show Gist options
  • Save mikesimons/662413 to your computer and use it in GitHub Desktop.
Save mikesimons/662413 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
class Ctags
attr_reader :tags_file, :target_dir
def initialize(target_dir, tags_file=nil)
@target_dir = target_dir + '/'
tags_file ||= File.join(target_dir, '.autotags')
@tags_file = tags_file
refresh_tags
end
def refresh_tags
cmd "rm '#{tags_file}'" if File.exists? tags_file
cmd "ctags -f '#{tags_file}' -R #{target_dir}"
end
def append_tags_for(f)
return if file_excluded? f
cmd "ctags -f '#{tags_file}' -a '#{f}'"
end
def clear_tags_for(f)
return if file_excluded? f
cmd "sed -i -e '/#{sed_escape(f)}/d' '#{tags_file}'"
end
private
def file_excluded?(f)
return f == tags_file || f =~ /#{File.join(target_dir, 'sed')}.*/
end
def sed_escape(f)
f.gsub(/\//, '\/')
end
def cmd(cmd)
#puts cmd
`#{cmd}`
end
end
['sed', 'ctags', 'rm', 'inotifywait'].each do |c|
if `which #{c}` == ""
puts "'#{c}' is required on the path for autotags to work!"
exit
end
end
if ARGV.empty?
puts "Usage: autotags <directory>"
exit
end
ctf = Ctags.new ARGV[0]
procs = {
/(.*) CREATE (.*)/ => Proc.new do |f|
ctf.append_tags_for(f)
end,
/(.*) DELETE (.*)/ => Proc.new do |f|
ctf.clear_tags_for(f)
end,
/(.*) MOVED_FROM (.*)/ => Proc.new do |f|
ctf.clear_tags_for(f)
end,
/(.*) MOVED_TO (.*)/ => Proc.new do |f|
ctf.append_tags_for(f)
end,
/(.*) MODIFY (.*)/ => Proc.new do |f|
ctf.clear_tags_for(f)
ctf.append_tags_for(f)
end
}
pipe = IO.popen("inotifywait -r -m -e modify -e create -e move -e delete #{ARGV[0]}")
puts "Watching #{ARGV[0]}"
while(line = pipe.gets) do
procs.each do |k,v|
v.call("#{$1}#{$2}") if line =~ k
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment