Skip to content

Instantly share code, notes, and snippets.

@diasjorge
Last active August 29, 2015 14:10
Show Gist options
  • Save diasjorge/4127db7dfa349f28480e to your computer and use it in GitHub Desktop.
Save diasjorge/4127db7dfa349f28480e to your computer and use it in GitHub Desktop.
Guradfile to work with fig
require 'guard/plugin'
require 'yaml'
module ::Guard
class Fig < ::Guard::Plugin
class FigProxy
def build(service = nil)
system "fig build #{service}"
end
def up recreate: true
cmd = "fig up -d"
unless recreate
cmd << " --no-recreate"
end
system cmd
end
def stop(service = nil)
system "fig stop #{service}"
end
def remove(service)
system "fig rm --force #{service}"
end
end
def initialize(options = {})
super(options)
@config = YAML.load_file('fig.yml')
@fig = FigProxy.new
end
def start
boot2docker_setup if @options[:boot2docker_start]
@fig.build if @options[:build_on_start]
@fig.up recreate: false
end
def stop
@fig.stop
end
def run_all
@fig.up
end
def run_on_changes(paths)
debug paths.inspect
if paths.any? { |x| x =~ /fig\.yml/ }
@fig.up
else
restart_services(paths)
end
end
private
def restart_services(paths)
directories = paths.flat_map { |p| p.split("/").first }.uniq
services = []
linked_services = []
@config.each do |service, options|
if directories.include? options['build']
services << service
end
end
@config.each do |service, options|
debug "SERVICE: #{service}. Links #{options['links'].inspect}"
if options['links'] && options['links'].any? { |l| services.include? l }
linked_services << service
end
end
debug services.inspect
services.each do |service|
@fig.stop service
@fig.remove service
@fig.build service
end
debug linked_services.inspect
linked_services.uniq.each do |service|
@fig.stop service
end
unless services.empty?
@fig.up recreate: false
end
end
def boot2docker_setup
system "boot2docker up"
%x{boot2docker shellinit}.scan /export\s+(\S+)=(\S+)/ do |k, v|
ENV[k] = v
end
end
def debug(msg)
puts msg if ENV['DEBUG']
end
end
end
guard :fig, boot2docker_start: true, build_on_start: false do
ignore %r{\.#}
watch %r{.*}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment