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:



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

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:



170
171
172
# File 'utils/ast.rb', line 170

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:



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
297
298
299
300
301
# File 'utils/ast.rb', line 269

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:



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

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:



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

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:



157
158
159
160
161
162
# File 'utils/ast.rb', line 157

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:



253
254
255
256
257
258
# File 'utils/ast.rb', line 253

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:



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

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:



182
183
184
# File 'utils/ast.rb', line 182

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:



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'utils/ast.rb', line 222

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
    groups = resource_stanza_groups(preserve_livecheck:)
    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:



142
143
144
# File 'utils/ast.rb', line 142

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:



137
138
139
# File 'utils/ast.rb', line 137

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:



261
262
263
264
265
266
# File 'utils/ast.rb', line 261

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
128
129
130
131
132
133
134
# File 'utils/ast.rb', line 120

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)


187
188
189
# File 'utils/ast.rb', line 187

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)


147
148
149
# File 'utils/ast.rb', line 147

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