Class: Homebrew::DevCmd::Bottle

Inherits:
AbstractCommand show all
Includes:
FileUtils
Defined in:
extend/os/mac/dev-cmd/bottle.rb,
dev-cmd/bottle.rb

Constant Summary collapse

BOTTLE_ERB =
<<-EOS.freeze
  bottle do
    <% if [HOMEBREW_BOTTLE_DEFAULT_DOMAIN.to_s,
     "#{HOMEBREW_BOTTLE_DEFAULT_DOMAIN}/bottles"].exclude?(root_url) %>
    root_url "<%= root_url %>"<% if root_url_using.present? %>,
using: <%= root_url_using %>
    <% end %>
    <% end %>
    <% if rebuild.positive? %>
    rebuild <%= rebuild %>
    <% end %>
    <% sha256_lines.each do |line| %>
    <%= line %>
    <% end %>
  end
EOS
MAXIMUM_STRING_MATCHES =
100
[
  %r{#{Regexp.escape(HOMEBREW_LIBRARY)}/Homebrew/os/(mac|linux)/pkgconfig},
].freeze

Instance Attribute Summary

Attributes inherited from AbstractCommand

#args

Instance Method Summary collapse

Methods inherited from AbstractCommand

command, command_name, #initialize, parser

Constructor Details

This class inherits a constructor from Homebrew::AbstractCommand

Instance Method Details

#bottle_output(bottle, root_url_using) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'dev-cmd/bottle.rb', line 125

def bottle_output(bottle, root_url_using)
  cellars = bottle.checksums.filter_map do |checksum|
    cellar = checksum["cellar"]
    next unless cellar_parameter_needed? cellar

    case cellar
    when String
      %Q("#{cellar}")
    when Symbol
      ":#{cellar}"
    end
  end
  tag_column = cellars.empty? ? 0 : "cellar: #{cellars.max_by(&:length)}, ".length

  tags = bottle.checksums.map { |checksum| checksum["tag"] }
  # Start where the tag ends, add the max length of the tag, add two for the `: `
  digest_column = tag_column + tags.max_by(&:length).length + 2

  sha256_lines = bottle.checksums.map do |checksum|
    generate_sha256_line(checksum["tag"], checksum["digest"], checksum["cellar"], tag_column, digest_column)
  end
  erb_binding = bottle.instance_eval { binding }
  erb_binding.local_variable_set(:sha256_lines, sha256_lines)
  erb_binding.local_variable_set(:root_url_using, root_url_using)
  erb = ERB.new BOTTLE_ERB
  erb.result(erb_binding).gsub(/^\s*$\n/, "")
end

#generate_sha256_line(tag, digest, cellar, tag_column, digest_column) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'dev-cmd/bottle.rb', line 110

def generate_sha256_line(tag, digest, cellar, tag_column, digest_column)
  line = "sha256 "
  tag_column += line.length
  digest_column += line.length
  if cellar.is_a?(Symbol)
    line += "cellar: :#{cellar},"
  elsif cellar_parameter_needed?(cellar)
    line += %Q(cellar: "#{cellar}",)
  end
  line += " " * (tag_column - line.length)
  line += "#{tag}:"
  line += " " * (digest_column - line.length)
  %Q(#{line}"#{digest}")
end

#gnu_tar(gnu_tar_formula) ⇒ String

Parameters:

Returns:



21
22
23
# File 'extend/os/mac/dev-cmd/bottle.rb', line 21

def gnu_tar(gnu_tar_formula)
  "#{gnu_tar_formula.opt_bin}/gtar"
end

#merge_bottle_spec(old_keys, old_bottle_spec, new_bottle_hash) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'dev-cmd/bottle.rb', line 172

def merge_bottle_spec(old_keys, old_bottle_spec, new_bottle_hash)
  mismatches = []
  checksums = []

  new_values = {
    root_url: new_bottle_hash["root_url"],
    rebuild:  new_bottle_hash["rebuild"],
  }

  skip_keys = [:sha256, :cellar]
  old_keys.each do |key|
    next if skip_keys.include?(key)

    old_value = old_bottle_spec.send(key).to_s
    new_value = new_values[key].to_s

    next if old_value.present? && new_value == old_value

    mismatches << "#{key}: old: #{old_value.inspect}, new: #{new_value.inspect}"
  end

  return [mismatches, checksums] if old_keys.exclude? :sha256

  old_bottle_spec.collector.each_tag do |tag|
    old_tag_spec = old_bottle_spec.collector.specification_for(tag)
    old_hexdigest = old_tag_spec.checksum.hexdigest
    old_cellar = old_tag_spec.cellar
    new_value = new_bottle_hash.dig("tags", tag.to_s)
    if new_value.present? && new_value["sha256"] != old_hexdigest
      mismatches << "sha256 #{tag}: old: #{old_hexdigest.inspect}, new: #{new_value["sha256"].inspect}"
    elsif new_value.present? && new_value["cellar"] != old_cellar.to_s
      mismatches << "cellar #{tag}: old: #{old_cellar.to_s.inspect}, new: #{new_value["cellar"].inspect}"
    else
      checksums << { cellar: old_cellar, tag.to_sym => old_hexdigest }
    end
  end

  [mismatches, checksums]
end

#merge_json_files(json_files) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
# File 'dev-cmd/bottle.rb', line 159

def merge_json_files(json_files)
  json_files.reduce({}) do |hash, json_file|
    json_file.each_value do |json_hash|
      json_bottle = json_hash["bottle"]
      cellar = json_bottle.delete("cellar")
      json_bottle["tags"].each_value do |json_platform|
        json_platform["cellar"] ||= cellar
      end
    end
    hash.deep_merge(json_file)
  end
end

#parse_json_files(filenames) ⇒ Object



153
154
155
156
157
# File 'dev-cmd/bottle.rb', line 153

def parse_json_files(filenames)
  filenames.map do |filename|
    JSON.parse(File.read(filename))
  end
end

#runvoid

This method returns an undefined value.



97
98
99
100
101
102
103
104
105
106
107
108
# File 'dev-cmd/bottle.rb', line 97

def run
  if args.merge?
    Homebrew.install_bundler_gems!(groups: ["ast"])
    return merge
  end

  gnu_tar_formula_ensure_installed_if_needed!

  args.named.to_resolved_formulae(uniq: false).each do |formula|
    bottle_formula formula
  end
end

#tar_argsArray<String>

Returns:



10
11
12
13
14
15
16
# File 'extend/os/mac/dev-cmd/bottle.rb', line 10

def tar_args
  if MacOS.version >= :catalina
    ["--no-mac-metadata", "--no-acls", "--no-xattrs"].freeze
  else
    [].freeze
  end
end