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

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:



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

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:



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

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:



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

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:



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
# File 'utils/ast.rb', line 282

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:



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

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:



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

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:



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

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:



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

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:



266
267
268
269
270
271
# File 'utils/ast.rb', line 266

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:



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

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:



195
196
197
# File 'utils/ast.rb', line 195

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:



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

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:



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'utils/ast.rb', line 235

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:



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

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:



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

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:



274
275
276
277
278
279
# File 'utils/ast.rb', line 274

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:



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'utils/ast.rb', line 121

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)


200
201
202
# File 'utils/ast.rb', line 200

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)


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

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:



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

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:



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

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