Skip to content

Commit

Permalink
render files in situ instead of copy & rewrite (#3045)
Browse files Browse the repository at this point in the history
  • Loading branch information
johrstrom authored Sep 14, 2023
1 parent c50ec5c commit c1fc0c9
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions apps/dashboard/app/models/batch_connect/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -260,15 +260,16 @@ def stage(root, context: nil)
staged_root.tap { |p| p.mkpath unless p.exist? }

# Sync the template files over
oe, s = Open3.capture2e("rsync", "-a", "#{root}/", "#{staged_root}")
oe, s = Open3.capture2e('rsync', '-av', '--exclude', '*.erb', "#{root}/", staged_root.to_s)
raise oe unless s.success?

# Output user submitted context attributes for debugging purposes
user_defined_context_file.write(JSON.pretty_generate context.as_json)

# Render all template files using ERB
render_erb_files(
template_files,
template_files(root),
root_dir: root,
binding: TemplateBinding.new(self, context).get_binding
)
true
Expand Down Expand Up @@ -465,9 +466,10 @@ def staged_root
end

# List of template files that need to be rendered
# @param dir [#Pathname] the directory where the templates exist
# @return [Array<Pathname>] list of template files
def template_files
Pathname.glob(staged_root.join("**", "*.erb")).select(&:file?)
def template_files(dir)
Pathname.glob(dir.join("**", "*.erb")).select(&:file?)
end

# Path to script that is sourced before main script is forked
Expand Down Expand Up @@ -596,14 +598,20 @@ def job_name
].reject(&:blank?).join("/")
end

# Render a list of files using ERB
def render_erb_files(files, binding: nil, remove_extension: true)
files.each do |file|
rendered_file = remove_extension ? file.sub_ext("") : file
# Render a list of files using ERB. Note the input .erb files are the system
# installed erb files (/var/www/ood/...). These are rendered and output into
# the staged_root.
def render_erb_files(input_files, binding: nil, remove_extension: true, output_dir: staged_root, root_dir: nil)
input_files.each do |file|
template = file.read
rendered = ERB.new(template, trim_mode: "-").result(binding)
file.rename rendered_file # keep same file permissions
rendered_file.write(rendered)

relative_fname = file.to_s.delete_prefix("#{root_dir}/")
output_file = output_dir.join(relative_fname)
output_file = remove_extension ? output_file.sub_ext('') : output_file

output_file.write(rendered)
output_file.chmod(file.stat.mode)
end
end
end
Expand Down

0 comments on commit c1fc0c9

Please sign in to comment.