From 38146893c3966d0ad1a359f866df4dbf49b9e176 Mon Sep 17 00:00:00 2001 From: apainintheneck Date: Tue, 21 Mar 2023 19:05:41 -0700 Subject: [PATCH] api_hashable: Make API path subs generic This turns the ability to replace common paths with placeholders into a mixin that can be used with both Casks and Formulae. The idea here is to make formula hash generation more consistent. --- Library/Homebrew/cask/cask.rb | 36 ++----------------- Library/Homebrew/cask/cask_loader.rb | 4 +-- .../Homebrew/dev-cmd/generate-formula-api.rb | 1 + Library/Homebrew/extend/api_hashable.rb | 34 ++++++++++++++++++ Library/Homebrew/formula.rb | 2 ++ Library/Homebrew/global.rb | 2 ++ Library/Homebrew/test/cask/cask_spec.rb | 2 ++ 7 files changed, 45 insertions(+), 36 deletions(-) create mode 100644 Library/Homebrew/extend/api_hashable.rb diff --git a/Library/Homebrew/cask/cask.rb b/Library/Homebrew/cask/cask.rb index 04f68cda0c..9b497a4082 100644 --- a/Library/Homebrew/cask/cask.rb +++ b/Library/Homebrew/cask/cask.rb @@ -6,6 +6,7 @@ require "cask/config" require "cask/dsl" require "cask/metadata" require "utils/bottles" +require "extend/api_hashable" module Cask # An instance of a cask. @@ -16,11 +17,9 @@ module Cask extend Forwardable extend Predicable + extend APIHashable include Metadata - # Needs a leading slash to avoid `File.expand.path` complaining about non-absolute home. - HOME_PLACEHOLDER = "/$HOME" - HOMEBREW_PREFIX_PLACEHOLDER = "$HOMEBREW_PREFIX" APPDIR_PLACEHOLDER = "$APPDIR" # TODO: can be removed when API JSON is regenerated with HOMEBREW_PREFIX_PLACEHOLDER. @@ -31,37 +30,6 @@ module Cask attr_predicate :loaded_from_api? - class << self - def generating_hash! - return if generating_hash? - - # Apply monkeypatches for API generation - @old_homebrew_prefix = HOMEBREW_PREFIX - @old_home = Dir.home - Object.send(:remove_const, :HOMEBREW_PREFIX) - Object.const_set(:HOMEBREW_PREFIX, Pathname(HOMEBREW_PREFIX_PLACEHOLDER)) - ENV["HOME"] = HOME_PLACEHOLDER - - @generating_hash = true - end - - def generated_hash! - return unless generating_hash? - - # Revert monkeypatches for API generation - Object.send(:remove_const, :HOMEBREW_PREFIX) - Object.const_set(:HOMEBREW_PREFIX, @old_homebrew_prefix) - ENV["HOME"] = @old_home - - @generating_hash = false - end - - def generating_hash? - @generating_hash ||= false - @generating_hash == true - end - end - def self.all # TODO: ideally avoid using ARGV by moving to e.g. CLI::Parser if ARGV.exclude?("--eval-all") && !Homebrew::EnvConfig.eval_all? diff --git a/Library/Homebrew/cask/cask_loader.rb b/Library/Homebrew/cask/cask_loader.rb index 0fa41924df..3f884a630b 100644 --- a/Library/Homebrew/cask/cask_loader.rb +++ b/Library/Homebrew/cask/cask_loader.rb @@ -329,8 +329,8 @@ module Cask # TODO: HOMEBREW_OLD_PREFIX_PLACEHOLDER can be removed when API JSON is # regenerated with HOMEBREW_PREFIX_PLACEHOLDER. string.to_s - .gsub(Cask::HOME_PLACEHOLDER, Dir.home) - .gsub(Cask::HOMEBREW_PREFIX_PLACEHOLDER, HOMEBREW_PREFIX) + .gsub(HOMEBREW_HOME_PLACEHOLDER, Dir.home) + .gsub(HOMEBREW_PREFIX_PLACEHOLDER, HOMEBREW_PREFIX) .gsub(Cask::APPDIR_PLACEHOLDER, appdir) .gsub(Cask::HOMEBREW_OLD_PREFIX_PLACEHOLDER, HOMEBREW_PREFIX) end diff --git a/Library/Homebrew/dev-cmd/generate-formula-api.rb b/Library/Homebrew/dev-cmd/generate-formula-api.rb index c47c0f4571..fb05cb187c 100644 --- a/Library/Homebrew/dev-cmd/generate-formula-api.rb +++ b/Library/Homebrew/dev-cmd/generate-formula-api.rb @@ -50,6 +50,7 @@ module Homebrew FileUtils.mkdir_p directories Formulary.enable_factory_cache! + Formula.generating_hash! tap.formula_names.each do |name| formula = Formulary.factory(name) diff --git a/Library/Homebrew/extend/api_hashable.rb b/Library/Homebrew/extend/api_hashable.rb new file mode 100644 index 0000000000..0527a94938 --- /dev/null +++ b/Library/Homebrew/extend/api_hashable.rb @@ -0,0 +1,34 @@ +# typed: true +# frozen_string_literal: true + +# Used to substitute common paths with generic placeholders when generating JSON for the API. +module APIHashable + def generating_hash! + return if generating_hash? + + # Apply monkeypatches for API generation + @old_homebrew_prefix = HOMEBREW_PREFIX + @old_home = Dir.home + Object.send(:remove_const, :HOMEBREW_PREFIX) + Object.const_set(:HOMEBREW_PREFIX, Pathname.new(HOMEBREW_PREFIX_PLACEHOLDER)) + ENV["HOME"] = HOMEBREW_HOME_PLACEHOLDER + + @generating_hash = true + end + + def generated_hash! + return unless generating_hash? + + # Revert monkeypatches for API generation + Object.send(:remove_const, :HOMEBREW_PREFIX) + Object.const_set(:HOMEBREW_PREFIX, @old_homebrew_prefix) + ENV["HOME"] = @old_home + + @generating_hash = false + end + + def generating_hash? + @generating_hash ||= false + @generating_hash == true + end +end diff --git a/Library/Homebrew/formula.rb b/Library/Homebrew/formula.rb index c71a854cc2..19f2bde8e6 100644 --- a/Library/Homebrew/formula.rb +++ b/Library/Homebrew/formula.rb @@ -30,6 +30,7 @@ require "find" require "utils/spdx" require "extend/on_system" require "api" +require "extend/api_hashable" # A formula provides instructions and metadata for Homebrew to install a piece # of software. Every Homebrew formula is a {Formula}. @@ -69,6 +70,7 @@ class Formula extend Forwardable extend Cachable extend Predicable + extend APIHashable # The name of this {Formula}. # e.g. `this-formula` diff --git a/Library/Homebrew/global.rb b/Library/Homebrew/global.rb index de6e01e5e4..1ed2ee7d21 100644 --- a/Library/Homebrew/global.rb +++ b/Library/Homebrew/global.rb @@ -58,6 +58,8 @@ HOMEBREW_MACOS_ARM_DEFAULT_REPOSITORY = ENV.fetch("HOMEBREW_MACOS_ARM_DEFAULT_RE HOMEBREW_LINUX_DEFAULT_PREFIX = ENV.fetch("HOMEBREW_LINUX_DEFAULT_PREFIX").freeze HOMEBREW_LINUX_DEFAULT_REPOSITORY = ENV.fetch("HOMEBREW_LINUX_DEFAULT_REPOSITORY").freeze HOMEBREW_PREFIX_PLACEHOLDER = "$HOMEBREW_PREFIX" +# Needs a leading slash to avoid `File.expand.path` complaining about non-absolute home. +HOMEBREW_HOME_PLACEHOLDER = "/$HOME" HOMEBREW_MACOS_NEWEST_UNSUPPORTED = ENV.fetch("HOMEBREW_MACOS_NEWEST_UNSUPPORTED").freeze HOMEBREW_MACOS_OLDEST_SUPPORTED = ENV.fetch("HOMEBREW_MACOS_OLDEST_SUPPORTED").freeze diff --git a/Library/Homebrew/test/cask/cask_spec.rb b/Library/Homebrew/test/cask/cask_spec.rb index ddc581c730..065d8fe6d0 100644 --- a/Library/Homebrew/test/cask/cask_spec.rb +++ b/Library/Homebrew/test/cask/cask_spec.rb @@ -337,6 +337,8 @@ describe Cask::Cask, :cask do expect(JSON.pretty_generate(h["variations"])).to eq expected_sha256_variations.strip end + # @note The calls to `APIHashable.generating_hash!` and `APIHashable.generated_hash!` + # are not idempotent so they can only be used in one test. it "returns the correct hash placeholders" do described_class.generating_hash! expect(described_class).to be_generating_hash