Debian Codegraph @

lib/bundler/plugin/api/source.rb

raw
1 # frozen_string_literal: true
2
3 module Bundler
4 module Plugin
5 class API
6 # This class provides the base to build source plugins
7 # All the method here are required to build a source plugin (except
8 # `uri_hash`, `gem_install_dir`; they are helpers).
9 #
10 # Defaults for methods, where ever possible are provided which is
11 # expected to work. But, all source plugins have to override
12 # `fetch_gemspec_files` and `install`. Defaults are also not provided for
13 # `remote!`, `cache!` and `unlock!`.
14 #
15 # The defaults shall work for most situations but nevertheless they can
16 # be (preferably should be) overridden as per the plugins' needs safely
17 # (as long as they behave as expected).
18 # On overriding `initialize` you should call super first.
19 #
20 # If required plugin should override `hash`, `==` and `eql?` methods to be
21 # able to match objects representing same sources, but may be created in
22 # different situation (like form gemfile and lockfile). The default ones
23 # checks only for class and uri, but elaborate source plugins may need
24 # more comparisons (e.g. git checking on branch or tag).
25 #
26 # @!attribute [r] uri
27 # @return [String] the remote specified with `source` block in Gemfile
28 #
29 # @!attribute [r] options
30 # @return [String] options passed during initialization (either from
31 # lockfile or Gemfile)
32 #
33 # @!attribute [r] name
34 # @return [String] name that can be used to uniquely identify a source
35 #
36 # @!attribute [rw] dependency_names
37 # @return [Array<String>] Names of dependencies that the source should
38 # try to resolve. It is not necessary to use this list internally. This
39 # is present to be compatible with `Definition` and is used by
40 # rubygems source.
41 module Source
42 attr_reader :uri, :options, :name, :checksum_store
43 attr_accessor :dependency_names
44
45 def initialize(opts)
46 @options = opts
47 @dependency_names = []
48 @uri = opts["uri"]
49 @type = opts["type"]
50 @name = opts["name"] || "#{@type} at #{@uri}"
51 @checksum_store = Checksum::Store.new
52 end
53
54 # This is used by the default `spec` method to constructs the
55 # Specification objects for the gems and versions that can be installed
56 # by this source plugin.
57 #
58 # Note: If the spec method is overridden, this function is not necessary
59 #
60 # @return [Array<String>] paths of the gemspec files for gems that can
61 # be installed
62 def fetch_gemspec_files
63 []
64 end
65
66 # Options to be saved in the lockfile so that the source plugin is able
67 # to check out same version of gem later.
68 #
69 # There options are passed when the source plugin is created from the
70 # lock file.
71 #
72 # @return [Hash]
73 def options_to_lock
74 {}
75 end
76
77 # Install the gem specified by the spec at appropriate path.
78 # `install_path` provides a sufficient default, if the source can only
79 # satisfy one gem, but is not binding.
80 #
81 # @return [String] post installation message (if any)
82 def install(spec, opts)
83 raise MalformattedPlugin, "Source plugins need to override the install method."
84 end
85
86 # It builds extensions, generates bins and installs them for the spec
87 # provided.
88 #
89 # It depends on `spec.loaded_from` to get full_gem_path. The source
90 # plugins should set that.
91 #
92 # It should be called in `install` after the plugin is done placing the
93 # gem at correct install location.
94 #
95 # It also runs Gem hooks `pre_install`, `post_build` and `post_install`
96 #
97 # Note: Do not override if you don't know what you are doing.
98 def post_install(spec, disable_exts = false)
99 opts = { env_shebang: false, disable_extensions: disable_exts }
100 installer = Bundler::Source::Path::Installer.new(spec, opts)
101 installer.post_install
102 end
103
104 # A default installation path to install a single gem. If the source
105 # servers multiple gems, it's not of much use and the source should one
106 # of its own.
107 def install_path
108 @install_path ||=
109 begin
110 base_name = File.basename(Gem::URI.parse(uri).normalize.path)
111
112 gem_install_dir.join("#{base_name}-#{uri_hash[0..11]}")
113 end
114 end
115
116 # Parses the gemspec files to find the specs for the gems that can be
117 # satisfied by the source.
118 #
119 # Few important points to keep in mind:
120 # - If the gems are not installed then it shall return specs for all
121 # the gems it can satisfy
122 # - If gem is installed (that is to be detected by the plugin itself)
123 # then it shall return at least the specs that are installed.
124 # - The `loaded_from` for each of the specs shall be correct (it is
125 # used to find the load path)
126 #
127 # @return [Bundler::Index] index containing the specs
128 def specs
129 files = fetch_gemspec_files
130
131 Bundler::Index.build do |index|
132 files.each do |file|
133 next unless spec = Bundler.load_gemspec(file)
134 spec.installed_by_version = Gem::VERSION
135
136 spec.source = self
137 Bundler.rubygems.validate(spec)
138
139 index << spec
140 end
141 end
142 end
143
144 # Set internal representation to fetch the gems/specs locally.
145 #
146 # When this is called, the source should try to fetch the specs and
147 # install from the local system.
148 def local!
149 end
150
151 # Set internal representation to fetch the gems/specs from remote.
152 #
153 # When this is called, the source should try to fetch the specs and
154 # install from remote path.
155 def remote!
156 end
157
158 # Set internal representation to fetch the gems/specs from app cache.
159 #
160 # When this is called, the source should try to fetch the specs and
161 # install from the path provided by `app_cache_path`.
162 def cached!
163 end
164
165 # This is called to update the spec and installation.
166 #
167 # If the source plugin is loaded from lockfile or otherwise, it shall
168 # refresh the cache/specs (e.g. git sources can make a fresh clone).
169 def unlock!
170 end
171
172 # Name of directory where plugin the is expected to cache the gems when
173 # #cache is called.
174 #
175 # Also this name is matched against the directories in cache for pruning
176 #
177 # This is used by `app_cache_path`
178 def app_cache_dirname
179 base_name = File.basename(Gem::URI.parse(uri).normalize.path)
180 "#{base_name}-#{uri_hash}"
181 end
182
183 # This method is called while caching to save copy of the gems that the
184 # source can resolve to path provided by `app_cache_app`so that they can
185 # be reinstalled from the cache without querying the remote (i.e. an
186 # alternative to remote)
187 #
188 # This is stored with the app and source plugins should try to provide
189 # specs and install only from this cache when `cached!` is called.
190 #
191 # This cache is different from the internal caching that can be done
192 # at sub paths of `cache_path` (from API). This can be though as caching
193 # by bundler.
194 def cache(spec, custom_path = nil)
195 new_cache_path = app_cache_path(custom_path)
196
197 FileUtils.rm_rf(new_cache_path)
198 FileUtils.cp_r(install_path, new_cache_path)
199 FileUtils.rm_rf(app_cache_path.join(".git"))
200 FileUtils.touch(app_cache_path.join(".bundlecache"))
201 end
202
203 # This shall check if two source object represent the same source.
204 #
205 # The comparison shall take place only on the attribute that can be
206 # inferred from the options passed from Gemfile and not on attributes
207 # that are used to pin down the gem to specific version (e.g. Git
208 # sources should compare on branch and tag but not on commit hash)
209 #
210 # The sources objects are constructed from Gemfile as well as from
211 # lockfile. To converge the sources, it is necessary that they match.
212 #
213 # The same applies for `eql?` and `hash`
214 def ==(other)
215 other.is_a?(self.class) && uri == other.uri
216 end
217
218 # When overriding `eql?` please preserve the behaviour as mentioned in
219 # docstring for `==` method.
220 alias_method :eql?, :==
221
222 # When overriding `hash` please preserve the behaviour as mentioned in
223 # docstring for `==` method, i.e. two methods equal by above comparison
224 # should have same hash.
225 def hash
226 [self.class, uri].hash
227 end
228
229 # A helper method, not necessary if not used internally.
230 def installed?
231 File.directory?(install_path)
232 end
233
234 # The full path where the plugin should cache the gem so that it can be
235 # installed latter.
236 #
237 # Note: Do not override if you don't know what you are doing.
238 def app_cache_path(custom_path = nil)
239 @app_cache_path ||= Bundler.app_cache(custom_path).join(app_cache_dirname)
240 end
241
242 # Used by definition.
243 #
244 # Note: Do not override if you don't know what you are doing.
245 def unmet_deps
246 specs.unmet_dependency_names
247 end
248
249 # Used by definition.
250 #
251 # Note: Do not override if you don't know what you are doing.
252 def spec_names
253 specs.spec_names
254 end
255
256 # Used by definition.
257 #
258 # Note: Do not override if you don't know what you are doing.
259 def add_dependency_names(names)
260 @dependencies |= Array(names)
261 end
262
263 # NOTE: Do not override if you don't know what you are doing.
264 def can_lock?(spec)
265 spec.source == self
266 end
267
268 # Generates the content to be entered into the lockfile.
269 # Saves type and remote and also calls to `options_to_lock`.
270 #
271 # Plugin should use `options_to_lock` to save information in lockfile
272 # and not override this.
273 #
274 # Note: Do not override if you don't know what you are doing.
275 def to_lock
276 out = String.new("#{LockfileParser::PLUGIN}\n")
277 out << " remote: #{@uri}\n"
278 out << " type: #{@type}\n"
279 options_to_lock.each do |opt, value|
280 out << " #{opt}: #{value}\n"
281 end
282 out << " specs:\n"
283 end
284
285 def to_s
286 "plugin source for #{@type} with uri #{@uri}"
287 end
288 alias_method :identifier, :to_s
289
290 # NOTE: Do not override if you don't know what you are doing.
291 def include?(other)
292 other == self
293 end
294
295 def uri_hash
296 SharedHelpers.digest(:SHA1).hexdigest(uri)
297 end
298
299 # NOTE: Do not override if you don't know what you are doing.
300 def gem_install_dir
301 Bundler.install_path
302 end
303
304 # It is used to obtain the full_gem_path.
305 #
306 # spec's loaded_from path is expanded against this to get full_gem_path
307 #
308 # Note: Do not override if you don't know what you are doing.
309 def root
310 Bundler.root
311 end
312
313 # @private
314 # This API on source might not be stable, and for now we expect plugins
315 # to download all specs in `#specs`, so we implement the method for
316 # compatibility purposes and leave it undocumented (and don't support)
317 # overriding it)
318 def double_check_for(*); end
319 end
320 end
321 end
322 end