Merge pull request #11316 from SMillerDev/feature/linux/write_unit

This commit is contained in:
Sean Molenaar 2021-05-05 17:10:43 +02:00 committed by GitHub
commit 42ffee75a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 13 deletions

View File

@ -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?

View File

@ -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

View File

@ -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