Class: Fontist::Macos::Catalog::CatalogManager

Inherits:
Object
  • Object
show all
Defined in:
lib/fontist/macos/catalog/catalog_manager.rb

Overview

Manages macOS Font catalogs across different versions Provides discovery, version detection, and parser selection Catalogs are downloaded from Apple’s servers

Constant Summary collapse

CATALOG_URLS =

Catalog URLs for different versions

{
  3 => "https://mesu.apple.com/assets/macos/com_apple_MobileAsset_Font3/com_apple_MobileAsset_Font3.xml",
  4 => "https://mesu.apple.com/assets/macos/com_apple_MobileAsset_Font4/com_apple_MobileAsset_Font4.xml",
  5 => "https://mesu.apple.com/assets/macos/com_apple_MobileAsset_Font5/com_apple_MobileAsset_Font5.xml",
  6 => "https://mesu.apple.com/assets/macos/com_apple_MobileAsset_Font6/com_apple_MobileAsset_Font6.xml",
  7 => "https://mesu.apple.com/assets/macos/com_apple_MobileAsset_Font7/com_apple_MobileAsset_Font7.xml",
  8 => "https://mesu.apple.com/assets/macos/com_apple_MobileAsset_Font8/com_apple_MobileAsset_Font8.xml",
}.freeze

Class Method Summary collapse

Class Method Details

.all_assetsObject



120
121
122
123
124
# File 'lib/fontist/macos/catalog/catalog_manager.rb', line 120

def all_assets
  available_catalogs.flat_map do |catalog_path|
    parser_for(catalog_path).assets
  end
end

.available_catalogsObject



28
29
30
# File 'lib/fontist/macos/catalog/catalog_manager.rb', line 28

def available_catalogs
  downloaded_catalogs
end

.catalog_cache_pathObject

Get path to catalog cache directory



33
34
35
36
37
# File 'lib/fontist/macos/catalog/catalog_manager.rb', line 33

def catalog_cache_path
  @catalog_cache_path ||= Fontist.fontist_version_path.join("macos_catalogs").tap do |path|
    FileUtils.mkdir_p(path) unless path.exist?
  end
end

.catalog_for_version(version) ⇒ Object



130
131
132
133
134
135
136
137
138
139
# File 'lib/fontist/macos/catalog/catalog_manager.rb', line 130

def catalog_for_version(version)
  catalog = available_catalogs.find do |path|
    path.include?("Font#{version}")
  end

  # If catalog not found locally or in cache, download it
  catalog ||= download_catalog(version)

  catalog
end

.detect_version(catalog_path) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/fontist/macos/catalog/catalog_manager.rb', line 107

def detect_version(catalog_path)
  # Extract version from directory name or filename
  # e.g., /path/com_apple_MobileAsset_Font7/file.xml -> 7
  match = catalog_path.match(/Font(\d+)/)

  unless match
    raise ArgumentError,
          "Cannot detect version from: #{catalog_path}"
  end

  match[1].to_i
end

.download_catalog(version, cache_path: nil) ⇒ Object

Download a catalog for a specific version if not already cached



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
68
69
70
71
72
73
74
75
76
77
# File 'lib/fontist/macos/catalog/catalog_manager.rb', line 40

def download_catalog(version, cache_path: nil)
  cache_dir = cache_path || catalog_cache_path

  # Check if catalog is already cached
  cached_catalog = Dir.glob("#{cache_dir}/com_apple_MobileAsset_Font#{version}/*.xml").first
  return cached_catalog if cached_catalog

  # Download the catalog
  url = CATALOG_URLS[version]
  unless url
    raise ArgumentError,
          "Unsupported Font catalog version: #{version}. Supported versions: 3, 4, 5, 6, 7, 8"
  end

  version_dir = File.join(cache_dir,
                          "com_apple_MobileAsset_Font#{version}")
  FileUtils.mkdir_p(version_dir)

  catalog_file = File.join(version_dir,
                           File.basename(URI.parse(url).path))

  Fontist.ui.say("Downloading Font#{version} catalog from #{url}...") if Fontist.ui.respond_to?(:say)

  URI(url).open(
    "User-Agent" => "Fontist/#{Fontist::VERSION}",
  ) do |response|
    File.write(catalog_file, response.read)
  end

  catalog_file
rescue ArgumentError
  # Re-raise ArgumentError (unsupported version) as-is
  raise
rescue StandardError => e
  # For other errors (network issues, etc.), return nil
  Fontist.ui.error("Failed to download Font#{version} catalog: #{e.message}") if Fontist.ui.respond_to?(:error)
  nil
end

.downloaded_catalogsObject

Get all downloaded catalogs from cache



80
81
82
# File 'lib/fontist/macos/catalog/catalog_manager.rb', line 80

def downloaded_catalogs
  Dir.glob("#{catalog_cache_path}/com_apple_MobileAsset_Font*/*.xml").sort
end

.latest_catalogObject



126
127
128
# File 'lib/fontist/macos/catalog/catalog_manager.rb', line 126

def latest_catalog
  available_catalogs.last
end

.parser_for(catalog_path) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/fontist/macos/catalog/catalog_manager.rb', line 84

def parser_for(catalog_path)
  version = detect_version(catalog_path)

  case version
  when 3
    Font3Parser.new(catalog_path)
  when 4
    Font4Parser.new(catalog_path)
  when 5
    Font5Parser.new(catalog_path)
  when 6
    Font6Parser.new(catalog_path)
  when 7
    Font7Parser.new(catalog_path)
  when 8
    Font8Parser.new(catalog_path)
  else
    raise ArgumentError,
          "Unsupported Font catalog version: #{version}. " \
          "Supported versions: 3, 4, 5, 6, 7, 8"
  end
end