Guest User

Untitled

a guest
Jun 20th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2.  
  3. class Ctags
  4. attr_reader :tags_file, :target_dir
  5.  
  6. def initialize(target_dir, tags_file=nil)
  7. @target_dir = target_dir + '/'
  8. tags_file ||= File.join(target_dir, '.autotags')
  9. @tags_file = tags_file
  10. refresh_tags
  11. end
  12.  
  13. def refresh_tags
  14. cmd "rm '#{tags_file}'" if File.exists? tags_file
  15. cmd "ctags -f '#{tags_file}' -R #{target_dir}"
  16. end
  17.  
  18. def append_tags_for(f)
  19. return if file_excluded? f
  20. cmd "ctags -f '#{tags_file}' -a '#{f}'"
  21. end
  22.  
  23. def clear_tags_for(f)
  24. return if file_excluded? f
  25. cmd "sed -i -e '/#{sed_escape(f)}/d' '#{tags_file}'"
  26. end
  27.  
  28. private
  29.  
  30. def file_excluded?(f)
  31. return f == tags_file || f =~ /#{File.join(target_dir, 'sed')}.*/
  32. end
  33.  
  34. def sed_escape(f)
  35. f.gsub(/\//, '\/')
  36. end
  37.  
  38. def cmd(cmd)
  39. #puts cmd
  40. `#{cmd}`
  41. end
  42. end
  43.  
  44. ['sed', 'ctags', 'rm', 'inotifywait'].each do |c|
  45. if `which #{c}` == ""
  46. puts "'#{c}' is required on the path for autotags to work!"
  47. exit
  48. end
  49. end
  50.  
  51. if ARGV.empty?
  52. puts "Usage: autotags <directory>"
  53. exit
  54. end
  55.  
  56. ctf = Ctags.new ARGV[0]
  57.  
  58. procs = {
  59. /(.*) CREATE (.*)/ => Proc.new do |f|
  60. ctf.append_tags_for(f)
  61. end,
  62. /(.*) DELETE (.*)/ => Proc.new do |f|
  63. ctf.clear_tags_for(f)
  64. end,
  65. /(.*) MOVED_FROM (.*)/ => Proc.new do |f|
  66. ctf.clear_tags_for(f)
  67. end,
  68. /(.*) MOVED_TO (.*)/ => Proc.new do |f|
  69. ctf.append_tags_for(f)
  70. end,
  71. /(.*) MODIFY (.*)/ => Proc.new do |f|
  72. ctf.clear_tags_for(f)
  73. ctf.append_tags_for(f)
  74. end
  75. }
  76.  
  77. pipe = IO.popen("inotifywait -r -m -e modify -e create -e move -e delete #{ARGV[0]}")
  78. puts "Watching #{ARGV[0]}"
  79.  
  80. while(line = pipe.gets) do
  81. procs.each do |k,v|
  82. v.call("#{$1}#{$2}") if line =~ k
  83. end
  84. end
Add Comment
Please, Sign In to add comment