guard-lacquer/lib/guard/lacquer.rb

87 lines
2.0 KiB
Ruby
Raw Normal View History

2011-09-28 15:22:05 +00:00
require 'guard'
require 'guard/guard'
2011-09-28 17:15:01 +00:00
require 'fileutils'
2011-09-28 15:22:05 +00:00
module Guard
2011-09-28 17:15:01 +00:00
class Lacquer < Guard
autoload :Varnishd, 'guard/lacquer/varnishd'
2011-09-29 16:18:20 +00:00
autoload :VarnishNCSA, 'guard/lacquer/varnishncsa'
2011-09-28 17:15:01 +00:00
2011-09-28 15:22:05 +00:00
def initialize(watchers = [], options = {})
super
2011-09-28 17:15:01 +00:00
@options = {
:port => 3001,
:backend => '127.0.0.1:3000',
:storage => 'file,tmp/cache/varnish.store,32M',
2011-09-29 16:18:20 +00:00
:pid_dir => 'tmp/pids',
:log_dir => 'log',
:name => File.split(Dir.pwd).last
2011-09-28 17:15:01 +00:00
}.merge(options)
2011-09-29 16:18:20 +00:00
if @options[:varnish_path]
@options.merge!(
:sbin_path => File.join(@options[:varnish_path], 'sbin'),
:bin_path => File.join(@options[:varnish_path], 'bin'),
)
else
@options.merge!(
:sbin_path => File.split(`which varnishd`.strip).first,
:bin_path => File.split(`which varnishncsa`.strip).first
)
end
@frontend = Varnishd.new(@options)
@logger = VarnishNCSA.new(@options)
2011-09-28 17:15:01 +00:00
if !File.file?(varnish_erb = 'config/varnish.vcl.erb')
UI.info "No config/varnish.vcl.erb found, copying default from Lacquer..."
FileUtils.mkdir_p File.split(varnish_erb).first
FileUtils.cp Gem.find_files('generators/lacquer/templates/varnish.vcl.erb').first, varnish_erb
end
2011-09-28 15:22:05 +00:00
end
def start
2011-09-29 16:18:20 +00:00
[ @frontend, @logger ].each do |which|
which.stop if which.running?
which.start
end
2011-09-28 17:15:01 +00:00
notify "Varnish started on port #{@options[:port]}, with backend #{@options[:backend]}."
2011-09-28 15:22:05 +00:00
end
def stop
2011-09-29 16:18:20 +00:00
[ @frontend, @logger ].each(&:stop)
2011-09-28 17:15:01 +00:00
2011-09-28 17:28:19 +00:00
notify "Until next time..."
2011-09-28 15:22:05 +00:00
end
def reload
2011-09-28 19:25:08 +00:00
true
2011-09-28 15:22:05 +00:00
end
def run_all
2011-09-28 19:25:08 +00:00
stop
start
2011-09-28 15:22:05 +00:00
end
# Called on file(s) modifications
def run_on_change(paths)
2011-09-28 19:25:08 +00:00
run_all
2011-09-28 15:22:05 +00:00
end
# Called on file(s) deletions
def run_on_deletion(paths)
2011-09-28 19:25:08 +00:00
run_all
2011-09-28 15:22:05 +00:00
end
2011-09-28 17:15:01 +00:00
private
def notify(message)
Notifier.notify(message, :title => 'guard-lacquer', :image => File.expand_path("../../../images/varnish.png", __FILE__))
end
2011-09-28 15:22:05 +00:00
end
end