class Icomoon::Cli::Exec::Import

Constants

CONFIG_FILE_NAME
EXTENSIONS

Attributes

config[R]

Public Instance Methods

run() click to toggle source
# File lib/icomoon/cli/exec/import.rb, line 24
def run
  Icomoon::Cli::Operation.new do
    validate_params
  end

  Icomoon::Cli::Operation.new do
    read_config
  end

  Icomoon::Cli::Operation.new 'Checking files' do
    EXTENSIONS.each do |ext|
      check_source_file("fonts/#{config.icons_set_name}.#{ext}")
    end
  end

  Icomoon::Cli::Operation.new 'Copying font files' do
    EXTENSIONS.each do |ext|
      filename = "#{config.icons_set_name}.#{ext}"
      copy_file(filename, source_subdir: 'fonts')
    end
  end

  Icomoon::Cli::Operation.new 'Copying JSON manifest' do
    copy_file('selection.json', target_filename: Icomoon::Cli::MANIFEST_FILENAME)
  end

  Icomoon::Cli::Operation.new do
    validate_and_print_code
  end

  Icomoon::Cli::Operation.dump_warnings
  Icomoon::Cli::Operation.dump_errors
end

Private Instance Methods

array_to_icons(array) click to toggle source
# File lib/icomoon/cli/exec/import.rb, line 135
def array_to_icons(array)
  array.map do |name, code|
    Icomoon::Cli::Icon.new(name, code)
  end
end
check_source_file(path) click to toggle source
# File lib/icomoon/cli/exec/import.rb, line 66
def check_source_file(path)
  absolute_path = File.join(source_dir, config.icons_set_name, path)
  return true if File.exists?(absolute_path)
  fail Icomoon::Cli::Error, "File not found: #{absolute_path}"
end
copy_file(filename, source_subdir: '', target_filename: filename) click to toggle source
# File lib/icomoon/cli/exec/import.rb, line 72
def copy_file(filename, source_subdir: '', target_filename: filename)
  FileUtils.cp(
    File.join(source_dir, config.icons_set_name, source_subdir, filename),
    File.expand_path(File.join(config.fonts_file_dir, target_filename))
  )
rescue StandardError => e
  fail Icomoon::Cli::Error, e.message
end
find_unused_icons(css_icons, svg_icons) click to toggle source
# File lib/icomoon/cli/exec/import.rb, line 94
def find_unused_icons(css_icons, svg_icons)
  unused_icons = css_icons.select do |css_icon|
    svg_icons.map(&:code).index(css_icon.code).nil?
  end

  n = unused_icons.count

  return true if n.zero?

  names   = unused_icons.map(&:name).join(', ')
  message =  "#{n} unused #{n == 1 ? 'icon' : 'icons'} detected: #{names}."
  Icomoon::Cli::Operation.warn! message
end
print_code_update(css_icons, svg_icons) click to toggle source
read_config() click to toggle source
# File lib/icomoon/cli/exec/import.rb, line 62
def read_config
  @config = Icomoon::Cli::Config.read
end
validate_and_print_code() click to toggle source
# File lib/icomoon/cli/exec/import.rb, line 81
def validate_and_print_code
  css_icons_path  = File.expand_path(config.icons_file_path)
  css_icon_regex  = /\&--([0-9a-z_\-]+)\:before \{\s+content: \"\\(e[0-9a-f]+)\"/
  css_icons       = array_to_icons(File.read(css_icons_path).scan(css_icon_regex))

  svg_icons_path = File.expand_path(File.join(config.fonts_file_dir, "#{config.icons_set_name}.svg"))
  svg_icon_regex = /unicode=\"&#x([0-9a-z]+);\" glyph-name=\"([0-9a-z_\-]+)\"/
  svg_icons      = array_to_icons(File.read(svg_icons_path).scan(svg_icon_regex).map(&:reverse))

  find_unused_icons(css_icons, svg_icons)
  print_code_update(css_icons, svg_icons)
end
validate_params() click to toggle source
# File lib/icomoon/cli/exec/import.rb, line 129
def validate_params
  if source_dir.nil?
    fail Icomoon::Cli::Error, 'Source directory argument (--dir, -d) is required.'
  end
end