Class: Utils::AST::FormulaAST Private
- 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( [*OnSystem::ARCH_OPTIONS, *OnSystem::BASE_OS_OPTIONS, *MacOSVersion::SYMBOLS.keys, :system].map do |name| :"on_#{name}" end.freeze, T::Array[Symbol], )
Constants included from Utils::AST
BlockNode, DefNode, Node, ProcessedSource, SendNode, TreeRewriter
Instance Attribute Summary collapse
-
#children ⇒ Array<Node>
readonly
private
Top-level nodes in the formula body.
Instance Method Summary collapse
- #add_bottle_block(bottle_output) ⇒ void private
- #add_stable_stanzas_after(after_name, new_stanzas) ⇒ void private
- #add_stanza(name, value, type: nil) ⇒ void private
- #add_stanzas_after(after_name, new_stanzas, parent: nil) ⇒ void private
- #bottle_block ⇒ Node? private
-
#contains_call?(name) ⇒ Boolean
private
Finds DSL calls regardless of their platform or enclosing scope.
- #initialize(formula_contents) ⇒ void constructor private
- #process(*args, &block) ⇒ T.untyped private
-
#remove_patches(&block) ⇒ void
private
Yields patch blocks in all scopes; the caller returns patches or branches to remove.
- #remove_stable_stanza(name) ⇒ void private
- #remove_stable_stanzas(name) ⇒ void private
- #remove_stanza(name, type: nil) ⇒ void private
- #replace_bottle_block(bottle_output) ⇒ void private
- #replace_resource_stanza_hash_value(resource_name, name, key, value) ⇒ void private
- #replace_resource_stanza_value(resource_name, name, value, old_value: nil) ⇒ void private
- #replace_resource_stanzas(resource_section, replace_existing: true, preserve_livecheck: false) ⇒ Symbol? private
- #replace_stable_stanza_hash_value(name, key, value) ⇒ void private
- #replace_stable_stanza_value(name, value) ⇒ void private
- #replace_stanza(name, replacement, type: nil) ⇒ void private
- #resource(name) ⇒ BlockNode private
- #resource_stanza?(resource_name, name) ⇒ Boolean private
- #stable_stanza?(name) ⇒ Boolean private
- #stanza(name, type: nil) ⇒ Node? private
- #stanzas(name, type: nil) ⇒ Array<Node> private
Methods included from Utils::AST
body_children, call_node_match?, component_match?, literal_value, process_source, ruby_literal, stanza_text
Methods included from Kernel
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.
109 110 111 112 113 114 115 |
# File 'utils/ast.rb', line 109 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 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.
106 107 108 |
# File 'utils/ast.rb', line 106 def children @children 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.
273 274 275 |
# File 'utils/ast.rb', line 273 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.
183 184 185 |
# File 'utils/ast.rb', line 183 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.
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 |
# File 'utils/ast.rb', line 327 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.
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
# File 'utils/ast.rb', line 223 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_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.
118 119 120 |
# File 'utils/ast.rb', line 118 def bottle_block stanza(:bottle, type: :block_call) 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.
310 311 312 313 314 315 316 |
# File 'utils/ast.rb', line 310 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 |
#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.
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 platform wrappers emptied by patch removal, including their comments.
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 |
# File 'utils/ast.rb', line 288 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) } 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.
165 166 167 |
# File 'utils/ast.rb', line 165 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.
170 171 172 173 174 175 |
# File 'utils/ast.rb', line 170 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.
278 279 280 281 282 283 |
# File 'utils/ast.rb', line 278 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.
268 269 270 |
# File 'utils/ast.rb', line 268 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.
207 208 209 |
# File 'utils/ast.rb', line 207 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.
195 196 197 |
# File 'utils/ast.rb', line 195 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.
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'utils/ast.rb', line 247 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.
155 156 157 |
# File 'utils/ast.rb', line 155 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.
150 151 152 |
# File 'utils/ast.rb', line 150 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.
319 320 321 322 323 324 |
# File 'utils/ast.rb', line 319 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.
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'utils/ast.rb', line 133 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.
212 213 214 |
# File 'utils/ast.rb', line 212 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.
160 161 162 |
# File 'utils/ast.rb', line 160 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.
123 124 125 |
# File 'utils/ast.rb', line 123 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.
128 129 130 |
# File 'utils/ast.rb', line 128 def stanzas(name, type: nil) matching_stanzas(children, name, type:) end |