Sha256: 0d5130c2589e081cb3613a6ebc2e61162808b89fe65b866bde38b65fad968bd1

Contents?: true

Size: 1.22 KB

Versions: 1

Compression:

Stored size: 1.22 KB

Contents

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>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")
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
launch-agent-0.0.1 lib/launch_agent/daemon.rb