Class: Guard::Mirror

Inherits:
Guard
  • Object
show all
Defined in:
lib/guard/mirror.rb

Instance Method Summary collapse

Constructor Details

#initialize(watchers = [], options = {}) ⇒ Mirror

Returns a new instance of Mirror.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/guard/mirror.rb', line 15

def initialize watchers = [], options = {}
  super

  @options = {
    notify: true
  }.merge options

  @env = ::Sprockets::Environment.new

  # CoffeeScript is baked into sprockets so we can skip that, but register
  # these other guys.
  @env.register_mime_type 'text/html', '.html'

  # The problem (awesomeness) with Jade is that it can export HTML or an
  # anonymous function for use as a JST. Use `.html.jade` for HTML and
  # `.jst.jade` for a jade template.
  @env.register_engine '.jade', ::Jade::Template

  @env.register_engine '.styl', ::Tilt::StylusTemplate

  # Turn on nib on demand
  Stylus.use :nib if @options[:nib]

  @options[:paths].each { |path| @env.append_path path }

  enable_compression if @options[:compress] == true

end

Instance Method Details

#run_all(compress = true) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/guard/mirror.rb', line 49

def run_all compress = true
  compress = (compress && @options[:compress] == :run_all) ||
    @options[:compress] == true
  UI.info "Mirroring #{compress ? 'and compressing ' : ''}all files..."
  if @options[:target]
    paths = [@options[:target]]
  else
    paths = @env.paths.map do |path|
      Dir[File.join path, '**{,/*/**}.*'].map do |file|
        file[path.length + 1 .. -1]
      end
    end.flatten
  end
  enable_compression if compress
  run_on_changes paths
  disable_compression unless @options[:compress] == true
end

#run_on_changes(paths) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/guard/mirror.rb', line 67

def run_on_changes paths
  (@options[:target] ? [@options[:target]] : paths).each do |path|
    dest = src_to_dest path
    dirname = File.dirname dest
    UI.info "IN -> #{path}..."
    FileUtils.mkdir_p dirname unless File.directory? dirname
    File.open(dest, 'w') do |f|
      f.write(
        begin
          @env[path]
        rescue => e
          if @options[:notify]
            Notifier.notify e.message,
              title: 'guard-mirror',
              image: :failed
          end
          UI.error e.message
          e.message
        end
      )
    end
    UI.info "OUT -> #{dest}"
  end
end

#startObject



44
45
46
47
# File 'lib/guard/mirror.rb', line 44

def start
  UI.info 'A mirror has started.'
  run_all false
end