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.
This commit is contained in:
parent
d42e9b40d5
commit
38146893c3
@ -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?
|
||||
|
@ -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
|
||||
|
@ -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)
|
||||
|
34
Library/Homebrew/extend/api_hashable.rb
Normal file
34
Library/Homebrew/extend/api_hashable.rb
Normal file
@ -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
|
@ -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`
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user