generators/base_generator.rb in howitzer-2.5.0 vs generators/base_generator.rb in howitzer-2.6.0

- old
+ new

@@ -52,12 +52,12 @@ class << base attr_accessor :destination end end - def initialize(_options) - super() + def initialize + super manifest.each do |type, list| case type when :files copy_files(list) @@ -70,10 +70,16 @@ def manifest [] end + def template_context + data = options + data = options.merge(project_name: project_name) unless project_name.nil? + OpenStruct.new(data) # rubocop:disable Style/OpenStructUse + end + protected def copy_files(list) list.each do |data| source_file = source_path(data[:source]) @@ -88,11 +94,11 @@ if File.exist?(destination_path) copy_templates_file_exist(data, destination_path, source_path) else write_template(destination_path, source_path) puts_info "#{ColorizedString.new('Added').light_green} template '#{data[:source]}' with " \ - "params '#{@options}' to destination '#{data[:destination]}'" + "params '#{template_context.to_h}' to destination '#{data[:destination]}'" end end end def source_path(file_name) @@ -116,12 +122,14 @@ rescue => e puts_error("Impossible to create '#{data[:destination]}' file. Reason: #{e.message}") end def write_template(dest_path, source_path) - File.write(dest_path, ERB.new(File.read(source_path), trim_mode: '-') - .result(OpenStruct.new(@options).instance_eval { binding })) # rubocop:disable Style/OpenStructUse + File.write( + dest_path, + ERB.new(File.read(source_path), trim_mode: '-').result(template_context.instance_eval { binding }) + ) end private def get_source_and_destination(data) @@ -175,16 +183,18 @@ end end # Parent class for all generators class BaseGenerator - attr_reader :options + attr_reader :options, :args, :project_name include Outputable include Copyable - def initialize(options = {}) + def initialize(options = {}, args = []) @options = options.symbolize_keys - super + @args = args + @project_name = args.first + super() end end end