Class: Buho::Watcher

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

Overview

Watcher class

Instance Method Summary collapse

Instance Method Details

#compile(path) ⇒ Object

Compile HAML



126
127
128
129
130
131
132
133
134
135
# File 'lib/buho.rb', line 126

def compile(path)
  begin
    # Read HAML and compile it
    haml = File.open(path).read
    @compiled = Haml::Engine.new(haml).render
  rescue Haml::Error => e
    #Error compiling message
    puts ">> Error: \"#{e.message}\" at line #{e.line}"
  end
end

#optsObject

Configure CLI options



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/buho.rb', line 78

def opts

  # Handle CLI options
  OptionParser.new do |opts|

    # Set separator
    opts.separator nil

    # Option to set input directory or raise a exception if not exists
    opts.on('-i', '--input [dir]', String, 'Set input directory.', 'If omit this option, input path has been used instead') do |i|
      begin
        @input = File.join('.', i)
        raise IOError unless File.exists? @input
      rescue IOError
        puts ">> Error: Input directory not found\n\n"
        exit
      end
    end

    # Option to set output directory or raise a exception if not exists
    opts.on('-o', '--output [dir]', String, 'Set output directory.') do |o|
      @output = File.join('.', o)
    end

    # Show help info
    opts.on('-h', '--help', 'Show help info.') do
      puts opts;
      exit
    end

    # Parse!
    opts.parse!
  end

  begin
    raise OptionParser::MissingArgument if @input == nil
  rescue OptionParser::MissingArgument
    puts ">> Error: Missing argument --input\n\n"
    exit
  end

  # If output dir has not been defined, use input dir instead
  @output ||= @input
end

#watchObject

Watch changes, compile and save it



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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/buho.rb', line 23

def watch

  # Input dir
  @input  = nil

  # Output dir
  @output = nil

  # Compiled HTML
  @compiled = nil

  # Gem name
  puts "\n","-" * 41
  puts " Buho #{Buho::VERSION}"
  puts " Copyright (c) 2013 Diego Saint Esteben"
  puts "-" * 41, "\n"

  # Options
  self.opts

  # Print input/output paths
  puts "Input dir: #{@input}"
  puts "Output dir: #{@output}"

  # Ready to watch..
  puts "\nReady to watch...\n\n"

  # Listen
  Listen.to(@input, :filter => /\.haml$/, :relative_paths => true) do |modified, added, removed|

    # Walk into modified files
    modified.each do |path|

      # Paths
      input  = File.join(@input, path)
      output = File.join(@output, path).gsub!('.haml', '.html')

      # Watching message
      puts ">> Watching #{input}"

      # Compile HAML file and save in @compiled
      self.compile input

      # Write HTML into output path
      self.write output

      # Compilation successfully
      puts ">> Compiling #{input} into #{output}"
    end
  end
end

#write(path) ⇒ Object

Write compiled HTML into HTML file



140
141
142
143
144
145
146
147
148
149
150
# File 'lib/buho.rb', line 140

def write(path)

  # Create directory tree
  dirname = File.dirname(path)
  FileUtils.mkdir_p(dirname)

  # Save compiled HTML into file
  File.open(path, 'w') do |f|
    f << @compiled
  end
end