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, 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?, process_source, stanza_text

Methods included from Kernel

#disk_usage_readable, #ensure_executable!, #ensure_formula_installed!, #exec_browser, #exec_editor, #ignore_interrupts, #interactive_shell, #number_readable, #odebug, #odeprecated, #odie, #odisabled, #ofail, #oh1, #oh1_title, #ohai, #ohai_title, #onoe, #opoo, #paths, #pretty_duration, #pretty_installed, #pretty_outdated, #pretty_uninstalled, #quiet_system, #redact_secrets, #redirect_stdout, #require?, #safe_system, #tap_and_name_comparison, #truncate_text_to_approximate_size, #which, #which_all, #which_editor, #with_custom_locale, #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:



82
83
84
85
86
87
88
# File 'utils/ast.rb', line 82

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:



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

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.

Parameters:



152
153
154
155
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
# File 'utils/ast.rb', line 152

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_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:



91
92
93
# File 'utils/ast.rb', line 91

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_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:



111
112
113
114
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
# File 'utils/ast.rb', line 111

def remove_stanza(name, type: nil)
  stanza_node = stanza(name, 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.

Parameters:



101
102
103
# File 'utils/ast.rb', line 101

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.

Parameters:



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

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

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



96
97
98
# File 'utils/ast.rb', line 96

def stanza(name, type: nil)
  children.find { |child| call_node_match?(child, name:, type:) }
end