Class: Bundler::LockfileParser

Inherits:
Object
  • Object
show all
Defined in:
lib/bundler/lockfile_parser.rb

Constant Summary collapse

BUNDLED =
"BUNDLED WITH"
DEPENDENCIES =
"DEPENDENCIES"
PLATFORMS =
"PLATFORMS"
RUBY =
"RUBY VERSION"
GIT =
"GIT"
GEM =
"GEM"
PATH =
"PATH"
PLUGIN =
"PLUGIN SOURCE"
SPECS =
"  specs:"
OPTIONS =
/^  ([a-z]+): (.*)$/i.freeze
SOURCE =
[GIT, GEM, PATH, PLUGIN].freeze
SECTIONS_BY_VERSION_INTRODUCED =
{
  Gem::Version.create("1.0") => [DEPENDENCIES, PLATFORMS, GIT, GEM, PATH].freeze,
  Gem::Version.create("1.10") => [BUNDLED].freeze,
  Gem::Version.create("1.12") => [RUBY].freeze,
  Gem::Version.create("1.13") => [PLUGIN].freeze,
}.freeze
KNOWN_SECTIONS =
SECTIONS_BY_VERSION_INTRODUCED.values.flatten!.freeze
ENVIRONMENT_VERSION_SECTIONS =
[BUNDLED, RUBY].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lockfile) ⇒ LockfileParser

Returns a new instance of LockfileParser.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/bundler/lockfile_parser.rb', line 62

def initialize(lockfile)
  @platforms    = []
  @sources      = []
  @dependencies = {}
  @parse_method = nil
  @specs        = {}

  if lockfile.match?(/<<<<<<<|=======|>>>>>>>|\|\|\|\|\|\|\|/)
    raise LockfileError, "Your lockfile contains merge conflicts.\n" \
      "Run `git checkout HEAD -- #{SharedHelpers.relative_lockfile_path}` first to get a clean lock."
  end

  lockfile.split(/(?:\r?\n)+/) do |line|
    if SOURCE.include?(line)
      @parse_method = :parse_source
      parse_source(line)
    elsif line == DEPENDENCIES
      @parse_method = :parse_dependency
    elsif line == PLATFORMS
      @parse_method = :parse_platform
    elsif line == RUBY
      @parse_method = :parse_ruby
    elsif line == BUNDLED
      @parse_method = :parse_bundled_with
    elsif /^[^\s]/.match?(line)
      @parse_method = nil
    elsif @parse_method
      send(@parse_method, line)
    end
  end
  @specs = @specs.values.sort_by!(&:full_name)
rescue ArgumentError => e
  Bundler.ui.debug(e)
  raise LockfileError, "Your lockfile is unreadable. Run `rm #{SharedHelpers.relative_lockfile_path}` " \
    "and then `bundle install` to generate a new lockfile."
end

Instance Attribute Details

#bundler_versionObject (readonly)

Returns the value of attribute bundler_version.



5
6
7
# File 'lib/bundler/lockfile_parser.rb', line 5

def bundler_version
  @bundler_version
end

#dependenciesObject (readonly)

Returns the value of attribute dependencies.



5
6
7
# File 'lib/bundler/lockfile_parser.rb', line 5

def dependencies
  @dependencies
end

#platformsObject (readonly)

Returns the value of attribute platforms.



5
6
7
# File 'lib/bundler/lockfile_parser.rb', line 5

def platforms
  @platforms
end

#ruby_versionObject (readonly)

Returns the value of attribute ruby_version.



5
6
7
# File 'lib/bundler/lockfile_parser.rb', line 5

def ruby_version
  @ruby_version
end

#sourcesObject (readonly)

Returns the value of attribute sources.



5
6
7
# File 'lib/bundler/lockfile_parser.rb', line 5

def sources
  @sources
end

#specsObject (readonly)

Returns the value of attribute specs.



5
6
7
# File 'lib/bundler/lockfile_parser.rb', line 5

def specs
  @specs
end

Class Method Details

.bundled_withObject



52
53
54
55
56
57
58
59
60
# File 'lib/bundler/lockfile_parser.rb', line 52

def self.bundled_with
  lockfile = Bundler.default_lockfile
  return unless lockfile.file?

  lockfile_contents = Bundler.read_file(lockfile)
  return unless lockfile_contents.include?(BUNDLED)

  lockfile_contents.split(BUNDLED).last.strip
end

.sections_in_lockfile(lockfile_contents) ⇒ Object



31
32
33
34
35
# File 'lib/bundler/lockfile_parser.rb', line 31

def self.sections_in_lockfile(lockfile_contents)
  sections = lockfile_contents.scan(/^\w[\w ]*$/)
  sections.uniq!
  sections
end

.sections_to_ignore(base_version = nil) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/bundler/lockfile_parser.rb', line 41

def self.sections_to_ignore(base_version = nil)
  base_version &&= base_version.release
  base_version ||= Gem::Version.create("1.0")
  attributes = []
  SECTIONS_BY_VERSION_INTRODUCED.each do |version, introduced|
    next if version <= base_version
    attributes += introduced
  end
  attributes
end

.unknown_sections_in_lockfile(lockfile_contents) ⇒ Object



37
38
39
# File 'lib/bundler/lockfile_parser.rb', line 37

def self.unknown_sections_in_lockfile(lockfile_contents)
  sections_in_lockfile(lockfile_contents) - KNOWN_SECTIONS
end

Instance Method Details

#may_include_redundant_platform_specific_gems?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/bundler/lockfile_parser.rb', line 99

def may_include_redundant_platform_specific_gems?
  bundler_version.nil? || bundler_version < Gem::Version.new("1.16.2")
end