From d477d1663acaee53ec73acf7e4c3a1b2fd256878 Mon Sep 17 00:00:00 2001 From: Sean Molenaar Date: Sun, 6 Nov 2022 13:52:53 +0100 Subject: [PATCH 1/3] Service: add method to define a root requirement --- Library/Homebrew/formula.rb | 14 +++++++++++++- Library/Homebrew/service.rb | 20 ++++++++++++++++++++ Library/Homebrew/test/formula_spec.rb | 2 +- Library/Homebrew/test/service_spec.rb | 25 +++++++++++++++++++++++++ 4 files changed, 59 insertions(+), 2 deletions(-) diff --git a/Library/Homebrew/formula.rb b/Library/Homebrew/formula.rb index b5e716082e..36927900e6 100644 --- a/Library/Homebrew/formula.rb +++ b/Library/Homebrew/formula.rb @@ -995,6 +995,8 @@ class Formula # # EOS # end + # + # @deprecated Please use {#service} instead def plist nil end @@ -1014,6 +1016,13 @@ class Formula # The generated launchd {.plist} file path. sig { returns(Pathname) } def plist_path + odeprecated "formula.plist_path", "formula.launchd_service_path" + launchd_service_path + end + + # The generated systemd {.service} file path. + sig { returns(Pathname) } + def launchd_service_path opt_prefix/"#{plist_name}.plist" end @@ -3121,7 +3130,10 @@ class Formula # # Or perhaps you'd like to give the user a choice? Ooh fancy. #
plist_options startup: true, manual: "foo start"
+ # + # @deprecated Please use {#service.require_root} instead def plist_options(options) + odeprecated "plist_options", "service.require_root" @plist_startup = options[:startup] @plist_manual = options[:manual] end @@ -3259,7 +3271,7 @@ class Formula # Service can be used to define services. # This method evaluates the DSL specified in the service block of the # {Formula} (if it exists) and sets the instance variables of a Service - # object accordingly. This is used by `brew install` to generate a plist. + # object accordingly. This is used by `brew install` to generate a service file. # #
service do
     #   run [opt_bin/"foo"]
diff --git a/Library/Homebrew/service.rb b/Library/Homebrew/service.rb
index 50e5652604..31d6d21100 100644
--- a/Library/Homebrew/service.rb
+++ b/Library/Homebrew/service.rb
@@ -129,6 +129,26 @@ module Homebrew
       end
     end
 
+    sig { params(value: T.nilable(T::Boolean)).returns(T.nilable(T::Boolean)) }
+    def require_root(value = nil)
+      case T.unsafe(value)
+      when nil
+        @require_root
+      when true, false
+        @require_root = value
+      else
+        raise TypeError, "Service#require_root expects a Boolean"
+      end
+    end
+
+    # Returns a `Boolean` describing if a service requires root access.
+    # @return [Boolean]
+    sig { returns(T::Boolean) }
+    def requires_root?
+      instance_eval(&@service_block)
+      @require_root.present? && @require_root == true
+    end
+
     sig { params(value: T.nilable(String)).returns(T.nilable(T::Hash[Symbol, String])) }
     def sockets(value = nil)
       case T.unsafe(value)
diff --git a/Library/Homebrew/test/formula_spec.rb b/Library/Homebrew/test/formula_spec.rb
index 6c8a74c93c..0c7b2ee5d9 100644
--- a/Library/Homebrew/test/formula_spec.rb
+++ b/Library/Homebrew/test/formula_spec.rb
@@ -733,7 +733,7 @@ describe Formula do
 
       expect(f.plist_name).to eq("homebrew.mxcl.formula_name")
       expect(f.service_name).to eq("homebrew.formula_name")
-      expect(f.plist_path).to eq(HOMEBREW_PREFIX/"opt/formula_name/homebrew.mxcl.formula_name.plist")
+      expect(f.launchd_service_path).to eq(HOMEBREW_PREFIX/"opt/formula_name/homebrew.mxcl.formula_name.plist")
       expect(f.systemd_service_path).to eq(HOMEBREW_PREFIX/"opt/formula_name/homebrew.formula_name.service")
       expect(f.systemd_timer_path).to eq(HOMEBREW_PREFIX/"opt/formula_name/homebrew.formula_name.timer")
     end
diff --git a/Library/Homebrew/test/service_spec.rb b/Library/Homebrew/test/service_spec.rb
index f5c68d3c85..b55df5b32b 100644
--- a/Library/Homebrew/test/service_spec.rb
+++ b/Library/Homebrew/test/service_spec.rb
@@ -64,6 +64,29 @@ describe Homebrew::Service do
     end
   end
 
+  describe "#requires_root?" do
+    it "returns status when set" do
+      f = stub_formula do
+        service do
+          run opt_bin/"beanstalkd"
+          require_root true
+        end
+      end
+
+      expect(f.service.requires_root?).to be(true)
+    end
+
+    it "returns status when not set" do
+      f = stub_formula do
+        service do
+          run opt_bin/"beanstalkd"
+        end
+      end
+
+      expect(f.service.requires_root?).to be(false)
+    end
+  end
+
   describe "#run_type" do
     it "throws for unexpected type" do
       f = stub_formula do
@@ -166,6 +189,7 @@ describe Homebrew::Service do
           error_log_path var/"log/beanstalkd.error.log"
           log_path var/"log/beanstalkd.log"
           input_path var/"in/beanstalkd"
+          require_root true
           root_dir var
           working_dir var
           keep_alive true
@@ -539,6 +563,7 @@ describe Homebrew::Service do
           error_log_path var/"log/beanstalkd.error.log"
           log_path var/"log/beanstalkd.log"
           input_path var/"in/beanstalkd"
+          require_root true
           root_dir var
           working_dir var
           keep_alive true

From a23d969df80befa69775c1a8239ec1a5bf9ffc25 Mon Sep 17 00:00:00 2001
From: Sean Molenaar 
Date: Tue, 8 Nov 2022 09:06:40 +0100
Subject: [PATCH 2/3] formula: comment out deprecation

---
 Library/Homebrew/formula.rb | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/Library/Homebrew/formula.rb b/Library/Homebrew/formula.rb
index 36927900e6..8e20cb5f99 100644
--- a/Library/Homebrew/formula.rb
+++ b/Library/Homebrew/formula.rb
@@ -1016,7 +1016,8 @@ class Formula
   # The generated launchd {.plist} file path.
   sig { returns(Pathname) }
   def plist_path
-    odeprecated "formula.plist_path", "formula.launchd_service_path"
+    # TODO: Add deprecation
+    # odeprecated "formula.plist_path", "formula.launchd_service_path"
     launchd_service_path
   end
 

From fb9cc17c43636a25b601d8aca2743fac04febac6 Mon Sep 17 00:00:00 2001
From: Sean Molenaar 
Date: Tue, 8 Nov 2022 09:07:24 +0100
Subject: [PATCH 3/3] formula.rb: add deprecation

---
 Library/Homebrew/formula.rb | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/Library/Homebrew/formula.rb b/Library/Homebrew/formula.rb
index 8e20cb5f99..299e338c20 100644
--- a/Library/Homebrew/formula.rb
+++ b/Library/Homebrew/formula.rb
@@ -3134,7 +3134,8 @@ class Formula
     #
     # @deprecated Please use {#service.require_root} instead
     def plist_options(options)
-      odeprecated "plist_options", "service.require_root"
+      # TODO: Deprecate
+      # odeprecated "plist_options", "service.require_root"
       @plist_startup = options[:startup]
       @plist_manual = options[:manual]
     end