class Librarian::Lockfile::Parser

Attributes

environment[RW]

Public Class Methods

new(environment) click to toggle source
# File lib/librarian/lockfile/parser.rb, line 19
def initialize(environment)
  self.environment = environment
end

Public Instance Methods

parse(string) click to toggle source
# File lib/librarian/lockfile/parser.rb, line 23
def parse(string)
  lines = string.lines.map{|l| l.sub(/\s+\z/, '')}.reject(&:empty?)
  sources = extract_and_parse_sources(lines)
  manifests = compile(sources)
  manifests_index = Hash[manifests.map{|m| [m.name, m]}]
  raise StandardError, "Expected DEPENDENCIES topic!" unless lines.shift == "DEPENDENCIES"
  dependencies = extract_and_parse_dependencies(lines, manifests_index)
  Resolution.new(dependencies, manifests)
end

Private Instance Methods

compile(sources_ast) click to toggle source
# File lib/librarian/lockfile/parser.rb, line 91
def compile(sources_ast)
  manifests = compile_placeholder_manifests(sources_ast)
  manifests = manifests.map do |name, manifest|
    dependencies = manifest.dependencies.map do |d|
      environment.dsl_class.dependency_type.new(d.name, d.requirement, manifests[d.name].source)
    end
    real = Manifest.new(manifest.source, manifest.name)
    real.version = manifest.version
    real.dependencies = manifest.dependencies
    real
  end
  ManifestSet.sort(manifests)
end
compile_placeholder_manifests(sources_ast) click to toggle source
# File lib/librarian/lockfile/parser.rb, line 74
def compile_placeholder_manifests(sources_ast)
  manifests = {}
  sources_ast.each do |source_ast|
    source_type = source_ast[:type]
    source = source_type.from_lock_options(environment, source_ast[:options])
    source_ast[:manifests].each do |manifest_name, manifest_ast|
      manifests[manifest_name] = ManifestPlaceholder.new(
        source,
        manifest_name,
        manifest_ast[:version],
        manifest_ast[:dependencies].map{|k, v| environment.dsl_class.dependency_type.new(k, v, nil)}
      )
    end
  end
  manifests
end
dsl_class() click to toggle source
# File lib/librarian/lockfile/parser.rb, line 105
def dsl_class
  environment.dsl_class
end
extract_and_parse_dependencies(lines, manifests_index) click to toggle source
# File lib/librarian/lockfile/parser.rb, line 64
def extract_and_parse_dependencies(lines, manifests_index)
  dependencies = []
  while lines.first =~ /^ {2}([\w\-\/]+)(?: \((.*)\))?$/
    lines.shift
    name, requirement = $1, $2.split(/,\s*/)
    dependencies << environment.dsl_class.dependency_type.new(name, requirement, manifests_index[name].source)
  end
  dependencies
end
extract_and_parse_sources(lines) click to toggle source
# File lib/librarian/lockfile/parser.rb, line 35
def extract_and_parse_sources(lines)
  sources = []
  while source_type_names.include?(lines.first)
    source = {}
    source_type_name = lines.shift
    source[:type] = source_type_names_map[source_type_name]
    options = {}
    while lines.first =~ /^ {2}([\w\-\/]+):\s+(.+)$/
      lines.shift
      options[$1.to_sym] = $2
    end
    source[:options] = options
    lines.shift # specs
    manifests = {}
    while lines.first =~ /^ {4}([\w\-\/]+) \((.*)\)$/
      lines.shift
      name = $1
      manifests[name] = {:version => $2, :dependencies => {}}
      while lines.first =~ /^ {6}([\w\-\/]+) \((.*)\)$/
        lines.shift
        manifests[name][:dependencies][$1] = $2.split(/,\s*/)
      end
    end
    source[:manifests] = manifests
    sources << source
  end
  sources
end
source_type_names() click to toggle source
# File lib/librarian/lockfile/parser.rb, line 115
def source_type_names
  @source_type_names ||= begin
    dsl_class.source_types.map{|t| t[1].lock_name}
  end
end
source_type_names_map() click to toggle source
# File lib/librarian/lockfile/parser.rb, line 109
def source_type_names_map
  @source_type_names_map ||= begin
    Hash[dsl_class.source_types.map{|t| [t[1].lock_name, t[1]]}]
  end
end