Guest User

Untitled

a guest
Apr 21st, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. require 'rubygems'
  2. require 'spec'
  3. require 'spec/autorun'
  4. @watches = []
  5.  
  6. def watch(matcher, &block)
  7. @watches << Matcher.new(matcher, &block)
  8. end
  9. def do_a_loop(sender, arg)
  10. puts "found a #{arg.change_type} with #{arg.name}"
  11. @watches.each {|w| w.do_if_matched arg.name }
  12. end
  13. def start_watching
  14. watcher = System::IO::FileSystemWatcher.new
  15. watcher.path = "."
  16. watcher.include_subdirectories = true
  17. watcher.notify_filter = System::IO::NotifyFilters.last_write | System::IO::NotifyFilters.file_name | System::IO::NotifyFilters.directory_name
  18.  
  19. # watcher.changed do |s,a|
  20. # do_a_loop s, a
  21. # end
  22. watcher.created do |s,a|
  23. do_a_loop s, a
  24. end
  25. # watcher.deleted do |s,a|
  26. # do_a_loop s, a
  27. # end
  28. # watcher.renamed do |s,a|
  29. # do_a_loop s, a
  30. # end
  31. watcher.enable_raising_events = true
  32. puts "pres 'q' to quit."
  33. while "q" != System::Console.read
  34. end
  35. end
  36. class Matcher
  37. def initialize(matcher, &block)
  38. @matcher = Regexp.compile(matcher)
  39. @block = block
  40. end
  41. def do_if_matched(arg)
  42. matches = @matcher.match arg
  43. if not matches.nil?
  44. if matches.size == 1
  45. @block.call arg
  46. else
  47. @block.call matches[1]
  48. end
  49. end
  50. end
  51. end
  52.  
  53. watch( 'lib\\\\(.*)\\.rb' ) do |md|
  54. puts "Running specs because of #{md}"
  55. puts "spec spec\\#{md}_spec.rb"
  56. system "spec spec\\#{md}_spec.rb"
  57. end
  58. watch( 'spec\\\\(.*)_spec.rb') do |md|
  59. puts "Running specs because of specs"
  60. system("spec spec\\#{md}_spec.rb")
  61. end
  62.  
  63. start_watching()
Add Comment
Please, Sign In to add comment