Class: Fontist::Manifest

Inherits:
Lutaml::Model::Collection
  • Object
show all
Defined in:
lib/fontist/manifest.rb

Overview

Manifest class for managing font manifests.

Direct Known Subclasses

ManifestRequest, ManifestResponse

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.font_classObject



190
191
192
# File 'lib/fontist/manifest.rb', line 190

def self.font_class
  ManifestFont
end

.from_file(path, locations: false) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/fontist/manifest.rb', line 132

def self.from_file(path, locations: false)
  Fontist.ui.debug("Manifest: #{path}")

  unless File.exist?(path)
    raise Fontist::Errors::ManifestCouldNotBeFoundError,
          "Manifest file not found: #{path}"
  end

  file_content = File.read(path).strip

  if file_content.empty?
    raise Fontist::Errors::ManifestCouldNotBeReadError,
          "Manifest file is empty: #{path}"
  end

  manifest_model = begin
    from_yaml(file_content)
  rescue StandardError => e
    raise Fontist::Errors::ManifestCouldNotBeReadError,
          "Manifest file could not be read: #{e.message}"
  end

  # Use performance optimizations for faster manifest compilation
  with_performance_optimizations do
    manifest_model.to_response(locations: locations)
  end
end

.from_hash(data, options = {}) ⇒ Object



160
161
162
163
164
165
166
167
168
169
# File 'lib/fontist/manifest.rb', line 160

def self.from_hash(data, options = {})
  locations = options.delete(:locations) || false

  model = super

  # Use performance optimizations for faster manifest compilation
  with_performance_optimizations do
    model.to_response(locations: locations)
  end
end

.with_performance_optimizationsObject

Enable performance optimizations for manifest compilation This includes:

  • Read-only mode for indexes (skip index_changed? checks)

  • Caching of find_styles results to avoid repeated lookups



175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/fontist/manifest.rb', line 175

def self.with_performance_optimizations
  # Enable read-only mode on all indexes to skip index_changed? checks
  Fontist::Indexes::FontistIndex.instance.read_only_mode
  Fontist::Indexes::UserIndex.instance.read_only_mode
  Fontist::Indexes::SystemIndex.instance.read_only_mode

  # Enable caching for find_styles lookups
  Fontist::SystemFont.enable_find_styles_cache

  yield
ensure
  # Always disable caching after the operation
  Fontist::SystemFont.disable_find_styles_cache
end

Instance Method Details

#fonts_castedObject



194
195
196
197
198
# File 'lib/fontist/manifest.rb', line 194

def fonts_casted
  Array(fonts).map do |font|
    self.class.font_class === font ? font : self.class.font_class.new(font.to_h)
  end
end

#install(confirmation: "no", hide_licenses: false, no_progress: false, location: nil) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/fontist/manifest.rb', line 200

def install(confirmation: "no", hide_licenses: false, no_progress: false,
location: nil)
  installed_any = false
  fonts_casted.each do |font|
    paths = font.group_paths

    if paths.empty?
      font.install(confirmation: confirmation,
                   hide_licenses: hide_licenses,
                   no_progress: no_progress,
                   location: location)
      installed_any = true
    end
  end
  # Only reset fontist index (not system index) if we actually installed fonts
  if installed_any
    # Reset only the fontist font cache, not system fonts
    Fontist::SystemFont.reset_fontist_font_paths_cache
  end
  to_response
end

#to_file(path) ⇒ Object



235
236
237
238
# File 'lib/fontist/manifest.rb', line 235

def to_file(path)
  FileUtils.mkdir_p(File.dirname(path))
  File.write(path, to_yaml)
end

#to_response(locations: false) ⇒ Object



222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/fontist/manifest.rb', line 222

def to_response(locations: false)
  return self if fonts_casted.any?(&:group_paths_empty?) && !locations

  # Use performance optimizations for faster manifest compilation
  self.class.with_performance_optimizations do
    ManifestResponse.new.tap do |response|
      response.fonts = fonts_casted.map do |font|
        font.to_response(locations: locations)
      end
    end
  end
end