Module: Sourcerer::Builder
- Defined in:
- lib/sourcerer/builder.rb
Class Method Summary collapse
-
.build_attributes(attributes) ⇒ Hash
private
Builds a hash of attributes from the given sources.
-
.build_outputs(entries, type:) ⇒ Hash
private
Builds output files from snippets or regions and returns a lookup hash.
-
.default_output_name(name, type) ⇒ String
private
Determines the default output filename for a given name and type.
- .generate_prebuild(generated: {}, **options) ⇒ Object
Class Method Details
.build_attributes(attributes) ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Builds a hash of attributes from the given sources.
61 62 63 64 65 66 67 |
# File 'lib/sourcerer/builder.rb', line 61 def self.build_attributes attributes attributes.each_with_object({}) do |entry, acc| source = entry[:source] name = entry[:name] || File.basename(source, '.adoc').to_sym acc[name.to_sym] = Sourcerer::AsciiDoc.load_attributes(source) end end |
.build_outputs(entries, type:) ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Builds output files from snippets or regions and returns a lookup hash.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/sourcerer/builder.rb', line 74 def self.build_outputs entries, type: lookup = {} names = [] outnames = [] entries.each do |entry| = normalize_output_entry( entry, type: type, names: names, outnames: outnames) names << [:name] outnames << [:outname] text = extract_output_text( type: type, source: [:source], tags: [:tags]) lookup[[:name].to_s] = [:outname] write_output_file(type: type, outname: [:outname], text: text) end lookup end |
.default_output_name(name, type) ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Determines the default output filename for a given name and type.
106 107 108 109 110 111 112 |
# File 'lib/sourcerer/builder.rb', line 106 def self.default_output_name name, type case type when :snippet then "#{name}.txt" when :region then "#{name}.adoc" else raise ArgumentError, "Unknown type: #{type}" end end |
.generate_prebuild(generated: {}, **options) ⇒ Object
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 |
# File 'lib/sourcerer/builder.rb', line 20 def self.generate_prebuild generated: {}, ** supported_option_keys = %i[attributes snippets regions templates render] unknown_option_keys = .keys - supported_option_keys raise ArgumentError, "unknown option(s): #{unknown_option_keys.join(', ')}" unless unknown_option_keys.empty? attributes = .fetch(:attributes, []) snippets = .fetch(:snippets, []) regions = .fetch(:regions, []) _templates = .fetch(:templates, []) _render_entries = .fetch(:render, []) # NOTE: templates/render parameters are accepted from config but handled separately by Sourcerer.render_outputs attr_result = build_attributes(attributes) snippet_lookup = build_outputs(snippets, type: :snippet) region_lookup = build_outputs(regions, type: :region) File.write(generated[:path].to_s, <<~RUBY) # frozen_string_literal: true # Auto-generated by Sourcerer::Builder module #{generated[:module]} ATTRIBUTES = #{attr_result.inspect} SNIPPET_LOOKUP = #{snippet_lookup.inspect} REGION_LOOKUP = #{region_lookup.inspect} def self.read_built_snippet name fname = SNIPPET_LOOKUP[name.to_s] || name.to_s path = File.expand_path("../../../build/snippets/\#{fname}", __FILE__) raise "Snippet not found: \#{name}" unless File.exist?(path) File.read(path) end end RUBY end |