Merge pull request #14981 from dduugg/tab-no-ostruct

Remove OpenStruct use in Tab
This commit is contained in:
Mike McQuaid 2023-03-15 12:16:49 -04:00 committed by GitHub
commit 83aea49d8a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 20 additions and 23 deletions

View File

@ -390,7 +390,6 @@ Style/NumericLiterals:
Style/OpenStructUse:
Exclude:
- "Homebrew/cli/args.rb"
- "Homebrew/tab.rb"
- "Taps/**/*.rb"
# Rescuing `StandardError` is an understood default.

View File

@ -400,7 +400,6 @@ module Homebrew
tab = Tab.for_keg(keg)
original_tab = tab.dup
tab.poured_from_bottle = false
tab.HEAD = nil
tab.time = nil
tab.changed_files = changed_files.dup
if args.only_json_tab?

View File

@ -157,8 +157,8 @@ class Keg
brewed_perl = runtime_dependencies&.any? { |dep| dep["full_name"] == "perl" && dep["declared_directly"] }
perl_path = if brewed_perl || name == "perl"
"#{HOMEBREW_PREFIX}/opt/perl/bin/perl"
elsif tab["built_on"].present?
perl_path = "/usr/bin/perl#{tab["built_on"]["preferred_perl"]}"
elsif tab.built_on.present?
perl_path = "/usr/bin/perl#{tab.built_on["preferred_perl"]}"
# For `:all` bottles, we could have built this bottle with a Perl we don't have.
# Such bottles typically don't have strict version requirements.

View File

@ -1,23 +1,25 @@
# typed: false
# typed: true
# frozen_string_literal: true
require "cxxstdlib"
require "ostruct"
require "options"
require "json"
require "development_tools"
require "extend/cachable"
# Inherit from OpenStruct to gain a generic initialization method that takes a
# hash and creates an attribute for each key and value. Rather than calling
# `new` directly, use one of the class methods like {Tab.create}.
class Tab < OpenStruct
# Rather than calling `new` directly, use one of the class methods like {Tab.create}.
class Tab
extend T::Sig
extend Cachable
FILENAME = "INSTALL_RECEIPT.json"
attr_accessor :homebrew_version, :tabfile, :built_as_bottle, :installed_as_dependency, :installed_on_request,
:changed_files, :poured_from_bottle, :loaded_from_api, :time, :stdlib, :aliases, :arch, :source,
:built_on
attr_writer :used_options, :unused_options, :compiler, :runtime_dependencies, :source_modified_time
# Instantiates a Tab for a new installation of a formula.
def self.create(formula, compiler, stdlib)
build = formula.build
@ -120,7 +122,7 @@ class Tab < OpenStruct
empty
end
tab["tabfile"] = path
tab.tabfile = path
tab
end
@ -226,6 +228,10 @@ class Tab < OpenStruct
end
end
def initialize(attributes = {})
attributes.each { |key, value| instance_variable_set("@#{key}", value) }
end
def any_args_or_options?
!used_options.empty? || !unused_options.empty?
end
@ -255,15 +261,15 @@ class Tab < OpenStruct
end
def used_options
Options.create(super)
Options.create(@used_options)
end
def unused_options
Options.create(super)
Options.create(@unused_options)
end
def compiler
super || DevelopmentTools.default_compiler
@compiler || DevelopmentTools.default_compiler
end
def parsed_homebrew_version
@ -275,7 +281,7 @@ class Tab < OpenStruct
def runtime_dependencies
# Homebrew versions prior to 1.1.6 generated incorrect runtime dependency
# lists.
super unless parsed_homebrew_version < "1.1.6"
@runtime_dependencies unless parsed_homebrew_version < "1.1.6"
end
def cxxstdlib
@ -324,7 +330,7 @@ class Tab < OpenStruct
sig { returns(Time) }
def source_modified_time
Time.at(super || 0)
Time.at(@source_modified_time || 0)
end
def to_json(options = nil)

View File

@ -47,5 +47,4 @@ TESTBALL_PATCHES_SHA256 = "799c2d551ac5c3a5759bea7796631a7906a6a24435b52261a3171
PATCH_A_SHA256 = "83404f4936d3257e65f176c4ffb5a5b8d6edd644a21c8d8dcc73e22a6d28fcfa"
PATCH_B_SHA256 = "57958271bb802a59452d0816e0670d16c8b70bdf6530bcf6f78726489ad89b90"
TEST_SHA1 = "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef"
TEST_SHA256 = "deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef"

View File

@ -29,7 +29,6 @@ describe Tab do
"changed_files" => [],
"time" => time,
"source_modified_time" => 0,
"HEAD" => TEST_SHA1,
"compiler" => "clang",
"stdlib" => "libcxx",
"runtime_dependencies" => [],
@ -71,7 +70,6 @@ describe Tab do
expect(tab).not_to be_head
expect(tab.tap).to be_nil
expect(tab.time).to be_nil
expect(tab.HEAD).to be_nil
expect(tab.runtime_dependencies).to be_nil
expect(tab.stable_version).to be_nil
expect(tab.head_version).to be_nil
@ -154,7 +152,6 @@ describe Tab do
end
specify "other attributes" do
expect(tab.HEAD).to eq(TEST_SHA1)
expect(tab.tap.name).to eq("homebrew/core")
expect(tab.time).to eq(time)
expect(tab).not_to be_built_as_bottle
@ -179,7 +176,6 @@ describe Tab do
expect(tab.tap.name).to eq("homebrew/core")
expect(tab.spec).to eq(:stable)
expect(tab.time).to eq(Time.at(1_403_827_774).to_i)
expect(tab.HEAD).to eq(TEST_SHA1)
expect(tab.cxxstdlib.compiler).to eq(:clang)
expect(tab.cxxstdlib.type).to eq(:libcxx)
expect(tab.runtime_dependencies).to eq(runtime_dependencies)
@ -207,7 +203,6 @@ describe Tab do
expect(tab.tap.name).to eq("homebrew/core")
expect(tab.spec).to eq(:stable)
expect(tab.time).to eq(Time.at(1_403_827_774).to_i)
expect(tab.HEAD).to eq(TEST_SHA1)
expect(tab.cxxstdlib.compiler).to eq(:clang)
expect(tab.cxxstdlib.type).to eq(:libcxx)
expect(tab.runtime_dependencies).to eq(runtime_dependencies)
@ -229,7 +224,6 @@ describe Tab do
expect(tab.tap.name).to eq("homebrew/core")
expect(tab.spec).to eq(:stable)
expect(tab.time).to eq(Time.at(1_403_827_774).to_i)
expect(tab.HEAD).to eq(TEST_SHA1)
expect(tab.cxxstdlib.compiler).to eq(:clang)
expect(tab.cxxstdlib.type).to eq(:libcxx)
expect(tab.runtime_dependencies).to be_nil