Class: Coyote::Compiler

Inherits:
Object
  • Object
show all
Defined in:
lib/coyote/compiler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input, output, options = {}) ⇒ Compiler

Returns a new instance of Compiler.



10
11
12
13
14
15
# File 'lib/coyote/compiler.rb', line 10

def initialize(input, output, options={})
  @options = options || {}
  @output = output
  @bundle = Coyote::Bundle.new(output)
  @bundle.add(input)
end

Instance Attribute Details

#bundleObject (readonly)

Returns the value of attribute bundle.



8
9
10
# File 'lib/coyote/compiler.rb', line 8

def bundle
  @bundle
end

Instance Method Details

#compile!Object



17
18
19
20
# File 'lib/coyote/compiler.rb', line 17

def compile!
  save!
  watch if @options.fetch(:watch, false)
end

#save!Object



23
24
25
26
27
28
29
30
31
# File 'lib/coyote/compiler.rb', line 23

def save!
  notify "\n" + @bundle.manifest unless @options.fetch(:quiet, false)
  time = Benchmark.realtime do
    @bundle.compress! if @options.fetch(:compress, false)
    @bundle.save!
  end
  
  notify "Saved bundle to #{@output}   [#{@bundle.files.length} files] [#{'%.4f' % time} sec]", :timestamp, :success
end

#watchObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/coyote/compiler.rb', line 34

def watch
  listener = Coyote::FSListener.new

  listener.on_change do |changed_files|
    changed_files = @bundle.files & changed_files.map {|file| File.expand_path file }

    if changed_files.length > 0
      notify "Detected change, recompiling...", :warning, :timestamp
      @bundle.update! changed_files
      save!
    end
  end

  notify "Watching for changes to your bundle. ctrl+c to stop.", :timestamp
  listener.start
end