Debian Codegraph @

lib/bundler/source/path.rb

raw
1 # frozen_string_literal: true
2
3 module Bundler
4 class Source
5 class Path < Source
6 autoload :Installer, File.expand_path("path/installer", __dir__)
7
8 attr_reader :path, :options, :root_path, :original_path
9 attr_writer :name
10 attr_accessor :version
11
12 protected :original_path
13
14 DEFAULT_GLOB = "{,*,*/*}.gemspec"
15
16 def initialize(options)
17 @checksum_store = Checksum::Store.new
18 @options = options.dup
19 @glob = options["glob"] || DEFAULT_GLOB
20
21 @root_path = options["root_path"] || root
22
23 if options["path"]
24 @path = Pathname.new(options["path"])
25 expanded_path = expand(@path)
26 @path = if @path.relative?
27 expanded_path.relative_path_from(root_path.expand_path)
28 else
29 expanded_path
30 end
31 end
32
33 @name = options["name"]
34 @version = options["version"]
35
36 # Stores the original path. If at any point we move to the
37 # cached directory, we still have the original path to copy from.
38 @original_path = @path
39 end
40
41 def self.from_lock(options)
42 new(options.merge("path" => options.delete("remote")))
43 end
44
45 def to_lock
46 out = String.new("PATH\n")
47 out << " remote: #{lockfile_path}\n"
48 out << " glob: #{@glob}\n" unless @glob == DEFAULT_GLOB
49 out << " specs:\n"
50 end
51
52 def to_s
53 "source at `#{@path}`"
54 end
55
56 alias_method :to_gemfile, :path
57
58 def hash
59 [self.class, expanded_path, version].hash
60 end
61
62 def eql?(other)
63 return unless other.class == self.class
64 expanded_original_path == other.expanded_original_path &&
65 version == other.version
66 end
67
68 alias_method :==, :eql?
69
70 def name
71 File.basename(expanded_path.to_s)
72 end
73
74 def install(spec, options = {})
75 using_message = "Using #{version_message(spec, options[:previous_spec])} from #{self}"
76 using_message += " and installing its executables" unless spec.executables.empty?
77 print_using_message using_message
78 generate_bin(spec, disable_extensions: true)
79 nil # no post-install message
80 end
81
82 def cache(spec, custom_path = nil)
83 app_cache_path = app_cache_path(custom_path)
84 return unless Bundler.feature_flag.cache_all?
85 return if expand(@original_path).to_s.index(root_path.to_s + "/") == 0
86
87 unless @original_path.exist?
88 raise GemNotFound, "Can't cache gem #{version_message(spec)} because #{self} is missing!"
89 end
90
91 FileUtils.rm_rf(app_cache_path)
92 FileUtils.cp_r("#{@original_path}/.", app_cache_path)
93 FileUtils.touch(app_cache_path.join(".bundlecache"))
94 end
95
96 def local_specs(*)
97 @local_specs ||= load_spec_files
98 end
99
100 def specs
101 if has_app_cache?
102 @path = app_cache_path
103 @expanded_path = nil # Invalidate
104 end
105 local_specs
106 end
107
108 def app_cache_dirname
109 name
110 end
111
112 def root
113 Bundler.root
114 end
115
116 def expanded_original_path
117 @expanded_original_path ||= expand(original_path)
118 end
119
120 private
121
122 def expanded_path
123 @expanded_path ||= expand(path)
124 end
125
126 def expand(somepath)
127 if Bundler.current_ruby.jruby? # TODO: Unify when https://github.com/rubygems/bundler/issues/7598 fixed upstream and all supported jrubies include the fix
128 somepath.expand_path(root_path).expand_path
129 else
130 somepath.expand_path(root_path)
131 end
132 rescue ArgumentError => e
133 Bundler.ui.debug(e)
134 raise PathError, "There was an error while trying to use the path " \
135 "`#{somepath}`.\nThe error message was: #{e.message}."
136 end
137
138 def lockfile_path
139 return relative_path(original_path) if original_path.absolute?
140 expand(original_path).relative_path_from(root)
141 end
142
143 def app_cache_path(custom_path = nil)
144 @app_cache_path ||= Bundler.app_cache(custom_path).join(app_cache_dirname)
145 end
146
147 def has_app_cache?
148 SharedHelpers.in_bundle? && app_cache_path.exist?
149 end
150
151 def load_gemspec(file)
152 return unless spec = Bundler.load_gemspec(file)
153 spec.installed_by_version = Gem::VERSION
154 spec
155 end
156
157 def validate_spec(spec)
158 Bundler.rubygems.validate(spec)
159 end
160
161 def load_spec_files
162 index = Index.new
163
164 if File.directory?(expanded_path)
165 # We sort depth-first since `<<` will override the earlier-found specs
166 Gem::Util.glob_files_in_dir(@glob, expanded_path).sort_by {|p| -p.split(File::SEPARATOR).size }.each do |file|
167 next unless spec = load_gemspec(file)
168 spec.source = self
169
170 # Validation causes extension_dir to be calculated, which depends
171 # on #source, so we validate here instead of load_gemspec
172 validate_spec(spec)
173 index << spec
174 end
175
176 if index.empty? && @name && @version
177 index << Gem::Specification.new do |s|
178 s.name = @name
179 s.source = self
180 s.version = Gem::Version.new(@version)
181 s.platform = Gem::Platform::RUBY
182 s.summary = "Fake gemspec for #{@name}"
183 s.relative_loaded_from = "#{@name}.gemspec"
184 s.authors = ["no one"]
185 if expanded_path.join("bin").exist?
186 executables = expanded_path.join("bin").children
187 executables.reject! {|p| File.directory?(p) }
188 s.executables = executables.map {|c| c.basename.to_s }
189 end
190 end
191 end
192 else
193 message = String.new("The path `#{expanded_path}` ")
194 message << if File.exist?(expanded_path)
195 "is not a directory."
196 else
197 "does not exist."
198 end
199 raise PathError, message
200 end
201
202 index
203 end
204
205 def relative_path(path = self.path)
206 if path.to_s.start_with?(root_path.to_s)
207 return path.relative_path_from(root_path)
208 end
209 path
210 end
211
212 def generate_bin(spec, options = {})
213 gem_dir = Pathname.new(spec.full_gem_path)
214
215 # Some gem authors put absolute paths in their gemspec
216 # and we have to save them from themselves
217 spec.files = spec.files.map do |path|
218 next path unless /\A#{Pathname::SEPARATOR_PAT}/o.match?(path)
219 next if File.directory?(path)
220 begin
221 Pathname.new(path).relative_path_from(gem_dir).to_s
222 rescue ArgumentError
223 path
224 end
225 end.compact
226
227 installer = Path::Installer.new(
228 spec,
229 env_shebang: false,
230 disable_extensions: options[:disable_extensions],
231 build_args: options[:build_args],
232 bundler_extension_cache_path: extension_cache_path(spec)
233 )
234 installer.post_install
235 rescue Gem::InvalidSpecificationException => e
236 Bundler.ui.warn "\n#{spec.name} at #{spec.full_gem_path} did not have a valid gemspec.\n" \
237 "This prevents bundler from installing bins or native extensions, but " \
238 "that may not affect its functionality."
239
240 if !spec.extensions.empty? && !spec.email.empty?
241 Bundler.ui.warn "If you need to use this package without installing it from a gem " \
242 "repository, please contact #{spec.email} and ask them " \
243 "to modify their .gemspec so it can work with `gem build`."
244 end
245
246 Bundler.ui.warn "The validation message from RubyGems was:\n #{e.message}"
247 end
248 end
249 end
250 end