summaryrefslogtreecommitdiff
path: root/lib/bundler08/dsl.rb
blob: 4365edda48387d20e730028db63469a7fd31782f (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
module Bundler
  class ManifestFileNotFound < StandardError; end
  class InvalidKey < StandardError; end
  class DefaultManifestNotFound < StandardError; end

  class Dsl
    def self.evaluate(file, bundle, environment)
      builder = new(bundle, environment)
      builder.instance_eval(File.read(file.to_s), file.to_s, 1)
      environment
    end

    def initialize(bundle, environment)
      @bundle = bundle
      @environment = environment
      @directory_sources = []
      @git_sources = {}
      @only, @except, @directory, @git = nil, nil, nil, nil
    end

    def bundle_path(path)
      @bundle.path = Pathname.new(path)
    end

    def bin_path(path)
      @bundle.bindir = Pathname.new(path)
    end

    def disable_rubygems
      @environment.rubygems = false
    end

    def disable_system_gems
      @environment.system_gems = false
    end

    def source(source)
      source = GemSource.new(@bundle, :uri => source)
      unless @environment.sources.include?(source)
        @environment.add_source(source)
      end
    end

    def only(*env)
      old, @only = @only, _combine_only(env)
      yield
      @only = old
    end

    def except(*env)
      old, @except = @except, _combine_except(env)
      yield
      @except = old
    end

    def directory(path, options = {})
      raise DirectorySourceError, "cannot nest calls to directory or git" if @directory || @git
      @directory = DirectorySource.new(@bundle, options.merge(:location => path))
      @directory_sources << @directory
      @environment.add_priority_source(@directory)
      retval = yield if block_given?
      @directory = nil
      retval
    end

    def git(uri, options = {})
      raise DirectorySourceError, "cannot nest calls to directory or git" if @directory || @git
      @git = GitSource.new(@bundle, options.merge(:uri => uri))
      @git_sources[uri] = @git
      @environment.add_priority_source(@git)
      retval = yield if block_given?
      @git = nil
      retval
    end

    def clear_sources
      @environment.clear_sources
    end

    def gem(name, *args)
      options = args.last.is_a?(Hash) ? args.pop : {}
      version = args.last

      keys = :vendored_at, :path, :only, :except, :git, :path, :bundle, :require_as, :tag, :branch, :ref
      unless (invalid = options.keys - keys).empty?
        raise InvalidKey, "Only #{keys.join(", ")} are valid options to #gem. You used #{invalid.join(", ")}"
      end

      if path = options.delete(:vendored_at)
        options[:path] = path
        warn "The :vendored_at option is deprecated. Use :path instead.\nFrom #{caller[0]}"
      end

      options[:only] = _combine_only(options[:only] || options["only"])
      options[:except] = _combine_except(options[:except] || options["except"])

      dep = Dependency.new(name, options.merge(:version => version))

      if options.key?(:bundle) && !options[:bundle]
        dep.source = SystemGemSource.new(@bundle)
      elsif @git || options[:git]
        dep.source = _handle_git_option(name, version, options)
      elsif @directory || options[:path]
        dep.source = _handle_vendored_option(name, version, options)
      end

      @environment.dependencies << dep
    end

  private

    def _version?(version)
      version && Gem::Version.new(version) rescue false
    end

    def _handle_vendored_option(name, version, options)
      dir, path = _find_directory_source(options[:path])

      if dir
        dir.required_specs << name
        dir.add_spec(path, name, version) if _version?(version)
        dir
      else
        directory options[:path] do
          _handle_vendored_option(name, version, {})
        end
      end
    end

    def _find_directory_source(path)
      if @directory
        return @directory, Pathname.new(path || '')
      end

      path = @bundle.gemfile.dirname.join(path)

      @directory_sources.each do |s|
        if s.location.expand_path.to_s < path.expand_path.to_s
          return s, path.relative_path_from(s.location)
        end
      end

      nil
    end

    def _handle_git_option(name, version, options)
      git    = options[:git].to_s
      ref    = options[:ref] || options[:tag]
      branch = options[:branch]

      if source = @git || @git_sources[git]
        if ref && source.ref != ref
          raise GitSourceError, "'#{git}' already specified with ref: #{source.ref}"
        elsif branch && source.branch != branch
          raise GitSourceError, "'#{git}' already specified with branch: #{source.branch}"
        end

        source.required_specs << name
        source.add_spec(Pathname.new(options[:path] || '.'), name, version) if _version?(version)
        source
      else
        git(git, :ref => ref, :branch => branch) do
          _handle_git_option(name, version, options)
        end
      end
    end

    def _combine_only(only)
      return @only unless only
      only = Array(only).compact.uniq.map { |o| o.to_s }
      only &= @only if @only
      only
    end

    def _combine_except(except)
      return @except unless except
      except = Array(except).compact.uniq.map { |o| o.to_s }
      except |= @except if @except
      except
    end
  end
end