Class: Berkshelf::Lockfile::LockfileParser

Inherits:
Object
  • Object
show all
Defined in:
lib/berkshelf/lockfile.rb

Overview

The class responsible for parsing the lockfile and turning it into a useful data structure.

Constant Summary collapse

NAME_VERSION =
'(?! )(.*?)(?: \(([^-]*)(?:-(.*))?\))?'.freeze
DEPENDENCY_PATTERN =
/^ {2}#{NAME_VERSION}$/.freeze
DEPENDENCIES_PATTERN =
/^ {4}#{NAME_VERSION}$/.freeze
OPTION_PATTERN =
/^ {4}(.+)\: (.+)/.freeze

Instance Method Summary collapse

Constructor Details

#initialize(lockfile) ⇒ LockfileParser

Create a new lockfile parser.

Parameters:



525
526
527
528
# File 'lib/berkshelf/lockfile.rb', line 525

def initialize(lockfile)
  @lockfile  = lockfile
  @berksfile = lockfile.berksfile
end

Instance Method Details

#runtrue

Parse the lockfile contents, adding the correct things to the lockfile.

Returns:

  • (true)


533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
# File 'lib/berkshelf/lockfile.rb', line 533

def run
  @parsed_dependencies = {}

  contents = File.read(@lockfile.filepath)

  if contents.strip.empty?
    Berkshelf.formatter.warn "Your lockfile at '#{@lockfile.filepath}' " \
      "is empty. I am going to parse it anyway, but there is a chance " \
      "that a larger problem is at play. If you manually edited your " \
      "lockfile, you may have corrupted it."
  end

  if contents.strip[0] == "{"
    Berkshelf.formatter.warn "It looks like you are using an older " \
      "version of the lockfile. Attempting to convert..."

    dependencies = "#{Lockfile::DEPENDENCIES}\n"
    graph        = "#{Lockfile::GRAPH}\n"

    begin
      hash = JSON.parse(contents)
    rescue JSON::ParserError
      Berkshelf.formatter.warn "Could not convert lockfile! This is a " \
      "problem. You see, previous versions of the lockfile were " \
      "actually a lie. It lied to you about your version locks, and we " \
      "are really sorry about that.\n\n" \
      "Here's the good news - we fixed it!\n\n" \
      "Here's the bad news - you probably should not trust your old " \
      "lockfile. You should manually delete your old lockfile and " \
      "re-run the installer."
    end

    hash["dependencies"] && hash["dependencies"].sort .each do |name, info|
      dependencies << "  #{name} (>= 0.0.0)\n"
      info.each do |key, value|
        unless key == "locked_version"
          dependencies << "    #{key}: #{value}\n"
        end
      end

      graph << "  #{name} (#{info["locked_version"]})\n"
    end

    contents = "#{dependencies}\n#{graph}"
  end

  contents.split(/(?:\r?\n)+/).each do |line|
    if line == Lockfile::DEPENDENCIES
      @state = :dependency
    elsif line == Lockfile::GRAPH
      @state = :graph
    else
      send("parse_#{@state}", line)
    end
  end

  @parsed_dependencies.each do |name, options|
    graph_item = @lockfile.graph.find(name)
    options[:locked_version] = graph_item.version if graph_item

    dependency = Dependency.new(@berksfile, name, options)
    @lockfile.add(dependency)
  end

  true
end