Class: Kdeploy::Template

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

Overview

ERB template rendering and upload handler

Class Method Summary collapse

Class Method Details

.create_template_context(variables) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/kdeploy/template.rb', line 15

def self.create_template_context(variables)
  # Use a simple class instead of OpenStruct for better performance
  context_class = Class.new
  variables.each do |key, value|
    context_class.define_method(key) { value }
  end
  context_class.new
end

.render(template_path, variables = {}) ⇒ Object



9
10
11
12
13
# File 'lib/kdeploy/template.rb', line 9

def self.render(template_path, variables = {})
  template_content = File.read(template_path)
  context = create_template_context(variables)
  ERB.new(template_content).result(context.instance_eval { binding })
end

.render_and_upload(executor, template_path, destination, variables = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/kdeploy/template.rb', line 24

def self.render_and_upload(executor, template_path, destination, variables = {})
  rendered_content = render(template_path, variables)

  # 创建临时文件
  temp_file = Tempfile.new('kdeploy')
  begin
    temp_file.write(rendered_content)
    temp_file.close

    # 上传渲染后的文件
    executor.upload(temp_file.path, destination)
  ensure
    temp_file.unlink
  end
end