From 0797a3e19e505cea3b9cf4db7295ae3e0bc37192 Mon Sep 17 00:00:00 2001 From: Mike McQuaid Date: Sat, 12 May 2018 10:42:07 -0400 Subject: [PATCH 1/4] formula: reorder to_hash. Let's reorder this to more closely match the ordering in a formula file. This makes `to_hash` (and our JSON output) more useful and easily readable for people used to formula files. --- Library/Homebrew/formula.rb | 48 +++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/Library/Homebrew/formula.rb b/Library/Homebrew/formula.rb index b1bd1484dd..c94d7aa2c8 100644 --- a/Library/Homebrew/formula.rb +++ b/Library/Homebrew/formula.rb @@ -1548,44 +1548,34 @@ class Formula hsh = { "name" => name, "full_name" => full_name, - "desc" => desc, - "homepage" => homepage, "oldname" => oldname, "aliases" => aliases.sort, + "desc" => desc, + "homepage" => homepage, "versions" => { "stable" => stable&.version&.to_s, - "bottle" => !bottle_specification.checksums.empty?, "devel" => devel&.version&.to_s, "head" => head&.version&.to_s, + "bottle" => !bottle_specification.checksums.empty?, }, "revision" => revision, "version_scheme" => version_scheme, + "bottle" => {}, + "keg_only" => keg_only?, + "options" => [], + "build_dependencies" => dependencies.select(&:build?).map(&:name).uniq, + "dependencies" => dependencies.map(&:name).uniq, + "recommended_dependencies" => dependencies.select(&:recommended?).map(&:name).uniq, + "optional_dependencies" => dependencies.select(&:optional?).map(&:name).uniq, + "requirements" => [], + "conflicts_with" => conflicts.map(&:name), + "caveats" => caveats, "installed" => [], "linked_keg" => linked_version&.to_s, "pinned" => pinned?, "outdated" => outdated?, - "keg_only" => keg_only?, - "dependencies" => dependencies.map(&:name).uniq, - "recommended_dependencies" => dependencies.select(&:recommended?).map(&:name).uniq, - "optional_dependencies" => dependencies.select(&:optional?).map(&:name).uniq, - "build_dependencies" => dependencies.select(&:build?).map(&:name).uniq, - "conflicts_with" => conflicts.map(&:name), - "caveats" => caveats, } - hsh["requirements"] = requirements.map do |req| - { - "name" => req.name, - "cask" => req.cask, - "download" => req.download, - } - end - - hsh["options"] = options.map do |opt| - { "option" => opt.flag, "description" => opt.description } - end - - hsh["bottle"] = {} %w[stable devel].each do |spec_sym| next unless spec = send(spec_sym) next unless spec.bottle_defined? @@ -1607,6 +1597,18 @@ class Formula hsh["bottle"][spec_sym] = bottle_info end + hsh["options"] = options.map do |opt| + { "option" => opt.flag, "description" => opt.description } + end + + hsh["requirements"] = requirements.map do |req| + { + "name" => req.name, + "cask" => req.cask, + "download" => req.download, + } + end + installed_kegs.each do |keg| tab = Tab.for_keg keg From 524379e89785865370dab5300a6e566bef0a0b81 Mon Sep 17 00:00:00 2001 From: Mike McQuaid Date: Sat, 12 May 2018 10:56:15 -0400 Subject: [PATCH 2/4] formula: improve dependencies hash output. It doesn't really make any sense for the `dependencies` hash to include all optional, recommended and build dependencies when these are already separately output. --- Library/Homebrew/formula.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Library/Homebrew/formula.rb b/Library/Homebrew/formula.rb index c94d7aa2c8..1d5a5b25d5 100644 --- a/Library/Homebrew/formula.rb +++ b/Library/Homebrew/formula.rb @@ -1564,7 +1564,7 @@ class Formula "keg_only" => keg_only?, "options" => [], "build_dependencies" => dependencies.select(&:build?).map(&:name).uniq, - "dependencies" => dependencies.map(&:name).uniq, + "dependencies" => dependencies.reject(&:optional?).reject(&:recommended?).reject(&:build?).map(&:name).uniq, "recommended_dependencies" => dependencies.select(&:recommended?).map(&:name).uniq, "optional_dependencies" => dependencies.select(&:optional?).map(&:name).uniq, "requirements" => [], From 840c97c1cce29cdb7c5e453deb17b2023b35ea42 Mon Sep 17 00:00:00 2001 From: Mike McQuaid Date: Sat, 12 May 2018 14:35:50 -0400 Subject: [PATCH 3/4] formula: add versioned formulae method. --- Library/Homebrew/dev-cmd/audit.rb | 6 ++-- Library/Homebrew/formula.rb | 9 +++++ Library/Homebrew/test/formula_spec.rb | 52 ++++++++++++++++++++++++++- 3 files changed, 62 insertions(+), 5 deletions(-) diff --git a/Library/Homebrew/dev-cmd/audit.rb b/Library/Homebrew/dev-cmd/audit.rb index 45c9561c62..f164a0fc60 100644 --- a/Library/Homebrew/dev-cmd/audit.rb +++ b/Library/Homebrew/dev-cmd/audit.rb @@ -251,11 +251,9 @@ module Homebrew problem "#{formula} is versioned but no #{unversioned_name} formula exists" end elsif ARGV.build_stable? && formula.stable? && - !(versioned_formulae = Dir[formula.path.to_s.gsub(/\.rb$/, "@*.rb")]).empty? + !(versioned_formulae = formula.versioned_formulae).empty? versioned_aliases = formula.aliases.grep(/.@\d/) - _, last_alias_version = - File.basename(versioned_formulae.sort.reverse.first) - .gsub(/\.rb$/, "").split("@") + _, last_alias_version = versioned_formulae.map(&:name).last.split("@") major, minor, = formula.version.to_s.split(".") alias_name_major = "#{formula.name}@#{major}" alias_name_major_minor = "#{alias_name_major}.#{minor}" diff --git a/Library/Homebrew/formula.rb b/Library/Homebrew/formula.rb index 1d5a5b25d5..fcc965fd18 100644 --- a/Library/Homebrew/formula.rb +++ b/Library/Homebrew/formula.rb @@ -370,6 +370,15 @@ class Formula name.include?("@") end + # Returns any `@`-versioned formulae for an non-`@`-versioned formula. + def versioned_formulae + return [] if versioned_formula? + + Pathname.glob(path.to_s.gsub(/\.rb$/, "@*.rb")).map do |path| + Formula[path.basename(".rb").to_s] + end.sort + end + # A named Resource for the currently active {SoftwareSpec}. # Additional downloads can be defined as {#resource}s. # {Resource#stage} will create a temporary directory and yield to a block. diff --git a/Library/Homebrew/test/formula_spec.rb b/Library/Homebrew/test/formula_spec.rb index 6ad102756e..da17a22fe4 100644 --- a/Library/Homebrew/test/formula_spec.rb +++ b/Library/Homebrew/test/formula_spec.rb @@ -107,6 +107,56 @@ describe Formula do end end + describe "#versioned_formula?" do + let(:f) do + formula "foo" do + url "foo-1.0" + end + end + + let(:f2) do + formula "foo@2.0" do + url "foo-2.0" + end + end + + it "returns true for @-versioned formulae" do + expect(f2.versioned_formula?).to be true + end + + it "returns false for non-@-versioned formulae" do + expect(f.versioned_formula?).to be false + end + end + + describe "#versioned_formulae" do + let(:f) do + formula "foo" do + url "foo-1.0" + end + end + + let(:f2) do + formula "foo@2.0" do + url "foo-2.0" + end + end + + it "returns true by default" do + FileUtils.touch f.path + FileUtils.touch f2.path + allow(Formulary).to receive(:load_formula_from_path).with(f2.name, f2.path).and_return(f2) + allow(Formulary).to receive(:factory).with(f2.name).and_return(f2) + expect(f.versioned_formulae).to eq [f2] + end + + it "returns empty array for non-@-versioned formulae" do + FileUtils.touch f.path + FileUtils.touch f2.path + expect(f2.versioned_formulae).to be_empty + end + end + example "installed alias with core" do f = formula do url "foo-1.0" @@ -708,7 +758,7 @@ describe Formula do expect(f3.runtime_dependencies.map(&:name)).to eq(["foo/bar/f1", "baz/qux/f2"]) end - it "includes non-declared direct dependencies", :focus do + it "includes non-declared direct dependencies" do formula = Class.new(Testball).new dependency = formula("dependency") { url "f-1.0" } From 11c7b08a345142775be8b9f647618920f7cb03ca Mon Sep 17 00:00:00 2001 From: Mike McQuaid Date: Sat, 12 May 2018 14:37:34 -0400 Subject: [PATCH 4/4] formula: add versioned_formulae to to_hash. --- Library/Homebrew/formula.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/Library/Homebrew/formula.rb b/Library/Homebrew/formula.rb index fcc965fd18..1f04ce54bd 100644 --- a/Library/Homebrew/formula.rb +++ b/Library/Homebrew/formula.rb @@ -1559,6 +1559,7 @@ class Formula "full_name" => full_name, "oldname" => oldname, "aliases" => aliases.sort, + "versioned_formulae" => versioned_formulae.map(&:name), "desc" => desc, "homepage" => homepage, "versions" => {