an wild Boxen::Service appears!

This could be cleaned up more, what with the com.boxen.*.plist
repetition. Later.
This commit is contained in:
Yossef Mendelssohn
2012-10-11 12:16:05 -04:00
parent ce4f86f35c
commit 329d4efe66
2 changed files with 72 additions and 0 deletions

44
lib/boxen/service.rb Normal file
View 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

View 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