lib/reality/generators/template.rb in reality-generators-1.12.0 vs lib/reality/generators/template.rb in reality-generators-1.13.0
- old
+ new
@@ -146,7 +146,49 @@
def render_to_string(context_binding)
Generators.error('render_to_string not implemented')
end
end
+
+ # Base class for templates that generate a single directory
+ class SingleDirectoryOutputTemplate < Reality::Generators::Template
+ attr_reader :output_directory_pattern
+
+ def initialize(template_set, facets, target, template_key, output_directory_pattern, helpers = [], options = {})
+ super(template_set, facets, target, template_key, helpers, options)
+ @output_directory_pattern = output_directory_pattern
+ end
+
+ def output_path
+ output_directory_pattern
+ end
+
+ protected
+
+ def generate!(target_basedir, element, unprocessed_files)
+ object_name = name_for_element(element)
+ render_context = create_context(element)
+ context_binding = render_context.context_binding
+ begin
+ output_directory = eval("\"#{self.output_directory_pattern}\"", context_binding, "#{self.template_key}#Filename")
+ output_directory = File.join(target_basedir, output_directory)
+ unprocessed_files.delete_if { |f| f =~ /^#{output_directory}\/.*/ }
+
+ FileUtils.mkdir_p File.dirname(output_directory) unless File.directory?(File.dirname(output_directory))
+ generated = generate_to_directory!(output_directory, element)
+
+ if generated
+ Generators.debug "Generated #{self.name} for #{self.target} #{object_name} to #{output_directory}"
+ else
+ Generators.debug "Skipped generation of #{self.name} for #{self.target} #{object_name} to #{output_filename} due to no changes"
+ end
+ rescue => e
+ raise Generators::GeneratorError.new("Error generating #{self.name} for #{self.target} #{object_name} due to '#{e}'", e)
+ end
+ end
+
+ def generate_to_directory!(output_directory, element)
+ Generators.error('generate_to_directory! not implemented')
+ end
+ end
end
end