Method: Falsework::Mould.extract

Defined in:
lib/falsework/mould.rb

.extract(from, bng, to) ⇒ Object

Extract file @from into @to.

bng

A binding for eval.



342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# File 'lib/falsework/mould.rb', line 342

def self.extract from, bng, to
  t = ERB.new File.read(from.to_s)
  t.filename = from.to_s # to report errors relative to this file
  begin
    output = t.result bng
    md5_system = Digest::MD5.hexdigest(output)
  rescue Exception
    fail MouldError, "bogus template file '#{from}': #{$!}"
  end

  unless File.exists?(to)
    # write a skeleton
    begin
      FileUtils.mkdir_p File.dirname(to)
      File.open(to, 'w+') { |fp| fp.puts output }
      # transfer the exec bit to the generated result
      File.chmod(0744, to) if !defined?(FakeFS) && File.stat(from.to_s).executable?
    rescue
      fail MouldError, "cannot generate: #{$!}"
    end
  else
    # warn a careless user
    CliUtils.warnx "'#{to}' already exists in modified state" if md5_system != Digest::MD5.file(to).hexdigest
  end
end