Class: Fangorn::App

Inherits:
Object
  • Object
show all
Defined in:
lib/fangorn/app.rb

Instance Method Summary collapse

Constructor Details

#initializeApp

Returns a new instance of App.



9
10
11
# File 'lib/fangorn/app.rb', line 9

def initialize
  @port = 8080
end

Instance Method Details

#of_type(type) ⇒ Object



100
101
102
# File 'lib/fangorn/app.rb', line 100

def of_type(type)
  ->(f) { File.extname(f) == type }
end

#ordered(files) ⇒ Object



104
105
106
107
108
109
110
111
112
# File 'lib/fangorn/app.rb', line 104

def ordered(files)
  files = files.sort_by(&:length)

  sass = files.select &of_type('.sass')
  js = files.select &of_type('.js')
  haml = files.select &of_type('.haml')

  (sass + js + haml + files).uniq
end

#report(type, input, output) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/fangorn/app.rb', line 59

def report(type, input, output)
  puts <<-eos
  #{File.extname(output.to_s).upcase} #{type} @ #{Time.now.strftime("%F %T")}:
  - from: #{input}
  - to: #{output}
  eos
end

#runObject



12
13
14
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/fangorn/app.rb', line 12

def run
  options = OptionParser.new do |opts|
    opts.on('-s', '--serve', 'Watch source files for change, and serve compiled results') do
      @serve = true
    end
    opts.on('-Jsrc=dest', 'Add javascript vendor directory') do |arg|
      if m = /^(.*?)\/?=(.*?)\/?$/.match(arg)
        Haml::SCRIPT_SOURCES[m[1]] = m[2]
      end
    end
    opts.on('-d', '--dist', 'Compile a distribution package') do
      Output::dist!
    end
    opts.on('-D', '--dest=DIR', 'dest dir') do |dir|
      Output::dest = dir
    end
    opts.on('-e', '--env=ENV', 'Use environment name with fangorn.yml settings') do |env|
      Output::env = env
    end
    opts.on('-p', '--port=PORT', /^\d+$/, 'Serve on port [8080]') do |port|
      @port = port.to_i
    end
  end

  begin
    options.parse! ARGV
  rescue OptionParser::ParseError => e
    STDERR.puts e
    puts options
    exit 1
  end

  if @serve
    puts "Watching #{Output::source}"
    listener = Listen.to(Output::source, :filter => /\.(haml|sass|js|ico|jpg|png|ttf)$/, &update)
    listener.start

    puts "Starting server on port #{@port}"
    server = WEBrick::HTTPServer.new Port: @port
    server.mount '/', NoCacheFileHandler, Output::dest
    trap('INT') { server.stop }
    server.start
  else
    puts "Updating #{Output::source}"
    update[Dir[File.join(Output::source, '**/*.{haml,sass,js,ico,jpg,png,ttf}')], [], []]
  end
end

#updateObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/fangorn/app.rb', line 67

def update
  ->(modified, added, removed) do
    unless added.select {|m| m =~ /\.(js|sass)$/}.empty?
      modified += Dir[File.join(Output::source, '**/*.haml')]
    end

    mixins, modified = modified.partition {|m| File.basename(m) =~ /^_.+.sass$/}
    puts "MIXINS #{mixins}"
    unless mixins.empty?
      modified += Dir[File.join(Output::source, '**/[^_]*.sass')]
    end

    ordered(modified + added).each do |input|
      if output = Output.make(input)
        begin
          output.create!
          report 'generated', input, output
        rescue => e
          puts e
        end
      end
    end

    ordered(removed).each do |input|
      if output = Output.make(input)
        output.remove!
        report 'removed', input, output
      end
    end
  end
end