Class: Utils::AST::FormulaAST Private
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, Node, ProcessedSource, SendNode, TreeRewriter
Instance Method Summary collapse
-
#add_bottle_block(bottle_output) ⇒ void
private
-
#add_stanza(name, value, type: nil) ⇒ void
private
-
#bottle_block ⇒ Node?
private
-
#initialize(formula_contents) ⇒ void
constructor
private
-
#remove_stanza(name, type: nil) ⇒ void
private
-
#replace_bottle_block(bottle_output) ⇒ void
private
-
#replace_stanza(name, replacement, type: nil) ⇒ void
private
-
#stanza(name, type: nil) ⇒ Node?
private
Methods included from Utils::AST
body_children, call_node_match?, component_match?, process_source, stanza_text
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.
86 87 88 89 90 91 92 |
# File 'utils/ast.rb', line 86 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.
110 111 112 |
# File 'utils/ast.rb', line 110 def add_bottle_block(bottle_output) add_stanza(:bottle, "\n#{bottle_output.chomp}", 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.
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'utils/ast.rb', line 156 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 |
#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.
95 96 97 |
# File 'utils/ast.rb', line 95 def bottle_block stanza(:bottle, type: :block_call) 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.
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'utils/ast.rb', line 115 def remove_stanza(name, type: nil) stanza_node = stanza(name, type: type) raise "Could not find '#{name}' stanza!" if stanza_node.blank? # stanza is probably followed by a newline character # try to delete it if so stanza_range = stanza_node.source_range trailing_range = stanza_range.with(begin_pos: stanza_range.end_pos, end_pos: stanza_range.end_pos + 1) if trailing_range.source.chomp.empty? stanza_range = stanza_range.adjust(end_pos: 1) # stanza_node is probably indented # since a trailing newline has been removed, # try to delete leading whitespace on line leading_range = stanza_range.with(begin_pos: stanza_range.begin_pos - stanza_range.column, end_pos: stanza_range.begin_pos) if leading_range.source.strip.empty? stanza_range = stanza_range.adjust(begin_pos: -stanza_range.column) # if the stanza was preceded by a blank line, it should be removed # that is, if the two previous characters are newlines, # then delete one of them leading_range = stanza_range.with(begin_pos: stanza_range.begin_pos - 2, end_pos: stanza_range.begin_pos) stanza_range = stanza_range.adjust(begin_pos: -1) if leading_range.source.chomp.chomp.empty? end end tree_rewriter.remove(stanza_range) 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.
105 106 107 |
# File 'utils/ast.rb', line 105 def replace_bottle_block(bottle_output) replace_stanza(:bottle, bottle_output.chomp, type: :block_call) 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.
148 149 150 151 152 153 |
# File 'utils/ast.rb', line 148 def replace_stanza(name, replacement, type: nil) stanza_node = stanza(name, type: 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 |
#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.
100 101 102 |
# File 'utils/ast.rb', line 100 def stanza(name, type: nil) children.find { |child| call_node_match?(child, name: name, type: type) } end |