class Fontist::FontInstaller

Attributes

formula[R]

Public Class Methods

new(formula, no_progress: false) click to toggle source
# File lib/fontist/font_installer.rb, line 6
def initialize(formula, no_progress: false)
  @formula = formula
  @no_progress = no_progress
end

Public Instance Methods

install(confirmation:) click to toggle source
# File lib/fontist/font_installer.rb, line 11
def install(confirmation:)
  if @formula.license_required && !"yes".casecmp?(confirmation)
    raise(Fontist::Errors::LicensingError)
  end

  install_font
end

Private Instance Methods

download_file(source) click to toggle source
# File lib/fontist/font_installer.rb, line 56
def download_file(source)
  request = source.urls.first
  url = request.respond_to?(:url) ? request.url : request
  Fontist.ui.say(%(Downloading font "#{@formula.key}" from #{url}))

  Fontist::Utils::Downloader.download(
    request,
    sha: source.sha256,
    file_size: source.file_size,
    progress_bar: !@no_progress
  )
end
extract() click to toggle source
# File lib/fontist/font_installer.rb, line 40
def extract
  archive = download_file(@formula.resources.first)

  install_fonts_from_archive(archive)
end
font_directory?(path) click to toggle source
# File lib/fontist/font_installer.rb, line 85
def font_directory?(path)
  return true unless subdirectory_pattern

  File.fnmatch?(subdirectory_pattern, File.dirname(path))
end
font_file?(path) click to toggle source
# File lib/fontist/font_installer.rb, line 69
def font_file?(path)
  source_file?(path) && font_directory?(path)
end
install_font() click to toggle source
# File lib/fontist/font_installer.rb, line 23
def install_font
  fonts_paths = run_in_temp_dir { extract }
  fonts_paths.empty? ? nil : fonts_paths
end
install_font_file(source) click to toggle source
# File lib/fontist/font_installer.rb, line 99
def install_font_file(source)
  target = Fontist.fonts_path.join(target_filename(File.basename(source))).to_s
  FileUtils.mv(source, target)

  target
end
install_fonts_from_archive(archive) click to toggle source
# File lib/fontist/font_installer.rb, line 46
def install_fonts_from_archive(archive)
  Fontist.ui.say(%(Installing font "#{@formula.key}".))

  Array.new.tap do |fonts_paths|
    Excavate::Archive.new(archive.path).files(recursive_packages: true) do |path|
      fonts_paths << install_font_file(path) if font_file?(path)
    end
  end
end
run_in_temp_dir() { || ... } click to toggle source
# File lib/fontist/font_installer.rb, line 28
def run_in_temp_dir
  Dir.mktmpdir(nil, Dir.tmpdir) do |dir|
    @temp_dir = Pathname.new(dir)

    result = yield

    @temp_dir = nil

    result
  end
end
source_file?(path) click to toggle source
# File lib/fontist/font_installer.rb, line 73
def source_file?(path)
  source_files.include?(File.basename(path))
end
source_files() click to toggle source
# File lib/fontist/font_installer.rb, line 77
def source_files
  @source_files ||= @formula.fonts.flat_map do |font|
    font.styles.map do |style|
      style.source_font || style.font
    end
  end
end
subdirectories() click to toggle source
# File lib/fontist/font_installer.rb, line 95
def subdirectories
  @subdirectories ||= [@formula.extract].flatten.map(&:options).compact.map(&:fonts_sub_dir).compact
end
subdirectory_pattern() click to toggle source
# File lib/fontist/font_installer.rb, line 91
def subdirectory_pattern
  @subdirectory_pattern ||= "*" + subdirectories.first.chomp("/") unless subdirectories.empty?
end
target_filename(source_filename) click to toggle source
# File lib/fontist/font_installer.rb, line 106
def target_filename(source_filename)
  target_filenames[source_filename]
end
target_filenames() click to toggle source
# File lib/fontist/font_installer.rb, line 110
def target_filenames
  @target_filenames ||= @formula.fonts.flat_map do |font|
    font.styles.map do |style|
      source = style.source_font || style.font
      target = style.font
      [source, target]
    end
  end.to_h
end