Class: SFSymbolCli

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/sf_symbol_converter/cli/sf_symbol_converter.rb

Overview

CLI for converting SF Symbols

Constant Summary collapse

GEM_ROOT =

Determine the root directory of the gem

File.expand_path("../../..", __dir__)
TEMPLATE_PATH =
File.join(GEM_ROOT, "assets", "template.svg")

Instance Method Summary collapse

Instance Method Details

#batch_convert(input_dir, output_dir) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/sf_symbol_converter/cli/sf_symbol_converter.rb', line 35

def batch_convert(input_dir, output_dir)
  Dir.glob("#{input_dir}/*.svg").each do |icon_svg_path|
    icon_svg = Nokogiri::XML(File.open(icon_svg_path))

    output_path = "#{output_dir}/#{File.basename(icon_svg_path)}"
    convert(icon_svg_path, output_path)
  end
end

#convert(icon_svg_path, output_path = "output.svg") ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/sf_symbol_converter/cli/sf_symbol_converter.rb', line 19

def convert(icon_svg_path, output_path = "output.svg")
  template_svg = Nokogiri::XML(File.open(TEMPLATE_PATH))
  icon_svg = Nokogiri::XML(File.open(icon_svg_path))

  output_path = output_path || "output.svg"

  converter = SFSymbolConverter.new(template_svg, icon_svg)
  converted_svg = converter.convert

  pretty_printed_svg = Nokogiri::XML(converted_svg.to_s, &:noblanks).to_xml(indent: 2)

  File.open(output_path, "w") { |file| file.write(pretty_printed_svg) }
end