Added init feature

This commit is contained in:
Thibaud Guillaume-Gentil 2010-10-07 22:37:30 +02:00
parent bc5cc10d42
commit 057acfbb2f
8 changed files with 84 additions and 22 deletions

View File

@ -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)

View File

@ -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'

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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 =
# ================

View File

@ -0,0 +1,2 @@
# A sample Guardfile
# More info at http://github.com/guard/guard#readme

View File

@ -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