class Licensed::Sources::Mix::LockfileParser

Constants

LINE_PATTERN

Top-level pattern extracting the name and Mix.SCM type

SCM_PATTERN

Patterns to extract the version and repo information for each Mix.SCM type.

Public Class Methods

new(lines) click to toggle source
# File lib/licensed/sources/mix.rb, line 125
def initialize(lines)
  @lines = lines
end
read(path) click to toggle source

Parses a mix.lock to extract raw package information.

path - The path to the mix.lock as a Pathname or String.

Returns an Array of Hash package entries, or raises a ParserError if unsuccessful.

# File lib/licensed/sources/mix.rb, line 120
def self.read(path)
  lines = File.readlines(path)
  new(lines).result
end

Public Instance Methods

result() click to toggle source

Parses the input lines.

Returns an Array of Hash package entries, or raises a ParseError if unsuccessful.

# File lib/licensed/sources/mix.rb, line 133
def result
  # Ignore the first and last lines of the file (the beginning and
  # ending of the enclosing map).
  @lines[1..-2].map do |line|
    parse_line(line)
  end
end

Private Instance Methods

invalid_package_entry(match, line) click to toggle source

Format an invalid package entry.

match - A MatchData containing name and scm information. line - The line from mix.lock that could not be parsed, as a String.

Returns a Hash representing the package, with error information.

# File lib/licensed/sources/mix.rb, line 185
def invalid_package_entry(match, line)
  {
    name: match[:name],
    version: nil,
    metadata: {
      "scm" => match[:scm]
    },
    error: "Could not extract data from mix.lock line: #{line}"
  }
end
parse_line(line) click to toggle source

Parse a line from the mix.lock file.

line - A line of input as a String.

Returns a Hash package entry, or raises a ParserError if unsuccessful.

# File lib/licensed/sources/mix.rb, line 148
def parse_line(line)
  match = LINE_PATTERN.match(line)
  if match
    data = SCM_PATTERN[match[:scm]].match(match[:contents])
    if data
      valid_package_entry(match, data)
    else
      invalid_package_entry(match, line)
    end
  else
    raise Licensed::Sources::Source::Error, "Unknown mix.lock line format: #{line}"
  end
end
valid_package_entry(match, data) click to toggle source

Format a valid package entry.

match - A MatchData containing name and scm information. data - A MatchData containing version and repo information.

Returns a Hash representing the package.

# File lib/licensed/sources/mix.rb, line 168
def valid_package_entry(match, data)
  {
    name: match[:name],
    version: data[:version],
    metadata: {
      "scm" => match[:scm],
      "repo" => data[:repo]
    }
  }
end