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:


486
487
488
489
# File 'lib/berkshelf/lockfile.rb', line 486

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)

494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
# File 'lib/berkshelf/lockfile.rb', line 494

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|
    dependency = Dependency.new(@berksfile, name, options)
    @lockfile.add(dependency)
  end

  true
end