Class: Iconmap::Packager

Inherits:
Object
  • Object
show all
Defined in:
lib/iconmap/packager.rb

Defined Under Namespace

Classes: Error, HTTPError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(iconmap_path = 'config/iconmap.rb', vendor_path: 'vendor/icons') ⇒ Packager

Returns a new instance of Packager.



17
18
19
20
# File 'lib/iconmap/packager.rb', line 17

def initialize(iconmap_path = 'config/iconmap.rb', vendor_path: 'vendor/icons')
  @iconmap_path = Pathname.new(iconmap_path)
  @vendor_path = Pathname.new(vendor_path)
end

Instance Attribute Details

#vendor_pathObject (readonly)

Returns the value of attribute vendor_path.



15
16
17
# File 'lib/iconmap/packager.rb', line 15

def vendor_path
  @vendor_path
end

Instance Method Details

#download(package, version, path, url = nil) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/iconmap/packager.rb', line 77

def download(package, version, path, url = nil)
  url ||= jsdelivr.download_url(package, version, path)
  ensure_vendor_directory_exists
  remove_existing_vendored_file(package, path)
  content = jsdelivr.fetch_file(url)
  save_vendored_file(package, path, version, url, content)
end

#packaged?(package_with_path) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/iconmap/packager.rb', line 73

def packaged?(package_with_path)
  iconmap.match(/^pin ['"]#{Regexp.escape(package_with_path)}['"].*$/)
end

#parse_icon_path(arg) ⇒ Object

Parse an icon argument like "@fortawesome/[email protected]/svgs/brands/github.svg" into { package:, path:, version: }



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/iconmap/packager.rb', line 24

def parse_icon_path(arg) # rubocop:disable Metrics/MethodLength
  if arg.start_with?('@')
    # Scoped package: @scope/name@version/path or @scope/name/path
    scope_and_rest = arg[1..] # remove leading @
    parts = scope_and_rest.split('/', 3)
    scope = parts[0]
    name_with_maybe_version = parts[1]
    remainder = parts[2]

    if name_with_maybe_version.include?('@')
      name, version = name_with_maybe_version.split('@', 2)
    else
      name = name_with_maybe_version
      version = nil
    end

    { package: "@#{scope}/#{name}", path: remainder, version: version }
  else
    # Unscoped package: name@version/path or name/path
    parts = arg.split('/', 2)
    name_with_maybe_version = parts[0]
    remainder = parts[1]

    if name_with_maybe_version.include?('@')
      name, version = name_with_maybe_version.split('@', 2)
    else
      name = name_with_maybe_version
      version = nil
    end

    { package: name, path: remainder, version: version }
  end
end

#pin(arg) ⇒ Object

Raises:



58
59
60
61
62
63
64
65
66
67
# File 'lib/iconmap/packager.rb', line 58

def pin(arg)
  parsed = parse_icon_path(arg)
  version = parsed[:version] || jsdelivr.resolve_version(parsed[:package])

  raise Error, "Could not resolve version for #{parsed[:package]}" unless version

  url = jsdelivr.download_url(parsed[:package], version, parsed[:path])
  download(parsed[:package], version, parsed[:path], url)
  vendored_pin_for(parsed[:package], parsed[:path], version)
end

#remove(package_with_path) ⇒ Object



85
86
87
88
89
# File 'lib/iconmap/packager.rb', line 85

def remove(package_with_path)
  parsed = parse_icon_path(package_with_path)
  remove_existing_vendored_file(parsed[:package], parsed[:path])
  remove_package_from_iconmap(package_with_path)
end

#vendored_filename(package, path) ⇒ Object



95
96
97
# File 'lib/iconmap/packager.rb', line 95

def vendored_filename(package, path)
  "#{package}/#{path}".gsub('/', '--')
end

#vendored_package_path(package, path) ⇒ Object



91
92
93
# File 'lib/iconmap/packager.rb', line 91

def vendored_package_path(package, path)
  @vendor_path.join(vendored_filename(package, path))
end

#vendored_pin_for(package, path, version) ⇒ Object



69
70
71
# File 'lib/iconmap/packager.rb', line 69

def vendored_pin_for(package, path, version)
  %(pin '#{package}/#{path}' # @#{version})
end