summaryrefslogtreecommitdiff
path: root/lib/bundler/definition.rb
blob: 4a96cd5c45fcc4547697279fcfb1035df0d208d3 (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
module Bundler
  class Definition
    def self.from_gemfile(gemfile)
      gemfile = Pathname.new(gemfile).expand_path

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

      Dsl.evaluate(gemfile)
    end

    def self.from_lock(lockfile)
      # gemfile_definition = from_gemfile(nil)
      locked_definition = Locked.new(YAML.load_file(lockfile))
      # raise GemfileError unless gemfile_definition.equivalent?(locked_definition)
      locked_definition
    end

    attr_reader :dependencies, :sources

    alias actual_dependencies dependencies

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

    def local_index
      @local_index ||= begin
        index = Index.from_installed_gems

        sources.reverse_each do |source|
          index.merge! source.local_specs  if source.respond_to?(:local_specs)
        end
        index
      end
    end

    # def equivalent?(other)
    #   self.matches?(other) && other.matches?(self)
    #   # other.matches?(self)
    # end

    # def matches?(other)
    #   dependencies.all? do |dep|
    #     dep =~ other.specs.find {|spec| spec.name == dep.name }
    #   end
    # end

    class Locked < Definition
      def initialize(details)
        @details = details
      end

      def sources
        @sources ||= @details["sources"].map do |args|
          name, options = args.to_a.flatten
          Bundler::Source.const_get(name).new(options)
        end
      end

      def actual_dependencies
        @actual_dependencies ||= @details["specs"].map do |args|
          Gem::Dependency.new(*args.to_a.flatten)
        end
      end

      def dependencies
        @dependencies ||= @details["dependencies"].map do |args|
          Gem::Dependency.new(*args.to_a.flatten)
        end
      end
    end
  end
end