From 157fed329c9eea24d4b52e9244d09eed499a4e83 Mon Sep 17 00:00:00 2001 From: Sean Molenaar Date: Tue, 4 May 2021 16:22:28 +0200 Subject: [PATCH] service: install linux service file --- Library/Homebrew/formula.rb | 12 +++++++++++ Library/Homebrew/formula_installer.rb | 16 +++++++++----- .../Homebrew/test/formula_installer_spec.rb | 21 ++++++++++++------- 3 files changed, 36 insertions(+), 13 deletions(-) diff --git a/Library/Homebrew/formula.rb b/Library/Homebrew/formula.rb index 71ddf5f128..5953b36097 100644 --- a/Library/Homebrew/formula.rb +++ b/Library/Homebrew/formula.rb @@ -962,12 +962,24 @@ class Formula "homebrew.mxcl.#{name}" end + # The generated service name. + sig { returns(String) } + def service_name + "homebrew.#{name}" + end + # The generated launchd {.plist} file path. sig { returns(Pathname) } def plist_path prefix/"#{plist_name}.plist" end + # The generated systemd {.service} file path. + sig { returns(Pathname) } + def systemd_service_path + prefix/"#{service_name}.service" + end + # The service specification of the software. def service return unless service? diff --git a/Library/Homebrew/formula_installer.rb b/Library/Homebrew/formula_installer.rb index eb0e538d34..b33601b826 100644 --- a/Library/Homebrew/formula_installer.rb +++ b/Library/Homebrew/formula_installer.rb @@ -1020,21 +1020,27 @@ class FormulaInstaller return end - plist = if formula.service? + if formula.service? + service_path = formula.systemd_service_path + service_path.atomic_write(formula.service.to_systemd_unit) + service_path.chmod 0644 + end + + service = if formula.service? formula.service.to_plist elsif formula.plist formula.plist end - return unless plist + return unless service plist_path = formula.plist_path - plist_path.atomic_write(plist) + plist_path.atomic_write(service) plist_path.chmod 0644 log = formula.var/"log" - log.mkpath if plist.include? log.to_s + log.mkpath if service.include? log.to_s rescue Exception => e # rubocop:disable Lint/RescueException - ofail "Failed to install plist file" + ofail "Failed to install service files" odebug e, e.backtrace end diff --git a/Library/Homebrew/test/formula_installer_spec.rb b/Library/Homebrew/test/formula_installer_spec.rb index 31a3a7c273..9ac8e38925 100644 --- a/Library/Homebrew/test/formula_installer_spec.rb +++ b/Library/Homebrew/test/formula_installer_spec.rb @@ -231,30 +231,34 @@ describe FormulaInstaller do installer = described_class.new(formula) expect { installer.install_service - }.not_to output(/Error: Failed to install plist file/).to_stderr + }.not_to output(/Error: Failed to install service files/).to_stderr expect(path).to exist end it "works if service is set" do formula = Testball.new - path = formula.plist_path + plist_path = formula.plist_path + service_path = formula.systemd_service_path service = Homebrew::Service.new(formula) formula.prefix.mkpath expect(formula).to receive(:plist).and_return(nil) - expect(formula).to receive(:service?).twice.and_return(true) - expect(formula).to receive(:service).and_return(service) + expect(formula).to receive(:service?).exactly(3).and_return(true) + expect(formula).to receive(:service).twice.and_return(service) expect(formula).to receive(:plist_path).and_call_original + expect(formula).to receive(:systemd_service_path).and_call_original expect(service).to receive(:to_plist).and_return("plist") + expect(service).to receive(:to_systemd_unit).and_return("unit") installer = described_class.new(formula) expect { installer.install_service - }.not_to output(/Error: Failed to install plist file/).to_stderr + }.not_to output(/Error: Failed to install service files/).to_stderr - expect(path).to exist + expect(plist_path).to exist + expect(service_path).to exist end it "returns without definition" do @@ -263,13 +267,14 @@ describe FormulaInstaller do formula.prefix.mkpath expect(formula).to receive(:plist).and_return(nil) - expect(formula).to receive(:service?).twice.and_return(nil) + expect(formula).to receive(:service?).exactly(3).and_return(nil) expect(formula).not_to receive(:plist_path) + expect(formula).not_to receive(:to_systemd_unit) installer = described_class.new(formula) expect { installer.install_service - }.not_to output(/Error: Failed to install plist file/).to_stderr + }.not_to output(/Error: Failed to install service files/).to_stderr expect(path).not_to exist end