lib/launch_agent/daemon.rb in launch-agent-0.1.0 vs lib/launch_agent/daemon.rb in launch-agent-0.2.0

- old
+ new

@@ -1,67 +1,12 @@ module LaunchAgent - class Daemon - DOMAIN = 'com.buycheapviagraonlinenow' - - def initialize(*args) - @args = args - end - - def load - open(plist_filename, 'w') do |file| - file.write(plist_content) - end - - `launchctl load -w #{plist_filename}` - end - - def unload - `launchctl unload -w #{plist_filename}` - File.unlink(plist_filename) - end - - def loaded? - `launchctl list | grep #{job_id}` =~ /#{job_id}/ - end - - def plist_filename - File.expand_path('~/Library/LaunchAgents/' + job_id + '.plist') - end - - def plist_content - template = <<PLIST -<?xml version="1.0" encoding="UTF-8"?> -<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> -<plist version="1.0"> -<dict> - <key>KeepAlive</key> - <dict> - <key>SuccessfulExit</key> - <false/> - </dict> - <key>Label</key> - <string>%s</string> - <key>ProgramArguments</key> - <array> -%s - </array> - <key>RunAtLoad</key> - <true/> -</dict> -</plist> -PLIST - template % [job_id, xmlized_args] - end - - def job_id - DOMAIN + '.' + @args.inject([]) do |m, arg| - m << arg.gsub(/\W/, '_') - end.join('__') - end - - def xmlized_args - @args.inject([]) do |m, arg| - m << "\t\t<string>#{arg}</string>" - end.join("\n") + class Daemon < Base + def build_params + @params['Label'] = job_id + @params['KeepAlive'] = { + 'SuccessfulExit' => false + } + @params['ProgramArguments'] = @args + @params['RunAtLoad'] = true end end end