Class: KuberKit::Preprocessing::FilePreprocessor

Inherits:
Object
  • Object
show all
Defined in:
lib/kuber_kit/preprocessing/file_preprocessor.rb

Constant Summary collapse

PreprocessingError =
Class.new(KuberKit::Error)

Instance Method Summary collapse

Instance Method Details

#compile(shell, source_path, destination_path: nil, context_helper: nil) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/kuber_kit/preprocessing/file_preprocessor.rb', line 9

def compile(shell, source_path, destination_path: nil, context_helper: nil)
  destination_path ||= source_path

  prepare_destination_dir(shell, destination_path)

  template = shell.read(source_path)
  content = text_preprocessor.compile(template, context_helper: context_helper)

  is_content_changed = template != content
  if !is_content_changed && source_path == destination_path
    return false
  end

  shell.write(destination_path, content)

  return true
rescue Exception => e
  message = "#{e.message}\r\n"
  message += e.backtrace.select{|l| l.include?("(erb)") }.join("\r\n")
  raise PreprocessingError, "Error while processing template #{source_path}.\r\n#{message}"
end

#prepare_destination_dir(shell, destination_path) ⇒ Object



31
32
33
# File 'lib/kuber_kit/preprocessing/file_preprocessor.rb', line 31

def prepare_destination_dir(shell, destination_path)
  bash_commands.mkdir_p(shell, File.dirname(destination_path))
end