Class: RubyInstaller::Build::ErbCompiler

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/ruby_installer/build/erb_compiler.rb

Overview

This class processes a template file with ERB. The ERB template is either taken from the current directory or, if it doesn’t exist, from the gem root directory.

Defined Under Namespace

Classes: Box

Constant Summary

Constants included from Utils

Utils::GEM_ROOT, Utils::WINDOWS_CMD_SHEBANG

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#eval_file, #file, #msys_sh, #ovl_expand_file, #ovl_glob, #ovl_read_file, #q_inno, #rubyinstaller_build_gem_files, #task, #with_env, #with_sandbox_ruby

Constructor Details

#initialize(erb_file_rel, result_file_rel = nil) ⇒ ErbCompiler

Create a new ERB object to process a template.

The ERB template erb_file_rel should be a relative path. It is either taken from the current directory or, if it doesn’t exist, from the gem root directory.



43
44
45
46
47
48
49
# File 'lib/ruby_installer/build/erb_compiler.rb', line 43

def initialize(erb_file_rel, result_file_rel=nil)
  @erb_filename = erb_file_rel
  @erb_filename_abs = ovl_expand_file(erb_file_rel)
  @erb = ERB.new(File.read(@erb_filename_abs, encoding: "UTF-8"))
  @result_file_rel = result_file_rel || erb_file_rel.sub(/\.erb$/, "")
  @erb.filename = @result_file_rel
end

Instance Attribute Details

#erb_filenameObject (readonly)

Returns the value of attribute erb_filename.



36
37
38
# File 'lib/ruby_installer/build/erb_compiler.rb', line 36

def erb_filename
  @erb_filename
end

#erb_filename_absObject (readonly)

Returns the value of attribute erb_filename_abs.



37
38
39
# File 'lib/ruby_installer/build/erb_compiler.rb', line 37

def erb_filename_abs
  @erb_filename_abs
end

Instance Method Details

#result(task = nil) ⇒ Object

Returns the ERB content as String with UTF-8 encoding.

A Box instance is used as binding to process the ERB template. All method calls are redirected to the task object.



59
60
61
62
# File 'lib/ruby_installer/build/erb_compiler.rb', line 59

def result(task=nil)
  box = Box.new(self, task)
  @erb.result(box.binding)
end

#result_filenameObject



51
52
53
# File 'lib/ruby_installer/build/erb_compiler.rb', line 51

def result_filename
  @result_file_rel
end

#write_result(task = nil, filename = nil) ⇒ Object

Write the ERB result to a file in UTF-8 encoding.

See #result

If no file name is given, it is derived from the template file name by cutting the .erb extension. If the file path contains non-exising directories, they are created.



70
71
72
73
74
75
# File 'lib/ruby_installer/build/erb_compiler.rb', line 70

def write_result(task=nil, filename=nil)
  filename ||= result_filename
  FileUtils.mkdir_p File.dirname(filename)
  File.binwrite(filename, result(task))
  filename
end