From 057acfbb2f9c06065c04a28c8e7f1acbd82961b6 Mon Sep 17 00:00:00 2001 From: Thibaud Guillaume-Gentil Date: Thu, 7 Oct 2010 22:37:30 +0200 Subject: [PATCH] Added init feature --- Gemfile.lock | 9 ++++++-- guard.gemspec | 7 +++--- lib/guard.rb | 43 ++++++++++++++++++++++++----------- lib/guard/cli.rb | 17 ++++++++++++++ lib/guard/dsl.rb | 10 +++++--- lib/guard/guard.rb | 16 +++++++++++++ lib/guard/templates/Guardfile | 2 ++ lib/guard/ui.rb | 2 +- 8 files changed, 84 insertions(+), 22 deletions(-) create mode 100644 lib/guard/templates/Guardfile diff --git a/Gemfile.lock b/Gemfile.lock index 63efe4a..1238098 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -2,11 +2,12 @@ PATH remote: . specs: guard (0.1.0.beta.1) + bundler (~> 1.0.2) growl (~> 1.0.3) libnotify (~> 0.1.3) rb-inotify (~> 0.8.1) sys-uname (~> 0.8.4) - thor (~> 0.14.2) + thor (~> 0.14.3) GEM remote: http://rubygems.org/ @@ -15,6 +16,9 @@ GEM ffi (0.6.3) rake (>= 0.8.7) growl (1.0.3) + guard-rspec (0.1.0.beta.2) + guard (~> 0.1.0.beta.1) + rspec (~> 2.0.0.rc) libnotify (0.1.4) ffi (>= 0.6.2) rake (0.8.7) @@ -40,8 +44,9 @@ DEPENDENCIES bundler (~> 1.0.2) growl (~> 1.0.3) guard! + guard-rspec (~> 0.1.0.beta.2) libnotify (~> 0.1.3) rb-inotify (~> 0.8.1) rspec (~> 2.0.0.rc) sys-uname (~> 0.8.4) - thor (~> 0.14.2) + thor (~> 0.14.3) diff --git a/guard.gemspec b/guard.gemspec index 2e467ce..5cc0eb9 100644 --- a/guard.gemspec +++ b/guard.gemspec @@ -15,10 +15,11 @@ Gem::Specification.new do |s| s.required_rubygems_version = '>= 1.3.6' s.rubyforge_project = 'guard' - s.add_development_dependency 'bundler', '~> 1.0.2' - s.add_development_dependency 'rspec', '~> 2.0.0.rc' + s.add_development_dependency 'rspec', '~> 2.0.0.rc' + s.add_development_dependency 'guard-rspec', '~> 0.1.0.beta.2' - s.add_dependency 'thor', '~> 0.14.2' + s.add_dependency 'bundler', '~> 1.0.2' + s.add_dependency 'thor', '~> 0.14.3' s.add_dependency 'sys-uname', '~> 0.8.4' # Mac OS X s.add_dependency 'growl', '~> 1.0.3' diff --git a/lib/guard.rb b/lib/guard.rb index cbb2d23..bf02511 100644 --- a/lib/guard.rb +++ b/lib/guard.rb @@ -1,3 +1,5 @@ +require 'bundler' + module Guard autoload :UI, 'guard/ui' @@ -16,26 +18,42 @@ module Guard @guards = [] Dsl.evaluate_guardfile - Interactor.init_signal_traps - - listener.on_change do |files| - run do - guards.each do |guard| - paths = Watcher.match_files(guard, files) - guard.run_on_change(paths) unless paths.empty? + if guards.empty? + UI.error "No guards found in Guardfile, too bad." + else + Interactor.init_signal_traps + + listener.on_change do |files| + run do + guards.each do |guard| + paths = Watcher.match_files(guard, files) + guard.run_on_change(paths) unless paths.empty? + end end end + + UI.info "Guard is now watching at '#{Dir.pwd}'" + guards.each(&:start) + listener.start end - - UI.info "Guard is now watching at '#{Dir.pwd}'" - guards.each(&:start) - listener.start end def add_guard(name, watchers = [], options = {}) + guard_class = get_guard_class(name) + @guards << guard_class.new(watchers, options) + end + + def get_guard_class(name) require "guard/#{name.downcase}" guard_class = ObjectSpace.each_object(Class).detect { |c| c.to_s.downcase.match "^guard::#{name.downcase}" } - @guards << guard_class.new(watchers, options) + rescue LoadError + UI.error "#{name} guard gem not found, try to add it to your Gemfile." + end + + def locate_guard(name) + spec = Bundler.load.specs.find{|s| s.name == "guard-#{name}" } + UI.error "Could not find gem '#{name}' in the current Gemfile." unless spec + spec.full_gem_path end def run @@ -45,5 +63,4 @@ module Guard end end - end \ No newline at end of file diff --git a/lib/guard/cli.rb b/lib/guard/cli.rb index 63e7627..8206e07 100644 --- a/lib/guard/cli.rb +++ b/lib/guard/cli.rb @@ -16,5 +16,22 @@ module Guard Guard::UI.info "Guard version #{Guard::VERSION}" end map %w(-v --version) => :version + + desc "init [GUARD]", "Generates a Guardfile into the current working directory, or add it given guard" + def init(guard_name = nil) + if !File.exist?("Guardfile") + puts "Writing new Guardfile to #{Dir.pwd}/Guardfile" + FileUtils.cp(File.expand_path('../templates/Guardfile', __FILE__), 'Guardfile') + elsif guard_name.nil? + Guard::UI.error "Guardfile already exists at #{Dir.pwd}/Guardfile" + exit 1 + end + + if guard_name + guard_class = Guard.get_guard_class(guard_name) + guard_class.init(guard_name) + end + end + end end \ No newline at end of file diff --git a/lib/guard/dsl.rb b/lib/guard/dsl.rb index eb01889..c9ebe5c 100644 --- a/lib/guard/dsl.rb +++ b/lib/guard/dsl.rb @@ -10,14 +10,18 @@ module Guard exit 1 end + def self.guardfile_included?(guard_name) + File.read('Guardfile').include?("guard '#{guard_name}'") + end + def guard(name, options = {}, &definition) @watchers = [] - definition.call - Guard.add_guard(name, @watchers, options) + definition.call if definition + ::Guard.add_guard(name, @watchers, options) end def watch(pattern, &action) - @watchers << Guard::Watcher.new(pattern, action) + @watchers << ::Guard::Watcher.new(pattern, action) end end diff --git a/lib/guard/guard.rb b/lib/guard/guard.rb index 8da5cbe..5b55b79 100644 --- a/lib/guard/guard.rb +++ b/lib/guard/guard.rb @@ -6,6 +6,22 @@ module Guard @watchers, @options = watchers, options end + # Guardfile template needed inside guard gem + def self.init(name) + if ::Guard::Dsl.guardfile_included?(name) + ::Guard::UI.info "Guardfile already include #{name} guard" + else + content = File.read('Guardfile') + guard = File.read("#{::Guard.locate_guard(name)}/lib/guard/#{name}/templates/Guardfile") + File.open('Guardfile', 'wb') do |f| + f.puts content + f.puts "" + f.puts guard + end + ::Guard::UI.info "#{name} guard added to Guardfile, feel free to edit it" + end + end + # ================ # = Guard method = # ================ diff --git a/lib/guard/templates/Guardfile b/lib/guard/templates/Guardfile new file mode 100644 index 0000000..d93a6f9 --- /dev/null +++ b/lib/guard/templates/Guardfile @@ -0,0 +1,2 @@ +# A sample Guardfile +# More info at http://github.com/guard/guard#readme diff --git a/lib/guard/ui.rb b/lib/guard/ui.rb index 80ba2ef..9491766 100644 --- a/lib/guard/ui.rb +++ b/lib/guard/ui.rb @@ -5,7 +5,7 @@ module Guard def info(message, options = {}) unless ENV["GUARD_ENV"] == "test" reset_line if options[:reset] - clear if options.key?(:clear) ? options[:clear] : ::Guard.options[:clear] + clear if options.key?(:clear) ? options[:clear] : (::Guard.options && ::Guard.options[:clear]) puts reset_color(message) if message != '' end end