Class: Licensed::Sources::Mix::LockfileParser

Inherits:
Object
  • Object
show all
Defined in:
lib/licensed/sources/mix.rb

Defined Under Namespace

Classes: ParseError

Constant Summary collapse

LINE_PATTERN =

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

/
  \A                 # At the beginning of input
  \s*                # after any number of spaces
  "(?<name>.*?)"     # capture the contents of a double-quoted string as the name
  :\s*\{             # then skipping a colon, any number of spaces, and an opening brace,
  :(?<scm>hex|git)   # capture the contents of a Elixir atom as the scm
  ,\s*               # and, skipping a comma and any number of spaces,
  (?<contents>.*)    # capture the rest of input as the contents.
/x
SCM_PATTERN =

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

{
  # The Hex Package Manager
  "hex" => /
    \A                # At the beginning of input
    :[a-zA-Z0-9_]+    # after an Elixir atom,
    ,\s*              # and skipping a comma and any number of spaces,
    "(?<version>.*?)" # capture the contents of a double-quoted string as the version,
    .*?\],\s*         # and later
    "(?<repo>.*?)"    # capture the contents of a double-quoted string as the repo
    (?:
      ,\s*            # a comma
      "[a-f0-9]{64}"  # a digest
    )?
    \},?\s*\Z         # right before the final closing brace.
  /x,

  # Git
  "git" => /
    \A                # At the beginning of input
    "(?<repo>.*?)"    # capture the contents of a double-quoted string as the repo
    ,\s*              # and, skipping a comma and any number of spaces,
    "(?<version>.*?)" # capture the contents of a second double-quoted string as the version.
  /x
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lines) ⇒ LockfileParser

Returns a new instance of LockfileParser.



125
126
127
# File 'lib/licensed/sources/mix.rb', line 125

def initialize(lines)
  @lines = lines
end

Class Method Details

.read(path) ⇒ Object

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.



120
121
122
123
# File 'lib/licensed/sources/mix.rb', line 120

def self.read(path)
  lines = File.readlines(path)
  new(lines).result
end

Instance Method Details

#resultObject

Parses the input lines.

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



133
134
135
136
137
138
139
# 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