formula: change how #to_hash is defined for backwards compatibility

Now the output of commands like `brew info --json=` and
`brew generate-formula-api` should be the same as before along with
the additional files for the internal API. Before this commit the
hash key order had changed.
This commit is contained in:
apainintheneck 2024-02-03 13:21:32 -08:00 committed by Patrick Linnane
parent 3c503cdf56
commit 54b54b7e93
No known key found for this signature in database

View File

@ -2215,50 +2215,56 @@ class Formula
# @private # @private
def to_hash def to_hash
hsh = { hsh = {
"name" => name, "name" => name,
"full_name" => full_name, "full_name" => full_name,
"tap" => tap&.name, "tap" => tap&.name,
"oldname" => oldnames.first, # deprecated "oldname" => oldnames.first, # deprecated
"oldnames" => oldnames, "oldnames" => oldnames,
"aliases" => aliases.sort, "aliases" => aliases.sort,
"versioned_formulae" => versioned_formulae.map(&:name), "versioned_formulae" => versioned_formulae.map(&:name),
"desc" => desc, "desc" => desc,
"license" => SPDX.license_expression_to_string(license), "license" => SPDX.license_expression_to_string(license),
"homepage" => homepage, "homepage" => homepage,
"versions" => { "versions" => {
"stable" => stable&.version&.to_s, "stable" => stable&.version&.to_s,
"head" => head&.version&.to_s, "head" => head&.version&.to_s,
"bottle" => bottle_defined?, "bottle" => bottle_defined?,
}, },
"urls" => urls_hash, "urls" => urls_hash,
"revision" => revision, "revision" => revision,
"version_scheme" => version_scheme, "version_scheme" => version_scheme,
"bottle" => {}, "bottle" => {},
"pour_bottle_only_if" => self.class.pour_bottle_only_if&.to_s, "pour_bottle_only_if" => self.class.pour_bottle_only_if&.to_s,
"keg_only" => keg_only?, "keg_only" => keg_only?,
"keg_only_reason" => keg_only_reason&.to_hash, "keg_only_reason" => keg_only_reason&.to_hash,
"options" => [], "options" => [],
**dependencies_hash, "build_dependencies" => [],
"requirements" => serialized_requirements, "dependencies" => [],
"conflicts_with" => conflicts.map(&:name), "test_dependencies" => [],
"conflicts_with_reasons" => conflicts.map(&:reason), "recommended_dependencies" => [],
"link_overwrite" => self.class.link_overwrite_paths.to_a, "optional_dependencies" => [],
"caveats" => caveats_with_placeholders, "uses_from_macos" => [],
"installed" => [], "uses_from_macos_bounds" => [],
"linked_keg" => linked_version&.to_s, "requirements" => serialized_requirements,
"pinned" => pinned?, "conflicts_with" => conflicts.map(&:name),
"outdated" => outdated?, "conflicts_with_reasons" => conflicts.map(&:reason),
"deprecated" => deprecated?, "link_overwrite" => self.class.link_overwrite_paths.to_a,
"deprecation_date" => deprecation_date, "caveats" => caveats_with_placeholders,
"deprecation_reason" => deprecation_reason, "installed" => [],
"disabled" => disabled?, "linked_keg" => linked_version&.to_s,
"disable_date" => disable_date, "pinned" => pinned?,
"disable_reason" => disable_reason, "outdated" => outdated?,
"post_install_defined" => post_install_defined?, "deprecated" => deprecated?,
"service" => (service.to_hash if service?), "deprecation_date" => deprecation_date,
"tap_git_head" => tap_git_head, "deprecation_reason" => deprecation_reason,
"ruby_source_path" => ruby_source_path, "disabled" => disabled?,
"ruby_source_checksum" => {}, "disable_date" => disable_date,
"disable_reason" => disable_reason,
"post_install_defined" => post_install_defined?,
"service" => (service.to_hash if service?),
"tap_git_head" => tap_git_head,
"ruby_source_path" => ruby_source_path,
"ruby_source_checksum" => {},
} }
hsh["bottle"]["stable"] = bottle_hash if stable && bottle_defined? hsh["bottle"]["stable"] = bottle_hash if stable && bottle_defined?
@ -2267,6 +2273,8 @@ class Formula
{ "option" => opt.flag, "description" => opt.description } { "option" => opt.flag, "description" => opt.description }
end end
hsh.merge!(dependencies_hash)
hsh["installed"] = installed_kegs.sort_by(&:version).map do |keg| hsh["installed"] = installed_kegs.sort_by(&:version).map do |keg|
tab = Tab.for_keg keg tab = Tab.for_keg keg
{ {
@ -2307,7 +2315,7 @@ class Formula
.transform_values(&:presence) .transform_values(&:presence)
.compact .compact
api_hash.merge(dep_hash) api_hash.merge!(dep_hash)
# Exclude default values. # Exclude default values.
api_hash["revision"] = revision unless revision.zero? api_hash["revision"] = revision unless revision.zero?