Class: RightScaleSelfService::Utilities::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/rightscale_selfservice/utilities/template.rb

Class Method Summary collapse

Class Method Details

.file_to_str_and_validate(file) ⇒ String

Validates that the specified file exists, raising an error if it does not. Then reads the file into a string which is returned

Parameters:

  • file (String)

    the path to the file which should be returned as a string

Returns:

  • (String)

    the content of the supplied file

Raises:

  • (Errno::ENOENT)

    If the file does not exist



32
33
34
# File 'lib/rightscale_selfservice/utilities/template.rb', line 32

def self.file_to_str_and_validate(file)
  cat_str = File.open(File.expand_path(file), 'r') { |f| f.read }
end

.get_include_list(file) ⇒ Hash

Returns a de duped hash of files to include by recursively parsing and looking for #include:<relative file path> in the file and all included files.

Parameters:

  • file (String)

    the path of the file to parse for includes

Returns:

  • (Hash)

    a de duped hash of include files, where the key is the fully qualified path and filename, and the values are the relative path supplied in the include statement.

Raises:

  • (Errno::ENOENT)

    if the specified file, or any included files do not exist



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rightscale_selfservice/utilities/template.rb', line 48

def self.get_include_list(file)
  dedupe_include_list = {}
  contents = file_to_str_and_validate(file)
  contents.scan(/#include:(.*)$/).each do |include|
    include_filepath = File.expand_path(include.first, File.dirname(file))
    dedupe_include_list.merge!({include_filepath => include.first})
    # This merges only the new keys by doing a diff
    child_includes_hash = get_include_list(include_filepath)
    new_keys = child_includes_hash.keys() - dedupe_include_list.keys()
    merge_these = child_includes_hash.select {|k,v| new_keys.include?(k) }
    dedupe_include_list.merge!(merge_these)
  end
  dedupe_include_list
end

.preprocess(file) ⇒ Object



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
# File 'lib/rightscale_selfservice/utilities/template.rb', line 63

def self.preprocess(file)
  parent_template = file_to_str_and_validate(file)
  dedup_include_list = get_include_list(file)

  dedup_include_list.each do |key,val|
    include_filepath = key
    include_contents = <<EOF
###############################################################################
# BEGIN Include from #{val}
###############################################################################
EOF

    include_contents += file_to_str_and_validate(key)

    include_contents += <<EOF
###############################################################################
# END Include from #{val}
###############################################################################
EOF

    parent_template.sub!("#include:#{val}",include_contents)
  end
  # Clear all include lines from templates which were included from other templates
  parent_template.gsub!(/#include:(.*)$/,"")
  parent_template
end