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

Constants included from Utils::AST

BlockNode, DefNode, Node, ProcessedSource, SendNode, TreeRewriter

Constants included from Kernel

Kernel::IGNORE_INTERRUPTS_MUTEX

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

#ensure_executable!, #exec_browser, #exec_editor, #ignore_interrupts, #interactive_shell, #quiet_system, #redirect_stdout, #safe_system, #which, #which_editor, #with_env, #with_homebrew_path

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:



96
97
98
99
100
101
102
# File 'utils/ast.rb', line 96

def initialize(formula_contents)
  @formula_contents = formula_contents
  processed_source, children = process_formula
  @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 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:



235
236
237
# File 'utils/ast.rb', line 235

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:



163
164
165
# File 'utils/ast.rb', line 163

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:



264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'utils/ast.rb', line 264

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

  tree_rewriter.insert_after(preceding_expr, "\n#{stanza_text(name, value, indent: 2)}")
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:



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'utils/ast.rb', line 191

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 = source_range_with_trailing_comments(preceding_component)
  text = new_stanzas.map do |stanza_name, value|
    "\n#{stanza_text(stanza_name, value, indent: preceding_component.source_range.column)}"
  end.join
  tree_rewriter.insert_after(preceding_expr, text)
end

#bottle_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.

Returns:



105
106
107
# File 'utils/ast.rb', line 105

def bottle_block
  stanza(:bottle, type: :block_call)
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_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:



145
146
147
# File 'utils/ast.rb', line 145

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:



150
151
152
153
154
155
# File 'utils/ast.rb', line 150

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:



240
241
242
243
244
245
# File 'utils/ast.rb', line 240

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

#remove_stanzas(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:



248
249
250
251
252
253
# File 'utils/ast.rb', line 248

def remove_stanzas(name, type: nil)
  stanza_nodes = stanzas(name, type:)
  raise "Could not find '#{name}' stanza!" if stanza_nodes.empty?

  stanza_nodes.each { |stanza_node| 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:



230
231
232
# File 'utils/ast.rb', line 230

def replace_bottle_block(bottle_output)
  replace_stanza(:bottle, bottle_output.chomp, type: :block_call)
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:



175
176
177
# File 'utils/ast.rb', line 175

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) ⇒ 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)

Returns:



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'utils/ast.rb', line 209

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

  if replace_existing
    groups = resource_stanza_groups
    return :multiple_groups if groups.length > 1

    if (group = groups.first)
      tree_rewriter.replace(resource_stanza_group_range(group), resource_section)
      return
    end
  end

  install_node = method_definition(:install)
  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:



135
136
137
# File 'utils/ast.rb', line 135

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:



130
131
132
# File 'utils/ast.rb', line 130

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:



256
257
258
259
260
261
# File 'utils/ast.rb', line 256

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:



120
121
122
123
124
125
126
127
# File 'utils/ast.rb', line 120

def resource(name)
  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)


180
181
182
# File 'utils/ast.rb', line 180

def resource_stanza?(resource_name, name)
  matching_stanzas(body_children(resource(resource_name).body), name).present?
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)


140
141
142
# File 'utils/ast.rb', line 140

def stable_stanza?(name)
  matching_stanzas(stable_children, name).present?
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:



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

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:



115
116
117
# File 'utils/ast.rb', line 115

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