Skip to content

Commit

Permalink
formula: change how #to_hash is defined for backwards compatibility
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
apainintheneck authored and p-linnane committed Feb 4, 2024
1 parent 3c503cd commit 54b54b7
Showing 1 changed file with 49 additions and 41 deletions.
90 changes: 49 additions & 41 deletions Library/Homebrew/formula.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2215,50 +2215,56 @@ def merge_spec_dependables(dependables)
# @private
def to_hash
hsh = {
"name" => name,
"full_name" => full_name,
"tap" => tap&.name,
"oldname" => oldnames.first, # deprecated
"oldnames" => oldnames,
"aliases" => aliases.sort,
"versioned_formulae" => versioned_formulae.map(&:name),
"desc" => desc,
"license" => SPDX.license_expression_to_string(license),
"homepage" => homepage,
"versions" => {
"name" => name,
"full_name" => full_name,
"tap" => tap&.name,
"oldname" => oldnames.first, # deprecated
"oldnames" => oldnames,
"aliases" => aliases.sort,
"versioned_formulae" => versioned_formulae.map(&:name),
"desc" => desc,
"license" => SPDX.license_expression_to_string(license),
"homepage" => homepage,
"versions" => {
"stable" => stable&.version&.to_s,
"head" => head&.version&.to_s,
"bottle" => bottle_defined?,
},
"urls" => urls_hash,
"revision" => revision,
"version_scheme" => version_scheme,
"bottle" => {},
"pour_bottle_only_if" => self.class.pour_bottle_only_if&.to_s,
"keg_only" => keg_only?,
"keg_only_reason" => keg_only_reason&.to_hash,
"options" => [],
**dependencies_hash,
"requirements" => serialized_requirements,
"conflicts_with" => conflicts.map(&:name),
"conflicts_with_reasons" => conflicts.map(&:reason),
"link_overwrite" => self.class.link_overwrite_paths.to_a,
"caveats" => caveats_with_placeholders,
"installed" => [],
"linked_keg" => linked_version&.to_s,
"pinned" => pinned?,
"outdated" => outdated?,
"deprecated" => deprecated?,
"deprecation_date" => deprecation_date,
"deprecation_reason" => deprecation_reason,
"disabled" => disabled?,
"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" => {},
"urls" => urls_hash,
"revision" => revision,
"version_scheme" => version_scheme,
"bottle" => {},
"pour_bottle_only_if" => self.class.pour_bottle_only_if&.to_s,
"keg_only" => keg_only?,
"keg_only_reason" => keg_only_reason&.to_hash,
"options" => [],
"build_dependencies" => [],
"dependencies" => [],
"test_dependencies" => [],
"recommended_dependencies" => [],
"optional_dependencies" => [],
"uses_from_macos" => [],
"uses_from_macos_bounds" => [],
"requirements" => serialized_requirements,
"conflicts_with" => conflicts.map(&:name),
"conflicts_with_reasons" => conflicts.map(&:reason),
"link_overwrite" => self.class.link_overwrite_paths.to_a,
"caveats" => caveats_with_placeholders,
"installed" => [],
"linked_keg" => linked_version&.to_s,
"pinned" => pinned?,
"outdated" => outdated?,
"deprecated" => deprecated?,
"deprecation_date" => deprecation_date,
"deprecation_reason" => deprecation_reason,
"disabled" => disabled?,
"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?
Expand All @@ -2267,6 +2273,8 @@ def to_hash
{ "option" => opt.flag, "description" => opt.description }
end

hsh.merge!(dependencies_hash)

hsh["installed"] = installed_kegs.sort_by(&:version).map do |keg|
tab = Tab.for_keg keg
{
Expand Down Expand Up @@ -2307,7 +2315,7 @@ def to_api_hash
.transform_values(&:presence)
.compact

api_hash.merge(dep_hash)
api_hash.merge!(dep_hash)

# Exclude default values.
api_hash["revision"] = revision unless revision.zero?
Expand Down

0 comments on commit 54b54b7

Please sign in to comment.