summaryrefslogtreecommitdiff
path: root/lib/bundler/source.rb
blob: 507aef9c886a1b0ec3c216b364b6d5e81301fd46 (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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
module Bundler
  class DirectorySourceError < StandardError; end
  class GitSourceError < StandardError ; end
  # Represents a source of rubygems. Initially, this is only gem repositories, but
  # eventually, this will be git, svn, HTTP
  class Source
    attr_accessor :repository, :local

    def initialize(options) ; end

  private

    def process_source_gems(gems)
      new_gems = Hash.new { |h,k| h[k] = [] }
      gems.values.each do |spec|
        spec.source = self
        new_gems[spec.name] << spec
      end
      new_gems
    end
  end

  class GemSource < Source
    attr_reader :uri

    def initialize(options)
      @uri = options[:uri]
      @uri = URI.parse(@uri) unless @uri.is_a?(URI)
      raise ArgumentError, "The source must be an absolute URI" unless @uri.absolute?
    end

    def can_be_local?
      false
    end

    def gems
      @specs ||= fetch_specs
    end

    def ==(other)
      uri == other.uri
    end

    def to_s
      @uri.to_s
    end

    class RubygemsRetardation < StandardError; end

    def download(spec)
      Bundler.logger.info "Downloading #{spec.full_name}.gem"

      destination = repository.path

      unless destination.writable?
        raise RubygemsRetardation, "destination: #{destination} is not writable"
      end

      # Download the gem
      Gem::RemoteFetcher.fetcher.download(spec, uri, destination)

      # Re-read the gemspec from the downloaded gem to correct
      # any errors that were present in the Rubyforge specification.
      new_spec = Gem::Format.from_file_by_path(destination.join('cache', "#{spec.full_name}.gem")).spec
      spec.__swap__(new_spec)
    end

  private

    def fetch_specs
      Bundler.logger.info "Updating source: #{to_s}"
      build_gem_index(fetch_main_specs + fetch_prerelease_specs)
    end

    def build_gem_index(index)
      gems = Hash.new { |h,k| h[k] = [] }
      index.each do |name, version, platform|
        spec = RemoteSpecification.new(name, version, platform, @uri)
        spec.source = self
        gems[spec.name] << spec if Gem::Platform.match(spec.platform)
      end
      gems
    end

    def fetch_main_specs
      Marshal.load(Gem::RemoteFetcher.fetcher.fetch_path("#{uri}/specs.4.8.gz"))
    rescue Gem::RemoteFetcher::FetchError => e
      raise ArgumentError, "#{to_s} is not a valid source: #{e.message}"
    end

    def fetch_prerelease_specs
      Marshal.load(Gem::RemoteFetcher.fetcher.fetch_path("#{uri}/prerelease_specs.4.8.gz"))
    rescue Gem::RemoteFetcher::FetchError
      Bundler.logger.warn "Source '#{uri}' does not support prerelease gems"
      []
    end
  end

  class SystemGemSource < Source

    def self.instance
      @instance ||= new({})
    end

    def initialize(options)
      @source = Gem::SourceIndex.from_installed_gems
    end

    def can_be_local?
      false
    end

    def gems
      @gems ||= process_source_gems(@source.gems)
    end

    def ==(other)
      other.is_a?(SystemGemSource)
    end

    def to_s
      "system"
    end

    def download(spec)
      gemfile = Pathname.new(spec.loaded_from)
      gemfile = gemfile.dirname.join('..', 'cache', "#{spec.full_name}.gem")
      repository.cache(gemfile)
    end

  private

  end

  class GemDirectorySource < Source
    attr_reader :location

    def initialize(options)
      @location = options[:location]
    end

    def can_be_local?
      true
    end

    def gems
      @specs ||= fetch_specs
    end

    def ==(other)
      location == other.location
    end

    def to_s
      location.to_s
    end

    def download(spec)
      # raise NotImplementedError
    end

  private

    def fetch_specs
      specs = Hash.new { |h,k| h[k] = [] }

      Dir["#{@location}/*.gem"].each do |gemfile|
        spec = Gem::Format.from_file_by_path(gemfile).spec
        spec.source = self
        specs[spec.name] << spec
      end

      specs
    end
  end

  class DirectorySource < Source
    attr_reader :location, :specs, :required_specs

    def initialize(options)
      if options[:location]
        @location = Pathname.new(options[:location]).expand_path
      end
      @glob           = options[:glob] || "**/*.gemspec"
      @specs          = {}
      @required_specs = []
    end

    def add_spec(path, name, version, require_paths = %w(lib))
      raise DirectorySourceError, "already have a gem defined for '#{path}'" if @specs[path.to_s]
      @specs[path.to_s] = Gem::Specification.new do |s|
        s.name     = name
        s.version  = Gem::Version.new(version)
      end
    end

    def can_be_local?
      true
    end

    def gems
      @gems ||= begin
        # Locate all gemspecs from the directory
        specs = locate_gemspecs
        specs = merge_defined_specs(specs)

        required_specs.each do |required|
          unless specs.any? {|k,v| v.name == required }
            raise DirectorySourceError, "No gemspec for '#{required}' was found in" \
              " '#{location}'. Please explicitly specify a version."
          end
        end

        process_source_gems(specs)
      end
    end

    def locate_gemspecs
      Dir["#{location}/#{@glob}"].inject({}) do |specs, file|
        file = Pathname.new(file)
        if spec = eval(File.read(file)) # and validate_gemspec(file.dirname, spec)
          spec.location = file.dirname.expand_path
          specs[spec.full_name] = spec
        end
        specs
      end
    end

    def merge_defined_specs(specs)
      @specs.each do |path, spec|
        # Set the spec location
        spec.location = "#{location}/#{path}"

        if existing = specs.values.find { |s| s.name == spec.name }
          if existing.version != spec.version
            raise DirectorySourceError, "The version you specified for #{spec.name}" \
              " is #{spec.version}. The gemspec is #{existing.version}."
          # Not sure if this is needed
          # ====
          # elsif File.expand_path(existing.location) != File.expand_path(spec.location)
          #   raise DirectorySourceError, "The location you specified for #{spec.name}" \
          #     " is '#{spec.location}'. The gemspec was found at '#{existing.location}'."
          end
        # elsif !validate_gemspec(spec.location, spec)
        #   raise "Your gem definition is not valid: #{spec}"
        else
          specs[spec.full_name] = spec
        end
      end
      specs
    end

    def validate_gemspec(path, spec)
      path = Pathname.new(path)
      msg  = "Gemspec for #{spec.name} (#{spec.version}) is invalid:"
      # Check the require_paths
      (spec.require_paths || []).each do |require_path|
        unless path.join(require_path).directory?
          Bundler.logger.warn "#{msg} Missing require path: '#{require_path}'"
          return false
        end
      end

      # Check the executables
      (spec.executables || []).each do |exec|
        unless path.join(spec.bindir, exec).file?
          Bundler.logger.warn "#{msg} Missing executable: '#{File.join(spec.bindir, exec)}'"
          return false
        end
      end

      true
    end

    def ==(other)
      # TMP HAX
      other.is_a?(DirectorySource)
    end

    def to_s
      "directory: '#{location}'"
    end

    def download(spec)
      # Nothing needed here
    end
  end

  class GitSource < DirectorySource
    attr_reader :ref, :uri, :branch

    def initialize(options)
      super
      @uri = options[:uri]
      @branch = options[:branch] || 'master'
      @ref = options[:ref] || "origin/#{@branch}"
    end

    def location
      # TMP HAX to get the *.gemspec reading to work
      repository.path.join('dirs', File.basename(@uri, '.git'))
    end

    def gems
      update
      checkout
      super
    end

    def download(spec)
      # Nothing needed here
    end

    def to_s
      "git: #{uri}"
    end

    private
      def update
        if location.directory?
          fetch
        else
          clone
        end
      end

      def fetch
        unless local
          Bundler.logger.info "Fetching git repository at: #{@uri}"
          Dir.chdir(location) { `git fetch origin` }
        end
      end

      def clone
        # Raise an error if the source should run in local mode,
        # but it has not been cached yet.
        if local
          raise SourceNotCached, "Git repository '#{@uri}' has not been cloned yet"
        end

        Bundler.logger.info "Cloning git repository at: #{@uri}"
        FileUtils.mkdir_p(location.dirname)
        `git clone #{@uri} #{location} --no-hardlinks`
      end

      def checkout
        Dir.chdir(location) { `git checkout --quiet #{@ref}` }
      end
  end
end