summaryrefslogtreecommitdiff
path: root/lib/bundler/resolver/builders.rb
blob: 2b7b48211c2bb7a0c29304da45bdb56eef7e3758 (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
module Bundler
  module Resolver
    module Builders
      def build_index(&block)
        index = Gem::SourceIndex.new
        IndexBuilder.run(index, &block) if block_given?
        index
      end

      def build_spec(name, version, &block)
        spec = Gem::Specification.new
        spec.instance_variable_set(:@name, name)
        spec.instance_variable_set(:@version, Gem::Version.new(version))
        DepBuilder.run(spec, &block) if block_given?
        spec
      end

      def build_dep(name, requirements, type = :runtime)
        Gem::Dependency.new(name, requirements, type)
      end

      class IndexBuilder
        include Builders

        def self.run(index, &block)
          new(index).run(&block)
        end

        def initialize(index)
          @index = index
        end

        def run(&block)
          instance_eval(&block)
        end

        def add_spec(*args, &block)
          @index.add_spec(build_spec(*args, &block))
        end
      end

      class DepBuilder
        def self.run(spec, &block)
          new(spec).run(&block)
        end

        def initialize(spec)
          @spec = spec
        end

        def run(&block)
          instance_eval(&block)
        end

        def runtime(name, requirements)
          @spec.add_runtime_dependency(name, requirements)
        end
      end
    end
  end
end