diff --git a/Library/Homebrew/cask/cask.rb b/Library/Homebrew/cask/cask.rb index 02703f440b..372eba7077 100644 --- a/Library/Homebrew/cask/cask.rb +++ b/Library/Homebrew/cask/cask.rb @@ -19,7 +19,7 @@ module Cask extend Searchable include Metadata - attr_reader :token, :sourcefile_path, :source, :config, :default_config + attr_reader :token, :sourcefile_path, :source, :config, :default_config, :loaded_from_api attr_accessor :download, :allow_reassignment @@ -44,12 +44,14 @@ module Cask @tap end - def initialize(token, sourcefile_path: nil, source: nil, tap: nil, config: nil, allow_reassignment: false, &block) + def initialize(token, sourcefile_path: nil, source: nil, tap: nil, config: nil, + allow_reassignment: false, loaded_from_api: false, &block) @token = token @sourcefile_path = sourcefile_path @source = source @tap = tap @allow_reassignment = allow_reassignment + @loaded_from_api = loaded_from_api @block = block @default_config = config || Config.new @@ -133,7 +135,10 @@ module Cask def installed_caskfile installed_version = timestamped_versions.last - metadata_main_container_path.join(*installed_version, "Casks", "#{token}.rb") + caskfile_dir = metadata_main_container_path.join(*installed_version, "Casks") + return caskfile_dir.join("#{token}.json") if caskfile_dir.join("#{token}.json").exist? + + caskfile_dir.join("#{token}.rb") end def config_path diff --git a/Library/Homebrew/cask/cask_loader.rb b/Library/Homebrew/cask/cask_loader.rb index 1b21258758..7b5b5d8774 100644 --- a/Library/Homebrew/cask/cask_loader.rb +++ b/Library/Homebrew/cask/cask_loader.rb @@ -51,7 +51,7 @@ module Cask class FromPathLoader < FromContentLoader def self.can_load?(ref) path = Pathname(ref) - path.extname == ".rb" && path.expand_path.exist? + %w[.rb .json].include?(path.extname) && path.expand_path.exist? end attr_reader :token, :path @@ -59,7 +59,7 @@ module Cask def initialize(path) # rubocop:disable Lint/MissingSuper path = Pathname(path).expand_path - @token = path.basename(".rb").to_s + @token = path.basename(path.extname).to_s @path = path end @@ -71,6 +71,10 @@ module Cask @content = path.read(encoding: "UTF-8") @config = config + if path.extname == ".json" + return FromAPILoader.new(token, from_json: JSON.parse(@content)).load(config: config) + end + begin instance_eval(content, path).tap do |cask| raise CaskUnreadableError.new(token, "'#{path}' does not contain a cask.") unless cask.is_a?(Cask) @@ -201,13 +205,15 @@ module Cask Homebrew::API::Cask.all_casks.key?(token) end - def initialize(token) + def initialize(token, from_json: nil) @token = token.delete_prefix("homebrew/cask/") @path = CaskLoader.default_path(token) + @from_json = from_json end def load(config:) - json_cask = Homebrew::API::Cask.all_casks[token] + json_cask = @from_json || Homebrew::API::Cask.all_casks[token] + cask_source = JSON.pretty_generate(json_cask) if (bottle_tag = ::Utils::Bottles.tag.to_s.presence) && (variations = json_cask["variations"].presence) && @@ -230,7 +236,7 @@ module Cask tap = Tap.fetch(json_cask[:tap]) if json_cask[:tap].to_s.include?("/") - Cask.new(token, tap: tap, source: cask_source, config: config) do + Cask.new(token, tap: tap, source: cask_source, config: config, loaded_from_api: true) do version json_cask[:version] if json_cask[:sha256] == "no_check" diff --git a/Library/Homebrew/cask/installer.rb b/Library/Homebrew/cask/installer.rb index 6f5c8d09e8..9075bb651a 100644 --- a/Library/Homebrew/cask/installer.rb +++ b/Library/Homebrew/cask/installer.rb @@ -386,7 +386,8 @@ module Cask return if @cask.source.blank? - (metadata_subdir/"#{@cask.token}.rb").write @cask.source + extension = @cask.loaded_from_api ? "json" : "rb" + (metadata_subdir/"#{@cask.token}.#{extension}").write @cask.source old_savedir&.rmtree end