class Fontist::Import::FormulaBuilder

Constants

FORMULA_ATTRIBUTES

Attributes

archive[RW]
extractor[RW]
font_collection_files[RW]
font_files[RW]
license_text[RW]
options[RW]
url[RW]

Public Class Methods

new() click to toggle source
# File lib/fontist/import/formula_builder.rb, line 19
def initialize
  @options = {}
end

Public Instance Methods

formula() click to toggle source
# File lib/fontist/import/formula_builder.rb, line 23
def formula
  FORMULA_ATTRIBUTES.map { |name| [name, send(name)] }.to_h.compact
end

Private Instance Methods

both_fonts() click to toggle source
# File lib/fontist/import/formula_builder.rb, line 37
def both_fonts
  @both_fonts ||= group_fonts
end
command() click to toggle source
# File lib/fontist/import/formula_builder.rb, line 186
def command
  Shellwords.shelljoin(ARGV)
end
description() click to toggle source
# File lib/fontist/import/formula_builder.rb, line 48
def description
  name
end
digest() click to toggle source
# File lib/fontist/import/formula_builder.rb, line 182
def digest
  @options[:digest]
end
download(url) click to toggle source
# File lib/fontist/import/formula_builder.rb, line 102
def download(url)
  Fontist::Utils::Downloader.download(url, progress_bar: true).path
rescue Errors::InvalidResourceError
  Fontist.ui.error("WARN: a mirror is not found '#{url}'")
  nil
end
downloads() { |url, archive| ... } click to toggle source
# File lib/fontist/import/formula_builder.rb, line 87
def downloads
  yield @url, @archive

  mirrors.each do |url|
    path = download(url)
    next unless path

    yield url, path
  end
end
extract() click to toggle source
# File lib/fontist/import/formula_builder.rb, line 157
def extract
  @extractor.operations
end
font_collections() click to toggle source
# File lib/fontist/import/formula_builder.rb, line 118
def font_collections
  return if @font_collection_files.empty?

  collections = @font_collection_files.map do |file|
    fonts = fonts_from_files(file.fonts, :to_collection_style)

    { filename: file.filename,
      source_filename: file.source_filename,
      fonts: fonts }.compact
  end

  collections.sort_by do |x|
    x[:filename]
  end
end
fonts() click to toggle source
# File lib/fontist/import/formula_builder.rb, line 134
def fonts
  return if @font_files.empty?

  fonts_from_files(@font_files, :to_style)
end
fonts_from_files(files, style_type = :to_style) click to toggle source
# File lib/fontist/import/formula_builder.rb, line 140
def fonts_from_files(files, style_type = :to_style)
  groups = files.group_by(&:family_name)

  fonts = groups.map do |name, group|
    { name: name,
      styles: styles_from_files(group, style_type) }
  end

  fonts.sort_by do |x|
    x[:name]
  end
end
group_fonts() click to toggle source
# File lib/fontist/import/formula_builder.rb, line 41
def group_fonts
  files = (@font_files + @font_collection_files.map(&:fonts)).flatten
  raise Errors::FontNotFoundError, "No font found" if files.empty?

  files
end
homepage() click to toggle source
# File lib/fontist/import/formula_builder.rb, line 52
def homepage
  both_fonts.map(&:homepage).compact.first
end
license_url() click to toggle source
# File lib/fontist/import/formula_builder.rb, line 165
def license_url
  both_fonts.map(&:license_url).compact.first
end
mirrors() click to toggle source
# File lib/fontist/import/formula_builder.rb, line 98
def mirrors
  @options[:mirror] || []
end
name() click to toggle source
# File lib/fontist/import/formula_builder.rb, line 29
def name
  return options[:name] if options[:name]

  unique_names = both_fonts.map(&:family_name).uniq
  TextHelper.longest_common_prefix(unique_names) ||
    both_fonts.first.family_name
end
open_license() click to toggle source
# File lib/fontist/import/formula_builder.rb, line 169
def open_license
  unless @license_text
    Fontist.ui.error("WARN: please add license manually")
    return
  end

  Fontist.ui.error("WARN: ensure it's an open license, otherwise " \
                   "change the 'open_license' attribute to " \
                   "'requires_license_agreement'")

  TextHelper.cleanup(@license_text)
end
prepare_sha256(input) click to toggle source
# File lib/fontist/import/formula_builder.rb, line 109
def prepare_sha256(input)
  output = input.uniq
  return output.first if output.size == 1

  checksums = output.join(", ")
  Fontist.ui.error("WARN: SHA256 differs (#{checksums})")
  output
end
resource_options() click to toggle source
# File lib/fontist/import/formula_builder.rb, line 62
def resource_options
  if @options[:skip_sha]
    resource_options_without_sha
  else
    resource_options_with_sha
  end
end
resource_options_with_sha() click to toggle source
# File lib/fontist/import/formula_builder.rb, line 74
def resource_options_with_sha
  urls = []
  sha = []
  downloads do |url, path|
    urls << url
    sha << Digest::SHA256.file(path).to_s
  end

  sha = prepare_sha256(sha)

  { urls: urls, sha256: sha }
end
resource_options_without_sha() click to toggle source
# File lib/fontist/import/formula_builder.rb, line 70
def resource_options_without_sha
  { urls: [@url] + mirrors }
end
resources() click to toggle source
# File lib/fontist/import/formula_builder.rb, line 56
def resources
  filename = name.gsub(" ", "_") + "." + @extractor.extension

  { filename => resource_options }
end
styles_from_files(files, style_type) click to toggle source
# File lib/fontist/import/formula_builder.rb, line 153
def styles_from_files(files, style_type)
  files.map(&style_type).sort_by { |x| x[:type] }
end