Skip to content

Commit

Permalink
Added init feature
Browse files Browse the repository at this point in the history
  • Loading branch information
thibaudgg committed Oct 7, 2010
1 parent bc5cc10 commit 057acfb
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 22 deletions.
9 changes: 7 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand All @@ -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)
Expand All @@ -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)
7 changes: 4 additions & 3 deletions guard.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
43 changes: 30 additions & 13 deletions lib/guard.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'bundler'

module Guard

autoload :UI, 'guard/ui'
Expand All @@ -16,26 +18,42 @@ def start(options = {})
@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
Expand All @@ -45,5 +63,4 @@ def run
end

end

end
17 changes: 17 additions & 0 deletions lib/guard/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,22 @@ def version
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
10 changes: 7 additions & 3 deletions lib/guard/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,18 @@ def self.evaluate_guardfile
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
Expand Down
16 changes: 16 additions & 0 deletions lib/guard/guard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,22 @@ def initialize(watchers = [], options = {})
@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 =
# ================
Expand Down
2 changes: 2 additions & 0 deletions lib/guard/templates/Guardfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# A sample Guardfile
# More info at http://github.com/guard/guard#readme
2 changes: 1 addition & 1 deletion lib/guard/ui.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class << self
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
Expand Down

0 comments on commit 057acfb

Please sign in to comment.