aboutsummaryrefslogtreecommitdiffstats
path: root/lib/bundler/definition.rb
blob: eb23042d16af4c7acf413a2e62c9eab292b52c70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
require "digest/sha1"

# TODO: In the 0.10 release, there shouldn't be a locked subclass of Definition
module Bundler
  class Definition
    attr_reader :dependencies, :sources, :locked_specs

    def self.build(gemfile, lockfile)
      gemfile = Pathname.new(gemfile).expand_path

      unless gemfile.file?
        raise GemfileNotFound, "#{gemfile} not found"
      end

      # TODO: move this back into DSL
      builder = Dsl.new
      builder.instance_eval(File.read(gemfile.to_s), gemfile.to_s, 1)
      builder.to_definition(lockfile)
    end

    def initialize(lockfile, dependencies, sources)
      @dependencies, @sources = dependencies, sources

      if lockfile && File.exists?(lockfile)
        locked = LockfileParser.new(File.read(lockfile))
        @locked_deps  = locked.dependencies
        @locked_specs = SpecSet.new(locked.specs)
      else
        @locked_deps  = []
        @locked_specs = SpecSet.new([])
      end
    end

    # TODO: OMG LOL
    def resolved_dependencies
      locked_specs_as_deps + dependencies
    end

    def groups
      dependencies.map { |d| d.groups }.flatten.uniq
    end

    # We have the dependencies from Gemfile.lock and the dependencies from the
    # Gemfile. Here, we are finding a list of all dependencies that were
    # originally present in the Gemfile that still satisfy the requirements
    # of the dependencies in the Gemfile.lock
    #
    # This allows us to add on the *new* requirements in the Gemfile and make
    # sure that the changes result in a conservative update to the Gemfile.lock.
    def locked_specs_as_deps
      deps = @dependencies & @locked_deps

      @dependencies.each do |dep|
        next if deps.include?(dep)
        deps << dep if @locked_specs.any? { |s| s.satisfies?(dep) }
      end

      @locked_specs.for(deps).map { |s| Gem::Dependency.new(s.name, s.version) }
    end
  end
end