diff --git a/Library/Homebrew/extend/os/formula.rb b/Library/Homebrew/extend/os/formula.rb new file mode 100644 index 0000000000000..fe06aea8c563d --- /dev/null +++ b/Library/Homebrew/extend/os/formula.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +if OS.mac? + require "extend/os/mac/formula" +elsif OS.linux? + require "extend/os/linux/formula" +end diff --git a/Library/Homebrew/extend/os/linux/formula.rb b/Library/Homebrew/extend/os/linux/formula.rb new file mode 100644 index 0000000000000..97d7bf2519b47 --- /dev/null +++ b/Library/Homebrew/extend/os/linux/formula.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +class Formula + class << self + undef on_linux + + def on_linux(&_block) + yield + end + end +end diff --git a/Library/Homebrew/extend/os/mac/formula.rb b/Library/Homebrew/extend/os/mac/formula.rb new file mode 100644 index 0000000000000..dba823599dc40 --- /dev/null +++ b/Library/Homebrew/extend/os/mac/formula.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +class Formula + class << self + undef on_macos + + def on_macos(&_block) + yield + end + end +end diff --git a/Library/Homebrew/formula.rb b/Library/Homebrew/formula.rb index 53f8377fdf7c6..68aa4138a6259 100644 --- a/Library/Homebrew/formula.rb +++ b/Library/Homebrew/formula.rb @@ -2423,10 +2423,25 @@ def depends_on(dep) specs.each { |spec| spec.depends_on(dep) } end + # Indicates use of dependencies provided by macOS. + # On macOS this is a no-op (as we use the system libraries there). + # On Linux this will act as `depends_on`. def uses_from_macos(dep) specs.each { |spec| spec.uses_from_macos(dep) } end + # Block executed only executed on macOS. No-op on Linux. + #
on_macos do + # depends_on "mac_only_dep" + # end+ def on_macos(&_block); end + + # Block executed only executed on Linux. No-op on macOS. + #
on_linux do + # depends_on "linux_only_dep" + # end+ def on_linux(&_block); end + # @!attribute [w] option # Options can be used as arguments to `brew install`. # To switch features on/off: `"with-something"` or `"with-otherthing"`. @@ -2657,3 +2672,5 @@ def link_overwrite_paths end end end + +require "extend/os/formula" diff --git a/Library/Homebrew/test/os/linux/formula_spec.rb b/Library/Homebrew/test/os/linux/formula_spec.rb index dcfcae9a039ca..7e7acfdea2d57 100644 --- a/Library/Homebrew/test/os/linux/formula_spec.rb +++ b/Library/Homebrew/test/os/linux/formula_spec.rb @@ -20,4 +20,82 @@ expect(f.class.head.deps.first.name).to eq("foo") end end + + describe "#on_linux" do + it "defines an url on Linux only" do + f = formula do + homepage "https://brew.sh" + + on_macos do + url "https://brew.sh/test-macos-0.1.tbz" + sha256 TEST_SHA256 + end + + on_linux do + url "https://brew.sh/test-linux-0.1.tbz" + sha256 TEST_SHA256 + end + end + + expect(f.stable.url).to eq("https://brew.sh/test-linux-0.1.tbz") + end + end + + describe "#on_linux" do + it "adds a dependency on Linux only" do + f = formula do + homepage "https://brew.sh" + + url "https://brew.sh/test-0.1.tbz" + sha256 TEST_SHA256 + + depends_on "hello_both" + + on_macos do + depends_on "hello_macos" + end + + on_linux do + depends_on "hello_linux" + end + end + + expect(f.class.stable.deps[0].name).to eq("hello_both") + expect(f.class.stable.deps[1].name).to eq("hello_linux") + expect(f.class.stable.deps[2]).to eq(nil) + end + end + + describe "#on_linux" do + it "adds a patch on Linux only" do + f = formula do + homepage "https://brew.sh" + + url "https://brew.sh/test-0.1.tbz" + sha256 TEST_SHA256 + + patch do + url "patch_both" + end + + on_macos do + patch do + url "patch_macos" + end + end + + on_linux do + patch do + url "patch_linux" + end + end + end + + expect(f.patchlist.length).to eq(2) + expect(f.patchlist.first.strip).to eq(:p1) + expect(f.patchlist.first.url).to eq("patch_both") + expect(f.patchlist.second.strip).to eq(:p1) + expect(f.patchlist.second.url).to eq("patch_linux") + end + end end diff --git a/Library/Homebrew/test/os/mac/formula_spec.rb b/Library/Homebrew/test/os/mac/formula_spec.rb new file mode 100644 index 0000000000000..289703cfddee2 --- /dev/null +++ b/Library/Homebrew/test/os/mac/formula_spec.rb @@ -0,0 +1,83 @@ +# frozen_string_literal: true + +require "formula" + +describe Formula do + describe "#on_macos" do + it "defines an url on macos only" do + f = formula do + homepage "https://brew.sh" + + on_macos do + url "https://brew.sh/test-macos-0.1.tbz" + sha256 TEST_SHA256 + end + + on_linux do + url "https://brew.sh/test-linux-0.1.tbz" + sha256 TEST_SHA256 + end + end + + expect(f.stable.url).to eq("https://brew.sh/test-macos-0.1.tbz") + end + end + + describe "#on_macos" do + it "adds a dependency on macos only" do + f = formula do + homepage "https://brew.sh" + + url "https://brew.sh/test-0.1.tbz" + sha256 TEST_SHA256 + + depends_on "hello_both" + + on_macos do + depends_on "hello_macos" + end + + on_linux do + depends_on "hello_linux" + end + end + + expect(f.class.stable.deps[0].name).to eq("hello_both") + expect(f.class.stable.deps[1].name).to eq("hello_macos") + expect(f.class.stable.deps[2]).to eq(nil) + end + end + + describe "#on_macos" do + it "adds a patch on macos only" do + f = formula do + homepage "https://brew.sh" + + url "https://brew.sh/test-0.1.tbz" + sha256 TEST_SHA256 + + patch do + url "patch_both" + end + + on_macos do + patch do + url "patch_macos" + end + end + + on_linux do + patch do + url "patch_linux" + end + end + end + + expect(f.patchlist.length).to eq(2) + expect(f.patchlist.first.strip).to eq(:p1) + expect(f.patchlist.first.url).to eq("patch_both") + expect(f.patchlist.second.strip).to eq(:p1) + expect(f.patchlist.second.url).to eq("patch_macos") + end + end +end