Class: Utils::AST::FormulaAST Private

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Utils::AST
Defined in:
utils/ast.rb,
sorbet/rbi/dsl/utils/ast/formula_ast.rbi

Overview

This class is part of a private API. This class may only be used in the Homebrew/brew repository. Third parties should avoid using this class if possible, as it may be removed or changed without warning.

Helper class for editing formulae.

Constant Summary collapse

PATCH_PLATFORM_BLOCKS =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

T.let(patch_platform_blocks.freeze, T::Array[Symbol])
BUMP_URL_ORIGIN =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

%r{\A(?i:https?|ftp)://[a-zA-Z0-9.:-]+(?=/|\z)}
PYPI_RESOURCE =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

%r{\Aresource[ ]"[a-zA-Z0-9._+-]+"[ ]do
\n[ ]+url[ ]"https://files\.pythonhosted\.org/[-a-zA-Z0-9_./%+]+"
\n[ ]+sha256[ ]"[a-f0-9]{64}"\n[ ]+end\z}x

Constants included from Utils::AST

BlockNode, DefNode, Node, ProcessedSource, SendNode, TreeRewriter

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils::AST

body_children, call_node_match?, component_match?, literal_value, process_source, ruby_literal, stanza_text

Methods included from Kernel

#which, #with_env

Constructor Details

#initialize(formula_contents) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'utils/ast.rb', line 113

def initialize(formula_contents)
  @formula_contents = formula_contents
  processed_source, root_node = process_source(formula_contents)

  class_node = root_node if root_node.class_type?
  if root_node.begin_type?
    nodes = root_node.children.select(&:class_type?)
    class_node = if nodes.count > 1
      nodes.find { |n| n.parent_class&.const_name == "Formula" }
    else
      nodes.first
    end
  end

  raise "Could not find formula class!" if class_node.nil?

  children = body_children(class_node.body)
  raise "Formula class is empty!" if children.empty?

  @processed_source = T.let(processed_source, ProcessedSource)
  @children = T.let(children, T::Array[Node])
  @tree_rewriter = T.let(TreeRewriter.new(processed_source.buffer), TreeRewriter)
end

Instance Attribute Details

#children ⇒ Array<Node> (readonly)

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Top-level nodes in the formula body.

Returns:



110
111
112
# File 'utils/ast.rb', line 110

def children
  @children
end

Class Method Details

.valid_bump?(original, proposed, version: nil) ⇒ Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Compare a prepared formula with the release edits allowed in its checkout.

Parameters:

Returns:

  • (Boolean)


139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'utils/ast.rb', line 139

def self.valid_bump?(original, proposed, version: nil)
  checkout = new(original)
  proposal = new(proposed)
  if version
    current = checkout.stable_version
    return false if !current || proposal.stable_version != version
    return false if Version.new(version) <= Version.new(current)
  end

  return true if checkout.bump_template(proposal) == proposal.bump_template(proposal)

  checkout.matches_patch_removals?(proposal)
end

Instance Method Details

#add_bottle_block(bottle_output) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Parameters:



550
551
552
# File 'utils/ast.rb', line 550

def add_bottle_block(bottle_output)
  add_stanza(:bottle, "\n#{bottle_output.chomp}", type: :block_call)
end

#add_stable_stanzas_after(after_name, new_stanzas) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Parameters:



395
396
397
# File 'utils/ast.rb', line 395

def add_stable_stanzas_after(after_name, new_stanzas)
  add_stanzas_after(after_name, new_stanzas, parent: stanza(:stable, type: :block_call))
end

#add_stanza(name, value, type: nil) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Parameters:



670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
# File 'utils/ast.rb', line 670

def add_stanza(name, value, type: nil)
  preceding_component = if children.length > 1
    children.reduce do |previous_child, current_child|
      if formula_component_before_target?(current_child,
                                          target_name: name,
                                          target_type: type)
        next current_child
      else
        break previous_child
      end
    end
  else
    children.first
  end
  preceding_component = preceding_component.last_argument if preceding_component.is_a?(SendNode)

  preceding_expr = preceding_component.location.expression
  processed_source.comments.each do |comment|
    comment_expr = comment.location.expression
    distance = comment_expr.first_line - preceding_expr.first_line
    case distance
    when 0
      if comment_expr.last_line > preceding_expr.last_line ||
         comment_expr.end_pos > preceding_expr.end_pos
        preceding_expr = comment_expr
      end
    when 1
      preceding_expr = comment_expr
    end
  end

  stanza = stanza_text(name, value, indent: 2)
  tree_rewriter.insert_after(preceding_expr, "\n#{stanza}")
end

#add_stanzas_after(after_name, new_stanzas, parent: nil) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Parameters:



435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
# File 'utils/ast.rb', line 435

def add_stanzas_after(after_name, new_stanzas, parent: nil)
  return if new_stanzas.empty?

  preceding_component = if parent
    matching_stanzas(body_children(T.cast(parent, BlockNode).body), after_name).last
  else
    stanza(after_name)
  end
  raise "Could not find '#{after_name}' stanza!" if preceding_component.blank?

  preceding_expr = preceding_component.location.expression
  processed_source.comments.each do |comment|
    comment_expr = comment.location.expression
    distance = comment_expr.first_line - preceding_expr.last_line
    case distance
    when 0
      if comment_expr.last_line > preceding_expr.last_line ||
         comment_expr.end_pos > preceding_expr.end_pos
        preceding_expr = comment_expr
      end
    when 1
      preceding_expr = comment_expr
    end
  end
  text = new_stanzas.map do |stanza_name, value|
    stanza = stanza_text(stanza_name, value, indent: preceding_component.source_range.column)
    "\n#{stanza}"
  end.join
  tree_rewriter.insert_after(preceding_expr, text)
end

#bottle_block ⇒ Node?

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



330
331
332
# File 'utils/ast.rb', line 330

def bottle_block
  stanza(:bottle, type: :block_call)
end

#bump_template(proposal) ⇒ String

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Keep executable source intact while comparing release declarations.

Parameters:

Returns:



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'utils/ast.rb', line 220

def bump_template(proposal)
  if (mirrors = stable_mirror_origins).present?
    retained = proposal.retained_mirror_origins
    mirrors.each do |node, origin|
      if retained.first == origin
        retained.shift
      else
        remove_stanza_node(node)
      end
    end
  end

  nodes = children.dup
  while (node = nodes.shift)
    case node
    when BlockNode
      next if node.send_node.receiver || node.arguments.children.present?

      if [:stable, *PATCH_PLATFORM_BLOCKS].include?(node.method_name)
        nodes.concat(body_children(node.body))
        next
      end
      next unless node.method?(:resource)

      statements = body_children(node.body)
      version = statements.grep(SendNode).find do |call|
        call.method?(:version) && call.receiver.nil? && call.arguments.one? && call.first_argument&.str_type?
      end
      # PyPI regenerates the resource with just its URL and checksum.
      source = node.source
      source = source.sub(/^ +#{Regexp.escape(version.source)}\n/, "") if version
      if PYPI_RESOURCE.match?(source)
        remove_stanza_node(node)
      else
        nodes.concat(statements)
      end
    when SendNode
      next if node.receiver

      value = node.first_argument
      if node.arguments.one? && ((node.method?(:revision) && value&.int_type?) ||
         ([:sha256, :version].include?(node.method_name) && value&.str_type?))
        remove_stanza_node(node)
      elsif [:url, :mirror].include?(node.method_name) && value&.str_type?
        origin = value.str_content[BUMP_URL_ORIGIN]
        tree_rewriter.replace(value.source_range, origin.inspect) if origin
        node.arguments.grep(RuboCop::AST::HashNode).each do |options|
          options.pairs.each do |pair|
            next if !pair.key.sym_type? || [:tag, :revision].exclude?(pair.key.value) || !pair.value.str_type?

            tree_rewriter.replace(pair.value.source_range, '""')
          end
        end
      end
    end
  end
  process
end

#contains_call?(name) ⇒ Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Finds DSL calls regardless of their platform or enclosing scope.

Parameters:

Returns:

  • (Boolean)


653
654
655
656
657
658
659
# File 'utils/ast.rb', line 653

def contains_call?(name)
  children.any? do |node|
    node.each_node(:send).any? do |call|
      call.method_name == name && call.receiver.nil?
    end
  end
end

#matches_patch_removals?(proposal) ⇒ Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Compare patch edits at their original positions in the two release templates.

Parameters:

Returns:

  • (Boolean)


281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# File 'utils/ast.rb', line 281

def matches_patch_removals?(proposal)
  scopes = patches_by_scope
  return false if scopes.empty?

  candidates_by_scope = proposal.patches_by_scope
  removals = {}.compare_by_identity
  scopes.each do |scope, patches|
    candidates = candidates_by_scope.fetch(scope, []).map(&:source)
    unmatched = patches.reject do |patch|
      index = candidates.index(patch.source)
      candidates.delete_at(index) if index
    end
    unmatched.each do |patch|
      candidate = candidates.first
      if candidate && (branches = prepared_patch_branches(patch, candidate))
        candidates.shift
        removals[patch] = branches
      else
        removals[patch] = patches_for_removal(patch) { |_urls, branch| branch.equal?(patch) }
      end
    end
  end
  removed_urls = Set.new
  remove_patches do |patch|
    selected = removals.fetch(patch, [])
    next [] if selected.empty?

    patches_for_removal(patch) do |urls, branch|
      next false unless selected.any? { |node| node.equal?(branch) }

      removed_urls.merge(urls)
      true
    end
  end
  return false if removed_urls.empty? || process != proposal.process

  source = proposal.stable_source
  return false unless source

  require "utils/github/patch_inclusion"

  inclusion = GitHub::PatchInclusion.new
  removed_urls.all? do |url|
    inclusion.removal_reason(url, source_url: source.to_s, tag: source.specs[:tag],
                                  revision: source.specs[:revision]).present?
  end
end

#patch_scope_nodes(nodes = children) ⇒ Array<Node>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Enumerates source declarations in stable and platform scopes.

Parameters:

  • nodes (Array<Node>) (defaults to: children)

Returns:



617
618
619
620
621
622
623
624
625
626
# File 'utils/ast.rb', line 617

def patch_scope_nodes(nodes = children)
  nodes.flat_map do |node|
    if node.is_a?(BlockNode) && node.send_node.receiver.nil? &&
       [:stable, *PATCH_PLATFORM_BLOCKS].include?(node.method_name)
      [node, *patch_scope_nodes(body_children(node.body))]
    else
      [node]
    end
  end
end

#patches_for_removal(node, &block) ⇒ Array<BlockNode>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Selects whole patches or independent platform branches containing only supported declarations. The caller decides whether to remove a branch given all its patch URLs.

Parameters:

Returns:



568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
# File 'utils/ast.rb', line 568

def patches_for_removal(node, &block)
  # Retain stable-only patches to avoid leaving a redundant `stable` block.
  return [] unless node.each_ancestor.all? do |parent|
    parent.class_type? || parent.begin_type? ||
    (parent.is_a?(BlockNode) && parent.send_node.receiver.nil? &&
     [:patch, *PATCH_PLATFORM_BLOCKS].include?(parent.method_name))
  end
  return [] unless node.arguments.empty?

  if node.method_name == :patch && (arguments = node.send_node.arguments).present?
    strip = literal_value(arguments.first)
    return [] if arguments.length != 1 || !strip.is_a?(Symbol) || !strip.to_s.match?(/\Ap\d+\z/)
  end

  children = body_children(node.body)
  calls = patch_scope_nodes(children)
  unsupported_calls = calls.reject do |call|
    case call
    when SendNode
      call.receiver.nil? && [:url, :sha256, :directory, :type, :resolves].include?(call.method_name)
    when BlockNode
      call.send_node.receiver.nil? && call.arguments.empty? && PATCH_PLATFORM_BLOCKS.include?(call.method_name)
    end
  end

  if unsupported_calls.empty?
    urls = calls.grep(SendNode).select { |call| call.method_name == :url }.map do |call|
      value = call.first_argument
      value.str_content if call.arguments.length == 1 && value&.str_type?
    end
    return [node] if urls.present? && urls.none?(&:nil?) && yield(urls.compact.uniq, node)
  end

  return [] if children.any? do |child|
    unsupported_calls.include?(child) || (child.is_a?(SendNode) && child.method_name == :url)
  end

  branches = children.grep(BlockNode)
  if branches.length > 1
    # Only split disjoint OS or architecture pairs.
    return [] unless branches.all? { |branch| branch.send_node.arguments.empty? }
    return [] unless [[:on_linux, :on_macos], [:on_arm, :on_intel]].include?(branches.map(&:method_name).sort)
  end

  branches.flat_map { |branch| patches_for_removal(branch, &block) }
end

#process(*args, &block) ⇒ T.untyped

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

  • args (T.untyped)
  • block (T.untyped)

Returns:

  • (T.untyped)


10
# File 'sorbet/rbi/dsl/utils/ast/formula_ast.rbi', line 10

def process(*args, &block); end

#remove_patches(&block) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Yields patch blocks in all scopes; the caller returns patches or branches to remove. Removes adjoining comments and platform wrappers emptied by patch removal.

Parameters:



631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
# File 'utils/ast.rb', line 631

def remove_patches(&block)
  nodes = children.flat_map { |node| node.each_node(:block).to_a }.grep(BlockNode)
  removals = nodes.flat_map do |node|
    next [] if node.method_name != :patch || node.send_node.receiver

    yield(node)
  end

  nodes.reverse_each do |node|
    next if node.send_node.receiver
    next unless PATCH_PLATFORM_BLOCKS.include?(node.method_name)
    next if node.body.nil?
    next unless body_children(node.body).all? { |child| removals.any? { |removal| removal.equal?(child) } }

    removals.reject! { |removal| node.source_range.contains?(removal.source_range) }
    removals << node
  end
  removals.each { |node| remove_stanza_node(node, remove_leading_comments: true) }
end

#remove_stable_stanza(name) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Parameters:



377
378
379
# File 'utils/ast.rb', line 377

def remove_stable_stanza(name)
  remove_stanza_node(stable_stanza(name))
end

#remove_stable_stanzas(name) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Parameters:



382
383
384
385
386
387
# File 'utils/ast.rb', line 382

def remove_stable_stanzas(name)
  stanza_nodes = matching_stanzas(stable_children, name)
  raise "Could not find '#{name}' stanza!" if stanza_nodes.empty?

  stanza_nodes.each { |stanza_node| remove_stanza_node(stanza_node) }
end

#remove_stanza(name, type: nil) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Parameters:



555
556
557
558
559
560
# File 'utils/ast.rb', line 555

def remove_stanza(name, type: nil)
  stanza_node = stanza(name, type:)
  raise "Could not find '#{name}' stanza!" if stanza_node.blank?

  remove_stanza_node(stanza_node)
end

#replace_bottle_block(bottle_output) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Parameters:



545
546
547
# File 'utils/ast.rb', line 545

def replace_bottle_block(bottle_output)
  replace_stanza(:bottle, bottle_output.chomp, type: :block_call)
end

#replace_resource_stanza_hash_value(resource_name, name, key, value) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Parameters:



419
420
421
# File 'utils/ast.rb', line 419

def replace_resource_stanza_hash_value(resource_name, name, key, value)
  replace_stanza_hash_value(resource_stanza(resource_name, name), key, value)
end

#replace_resource_stanza_value(resource_name, name, value, old_value: nil) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Parameters:



407
408
409
# File 'utils/ast.rb', line 407

def replace_resource_stanza_value(resource_name, name, value, old_value: nil)
  replace_stanza_value(resource_stanza(resource_name, name, old_value:), value)
end

#replace_resource_stanzas(resource_section, replace_existing: true, preserve_livecheck: false) ⇒ Symbol?

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

  • resource_section (String)
  • replace_existing (Boolean) (defaults to: true)
  • preserve_livecheck (Boolean) (defaults to: false)

Returns:



473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
# File 'utils/ast.rb', line 473

def replace_resource_stanzas(resource_section, replace_existing: true, preserve_livecheck: false)
  resource_section = resource_section.gsub(/^(?!$)/, "  ") unless resource_section.match?(/\A\n* +/)

  if replace_existing
    test_node = stanza(:test, type: :block_call)
    resource_nodes = stanzas(:resource, type: :block_call).filter_map do |node|
      node = T.cast(node, BlockNode)
      next if test_node.present? && node.source_range.begin_pos > test_node.source_range.begin_pos

      next if preserve_livecheck &&
              matching_stanzas(body_children(node.body), :livecheck, type: :block_call).present?

      node
    end

    groups = T.let([], T::Array[T::Array[BlockNode]])
    resource_nodes.each do |resource_node|
      previous_group = groups.last
      if previous_group
        previous_end = whole_line_range(previous_group.fetch(-1).source_range).end_pos
        current_start = resource_node.source_range.begin_pos - resource_node.source_range.column
        contiguous = formula_contents[previous_end...current_start].to_s.lines.all? do |line|
          line.strip.empty? || line.strip.start_with?("# RESOURCE-ERROR:")
        end
      end

      if previous_group && contiguous
        previous_group << resource_node
      else
        groups << [resource_node]
      end
    end
    return :multiple_groups if groups.length > 1

    if (group = groups.first)
      # Include any `# RESOURCE-ERROR:` comments directly above the first resource.
      first_range = group.fetch(0).source_range
      loop do
        line_start = first_range.begin_pos - first_range.column
        previous_comments = processed_source.comments.select do |comment|
          comment.location.expression.end_pos <= line_start &&
            comment.text.start_with?("# RESOURCE-ERROR:")
        end
        previous_comment = previous_comments.max_by { |comment| comment.location.expression.end_pos }
        break if previous_comment.blank?

        comment_range = previous_comment.location.expression
        break unless formula_contents[comment_range.end_pos...line_start].to_s.lines.all? do |line|
          line.strip.empty?
        end

        first_range = T.cast(first_range.with(begin_pos: comment_range.begin_pos), Parser::Source::Range)
      end
      last_range = whole_line_range(group.fetch(-1).source_range, include_following_blank_lines: true)
      group_range = first_range.with(
        begin_pos: first_range.begin_pos - first_range.column,
        end_pos:   last_range.end_pos,
      )
      tree_rewriter.replace(group_range, resource_section)
      return
    end
  end

  install_node = children.find { |child| child.def_type? && T.cast(child, DefNode).method_name == :install }
  install_node = T.cast(install_node, T.nilable(DefNode))
  raise "Could not find 'install' method!" if install_node.blank?

  tree_rewriter.insert_before(whole_line_range(install_node.source_range), resource_section)
  nil
end

#replace_stable_stanza_hash_value(name, key, value) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Parameters:



367
368
369
# File 'utils/ast.rb', line 367

def replace_stable_stanza_hash_value(name, key, value)
  replace_stanza_hash_value(stable_stanza(name), key, value)
end

#replace_stable_stanza_value(name, value) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Parameters:



362
363
364
# File 'utils/ast.rb', line 362

def replace_stable_stanza_value(name, value)
  replace_stanza_value(stable_stanza(name), value)
end

#replace_stanza(name, replacement, type: nil) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Parameters:



662
663
664
665
666
667
# File 'utils/ast.rb', line 662

def replace_stanza(name, replacement, type: nil)
  stanza_node = stanza(name, type:)
  raise "Could not find '#{name}' stanza!" if stanza_node.blank?

  tree_rewriter.replace(stanza_node.source_range, stanza_text(name, replacement, indent: 2).lstrip)
end

#resource(name) ⇒ BlockNode

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
# File 'utils/ast.rb', line 345

def resource(name)
  possible_resource_stanzas = if stanza(:stable, type: :block_call).present?
    # Needed resource block may be either in `stable` or in formula body, hence appending
    # `children` nodes
    stable_children + children
  else
    children
  end
  resource = matching_stanzas(possible_resource_stanzas, :resource, type: :block_call).find do |resource_node|
    T.cast(resource_node, BlockNode).send_node.first_argument&.str_content == name
  end
  raise "Could not find resource '#{name}' block!" if resource.blank?

  T.cast(resource, BlockNode)
end

#resource_stanza?(resource_name, name) ⇒ Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:

  • (Boolean)


424
425
426
# File 'utils/ast.rb', line 424

def resource_stanza?(resource_name, name)
  matching_stanzas(body_children(resource(resource_name).body), name).present?
end

#stable_source(call = , keys: [:tag, :revision]) ⇒ URL?

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Extract literal source details used to identify the proposed release.

Parameters:

Returns:



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'utils/ast.rb', line 197

def stable_source(call = stable_declarations[:url], keys: [:tag, :revision])
  value = call&.first_argument&.str_content
  return if !call || value.blank?

  specs = {}
  if (options = call.arguments[1])
    return if call.arguments.length != 2 || !options.is_a?(RuboCop::AST::HashNode)

    return unless options.children.all? { |pair| pair.pair_type? && pair.key.sym_type? }

    keys.each do |key|
      pair = options.pairs.rfind { |entry| entry.key.value == key }
      next unless pair
      return nil unless pair.value.str_type?

      specs[key] = pair.value.str_content
    end
  end
  URL.new(value, specs)
end

#stable_stanza?(name) ⇒ Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:

  • (Boolean)


372
373
374
# File 'utils/ast.rb', line 372

def stable_stanza?(name)
  matching_stanzas(stable_children, name).present?
end

#stable_version ⇒ String?

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Read the stable version without evaluating the formula.

Returns:



183
184
185
186
187
188
189
190
191
192
193
# File 'utils/ast.rb', line 183

def stable_version
  declarations = stable_declarations
  unless declarations.key?(:version)
    return stable_source(declarations[:url], keys: [:tag])&.version&.to_s.presence
  end

  call = declarations[:version]
  return unless call

  call.first_argument&.str_content.presence if call.arguments.one?
end

#stanza(name, type: nil) ⇒ Node?

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



335
336
337
# File 'utils/ast.rb', line 335

def stanza(name, type: nil)
  stanzas(name, type:).first
end

#stanzas(name, type: nil) ⇒ Array<Node>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



340
341
342
# File 'utils/ast.rb', line 340

def stanzas(name, type: nil)
  matching_stanzas(children, name, type:)
end