Class: Utils::AST::CaskAST Private

Inherits:
Object show all
Includes:
Utils::AST
Defined in:
utils/ast.rb

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 casks.

Constant Summary

Constants included from Utils::AST

BlockNode, DefNode, Node, ProcessedSource, SendNode, TreeRewriter

Class Method Summary collapse

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(cask_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:



931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
# File 'utils/ast.rb', line 931

def initialize(cask_contents)
  @cask_contents = cask_contents
  processed_source, root_node = process_source(cask_contents)
  cask_block = if root_node.block_type? && T.cast(root_node, BlockNode).method_name == :cask
    T.cast(root_node, BlockNode)
  elsif root_node.begin_type?
    root_node.children.find { |node| node.block_type? && node.method_name == :cask }
  end

  raise "Could not find cask block!" if cask_block.nil?

  @processed_source = T.let(processed_source, ProcessedSource)
  @cask_block = T.let(T.cast(cask_block, BlockNode), BlockNode)
  @tree_rewriter = T.let(TreeRewriter.new(processed_source.buffer), TreeRewriter)
end

Class Method Details

.valid_bump?(original, proposed, version:) ⇒ 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.

Compare a prepared cask with the release edits allowed in its checkout.

Parameters:

Returns:

  • (Boolean)


949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
# File 'utils/ast.rb', line 949

def self.valid_bump?(original, proposed, version:)
  checkout = new(original)
  proposal = new(proposed)
  # `brew audit --fix` may have corrected the minimum macOS version.
  minimum = proposal.depends_on_macos_value
  if minimum.is_a?(RuboCop::AST::SymbolNode) && !checkout.update_depends_on_macos_minimum!(minimum.value)
    return false
  end

  old_values = checkout.blank_bump_values!
  new_values = proposal.blank_bump_values!
  return false if checkout.process != proposal.process
  return false unless new_values.fetch(:sha256).all? { |checksum| checksum.match?(/\A\h{64}\z/) }

  require "livecheck/livecheck_version"

  livecheck_version = lambda do |value|
    Homebrew::Livecheck::LivecheckVersion.new(value.to_s.split(",").map { |part| Version.new(part) })
  end
  bumps = old_values.fetch(:version).zip(new_values.fetch(:version)).reject { |current, bump| current == bump }
  bumps.any? { |_, bump| bump == version } && bumps.all? do |current, bump|
    bump.to_s.match?(/\A[[:alnum:]][[:alnum:].,:+_-]*\z/) &&
      livecheck_version.call(bump) > livecheck_version.call(current)
  end
end

Instance Method Details

#blank_bump_values! ⇒ Hash{Symbol => Array<String>}

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.

Blank the version and checksum strings replaced by a bump and return them by stanza.

Returns:



982
983
984
985
986
987
988
989
990
991
992
# File 'utils/ast.rb', line 982

def blank_bump_values!
  [:version, :sha256].to_h do |name|
    values = stanzas(name).flat_map do |stanza|
      stanza.arguments.flat_map do |argument|
        argument.is_a?(RuboCop::AST::HashNode) ? argument.pairs.map(&:value) : [argument]
      end
    end.select(&:str_type?)
    values.each { |value| tree_rewriter.replace(value.source_range, '""') }
    [name, values.map(&:str_content)]
  end
end

#depends_on_macos? ⇒ 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.

Returns:

  • (Boolean)


1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
# File 'utils/ast.rb', line 1076

def depends_on_macos?
  stanzas(:depends_on).any? do |stanza_node|
    stanza_node.arguments.any? do |argument|
      literal_value(argument) == :macos ||
        (argument.hash_type? && T.cast(argument, RuboCop::AST::HashNode).pairs.any? do |pair|
          literal_value(pair.key) == :macos
        end)
    end
  end
end

#depends_on_macos_value ⇒ 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.

The value of the top-level depends_on macos: stanza.

Returns:



1089
1090
1091
1092
1093
1094
# File 'utils/ast.rb', line 1089

def depends_on_macos_value
  top_level_stanzas(:depends_on).flat_map { |stanza| stanza.arguments.grep(RuboCop::AST::HashNode) }
                                .flat_map(&:pairs)
                                .find { |pair| literal_value(pair.key) == :macos }
                                &.value
end

#first_stanza_value(name, within: nil) ⇒ 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:

Returns:

  • (T.untyped)


1020
1021
1022
1023
1024
1025
# File 'utils/ast.rb', line 1020

def first_stanza_value(name, within: nil)
  stanza_node = stanzas(name, within:).first
  return if stanza_node.blank?

  literal_value(stanza_node.first_argument)
end

#process ⇒ String

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:



976
977
978
# File 'utils/ast.rb', line 976

def process
  tree_rewriter.process
end

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



995
996
997
998
999
1000
# File 'utils/ast.rb', line 995

def replace_first_stanza_value(name, value)
  stanza_node = stanzas(name).first
  raise "Could not find '#{name}' stanza!" if stanza_node.blank?

  replace_stanza_argument(stanza_node, value)
end

#replace_root_stanza_with_arch_blocks(name, old_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:



1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
# File 'utils/ast.rb', line 1057

def replace_root_stanza_with_arch_blocks(name, old_value)
  stanza_node = top_level_stanzas(name).find do |node|
    literal_value(node.first_argument) == old_value
  end
  return if stanza_node.blank?

  indent = " " * stanza_node.source_range.column
  replacement = <<~EOS
    #{indent}on_arm do
    #{indent}  #{name} #{ruby_literal(old_value)}
    #{indent}end
    #{indent}on_intel do
    #{indent}  #{name} #{ruby_literal(old_value)}
    #{indent}end
  EOS
  tree_rewriter.replace(whole_line_range(stanza_node.source_range), replacement)
end

#replace_stanza_value(name, old_value, new_value, within: nil) ⇒ Integer

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:



1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
# File 'utils/ast.rb', line 1035

def replace_stanza_value(name, old_value, new_value, within: nil)
  replacement_count = T.let(0, Integer)
  stanzas(name, within:).each do |stanza_node|
    if literal_value(stanza_node.first_argument) == old_value
      replace_stanza_argument(stanza_node, new_value)
      replacement_count += 1
    end

    stanza_node.arguments.grep(RuboCop::AST::HashNode).each do |hash_node|
      hash_node.pairs.each do |pair|
        next if literal_value(pair.value) != old_value

        tree_rewriter.replace(pair.value.source_range, ruby_literal(new_value))
        replacement_count += 1
      end
    end
  end

  replacement_count
end

#stanza?(name, within: nil) ⇒ 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)


1003
1004
1005
# File 'utils/ast.rb', line 1003

def stanza?(name, within: nil)
  stanzas(name, within:).present?
end

#stanza_anywhere?(name, within:) ⇒ 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)


1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
# File 'utils/ast.rb', line 1008

def stanza_anywhere?(name, within:)
  cask_block.each_node(:block).any? do |node|
    block_node = T.cast(node, BlockNode)
    next false if block_node.method_name != within || block_node.receiver.present?

    block_node.each_node(:send).any? do |send_node|
      send_node.method_name == name && send_node.receiver.nil? && send_node.first_argument.present?
    end
  end
end

#update_depends_on_macos_minimum!(version) ⇒ 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.

Sets the minimum macOS version of the depends_on macos: stanza, adding the stanza when it is missing. Returns false when the minimum version cannot be expressed, e.g. an exact version list or a maximum.

Parameters:

Returns:

  • (Boolean)


1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
# File 'utils/ast.rb', line 1100

def update_depends_on_macos_minimum!(version)
  if (macos_value = depends_on_macos_value)
    return false unless minimum_macos_value?(macos_value)

    tree_rewriter.replace(macos_value.source_range, ruby_literal(version))
    return true
  end

  # A second stanza alongside a bare `depends_on :macos` would be flagged
  # as redundant by `Homebrew/OSDependsOn`.
  bare_stanza_node = top_level_stanzas(:depends_on).find do |node|
    argument = node.first_argument
    argument&.sym_type? && literal_value(argument) == :macos
  end
  if (bare_argument = bare_stanza_node&.first_argument)
    tree_rewriter.replace(bare_argument.source_range, "macos: #{ruby_literal(version)}")
    return true
  end

  return false if depends_on_macos?

  new_stanza = "depends_on macos: #{ruby_literal(version)}"

  if (existing_stanza = top_level_stanzas(:depends_on).last)
    indent = " " * existing_stanza.source_range.column
    tree_rewriter.insert_after(whole_line_range(existing_stanza.source_range), "#{indent}#{new_stanza}\n")
    return true
  end

  successor = stanza_successor(:depends_on)
  return false if successor.nil?

  indent = " " * successor.source_range.column
  line_range = whole_line_range(successor.source_range)
  separator = "\n" unless cask_contents[0...line_range.begin_pos].to_s.match?(/(\n\n|do\n)\z/)
  tree_rewriter.insert_before(line_range, "#{separator}#{indent}#{new_stanza}\n\n")
  true
end