Class: InspecPlugins::Init::Renderer
- Inherits:
-
Object
- Object
- InspecPlugins::Init::Renderer
- Defined in:
- lib/plugins/inspec-init/lib/inspec-init/renderer.rb
Instance Attribute Summary collapse
-
#overwrite_mode ⇒ Object
readonly
Creates a renderer able to render the given template type 1.
-
#ui ⇒ Object
readonly
Creates a renderer able to render the given template type 1.
Instance Method Summary collapse
-
#initialize(cli_ui, cli_options = {}) ⇒ Renderer
constructor
A new instance of Renderer.
-
#render(template_content, hash) ⇒ Object
This is a render helper to bind hash values to a ERB template ERB provides result_with_hash in ruby 2.5.0+, which does exactly this.
-
#render_with_values(template_type, template_values = {}) ⇒ Object
rubocop: disable Metrics/AbcSize.
Constructor Details
#initialize(cli_ui, cli_options = {}) ⇒ Renderer
Returns a new instance of Renderer.
13 14 15 16 |
# File 'lib/plugins/inspec-init/lib/inspec-init/renderer.rb', line 13 def initialize(cli_ui, = {}) @ui = cli_ui @overwrite_mode = ['overwrite'] end |
Instance Attribute Details
#overwrite_mode ⇒ Object (readonly)
Creates a renderer able to render the given template type
-
iterate over all files
-
read content in erb
-
write to full_destination_root_path
12 13 14 |
# File 'lib/plugins/inspec-init/lib/inspec-init/renderer.rb', line 12 def overwrite_mode @overwrite_mode end |
#ui ⇒ Object (readonly)
Creates a renderer able to render the given template type
-
iterate over all files
-
read content in erb
-
write to full_destination_root_path
12 13 14 |
# File 'lib/plugins/inspec-init/lib/inspec-init/renderer.rb', line 12 def ui @ui end |
Instance Method Details
#render(template_content, hash) ⇒ Object
This is a render helper to bind hash values to a ERB template ERB provides result_with_hash in ruby 2.5.0+, which does exactly this
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/plugins/inspec-init/lib/inspec-init/renderer.rb', line 64 def render(template_content, hash) # create a new binding class cls = Class.new do hash.each do |key, value| define_method key.to_sym do value end end # expose binding define_method :bind do binding end end ERB.new(template_content).result(cls.new.bind) end |
#render_with_values(template_type, template_values = {}) ⇒ Object
rubocop: disable Metrics/AbcSize
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/plugins/inspec-init/lib/inspec-init/renderer.rb', line 19 def render_with_values(template_type, template_values = {}) # look for template directory base_dir = File.join(File.dirname(__FILE__), 'templates', template_type) # prepare glob for all subdirectories and files template_glob = File.join(base_dir, '**', '{*,.*}') # Use the name attribute to define the path to the profile. profile_path = template_values[:name] # Use slashes (\, /) to split up the name into an Array then use the last entry # to reset the name of the profile. template_values[:name] = template_values[:name].split(%r{\\|\/}).last # Generate the full full_destination_root_path path on disk full_destination_root_path = Pathname.new(Dir.pwd).join(profile_path) ui.plain_text "Create new #{template_type} at #{ui.mark_text(full_destination_root_path)}" # check that the directory does not exist if File.exist?(full_destination_root_path) && !overwrite_mode ui.plain_text "#{ui.mark_text(full_destination_root_path)} exists already, use --overwrite" ui.exit(1) end # ensure that full_destination_root_path directory is available FileUtils.mkdir_p(full_destination_root_path) # iterate over files and write to full_destination_root_path Dir.glob(template_glob) do |file| relative_destination_item_path = Pathname.new(file).relative_path_from(Pathname.new(base_dir)) full_destination_item_path = Pathname.new(full_destination_root_path).join(relative_destination_item_path) if File.directory?(file) ui.li "Create directory #{ui.mark_text(relative_destination_item_path)}" FileUtils.mkdir_p(full_destination_item_path) elsif File.file?(file) ui.li "Create file #{ui.mark_text(relative_destination_item_path)}" # read & render content content = render(File.read(file), template_values) # write file content File.write(full_destination_item_path, content) else ui.plain_text "Ignore #{file}, because its not an file or directoy" end end end |