summaryrefslogtreecommitdiff
path: root/lib/bundler/source.rb
blob: 702decbfa10603e38c76c7904aefc7ef2e2237d5 (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
require "rubygems/remote_fetcher"
require "rubygems/format"
require "digest/sha1"

module Bundler
  module Source
    class Rubygems
      attr_reader :uri, :options

      def initialize(options = {})
        @options = 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 specs
        @specs ||= fetch_specs
      end

      def install(spec)
        Bundler.ui.info "* #{spec.name} (#{spec.version})"
        if Index.from_installed_gems[spec].any?
          Bundler.ui.info "  * already installed... skipping"
          return
        end

        destination = Gem.dir

        Bundler.ui.info "  * Downloading..."
        gem_path  = Gem::RemoteFetcher.fetcher.download(spec, uri, destination)
        Bundler.ui.info "  * Installing..."
        installer = Gem::Installer.new gem_path,
          :install_dir => Gem.dir,
          :ignore_dependencies => true

        installer.install
      end

    private

      def fetch_specs
        index = Index.new
        Bundler.ui.info "Source: Fetching remote index for `#{uri}`... "
        (main_specs + prerelease_specs).each do |name, version, platform|
          spec = RemoteSpecification.new(name, version, platform, @uri)
          spec.source = self
          index << spec
        end
        Bundler.ui.info "done."
        index.freeze
      end

      def 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 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 GemCache
      def initialize(options)
        @path = options[:path]
      end

      def specs
        @specs ||= begin
          index = Index.new

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

          index.freeze
        end
      end

      def install(spec)
        destination = Gem.dir

        installer = Gem::Installer.new "#{@path}/#{spec.full_name}.gem",
          :install_dir => Gem.dir,
          :ignore_dependencies => true

        installer.install
      end
    end

    class Path
      attr_reader :path, :options

      def initialize(options)
        @options = options
        @glob = options[:glob] || "{,*/}*.gemspec"
        @path = options[:path]
      end

      def specs
        @specs ||= begin
          index = Index.new

          Dir["#{path}/#{@glob}"].each do |file|
            file = Pathname.new(file)
            if spec = eval(File.read(file))
              spec = Specification.from_gemspec(spec)
              spec.loaded_from = file
              spec.source      = self
              index << spec
            end
          end
          index.freeze
        end
      end

      alias local_specs specs
    end

    class Git < Path
      attr_reader :uri, :ref

      def initialize(options)
        @options = options
        @glob = options[:glob] || "{,*/}*.gemspec"
        @uri  = options[:uri]
        @ref  = options[:ref] || options[:branch] || 'master'
      end

      def options
        @options.merge(:ref => revision)
      end

      def path
        Bundler.install_path.join("#{base_name}-#{uri_hash}-#{ref}")
      end

      def to_s
        @uri
      end

      def specs
        @specs ||= begin
          index = Index.new
          # Start by making sure the git cache is up to date
          cache
          # Find all gemspecs in the repo
          in_cache do
            out   = %x(git ls-tree -r #{revision}).strip
            lines = out.split("\n").select { |l| l =~ /\.gemspec$/ }
            # Loop over the lines and extract the relative path and the
            # git hash
            lines.each do |line|
              next unless line =~ %r{^(\d+) (blob|tree) ([a-zf0-9]+)\t(.*)$}
              hash, file = $3, $4
              # Read the gemspec
              if spec = eval(%x(git cat-file blob #{$3}))
                spec = Specification.from_gemspec(spec)
                spec.relative_loaded_from = file
                spec.source = self
                index << spec
              end
            end
          end
          index.freeze
        end
      end

      def install(spec)
        @installed ||= begin
          FileUtils.mkdir_p(path)
          Dir.chdir(path) do
            unless File.exist?(".git")
              %x(git clone --recursive --no-checkout #{cache_path} #{path})
            end
            %x(git fetch --quiet)
            %x(git reset --hard #{revision})
            %x(git submodule init)
            %x(git submodule update)
          end
          true
        end
      end

    private

      def base_name
        File.basename(uri, ".git")
      end

      def uri_hash
        Digest::SHA1.hexdigest(URI.parse(uri).normalize.to_s.sub(%r{/$}, ''))
      end

      def cache_path
        @cache_path ||= Bundler.cache.join("git", "#{base_name}-#{uri_hash}")
      end

      def cache
        if cache_path.exist?
          Bundler.ui.info "Source: Updating `#{uri}`... "
          in_cache { `git fetch --quiet #{uri} master:master` }
        else
          Bundler.ui.info "Source: Cloning `#{uri}`... "
          FileUtils.mkdir_p(cache_path.dirname)
          `git clone #{uri} #{cache_path} --bare --no-hardlinks`
        end
        Bundler.ui.info "Done."
      end

      def revision
        @revision ||= in_cache { `git rev-parse #{ref}`.strip }
      end

      def in_cache(&blk)
        Dir.chdir(cache_path, &blk)
      end
    end
  end
end