Class: Fontico::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/fontico/builder.rb

Overview

Resolve -> preprocess -> lock -> emit. Everything the rake task does.

Defined Under Namespace

Classes: Report

Constant Summary collapse

PENDING =

Declared in manifests, emitter not landed yet. Skipped with a notice so the manifest can state intent without breaking the build.

%w[woff2].freeze

Instance Method Summary collapse

Constructor Details

#initialize(manifest, root: Dir.pwd, output: "app/assets/builds", offline: false) ⇒ Builder

Returns a new instance of Builder.



15
16
17
18
19
20
21
# File 'lib/fontico/builder.rb', line 15

def initialize(manifest, root: Dir.pwd, output: "app/assets/builds", offline: false)
  @manifest = manifest
  @root = root
  @output = File.join(root, output)
  @offline = offline
  @lock = Lockfile.new(File.join(root, "icons.lock"))
end

Instance Method Details

#callObject



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
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/fontico/builder.rb', line 23

def call
  warnings = Hash.new { |h, k| h[k] = [] }
  cached, fetched = [], []
  missing = {}

  stale = @manifest.icons.reject { @lock.fresh?(_1.name, _1.source) }
  cached = @manifest.icons.map(&:name) - stale.map(&:name)

  unless stale.empty?
    raise Error, "icons.lock is missing #{stale.size} icon(s) and --offline was given" if @offline

    resolver = Resolver.new(@manifest, root: @root)
    sources = resolver.call(only: stale.map(&:name))
    missing = resolver.missing
    stale.each do |icon|
      src = sources[icon.name]
      # Named in #missing already, and reported by the caller. It keeps
      # its codepoint and whatever the lock still holds, so fixing the
      # manifest entry is all it takes to bring it back.
      next if src.nil?

      pre = Preprocessor.new(icon, size: @manifest.size)
                        .call(src.markup, width: src.width, height: src.height)
      @lock.store(icon.name, source: icon.source, body: pre.body,
                  multicolor: pre.multicolor, warnings: pre.warnings)
      fetched << icon.name
    end
  end

  # Missing icons stay in the list: their codepoints must not be reissued
  # while the manifest still claims them.
  @lock.retire_missing!(@manifest.icons.map(&:name))
  @lock.save!

  buildable = @manifest.icons.reject { missing.key?(_1.name) }
  buildable.each do |icon|
    found = @lock.warnings(icon.name)
    warnings[icon.name] = found if found.any?
  end

  written, skipped = emit(buildable)
  Report.new(written: written, warnings: warnings, skipped: skipped,
             cached: cached, fetched: fetched, missing: missing,
             pending: @manifest.targets & PENDING)
end