mirror of
https://github.com/zhigang1992/boxen.git
synced 2026-04-29 04:24:52 +08:00
an wild Boxen::Service appears!
This could be cleaned up more, what with the com.boxen.*.plist repetition. Later.
This commit is contained in:
44
lib/boxen/service.rb
Normal file
44
lib/boxen/service.rb
Normal file
@@ -0,0 +1,44 @@
|
||||
require "boxen/util"
|
||||
|
||||
module Boxen
|
||||
class Service
|
||||
attr_reader :name
|
||||
|
||||
def self.list
|
||||
files.collect do |service|
|
||||
new(human_name(service))
|
||||
end
|
||||
end
|
||||
|
||||
def initialize(name)
|
||||
@name = name
|
||||
end
|
||||
|
||||
def enable
|
||||
Boxen::Util.sudo('/bin/launchctl', 'load', '-w', location)
|
||||
end
|
||||
|
||||
def disable
|
||||
Boxen::Util.sudo('/bin/launchctl', 'unload', '-w', location)
|
||||
end
|
||||
|
||||
|
||||
private
|
||||
|
||||
def location
|
||||
"#{self.class.location}/com.boxen.#{name}.plist"
|
||||
end
|
||||
|
||||
def self.location
|
||||
"/Library/LaunchDaemons"
|
||||
end
|
||||
|
||||
def self.files
|
||||
Dir["#{location}/com.boxen.*.plist"]
|
||||
end
|
||||
|
||||
def self.human_name(service)
|
||||
service.match(/com\.boxen\.(.+)\.plist$/)[1]
|
||||
end
|
||||
end
|
||||
end
|
||||
28
test/boxen_service_test.rb
Normal file
28
test/boxen_service_test.rb
Normal file
@@ -0,0 +1,28 @@
|
||||
require "boxen/test"
|
||||
require "boxen/service"
|
||||
|
||||
class BoxenServiceTest < Boxen::Test
|
||||
def test_list
|
||||
Dir.expects(:[]).with("/Library/LaunchDaemons/com.boxen.*.plist").returns([
|
||||
"/Library/LaunchDaemons/com.boxen.test.plist",
|
||||
"/Library/LaunchDaemons/com.boxen.other.plist"
|
||||
])
|
||||
|
||||
services = Boxen::Service.list
|
||||
assert_equal ['other', 'test'], services.collect(&:name).sort
|
||||
end
|
||||
|
||||
def test_enable
|
||||
service = Boxen::Service.new('blip')
|
||||
Boxen::Util.expects(:sudo).with('/bin/launchctl', 'load', '-w',
|
||||
'/Library/LaunchDaemons/com.boxen.blip.plist')
|
||||
service.enable
|
||||
end
|
||||
|
||||
def test_disable
|
||||
service = Boxen::Service.new('thing')
|
||||
Boxen::Util.expects(:sudo).with('/bin/launchctl', 'unload', '-w',
|
||||
'/Library/LaunchDaemons/com.boxen.thing.plist')
|
||||
service.disable
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user