Class: Walrus::Runner

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

Defined Under Namespace

Classes: ArgumentError, Error

Instance Method Summary collapse

Constructor Details

#initializeRunner

Returns a new instance of Runner.

Raises:



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
74
75
76
77
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
122
123
124
125
126
127
128
129
130
# File 'lib/walrus/runner.rb', line 29

def initialize
  @options = OpenStruct.new
  @options.output_dir         = nil
  @options.input_extension    = 'tmpl'
  @options.output_extension   = 'html'
  @options.recurse            = false
  @options.backup             = true
  @options.force              = false
  @options.debug              = false
  @options.halt               = false
  @options.dry                = false
  @options.verbose            = false

  @command  = nil # "compile", "fill" (saves to disk), "run" (prints to standard out)
  @inputs   = []  # list of input files and/or directories
  parser    = OptionParser.new do |o|
    o.banner  = "Usage: #{o.program_name} command input-file(s)-or-directory/ies [options]"
    o.separator ''
    o.separator '   _____________'
    o.separator '  /             \\'
    o.separator " /     o   o     \\  #{o.program_name}"
    o.separator ' |       b       |  Command-line front-end for the Walrus templating system'
    o.separator '  \\   ~-----~   /   Copyright 2007 Wincent Colaiuta'
    o.separator '   \\ / /   \\ \\ /'
    o.separator '    / /-----\\ \\'
    o.separator '   |_/       \\_|'
    o.separator ''
    o.separator 'Commands: compile -- compile templates to Ruby code'
    o.separator '          fill    -- runs compiled templates, writing output to disk'
    o.separator '          run     -- runs compiled templates, printing output to standard output'
    o.separator ''

    o.on('-o', '--output-dir DIR', 'Output directory (when filling)', 'defaults to same directory as input file') do |opt|
      @options.output_dir = Pathname.new(opt)
    end

    o.on('-i', '--input-extension EXT', 'Extension for input file(s)', 'default: tmpl') do |opt|
      @options.input_extension = opt
    end

    o.on('-e', '--output-extension EXT', 'Extension for output file(s) (when filling)', 'default: html') do |opt|
      @options.output_extension = opt
    end

    o.on('-R', 'Search subdirectories recursively for input files', 'default: on') do |opt|
      @options.recurse = opts
    end

    o.on('-b', '--[no-]backup', 'Make backups before overwriting', 'default: on') do |opt|
      @options.backup = opt
    end

    o.on('-f', '--force', 'Force a recompile (when filling)', 'default: off (files only recompiled if source newer than output)') do |opt|
      @options.force = opt
    end

    o.on('--halt', 'Halts on encountering an error (even a non-fatal error)', 'default: off') do |opt|
      @options.halt = opt
    end

    o.on('-t', '--test', 'Performs a "dry" (test) run', 'default: off') do |opt|
      @options.dry = opt
    end

    o.on('-d', '--debug', 'Print debugging information to standard error', 'default: off') do |opt|
      @options.debug = opt
    end

    o.on('-v', '--verbose', 'Run verbosely', 'default: off') do |opt|
      @options.verbose = opt
    end

    o.separator ''

    o.on_tail('-h', '--help', 'Show this message') do
      $stderr.puts o
      exit
    end

    o.on_tail('--version', 'Show version') do
      $stderr.puts 'Walrus ' + Walrus::VERSION
      exit
    end
  end

  begin
    parser.parse!
  rescue OptionParser::InvalidOption => e
    raise ArgumentError.new(e)
  end

  parser.order! do |item|
    if @command.nil?
      @command = item               # get command first ("compile", "fill" or "run")
    else
      @inputs << Pathname.new(item) # all others (and there must be at least one) are file or directory names
    end
  end

  raise ArgumentError.new('no command specified') if @command.nil?
  raise ArgumentError.new('no inputs specified') unless @inputs.length > 0
end

Instance Method Details

#adjusted_output_path(path) ⇒ Object



280
281
282
283
284
285
286
287
288
289
290
# File 'lib/walrus/runner.rb', line 280

def adjusted_output_path(path)
  if @options.output_dir
    if path.absolute?
      path = @options.output_dir + path.to_s.sub(/\A\//, '')
    else
      path = @options.output_dir + path
    end
  else
    path
  end
end

#compile(input, force = true) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/walrus/runner.rb', line 203

def compile(input, force = true)
  template_source_path  = template_source_path_for_input(input)
  compiled_path         = compiled_source_path_for_input(input)
  if force or @options.force or not compiled_path.exist? or compiled_path_older_than_source_path(compiled_path, template_source_path)
    begin
      template = Template.new(template_source_path)
    rescue Exception => e
      handle_error("failed to read input template '#{template_source_path}' (#{e.to_s})")
      return
    end

    begin
      compiled = template.compile
    rescue Grammar::ParseError => e
      handle_error("failed to compile input template '#{template_source_path}' (#{e.to_s})")
      return
    end

    write_string_to_path(compiled, compiled_path, true)
  end
end

#compile_if_needed(input) ⇒ Object



189
190
191
# File 'lib/walrus/runner.rb', line 189

def compile_if_needed(input)
  compile(input, false)
end

#compiled_path_older_than_source_path(compiled_path, source_path) ⇒ Object



193
194
195
196
197
198
199
200
201
# File 'lib/walrus/runner.rb', line 193

def compiled_path_older_than_source_path(compiled_path, source_path)
  begin
    compiled  = File.mtime(compiled_path)
    source    = File.mtime(source_path)
  rescue SystemCallError # perhaps one of them doesn't exist
    return true
  end
  compiled < source
end

#compiled_source_path_for_input(input) ⇒ Object



302
303
304
305
306
307
308
309
310
311
312
# File 'lib/walrus/runner.rb', line 302

def compiled_source_path_for_input(input)
  # remove input extension if present
  if input.extname == ".#{@options.input_extension}" and @options.input_extension.length > 0
    dir, base = input.split
    input = dir + base.basename(base.extname)
  end

  # add rb as an extension
  dir, base = input.split
  dir + "#{base.to_s}.rb"
end

#expand(inputs) ⇒ Object

Expects an array of Pathname objects. Directory inputs are themselves recursively expanded if the “recurse” option is set to true; otherwise only their top-level entries are expanded. Returns an expanded array of Pathname objects.



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/walrus/runner.rb', line 169

def expand(inputs)
  expanded = []
  inputs.each do |input|
    if input.directory?
      input.entries.each do |entry|
        if entry.directory?
          if @options.recurse
            expanded.concat expand(entry.entries)
          end
        else # not a directory
          expanded << entry
        end
      end
    else # not a directory
      expanded << input
    end
  end
  expanded
end

#filled_output_path_for_input(input) ⇒ Object



314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/walrus/runner.rb', line 314

def filled_output_path_for_input(input)
  # remove input extension if present
  if input.extname == ".#{@options.input_extension}" and @options.input_extension.length > 0
    dir, base = input.split
    input = dir + base.basename(base.extname)
  end

  # add output extension if appropriate
  if @options.output_extension.length > 0
    dir, base = input.split
    adjusted_output_path(dir + "#{base.to_s}.#{@options.output_extension}")
  else
    adjusted_output_path(input)
  end
end

#get_output(input) ⇒ Object



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/walrus/runner.rb', line 225

def get_output(input)
  if @options.dry
    "(no output: dry run)\n"
  else
    # use Wopen3 (backticks choke if there is a space in the path, open3 throws away the exit status)
    output = ''
    Wopen3.popen3([compiled_source_path_for_input(input).realpath, '']) do |stdin, stdout, stderr|
      threads = []
      threads << Thread.new(stdout) do |out|
        out.each { |line| output << line }
      end
      threads << Thread.new(stderr) do |err|
        err.each { |line| STDERR.puts line }
      end
      threads.each { |thread| thread.join }
    end
    status = $?.exitstatus
    raise SystemCallError.new("non-zero exit status (#{status})") if status != 0
    output
  end
end

#runObject



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/walrus/runner.rb', line 132

def run
  log "Beginning processing: #{Time.new.to_s}."

  # TODO: flush memoizing cache after each file

  expand(@inputs).each do |input|
    case @command
    when 'compile'
      log "Compiling '#{input}'."
      compile(input)
    when 'fill'
      log "Filling '#{input}'."
      compile_if_needed(input)
      begin
        write_string_to_path(get_output(input), filled_output_path_for_input(input))
      rescue Exception => e
        handle_error(e)
      end
    when 'run'
      log "Running '#{input}'."
      compile_if_needed(input)
      begin
        printf('%s', get_output(input))
        $stdout.flush
      rescue Exception => e
        handle_error(e)
      end
    else
      raise ArgumentError.new("unrecognized command '#{@command}'")
    end
  end
  log "Processing complete: #{Time.new.to_s}."
end

#template_source_path_for_input(input) ⇒ Object

If “input” already has the right extension it is returned unchanged. If the “input extension” is zero-length then “input” is returned unchanged. Otherwise the “input extension” is added to “input” and returned.



295
296
297
298
299
300
# File 'lib/walrus/runner.rb', line 295

def template_source_path_for_input(input)
  return input if input.extname == ".#{@options.input_extension}" # input already has the right extension
  return input if @options.input_extension.length == 0            # zero-length extension, nothing to add
  dir, base = input.split
  dir + "#{base.to_s}.#{@options.input_extension}"                # otherwise, add extension and return
end

#write_string_to_path(string, path, executable = false) ⇒ Object



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/walrus/runner.rb', line 247

def write_string_to_path(string, path, executable = false)
  if @options.dry
    log "Would write '#{path}' (dry run)."
  else
    unless path.dirname.exist?
      begin
        log "Creating directory '#{path.dirname}'."
        FileUtils.mkdir_p path.dirname
      rescue SystemCallError => e
        handle_error(e)
        return
      end
    end

    log "Writing '#{path}'."
    begin
      File.open(path, "a+") do |f|
        if not File.zero? path and @options.backup
          log "Making backup of existing file at '#{path}'."
          dir, base = path.split
          FileUtils.cp path, dir + "#{base.to_s}.bak"
        end
        f.flock File::LOCK_EX
        f.truncate 0
        f.write string
        f.chmod 0744 if executable
      end
    rescue SystemCallError => e
      handle_error(e)
    end
  end
end