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.


53
54
55
56
57
58
59
# File 'lib/sourcerer/builder.rb', line 53

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.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.


66
67
68
69
70
71
72
73
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
100
101
102
103
104
105
# File 'lib/sourcerer/builder.rb', line 66

def self.build_outputs entries, type:
  lookup   = {}
  names    = []
  outnames = []

  entries.each do |entry|
    source = entry[:source] or raise ArgumentError, "#{type} entry is missing :source"
    tag    = entry[:tag]
    tags   = entry[:tags]

    raise ArgumentError, 'use only one of :tag or :tags' if tag && tags
    raise ArgumentError, "#{type} must include a :tag or :tags" unless tag || tags

    name    = entry[:name] || tag || File.basename(source, '.adoc')
    outname = entry[:out]  || default_output_name(name, type)

    raise ArgumentError, "name value must be unique; #{name} already used"   if names.include? name
    raise ArgumentError, "out value must be unique; #{outname} already used" if outnames.include? outname

    names    << name
    outnames << outname

    tags = [tag] if tag

    text =
      case type
      when :snippet then Sourcerer.load_include(source, tags: tags)
      when :region  then Sourcerer.extract_tagged_content(source, tags: tags)
      else raise ArgumentError, "Unsupported type: #{type}"
      end

    lookup[name.to_s] = outname

    outpath = File.join("build/#{type}s", outname)
    FileUtils.mkdir_p File.dirname(outpath)
    File.write(outpath, 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.


112
113
114
115
116
117
118
# File 'lib/sourcerer/builder.rb', line 112

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: {}, attributes: [], snippets: [], regions: [], templates: [], render: []) ⇒ Object



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
# File 'lib/sourcerer/builder.rb', line 21

def self.generate_prebuild generated: {}, attributes: [], snippets: [], regions: [], templates: [], render: []
  # rubocop:enable Lint/UnusedMethodArgument
  # 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