Debian Codegraph @

vendor/mruby-source/lib/mruby/build/load_gems.rb

raw
1 require 'yaml'
2
3 module MRuby
4 module LoadGems
5
6 def gembox(gemboxfile)
7 gembox = File.expand_path("#{gemboxfile}.gembox", "#{MRUBY_ROOT}/mrbgems")
8 fail "Can't find gembox '#{gembox}'" unless File.exist?(gembox)
9
10 GemBox.config = self
11 GemBox.path = gembox
12
13 instance_eval File.read(gembox)
14
15 GemBox.path = nil
16 end
17
18 def gem(gem_src, &block)
19
20 caller_dir = File.expand_path(File.dirname(caller(1,1)[0][/^(.*?):\d/,1]))
21
22 gem_src = {gemdir: gem_src} if gem_src.is_a? String
23
24 @gem_checkouts ||= {}
25 checkout = GemLoader
26 .new(self, caller_dir, @gem_checkouts, **gem_src)
27 .fetch!
28 return nil unless checkout
29 @gem_checkouts[checkout.gemdir] = checkout
30
31 # Load the gem's rakefile
32 gemrake = File.join(checkout.full_gemdir, "mrbgem.rake")
33 fail "Can't find #{gemrake}" unless File.exist?(gemrake)
34
35 Gem.current = nil
36 load gemrake
37 return nil unless Gem.current
38 current = Gem.current
39
40 # Add it to gems
41 current.dir = checkout.full_gemdir
42 current.build = self.is_a?(MRuby::Build) ? self : MRuby::Build.current
43 current.build_config_initializer = block
44 gems << current
45
46 cxx_srcs = Dir.glob("#{current.dir}/{src,test,tools}/*.{cpp,cxx,cc}")
47 enable_cxx_exception unless cxx_srcs.empty?
48
49 current
50 end
51
52
53 # Class to represent the relationship between a gem dependency and
54 # its remote repository (if any).
55 class GemCheckout
56 attr_reader :gemdir, :repo, :branch, :commit
57
58 def initialize(gemdir, repo, branch, commit, canonical, path = nil)
59 @gemdir = gemdir # Working copy of the gem
60 @path = path # Path to gem relative to checkout
61
62 @repo = repo # Remote gem repo
63 @branch = branch # Branch to check out
64 @commit = commit # Commit-id to use
65
66 @canonical = canonical # This is the One True checkout
67 end
68
69 def full_gemdir
70 return @gemdir unless @path
71 return File.join(@gemdir, @path)
72 end
73
74 def canonical?() return @canonical; end
75 def git?() return !!@repo; end
76 def gemname() return File.basename(@gemdir); end
77
78 def hash
79 return [@gemdir, @repo, @branch, @commit, @canonical, @path].hash
80 end
81
82 def ==(other)
83 return @gemdir == other.gemdir &&
84 @repo == other.repo &&
85 @branch == other.branch &&
86 @commit == other.commit &&
87 @canonical == other.canonical? &&
88 full_gemdir == other.full_gemdir
89 end
90 alias_method :eql?, :==
91
92 def to_s
93 desc = @gemdir
94 desc += " -> #{@repo}/#{@branch}" if git?
95 desc += "/#{commit}" if commit
96 return desc
97 end
98 end
99
100 # Class to decode the argument set given to 'MRuby::Build::gem',
101 # and git-clone+git-checkout the sources if needed.
102 class GemLoader
103 def initialize(build,
104 build_config_dir, # Parent dir. of build_config
105 gem_checkouts, # Hash of existing checkouts
106
107 # Git repo:
108 git: nil,
109 branch: "master",
110 checksum_hash: nil,
111 options: [],
112 path: nil, # path to root relative to gem checkout
113
114 # Git repo on GitHub
115 github: nil,
116
117 # Git repo on BitBucket
118 bitbucket: nil,
119 method: nil,
120
121 # mgem entry
122 mgem: nil,
123
124 # Core package
125 core: nil,
126
127 # Local file(s)
128 gemdir: nil,
129
130 # Related flags:
131 canonical: false # Ignore subsequent checkout of this gem
132 )
133 # Tolerate a single string option
134 options = [options] unless options.is_a? Array
135
136 @build = build
137 @build_config_dir = build_config_dir
138 @gem_checkouts = gem_checkouts
139 @canonical = canonical
140
141 @git = git
142 @path = path
143 @branch = branch
144 @checksum_hash = checksum_hash
145 @options = options
146 @canonical = canonical
147
148 @github = github
149
150 @bitbucket = bitbucket
151 @method = method
152
153 @mgem = mgem
154 @core = core
155 @gemdir = gemdir
156
157
158 actions = [git, github, bitbucket, mgem, core, gemdir]
159 fail("Need to set exactly ONE of git, github, bitbucket, mgem, core, " +
160 "or gemdir") unless actions.compact.size == 1
161 end
162
163 # Retrieve the repo and return the details in a GemCheckout
164 # object or nil if nothing needed to be done.
165 def fetch!
166 return fromGemdir! if @gemdir
167 return fromCore! if @core
168 return fromGitHub! if @github
169 return fromBitBucket! if @bitbucket
170
171 return fromMGem! if @mgem
172
173 return fromGit!(@git, @branch) if @git
174
175 # Shouldn't be reachable, but...
176 fail "Invalid gem configuration!"
177 end
178
179 private
180
181 #
182 # Local Paths
183 #
184
185 def fromGemdir!
186 gem_src = @gemdir
187
188 # If @gemdir is a relative path, we first convert it to an
189 # absolute path; this depends on circumstances.
190 if MRuby::GemBox.path
191 # If GemBox.path is set, it means that this fetch operation is
192 # happening as part of a gembox evaluation and we use the
193 # gembox's path as the starting point.
194 gem_src = File.expand_path(gem_src, File.dirname(MRuby::GemBox.path))
195 else
196 # Otherwise, we use the path to the build_config.rb file that
197 # requested this gem. This path was extracted earlier and
198 # stored in @build_config_dir via the second argument of
199 # 'initialize'.
200 root_dir = @build_config_dir
201
202 # And we default to the repo root if the file is one of the
203 # stock configs in build_config/.
204 root_dir = MRUBY_ROOT if root_dir == "#{MRUBY_ROOT}/build_config"
205
206 gem_src = File.expand_path(gem_src, root_dir)
207 end
208
209 return GemCheckout.new(gem_src, nil, nil, nil, @canonical)
210 end
211
212 def fromCore!
213 return GemCheckout.new("#{@build.root}/mrbgems/#{@core}", nil, nil,
214 nil, @canonical)
215 end
216
217
218 #
219 # Git forges
220 #
221
222 def fromGitHub!
223 url = "https://github.com/#{@github}.git"
224 return fromGit!(url, @branch)
225 end
226
227 def fromBitBucket!
228 if @method == "ssh"
229 url = "[email protected]:#{@bitbucket}.git"
230 else
231 url = "https://bitbucket.org/#{@bitbucket}.git"
232 end
233
234 return fromGit!(url, @branch)
235 end
236
237
238 #
239 # mgem file
240 #
241
242 def fromMGem!
243 mgem = fetchMGem(@mgem)
244
245 url = mgem['repository']
246 branch = mgem['branch'] || @branch
247
248 return fromGit!(url, branch)
249 end
250
251 # Fetch the contents of the named mgem item. Will clone the
252 # mgem-list repo if not present
253 def fetchMGem(mgem)
254 list_dir = "#{@build.gem_clone_dir}/mgem-list"
255 url = 'https://github.com/mruby/mgem-list.git'
256
257 git_clone_dependency(url, list_dir, nil, 'master')
258
259 conf_path = "#{list_dir}/#{mgem}.gem"
260 conf_path = "#{list_dir}/mruby-#{mgem}.gem" unless
261 File.exist? conf_path
262 fail "mgem not found: #{mgem}" unless File.exist? conf_path
263
264 conf = YAML.load File.read conf_path
265 fail "unknown mgem protocol: #{conf['protocol']}" if
266 conf['protocol'] != 'git'
267
268 return conf
269 end
270
271
272 #
273 # Git checkouts
274 #
275
276 def fromGit!(url, branch)
277 repo_dir = "#{@build.gem_clone_dir}/" +
278 "#{url.match(/([-\w]+)(\.[-\w]+|)$/).to_a[1]}"
279 commit = @checksum_hash
280
281 return nil if skip_this?(url, repo_dir, branch, commit)
282
283 # If there's a lockfile entry for this repo AND the user hasn't
284 # specified a specific commit ID, we use the locked branch and
285 # commit.
286 lock = @build.locks[url] if @build.lock_enabled?
287 if !commit && lock
288 branch = lock['branch']
289 commit = lock['commit']
290 end
291
292 # Clone the dependency (if needed) and checkout the expected
293 # revision.
294 git_clone_dependency(url, repo_dir, commit, branch)
295 git_checkout_dependency(repo_dir, commit, branch)
296
297 # Set the lockfile entry if enabled
298 if @build.lock_enabled?
299 @build.gem_dir_to_repo_url[repo_dir] = url
300 @build.locks[url] = {
301 'url' => url,
302 'branch' => @build.git.current_branch(repo_dir),
303 'commit' => @build.git.commit_hash(repo_dir),
304 }
305 end
306
307 return GemCheckout.new(repo_dir, url, branch, commit, @canonical,@path)
308 end
309
310
311 # Test if this repo can be skipped. This will happen if it's
312 # already in @gem_checkouts and EITHER it is identical (same
313 # url, branch, commit-ID and subdirectory path) as the current
314 # checkout OR its "canonical" flag is true. If it's in
315 # @gem_checkouts and neither of these conditions is true, that's
316 # a fatal error; it means there are multiple incompatible
317 # versions of this gem to be checked out into this directory.
318 #
319 # Otherwise, returns false.
320 def skip_this?(url, repo_dir, branch, commit)
321 prev = @gem_checkouts[repo_dir]
322 return false unless prev
323
324 # Canonical declarations must precede all others.
325 fail("Attempted to re-declare #{prev.gemname} as canonical!\n" +
326 "('canonical' can only be used on its first declaration.)") if
327 prev && @canonical
328
329 # If prev is canonical, we can ignore this
330 if prev.canonical?
331 puts "Found canonical #{prev.gemname}; skipping this one."
332 return true
333 end
334
335 # If this checkout is identical to the current one, we can skip it.
336 candidate = GemCheckout.new(repo_dir, url, branch, commit, @canonical,
337 @path)
338 if prev == candidate
339 puts "Found duplicate checkout for #{repo_dir}; ignoring."
340 return true
341 end
342
343 # Otherwise, we have a checkout conflict. This is an error.
344 fail "Conflicting gem definitions for '#{repo_dir}':\n" +
345 " #{candidate}\n" +
346 " #{prev}\n"
347 end
348
349
350 # Retrieve a git repo if it's not present. Return
351 # [path_to_checkout, did_clone]
352 def git_clone_dependency(url, repo_dir, commit, branch)
353 return if
354 File.exist?(repo_dir) && File.exist?(File.join(repo_dir, '.git'))
355
356 FileUtils.mkdir_p repo_dir
357
358 options = @options.dup
359 options << "--recursive"
360 options << "--branch \"#{branch}\""
361 options << "--depth 1" unless commit
362
363 @build.git.run_clone repo_dir, url, options
364 end
365
366 def git_checkout_dependency(repo_dir, commit, branch)
367 return unless commit
368 @build.git.run_checkout_detach(repo_dir, commit)
369 end
370 end
371
372 def enable_gems?
373 [email protected]?
374 end
375 end # LoadGems
376 end # MRuby