bundler 4.1.0.beta3 → 4.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. checksums.yaml +4 -4
  2. data/bundler.gemspec +5 -3
  3. data/lib/bundler/build_metadata.rb +2 -2
  4. data/lib/bundler/checksum.rb +17 -2
  5. data/lib/bundler/cli/clean.rb +19 -3
  6. data/lib/bundler/cli/exec.rb +1 -1
  7. data/lib/bundler/cli/install.rb +1 -1
  8. data/lib/bundler/cli/lock.rb +12 -0
  9. data/lib/bundler/cli/outdated.rb +1 -4
  10. data/lib/bundler/cli/update.rb +1 -1
  11. data/lib/bundler/cli.rb +2 -0
  12. data/lib/bundler/definition.rb +152 -7
  13. data/lib/bundler/dependency.rb +17 -2
  14. data/lib/bundler/dsl.rb +9 -2
  15. data/lib/bundler/errors.rb +8 -0
  16. data/lib/bundler/fetcher/downloader.rb +7 -3
  17. data/lib/bundler/index.rb +7 -2
  18. data/lib/bundler/injector.rb +1 -1
  19. data/lib/bundler/inline.rb +19 -9
  20. data/lib/bundler/installer.rb +19 -0
  21. data/lib/bundler/lazy_specification.rb +46 -25
  22. data/lib/bundler/lockfile_generator.rb +21 -9
  23. data/lib/bundler/lockfile_parser.rb +21 -5
  24. data/lib/bundler/man/bundle-lock.1 +8 -2
  25. data/lib/bundler/man/bundle-lock.1.ronn +15 -1
  26. data/lib/bundler/man/bundle-plugin.1 +61 -0
  27. data/lib/bundler/man/bundle-plugin.1.ronn +84 -0
  28. data/lib/bundler/man/gemfile.5 +3 -1
  29. data/lib/bundler/man/gemfile.5.ronn +11 -3
  30. data/lib/bundler/match_platform.rb +3 -3
  31. data/lib/bundler/match_remote_metadata.rb +6 -0
  32. data/lib/bundler/mirror.rb +7 -5
  33. data/lib/bundler/plugin/index.rb +14 -11
  34. data/lib/bundler/remote_specification.rb +5 -0
  35. data/lib/bundler/resolver/candidate.rb +5 -0
  36. data/lib/bundler/resolver.rb +89 -7
  37. data/lib/bundler/ruby_dsl.rb +2 -1
  38. data/lib/bundler/rubygems_ext.rb +54 -9
  39. data/lib/bundler/rubygems_integration.rb +2 -4
  40. data/lib/bundler/runtime.rb +29 -11
  41. data/lib/bundler/settings.rb +2 -3
  42. data/lib/bundler/source/git/git_proxy.rb +18 -6
  43. data/lib/bundler/source/git.rb +0 -2
  44. data/lib/bundler/spec_set.rb +54 -14
  45. data/lib/bundler/stub_specification.rb +4 -0
  46. data/lib/bundler/vendored_securerandom.rb +5 -2
  47. data/lib/bundler/vendored_uri.rb +18 -3
  48. data/lib/bundler/version.rb +1 -1
  49. data/lib/bundler/worker.rb +4 -0
  50. data/lib/bundler.rb +1 -1
  51. data/lib/rubygems/compact_index_client/http_fetcher.rb +3 -1
  52. data/lib/rubygems/vendor/pub_grub/lib/pub_grub/term.rb +1 -1
  53. data/lib/rubygems/yaml_serializer/builder.rb +261 -0
  54. data/lib/rubygems/yaml_serializer/core.rb +41 -0
  55. data/lib/rubygems/yaml_serializer/emitter.rb +197 -0
  56. data/lib/rubygems/yaml_serializer/nodes.rb +10 -0
  57. data/lib/rubygems/yaml_serializer/parser.rb +531 -0
  58. data/lib/rubygems/yaml_serializer/psych_errors.rb +11 -0
  59. metadata +8 -4
  60. data/lib/rubygems/vendor/uri/lib/uri.rb +0 -108
  61. data/lib/rubygems/yaml_serializer.rb +0 -908
@@ -1,908 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # Skip reloading when an identical copy (e.g. the one shipped inside the Bundler
4
- # gem) was already required from a different path, to avoid redefinition warnings.
5
- return if defined?(Gem::YAMLSerializer)
6
-
7
- unless defined?(Psych::VERSION)
8
- module Psych
9
- class Exception < ::RuntimeError; end
10
- class SyntaxError < Exception; end
11
- class DisallowedClass < Exception; end
12
- class BadAlias < Exception; end
13
- class AliasesNotEnabled < BadAlias; end
14
- end
15
- end
16
-
17
- module Gem
18
- module YAMLSerializer
19
- Scalar = Struct.new(:value, :tag, :anchor)
20
- Mapping = Struct.new(:pairs, :tag, :anchor)
21
- Sequence = Struct.new(:items, :tag, :anchor)
22
- AliasRef = Struct.new(:name)
23
-
24
- class Parser
25
- # A plain (unquoted) mapping key followed by ":". "#" inside a key is
26
- # literal unless preceded by a space (which would start a comment).
27
- MAPPING_KEY_RE = /\G((?:[^#:\s]|:[^ ])(?:[^#:]|:[^ ]|(?<=[^ ])#)*):(?:[ ]+(.*))?$/
28
- # A quoted mapping key followed by ":". A whole-line quoted scalar
29
- # cannot match because its closing quote is at the end of the line.
30
- QUOTED_KEY_RE = /\G("(?:[^"\\]|\\.)*"|'(?:[^']|'')*'):(?:[ ]+(.*))?$/
31
- # An entry of a flow sequence. A quoted entry may contain commas.
32
- FLOW_SEQUENCE_ENTRY_RE = /"(?:[^"\\]|\\.)*"|'(?:[^']|'')*'|[^,\s](?:[^,]*[^,\s])?/
33
- SEQUENCE_ENTRY_RE = /\G-(?: |\z)/
34
- RUBY_OBJECT_TAG_RE = %r{\G!ruby/object:}
35
- LSTRIP_BYTES = [0, 9, 10, 11, 12, 13, 32].freeze
36
- MAX_NESTING_DEPTH = 1_000
37
- NULL_SCALARS = %w[~ null].freeze
38
- BLANK_RE = /\A[\0\s]*\z/
39
- BLANK_OR_COMMENT_RE = /\A[\0\s]*(?:#|\z)/
40
-
41
- STRING_UNESCAPES = {
42
- "\\\\" => "\\",
43
- "\\\"" => "\"",
44
- "\\0" => "\0",
45
- "\\a" => "\a",
46
- "\\b" => "\b",
47
- "\\t" => "\t",
48
- "\\n" => "\n",
49
- "\\v" => "\v",
50
- "\\f" => "\f",
51
- "\\r" => "\r",
52
- "\\e" => "\e",
53
- "\\N" => "\u0085",
54
- "\\_" => "\u00A0",
55
- "\\L" => "\u2028",
56
- "\\P" => "\u2029",
57
- }.freeze
58
-
59
- def initialize(source)
60
- @lines = source.split(/\r?\n/).each(&:freeze)
61
- @anchors = {}
62
- @depth = 0
63
- strip_document_prefix
64
- end
65
-
66
- def parse
67
- return nil if @lines.empty?
68
-
69
- root = nil
70
- while @lines.any?
71
- before = @lines.size
72
- node = parse_node(-1)
73
- @lines.shift if @lines.size == before && @lines.any?
74
-
75
- if root.is_a?(Mapping) && node.is_a?(Mapping)
76
- root.pairs.concat(node.pairs)
77
- elsif root.nil?
78
- root = node
79
- end
80
- end
81
- root
82
- end
83
-
84
- private
85
-
86
- def strip_document_prefix
87
- return if @lines.empty?
88
- return unless @lines[0]&.start_with?("---")
89
-
90
- if @lines[0].strip == "---"
91
- @lines.shift
92
- else
93
- @lines[0] = @lines[0].sub(/^---\s*/, "")
94
- end
95
- end
96
-
97
- def parse_node(base_indent)
98
- @depth += 1
99
- raise_max_nesting! if @depth > MAX_NESTING_DEPTH
100
-
101
- skip_blank_and_comments
102
- return nil if @lines.empty?
103
-
104
- line = @lines[0]
105
- indent = indent_of(line)
106
- return nil if indent < base_indent
107
-
108
- return parse_alias_ref if /\G\*/.match?(line, indent)
109
-
110
- pos = indent
111
- if /\G&/.match?(line, indent) && (anchor = consume_anchor)
112
- line = @lines[0]
113
- pos = indent_of(line)
114
- end
115
-
116
- if SEQUENCE_ENTRY_RE.match?(line, pos)
117
- parse_sequence(indent, anchor)
118
- elsif QUOTED_KEY_RE.match?(line, pos)
119
- # A mapping whose key is quoted, e.g. '"have: colon": value'.
120
- parse_mapping(indent, anchor)
121
- elsif (/\G"/.match?(line, pos) && line.end_with?("\"")) ||
122
- (/\G'/.match?(line, pos) && line.end_with?("'"))
123
- # A whole-line quoted scalar, e.g. '"system: foo: bar"'. It may
124
- # contain ": ", so it must be checked before MAPPING_KEY_RE.
125
- parse_plain_scalar(indent, anchor)
126
- elsif MAPPING_KEY_RE.match?(line, pos) && !RUBY_OBJECT_TAG_RE.match?(line, pos)
127
- parse_mapping(indent, anchor)
128
- elsif RUBY_OBJECT_TAG_RE.match?(line, pos)
129
- parse_tagged_node(indent, anchor)
130
- elsif /\G\|/.match?(line, pos)
131
- modifier = line[pos + 1, line.size].strip
132
- @lines.shift
133
- register_anchor(anchor, Scalar.new(parse_block_scalar(indent, modifier)))
134
- else
135
- parse_plain_scalar(indent, anchor)
136
- end
137
- ensure
138
- @depth -= 1
139
- end
140
-
141
- def parse_sequence(indent, anchor)
142
- items = []
143
- while @lines.any?
144
- skip_blank_and_comments
145
- break if @lines.empty?
146
- line = @lines[0]
147
- break unless indent_of(line) == indent && SEQUENCE_ENTRY_RE.match?(line, indent)
148
- @lines.shift
149
- content = line[indent + 1, line.size]
150
- content.strip!
151
- item_anchor = nil
152
- item_anchor, content = extract_item_anchor(content) if content.start_with?("&")
153
- item = parse_sequence_item(content, indent)
154
- items << register_anchor(item_anchor, item)
155
- end
156
- register_anchor(anchor, Sequence.new(items))
157
- end
158
-
159
- def parse_sequence_item(content, indent)
160
- if content.start_with?("*")
161
- parse_inline_alias(content)
162
- elsif content.empty?
163
- skip_blank_and_comments
164
- @lines.any? && current_indent > indent ? parse_node(indent) : nil
165
- elsif content.start_with?("!ruby/object:")
166
- parse_tagged_content(content.strip, indent)
167
- elsif content.start_with?("!binary ")
168
- parse_binary_value(content, indent)
169
- elsif content.start_with?("-")
170
- @lines.unshift("#{" " * (indent + 2)}#{content}")
171
- parse_node(indent)
172
- elsif QUOTED_KEY_RE.match?(content) ||
173
- (MAPPING_KEY_RE.match?(content) && !content.start_with?("!ruby/object:"))
174
- @lines.unshift("#{" " * (indent + 2)}#{content}")
175
- parse_node(indent)
176
- elsif content.start_with?("|")
177
- Scalar.new(parse_block_scalar(indent, content[1..].to_s.strip))
178
- else
179
- parse_inline_scalar(content, indent)
180
- end
181
- end
182
-
183
- def parse_mapping(indent, anchor)
184
- pairs = []
185
- while @lines.any?
186
- skip_blank_and_comments
187
- break if @lines.empty?
188
- line = @lines[0]
189
- break unless indent_of(line) == indent
190
-
191
- if (match = QUOTED_KEY_RE.match(line, indent))
192
- key = coerce(match[1])
193
- elsif (match = MAPPING_KEY_RE.match(line, indent)) && !RUBY_OBJECT_TAG_RE.match?(line, indent)
194
- key = match[1]
195
- key.strip!
196
- key = decode_binary_tag(key) if key.start_with?("!binary ")
197
- else
198
- break
199
- end
200
- @lines.shift
201
- val = match[2]
202
- val&.strip!
203
- val = strip_comment(val || "")
204
-
205
- val_anchor = nil
206
- val_anchor, val = consume_value_anchor(val) if val.start_with?("&")
207
- value = parse_mapping_value(val, indent)
208
- value = register_anchor(val_anchor, value) if val_anchor
209
-
210
- pairs << key << value
211
- end
212
- register_anchor(anchor, Mapping.new(pairs))
213
- end
214
-
215
- def parse_mapping_value(val, indent)
216
- if val.start_with?("*")
217
- parse_inline_alias(val)
218
- elsif val.start_with?("!ruby/object:")
219
- parse_tagged_content(val.strip, indent)
220
- elsif val.start_with?("!binary ")
221
- parse_binary_value(val, indent)
222
- elsif val.empty?
223
- skip_blank_and_comments
224
- line = @lines[0]
225
- if line && indent_of(line) == indent && SEQUENCE_ENTRY_RE.match?(line, indent)
226
- parse_node(indent)
227
- else
228
- parse_node(indent + 1)
229
- end
230
- elsif val == "[]"
231
- Sequence.new([])
232
- elsif val == "{}"
233
- Mapping.new([])
234
- elsif val.start_with?("|")
235
- Scalar.new(parse_block_scalar(indent, val[1..].to_s.strip))
236
- else
237
- parse_inline_scalar(val, indent)
238
- end
239
- end
240
-
241
- def parse_tagged_node(indent, anchor)
242
- tag = @lines.shift.strip
243
- nested = parse_node(indent)
244
- apply_tag(nested, tag, anchor)
245
- end
246
-
247
- def parse_tagged_content(tag, indent)
248
- nested = parse_node(indent)
249
- apply_tag(nested, tag, nil)
250
- end
251
-
252
- def apply_tag(node, tag, anchor)
253
- if node.is_a?(Mapping)
254
- node.tag = tag
255
- node.anchor = anchor
256
- node
257
- else
258
- Mapping.new(["value", node], tag, anchor)
259
- end
260
- end
261
-
262
- def parse_block_scalar(base_indent, modifier)
263
- parts = []
264
- indentation = modifier[/\A[+-]?([1-9])/, 1]
265
- chomping = modifier[/\A[1-9]?([+-])/, 1]
266
- block_indent = indentation && base_indent + indentation.to_i
267
-
268
- while @lines.any?
269
- line = @lines[0]
270
- if BLANK_RE.match?(line)
271
- parts << line[block_indent..] if block_indent && line.size > block_indent
272
- parts << "\n"
273
- @lines.shift
274
- else
275
- line_indent = indent_of(line)
276
- break if line_indent <= base_indent
277
- block_indent ||= line_indent
278
- parts << @lines.shift[block_indent..].to_s << "\n"
279
- end
280
- end
281
-
282
- res = parts.join
283
- res.delete_suffix!("\n") while chomping == "-" && res.end_with?("\n")
284
- res
285
- end
286
-
287
- def parse_plain_scalar(indent, anchor)
288
- val = @lines.shift.strip
289
- result = coerce(val)
290
- return register_anchor(anchor, result) if result.is_a?(Mapping) || result.is_a?(Sequence)
291
-
292
- fold_continuation_lines(result, val, indent)
293
- register_anchor(anchor, Scalar.new(result))
294
- end
295
-
296
- def parse_inline_scalar(val, indent)
297
- result = coerce(val)
298
- return result if result.is_a?(Mapping) || result.is_a?(Sequence)
299
-
300
- fold_continuation_lines(result, val, indent)
301
- Scalar.new(result)
302
- end
303
-
304
- def fold_continuation_lines(result, val, indent)
305
- return unless result.is_a?(String)
306
-
307
- # "#" does not start a comment inside a quoted scalar that runs onto the next lines.
308
- stop = val.start_with?("'", "\"") && result == val ? BLANK_RE : BLANK_OR_COMMENT_RE
309
- while @lines.any? && !stop.match?(@lines[0]) && current_indent > indent
310
- result << " " << @lines.shift.strip
311
- end
312
- end
313
-
314
- def coerce(val, depth = 0)
315
- raise_max_nesting! if depth > MAX_NESTING_DEPTH
316
-
317
- val = val.sub(/^! /, "") if val.start_with?("! ")
318
-
319
- if val.size >= 2 && val.start_with?("\"") && val.end_with?("\"")
320
- inner = val[1..-2]
321
- return inner unless inner.include?("\\")
322
- inner.gsub(/\\(?:["\\0abtnvfreN_LP]|x\h{2}|u\h{4}|U\h{8})/) do |m|
323
- char = STRING_UNESCAPES[m] || unescape_code_point(m)
324
- # Metadata is read as binary, where non-ASCII text stays as UTF-8 bytes.
325
- Encoding.compatible?(inner, char) ? char : char.b
326
- end
327
- elsif val.size >= 2 && val.start_with?("'") && val.end_with?("'")
328
- inner = val[1..-2]
329
- inner.include?("''") ? inner.gsub("''", "'") : inner
330
- elsif val == "true"
331
- true
332
- elsif val == "false"
333
- false
334
- elsif NULL_SCALARS.include?(val)
335
- nil
336
- elsif val == "{}"
337
- Mapping.new([])
338
- elsif val =~ /^\[(.*)\]$/
339
- inner = $1.strip
340
- return Sequence.new([]) if inner.empty?
341
- items = inner.scan(FLOW_SEQUENCE_ENTRY_RE).map {|e| Scalar.new(coerce(e, depth + 1)) }
342
- Sequence.new(items)
343
- elsif /\A\d{4}-\d{2}-\d{2}([ T]\d{2}:\d{2}:\d{2})?/.match?(val)
344
- begin
345
- Time.new(val)
346
- rescue ArgumentError
347
- # date-only format like "2024-06-15" is not supported by Time.new
348
- if /\A(\d{4})-(\d{2})-(\d{2})\z/.match(val)
349
- Time.utc($1.to_i, $2.to_i, $3.to_i)
350
- else
351
- val
352
- end
353
- end
354
- elsif /^-?\d+$/.match?(val)
355
- val.to_i
356
- else
357
- val
358
- end
359
- end
360
-
361
- def unescape_code_point(escape)
362
- code = escape[2..].hex
363
- return escape if code > 0x10FFFF || code.between?(0xD800, 0xDFFF)
364
- code.chr(Encoding::UTF_8)
365
- end
366
-
367
- def decode_binary_tag(str)
368
- content = str.sub(/\A!binary\s+/, "")
369
- content = $1 if content =~ /\A"(.*)"\z/ || content =~ /\A'(.*)'\z/
370
- content.unpack1("m")
371
- end
372
-
373
- def parse_binary_value(val, indent)
374
- rest = val.sub(/\A!binary\s+/, "")
375
- if rest.start_with?("|")
376
- content = parse_block_scalar(indent, rest[1..].to_s.strip)
377
- Scalar.new(content.unpack1("m"))
378
- else
379
- Scalar.new(decode_binary_tag(val))
380
- end
381
- end
382
-
383
- def parse_alias_ref
384
- AliasRef.new(@lines.shift.lstrip[1..].strip)
385
- end
386
-
387
- def parse_inline_alias(content)
388
- AliasRef.new(content[1..].strip)
389
- end
390
-
391
- def current_indent
392
- indent_of(@lines[0])
393
- end
394
-
395
- def indent_of(line)
396
- i = 0
397
- i += 1 while LSTRIP_BYTES.include?(line.getbyte(i))
398
- i
399
- end
400
-
401
- def consume_anchor
402
- line = @lines[0]
403
- stripped = line.lstrip
404
- return nil unless stripped.start_with?("&") && stripped =~ /^&(\S+)\s+/
405
-
406
- anchor = $1
407
- @lines[0] = line.sub(/&#{Regexp.escape(anchor)}\s+/, "")
408
- anchor
409
- end
410
-
411
- def extract_item_anchor(content)
412
- return [nil, content] unless content =~ /^&(\S+)/
413
-
414
- anchor = $1
415
- [anchor, content.sub(/^&#{Regexp.escape(anchor)}\s*/, "")]
416
- end
417
-
418
- def consume_value_anchor(val)
419
- return [nil, val] unless val =~ /^&(\S+)\s+/
420
-
421
- anchor = $1
422
- [anchor, val.sub(/^&#{Regexp.escape(anchor)}\s+/, "")]
423
- end
424
-
425
- def register_anchor(name, node)
426
- if name
427
- @anchors[name] = node
428
- node.anchor = name if node.respond_to?(:anchor=)
429
- end
430
- node
431
- end
432
-
433
- def raise_max_nesting!
434
- message = "exceeded maximum nesting depth (#{MAX_NESTING_DEPTH})"
435
- if defined?(Psych::VERSION)
436
- raise Psych::SyntaxError.new(nil, 0, 0, 0, message, nil)
437
- else
438
- raise Psych::SyntaxError, message
439
- end
440
- end
441
-
442
- def skip_blank_and_comments
443
- @lines.shift while @lines.any? && BLANK_OR_COMMENT_RE.match?(@lines[0])
444
- end
445
-
446
- def strip_comment(val)
447
- return val unless val.include?("#")
448
- return "" if val.lstrip.start_with?("#")
449
-
450
- in_single = false
451
- in_double = false
452
- escape = false
453
-
454
- val.each_char.with_index do |ch, i|
455
- if escape
456
- escape = false
457
- next
458
- end
459
-
460
- if in_single
461
- in_single = false if ch == "'"
462
- elsif in_double
463
- if ch == "\\"
464
- escape = true
465
- elsif ch == '"'
466
- in_double = false
467
- end
468
- else
469
- case ch
470
- when "'" then in_single = true
471
- when '"' then in_double = true
472
- when "#"
473
- # A "#" starts a comment only when preceded by whitespace.
474
- return val[0...i].rstrip if [" ", "\t"].include?(val[i - 1])
475
- end
476
- end
477
- end
478
-
479
- val
480
- end
481
- end
482
-
483
- class Builder
484
- VALID_OPS = %w[= != > < >= <= ~>].freeze
485
- ARRAY_FIELDS = %w[files test_files executables extra_rdoc_files].freeze
486
- MAX_ALIAS_RESOLUTIONS = 1_000
487
-
488
- def initialize(permitted_classes: [], permitted_symbols: [], aliases: true)
489
- @permitted_classes = permitted_classes.map {|c| "!ruby/object:#{c}" }
490
- @permitted_symbols = permitted_symbols
491
- @aliases = aliases
492
- @anchor_values = {}
493
- @alias_count = 0
494
- end
495
-
496
- def build(node)
497
- return nil if node.nil?
498
-
499
- result = build_node(node)
500
-
501
- if result.is_a?(Hash) && result[:tag] == "!ruby/object:Gem::Specification"
502
- build_specification(result)
503
- else
504
- result
505
- end
506
- end
507
-
508
- private
509
-
510
- def build_node(node)
511
- case node
512
- when nil then nil
513
- when AliasRef then resolve_alias(node)
514
- when Scalar then store_anchor(node.anchor, node.value)
515
- when Mapping then build_mapping(node)
516
- when Sequence then store_anchor(node.anchor, node.items.map {|item| build_node(item) })
517
- else node # already a Ruby object
518
- end
519
- end
520
-
521
- def resolve_alias(node)
522
- raise Psych::AliasesNotEnabled unless @aliases
523
- @alias_count += 1
524
- if @alias_count > MAX_ALIAS_RESOLUTIONS
525
- raise Psych::BadAlias, "exceeded maximum alias resolutions (#{MAX_ALIAS_RESOLUTIONS})"
526
- end
527
- unless @anchor_values.key?(node.name)
528
- klass = defined?(Psych::AnchorNotDefined) ? Psych::AnchorNotDefined : Psych::BadAlias
529
- raise klass, "An alias referenced an unknown anchor: #{node.name}"
530
- end
531
- @anchor_values.fetch(node.name)
532
- end
533
-
534
- def store_anchor(name, value)
535
- @anchor_values[name] = value if name
536
- value
537
- end
538
-
539
- def build_mapping(node)
540
- validate_tag!(node.tag) if node.tag
541
-
542
- result = case node.tag
543
- when "!ruby/object:Gem::Version"
544
- build_version(node)
545
- when "!ruby/object:Gem::Platform"
546
- build_platform(node)
547
- when "!ruby/object:Gem::Requirement", "!ruby/object:Gem::Version::Requirement"
548
- build_requirement(node)
549
- when "!ruby/object:Gem::Dependency"
550
- build_dependency(node)
551
- when nil
552
- build_hash(node)
553
- when "!ruby/object:Gem::Specification"
554
- hash = build_hash(node)
555
- hash[:tag] = node.tag
556
- hash
557
- else
558
- raise ArgumentError, "undefined class/module #{node.tag.sub("!ruby/object:", "")}"
559
- end
560
-
561
- store_anchor(node.anchor, result)
562
- end
563
-
564
- def build_hash(node)
565
- result = {}
566
- each_pair(node) do |key, value_node|
567
- value = build_node(value_node)
568
-
569
- if ARRAY_FIELDS.include?(key)
570
- value = normalize_array_field(value)
571
- end
572
-
573
- result[key] = value
574
- end
575
- result
576
- end
577
-
578
- def build_version(node)
579
- hash = pairs_to_hash(node)
580
- Gem::Version.new((hash["version"] || hash["value"]).to_s)
581
- end
582
-
583
- PLATFORM_FIELDS = %w[cpu os version].freeze
584
- PLATFORM_ALLOWED_IVARS = %w[cpu os version value].freeze
585
-
586
- def build_platform(node)
587
- hash = pairs_to_hash(node)
588
- if (hash.keys & PLATFORM_FIELDS).any?
589
- Gem::Platform.new([hash["cpu"], hash["os"], hash["version"]])
590
- elsif hash["value"].is_a?(Array)
591
- # Malformed platform (e.g. sequence instead of mapping).
592
- # Return the raw value so yaml_initialize handles it like Psych does.
593
- hash["value"]
594
- else
595
- plat = Gem::Platform.allocate
596
- hash.each do |k, v|
597
- plat.instance_variable_set(:"@#{k}", v) if PLATFORM_ALLOWED_IVARS.include?(k)
598
- end
599
- plat
600
- end
601
- end
602
-
603
- def build_requirement(node)
604
- r = Gem::Requirement.allocate
605
- hash = pairs_to_hash(node)
606
- reqs = hash["requirements"] || hash["value"]
607
-
608
- if reqs.is_a?(Array) && !reqs.empty?
609
- safe_reqs = []
610
- reqs.each do |item|
611
- if item.is_a?(Array) && item.size == 2
612
- op = item[0].to_s
613
- ver = item[1]
614
- if VALID_OPS.include?(op)
615
- version_obj = ver.is_a?(Gem::Version) ? ver : Gem::Version.new(ver.to_s)
616
- safe_reqs << [op, version_obj]
617
- end
618
- elsif item.is_a?(String)
619
- parsed = Gem::Requirement.parse(item)
620
- safe_reqs << parsed
621
- end
622
- rescue Gem::Requirement::BadRequirementError, Gem::Version::BadVersionError
623
- # Skip malformed items silently
624
- end
625
- reqs = safe_reqs unless safe_reqs.empty?
626
- end
627
-
628
- r.instance_variable_set(:@requirements, reqs)
629
- r
630
- end
631
-
632
- def build_dependency(node)
633
- hash = pairs_to_hash(node)
634
- d = Gem::Dependency.allocate
635
- d.instance_variable_set(:@name, hash["name"])
636
-
637
- d.instance_variable_set(:@requirement, hash["requirement"] || hash["version_requirements"])
638
-
639
- raw_type = hash["type"]
640
- if raw_type
641
- name = raw_type.to_s.sub(/^:/, "")
642
- validate_symbol!(name)
643
- type = name.to_sym
644
- else
645
- type = :runtime
646
- end
647
- d.instance_variable_set(:@type, type)
648
-
649
- d.instance_variable_set(:@prerelease, ["true", true].include?(hash["prerelease"]))
650
- d.instance_variable_set(:@version_requirements, d.instance_variable_get(:@requirement))
651
- d
652
- end
653
-
654
- def build_specification(hash)
655
- spec = Gem::Specification.allocate
656
-
657
- normalize_specification_version!(hash)
658
- normalize_array_fields!(hash)
659
-
660
- spec.yaml_initialize("!ruby/object:Gem::Specification", hash)
661
- spec
662
- end
663
-
664
- def pairs_to_hash(node)
665
- result = {}
666
- each_pair(node) do |key, value_node|
667
- result[key] = build_node(value_node)
668
- end
669
- result
670
- end
671
-
672
- def each_pair(node)
673
- pairs = node.pairs
674
- i = 0
675
- while i < pairs.size
676
- yield pairs[i], pairs[i + 1]
677
- i += 2
678
- end
679
- end
680
-
681
- def validate_tag!(tag)
682
- return if @permitted_classes.include?(tag)
683
- raise_disallowed_class!(tag)
684
- end
685
-
686
- def raise_disallowed_class!(tag)
687
- if defined?(Psych::VERSION)
688
- raise Psych::DisallowedClass.new("load", tag)
689
- else
690
- raise Psych::DisallowedClass, "Tried to load unspecified class: #{tag}"
691
- end
692
- end
693
-
694
- def validate_symbol!(name)
695
- return if @permitted_symbols.empty? || @permitted_symbols.include?(name)
696
-
697
- label = ":#{name}"
698
- if defined?(Psych::VERSION)
699
- raise Psych::DisallowedClass.new("load", label)
700
- else
701
- raise Psych::DisallowedClass, "Tried to load unspecified class: #{label}"
702
- end
703
- end
704
-
705
- def normalize_specification_version!(hash)
706
- val = hash["specification_version"]
707
- return unless val && !val.is_a?(Integer)
708
- hash["specification_version"] = val.to_i if val.is_a?(String) && /\A\d+\z/.match?(val)
709
- end
710
-
711
- def normalize_array_fields!(hash)
712
- ARRAY_FIELDS.each do |field|
713
- hash[field] = normalize_array_field(hash[field]) if hash[field]
714
- end
715
- end
716
-
717
- def normalize_array_field(value)
718
- if value.is_a?(Hash)
719
- value.values.flatten.compact
720
- elsif !value.is_a?(Array) && value
721
- [value].flatten.compact
722
- else
723
- value
724
- end
725
- end
726
- end
727
-
728
- class Emitter
729
- STRING_ESCAPES = {
730
- "\\" => "\\\\",
731
- "\"" => "\\\"",
732
- "\0" => "\\0",
733
- "\a" => "\\a",
734
- "\b" => "\\b",
735
- "\t" => "\\t",
736
- "\n" => "\\n",
737
- "\v" => "\\v",
738
- "\f" => "\\f",
739
- "\r" => "\\r",
740
- "\e" => "\\e",
741
- }.freeze
742
-
743
- def emit(obj)
744
- "---#{emit_node(obj, 0)}"
745
- end
746
-
747
- private
748
-
749
- def emit_node(obj, indent, quote: false)
750
- case obj
751
- when Gem::Specification then emit_specification(obj, indent)
752
- when Gem::Version then emit_version(obj, indent)
753
- when Gem::Platform then emit_platform(obj, indent)
754
- when Gem::Requirement then emit_requirement(obj, indent)
755
- when Gem::Dependency then emit_dependency(obj, indent)
756
- when Hash then emit_hash(obj, indent)
757
- when Array then emit_array(obj, indent)
758
- when Time then emit_time(obj)
759
- when String then emit_string(obj, indent, quote: quote)
760
- when NilClass
761
- "\n"
762
- when Numeric, Symbol, TrueClass, FalseClass
763
- " #{obj.inspect}\n"
764
- else
765
- " #{quote_string(obj.to_s)}\n"
766
- end
767
- end
768
-
769
- def emit_specification(spec, indent)
770
- parts = [" !ruby/object:Gem::Specification\n"]
771
- parts << "#{pad(indent)}name:#{emit_node(spec.name, indent + 2)}"
772
- parts << "#{pad(indent)}version:#{emit_node(spec.version, indent + 2)}"
773
- parts << "#{pad(indent)}platform: #{spec.platform}\n"
774
- if spec.platform.to_s != spec.original_platform.to_s
775
- parts << "#{pad(indent)}original_platform: #{spec.original_platform}\n"
776
- end
777
-
778
- attributes = Gem::Specification.attribute_names.map(&:to_s).sort - %w[name version platform]
779
- attributes.each do |name|
780
- val = spec.instance_variable_get("@#{name}")
781
- next if val.nil?
782
- parts << "#{pad(indent)}#{name}:#{emit_node(val, indent + 2)}"
783
- end
784
-
785
- res = parts.join
786
- res << "\n" unless res.end_with?("\n")
787
- res
788
- end
789
-
790
- def emit_version(ver, indent)
791
- " !ruby/object:Gem::Version\n" \
792
- "#{pad(indent)}version: #{emit_node(ver.version.to_s, indent + 2).lstrip}"
793
- end
794
-
795
- def emit_platform(plat, indent)
796
- " !ruby/object:Gem::Platform\n" \
797
- "#{pad(indent)}cpu:#{emit_node(plat.cpu, indent + 2)}" \
798
- "#{pad(indent)}os:#{emit_node(plat.os, indent + 2)}" \
799
- "#{pad(indent)}version:#{emit_node(plat.version, indent + 2)}"
800
- end
801
-
802
- def emit_requirement(req, indent)
803
- " !ruby/object:Gem::Requirement\n" \
804
- "#{pad(indent)}requirements:#{emit_node(req.requirements, indent + 2)}"
805
- end
806
-
807
- def emit_dependency(dep, indent)
808
- [
809
- " !ruby/object:Gem::Dependency\n",
810
- "#{pad(indent)}name: #{emit_node(dep.name, indent + 2).lstrip}",
811
- "#{pad(indent)}requirement:#{emit_node(dep.requirement, indent + 2)}",
812
- "#{pad(indent)}type: #{emit_node(dep.type, indent + 2).lstrip}",
813
- "#{pad(indent)}prerelease: #{emit_node(dep.prerelease?, indent + 2).lstrip}",
814
- "#{pad(indent)}version_requirements:#{emit_node(dep.requirement, indent + 2)}",
815
- ].join
816
- end
817
-
818
- def emit_hash(hash, indent)
819
- if hash.empty?
820
- " {}\n"
821
- else
822
- parts = ["\n"]
823
- hash.each do |k, v|
824
- is_symbol = k.is_a?(Symbol) || (k.is_a?(String) && k.start_with?(":"))
825
- key_str = k.is_a?(Symbol) ? k.inspect : k.to_s
826
- key_str = quote_string(key_str) if !is_symbol && needs_quoting?(key_str)
827
- parts << "#{pad(indent)}#{key_str}:#{emit_node(v, indent + 2, quote: is_symbol)}"
828
- end
829
- parts.join
830
- end
831
- end
832
-
833
- def emit_array(arr, indent)
834
- if arr.empty?
835
- " []\n"
836
- else
837
- parts = ["\n"]
838
- arr.each do |v|
839
- parts << "#{pad(indent)}-#{emit_node(v, indent + 2)}"
840
- end
841
- parts.join
842
- end
843
- end
844
-
845
- def emit_time(time)
846
- " #{time.utc.strftime("%Y-%m-%d %H:%M:%S.%N Z")}\n"
847
- end
848
-
849
- def emit_string(str, indent, quote: false)
850
- # A CR cannot be represented in a block scalar because parsing
851
- # normalizes CRLF line breaks to LF, so quote and escape instead.
852
- if str.include?("\n") && !str.include?("\r")
853
- emit_block_scalar(str, indent)
854
- elsif needs_quoting?(str, quote)
855
- " #{quote_string(str)}\n"
856
- else
857
- " #{str}\n"
858
- end
859
- end
860
-
861
- def emit_block_scalar(str, indent)
862
- parts = [str.end_with?("\n") ? " |\n" : " |-\n"]
863
- str.each_line do |line|
864
- parts << "#{pad(indent + 2)}#{line}"
865
- end
866
- res = parts.join
867
- res << "\n" unless res.end_with?("\n")
868
- res
869
- end
870
-
871
- def needs_quoting?(str, quote = false)
872
- quote || str.empty? || str != str.strip || str =~ /[[:cntrl:]]/ ||
873
- str =~ /^[!*&:@%$"'|`]/ || str =~ /^-?\d+(\.\d+)?$/ || str =~ /^[<>=-]/ ||
874
- str == "true" || str == "false" || str == "nil" || str == "null" || str == "~" ||
875
- str.include?(":") || str.include?("#") || str.include?("[") || str.include?("]") ||
876
- str.include?("{") || str.include?("}") || str.include?(",")
877
- end
878
-
879
- def quote_string(str)
880
- %("#{str.gsub(/[\\"]|[[:cntrl:]]/) {|c| STRING_ESCAPES[c] || format("\\x%02X", c.ord) }}")
881
- end
882
-
883
- def pad(indent)
884
- " " * indent
885
- end
886
- end
887
-
888
- module_function
889
-
890
- def dump(obj)
891
- Emitter.new.emit(obj)
892
- end
893
-
894
- def load(str, permitted_classes: [], permitted_symbols: [], aliases: true)
895
- raise TypeError, "no implicit conversion of nil into String" if str.nil?
896
- return nil if str.empty?
897
-
898
- ast = Parser.new(str).parse
899
- return nil if ast.nil?
900
-
901
- Builder.new(
902
- permitted_classes: permitted_classes,
903
- permitted_symbols: permitted_symbols,
904
- aliases: aliases
905
- ).build(ast)
906
- end
907
- end
908
- end