Module: Sourcerer::Builder

Defined in:
lib/sourcerer/builder.rb

Class Method Summary collapse

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.

Parameters:

  • attributes (Array<Hash>)

    The attribute sources.

Returns:

  • (Hash)

    The built attributes.



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.

Parameters:

  • entries (Array<Hash>)

    The entries to build.

  • type (Symbol)

    The type of output (‘:snippet` or `:region`).

Returns:

  • (Hash)

    A lookup hash mapping names to output filenames.



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|
    entry_options = normalize_output_entry(
      entry,
      type: type,
      names: names,
      outnames: outnames)

    names << entry_options[:name]
    outnames << entry_options[:outname]

    text = extract_output_text(
      type: type,
      source: entry_options[:source],
      tags: entry_options[:tags])

    lookup[entry_options[:name].to_s] = entry_options[:outname]
    write_output_file(type: type, outname: entry_options[: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.

Parameters:

  • name (String)

    The name of the output.

  • type (Symbol)

    The type of output.

Returns:

  • (String)

    The default filename.



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

Raises:

  • (ArgumentError)


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: {}, **options
  supported_option_keys = %i[attributes snippets regions templates render]
  unknown_option_keys = options.keys - supported_option_keys
  raise ArgumentError, "unknown option(s): #{unknown_option_keys.join(', ')}" unless unknown_option_keys.empty?

  attributes = options.fetch(:attributes, [])
  snippets = options.fetch(:snippets, [])
  regions = options.fetch(:regions, [])
  _templates = options.fetch(:templates, [])
  _render_entries = options.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