Class: Fontist::Import::UpgradeFormulas

Inherits:
Object
  • Object
show all
Defined in:
lib/fontist/import/upgrade_formulas.rb

Overview

Batch upgrade existing formulas to v4 schema with VF/WOFF2 support

This script upgrades existing formula files by adding optional format and variable_axes attributes to resources while maintaining full backward compatibility.

Key features:

  • Distinguishes archives from direct font files

  • Downloads and recursively extracts archives to detect fonts

  • Detects font formats (ttf, otf, woff2, ttc, otc)

  • Detects variable font axes from filenames

Constant Summary collapse

ARCHIVE_EXTENSIONS =
%w[zip tar gz tgz bz2 7z rar].freeze
FONT_EXTENSIONS =
%w[ttf otf woff woff2 ttc otc].freeze

Instance Method Summary collapse

Constructor Details

#initialize(formulas_path, options = {}) ⇒ UpgradeFormulas

Returns a new instance of UpgradeFormulas.



24
25
26
27
28
29
# File 'lib/fontist/import/upgrade_formulas.rb', line 24

def initialize(formulas_path, options = {})
  @formulas_path = formulas_path
  @verbose = options[:verbose]
  @dry_run = options[:dry_run]
  @skip_download = options[:skip_download] # Skip downloading for testing
end

Instance Method Details

#upgrade_allObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/fontist/import/upgrade_formulas.rb', line 31

def upgrade_all
  results = { upgraded: 0, skipped: 0, failed: 0, errors: [] }

  files = formula_files
  log "Found #{files.size} formula file(s) to process"

  files.each do |path|
    upgrade_formula(path)
    results[:upgraded] += 1
    log "✓ Upgraded #{File.basename(path)}"
  rescue StandardError => e
    results[:failed] += 1
    results[:errors] << { formula: path, error: e.message }
    log "✗ Failed #{File.basename(path)}: #{e.message}"
  end

  results
end

#upgrade_formula(path) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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
106
107
108
109
110
111
# File 'lib/fontist/import/upgrade_formulas.rb', line 50

def upgrade_formula(path)
  # Load formula
  formula_data = YAML.load_file(path)

  # Skip if no resources
  return unless formula_data["resources"]

  # Upgrade resources
  upgraded = false
  formula_data["resources"].each do |resource_name, resource_data|
    next unless resource_data.is_a?(Hash)

    # Check if this resource is an archive
    is_archive = archive_resource?(resource_name, resource_data)

    # Handle format attribute
    if is_archive
      # CRITICAL: Archives must NOT have format attribute
      if resource_data["format"]
        log "  Removing incorrect format from archive: #{resource_name}"
        resource_data.delete("format")
        upgraded = true
      end
      # Also remove variable_axes from archives (doesn't make sense)
      if resource_data["variable_axes"]
        log "  Removing variable_axes from archive: #{resource_name}"
        resource_data.delete("variable_axes")
        upgraded = true
      end
    else
      # This is a direct font file, add format if missing
      unless resource_data["format"]
        format = detect_format_from_resource(resource_data, resource_name)
        if format
          log "  Adding format '#{format}' to: #{resource_name}"
          resource_data["format"] = format
          upgraded = true
        end
      end

      # Detect and add variable_axes if missing
      unless resource_data["variable_axes"]
        axes = detect_axes_from_resource(resource_data, resource_name)
        if axes && !axes.empty?
          log "  Adding variable_axes #{axes.inspect} to: #{resource_name}"
          resource_data["variable_axes"] = axes
          upgraded = true
        end
      end
    end
  end

  # Save if upgraded and not dry run
  if upgraded && !@dry_run
    File.write(path, YAML.dump(formula_data))
    log "  Saved: #{File.basename(path)}"
  elsif upgraded && @dry_run
    log "  Would save: #{File.basename(path)}"
  elsif !upgraded
    log "  No changes needed: #{File.basename(path)}"
  end
end