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
- 
  
    
      #file_rename_map  ⇒ Object 
    
    
  
  
  
  
    
      readonly
    
    
  
  
  
  
  
  
    Creates a renderer able to render the given template type 1. 
- 
  
    
      #overwrite_mode  ⇒ Object 
    
    
  
  
  
  
    
      readonly
    
    
  
  
  
  
  
  
    Creates a renderer able to render the given template type 1. 
- 
  
    
      #skip_files  ⇒ Object 
    
    
  
  
  
  
    
      readonly
    
    
  
  
  
  
  
  
    Creates a renderer able to render the given template type 1. 
- 
  
    
      #templates_path  ⇒ 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, 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_subdir_path, template_type, template_values = {})  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    rubocop: disable Metrics/AbcSize. 
Constructor Details
#initialize(cli_ui, options = {}) ⇒ Renderer
Returns a new instance of Renderer.
| 13 14 15 16 17 18 19 | # File 'lib/plugins/inspec-init/lib/inspec-init/renderer.rb', line 13 def initialize(cli_ui, = {}) @ui = cli_ui @overwrite_mode = [:overwrite] @templates_path ||= [:templates_path] @file_rename_map = [:file_rename_map] || {} @skip_files = [:skip_files] || [] end | 
Instance Attribute Details
#file_rename_map ⇒ Object (readonly)
Creates a renderer able to render the given template type
- 
iterate over all files 
- 
read content in erb 
- 
write to destination path 
| 12 13 14 | # File 'lib/plugins/inspec-init/lib/inspec-init/renderer.rb', line 12 def file_rename_map @file_rename_map end | 
#overwrite_mode ⇒ Object (readonly)
Creates a renderer able to render the given template type
- 
iterate over all files 
- 
read content in erb 
- 
write to destination path 
| 12 13 14 | # File 'lib/plugins/inspec-init/lib/inspec-init/renderer.rb', line 12 def overwrite_mode @overwrite_mode end | 
#skip_files ⇒ Object (readonly)
Creates a renderer able to render the given template type
- 
iterate over all files 
- 
read content in erb 
- 
write to destination path 
| 12 13 14 | # File 'lib/plugins/inspec-init/lib/inspec-init/renderer.rb', line 12 def skip_files @skip_files end | 
#templates_path ⇒ Object (readonly)
Creates a renderer able to render the given template type
- 
iterate over all files 
- 
read content in erb 
- 
write to destination path 
| 12 13 14 | # File 'lib/plugins/inspec-init/lib/inspec-init/renderer.rb', line 12 def templates_path @templates_path end | 
#ui ⇒ Object (readonly)
Creates a renderer able to render the given template type
- 
iterate over all files 
- 
read content in erb 
- 
write to destination 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
| 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | # File 'lib/plugins/inspec-init/lib/inspec-init/renderer.rb', line 80 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_subdir_path, template_type, template_values = {}) ⇒ Object
rubocop: disable Metrics/AbcSize
| 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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | # File 'lib/plugins/inspec-init/lib/inspec-init/renderer.rb', line 22 def render_with_values(template_subdir_path, template_type, template_values = {}) # look for template directory source_dir = File.join(templates_path, template_subdir_path) # prepare glob for all subdirectories and files template_glob = File.join(source_dir, "**", "{*,.*}") # Use the name attribute to define the path to the new thing. # May contain slashes. relative_destination_path = template_values[:name] # Now reset the :name variable to be the basename. # This is important in profiles, for example. template_values[:name] = File.basename(template_values[:name]) # Generate the full full_destination_path path on disk full_destination_path = Pathname.new(Dir.pwd).join(relative_destination_path) # check that the directory does not exist if File.exist?(full_destination_path) && !overwrite_mode ui.plain_line "#{ui.emphasis(full_destination_path)} exists already, use --overwrite" ui.exit(:usage_error) end ui.headline("InSpec Code Generator") ui.plain_line "Creating new #{template_type} at #{ui.emphasis(full_destination_path)}" # ensure that full_destination_root_path directory is available FileUtils.mkdir_p(full_destination_path) # iterate over files and write to full_destination_path Dir.glob(template_glob) do |source_file| relative_destination_item_path = Pathname.new(source_file).relative_path_from(Pathname.new(source_dir)).to_s next if skip_files.include? relative_destination_item_path relative_destination_item_path = file_rename_map[relative_destination_item_path] || relative_destination_item_path full_destination_item_path = Pathname.new(full_destination_path).join(relative_destination_item_path) if File.directory?(source_file) ui.list_item "Creating directory #{ui.emphasis(relative_destination_item_path)}" FileUtils.mkdir_p(full_destination_item_path) elsif File.file?(source_file) ui.list_item "Creating file #{ui.emphasis(relative_destination_item_path)}" # read & render content content = render(File.read(source_file), template_values) # write file content File.write(full_destination_item_path, content) else ui.warning "Ignoring #{ui.emphasis(source_file)}, because its not an file or directoy" end end ui.plain_line end |