Module: LaunchDr::Task

Defined in:
lib/launchdr/task.rb

Class Method Summary collapse

Class Method Details

.new(name = :launchd, opts = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/launchdr/task.rb', line 6

def self.new name = :launchd, opts = {}
  raise "`LaunchDr.task`'s options must include the name of the binary for your gem!" unless opts[:bin]
  opts = {:desc => 'Creates a launchd property list for this gem', :arguments => []}.merge opts

  desc opts[:desc] unless opts[:no_desc]
  task name do
    path = File.expand_path File.join('', opts[:bin])
    unless File.file? path
      path = File.expand_path File.join(Gem::default_bindir, opts[:bin])
      unless File.file? path
        path = File.expand_path %x[which "#{opts[:bin]}"].chomp
      end
    end
    unless File.file? path
      raise "** Unable to locate binary #{opts[:bin]}"
    end
    LaunchDr "rb.launchdr.#{opts[:bin]}" do |plist|
      plist[:program_arguments] = [path, opts[:arguments]].flatten
      plist[:keep_alive] = true
      plist[:run_at_load] = true
    end
    puts "** `#{[path, opts[:arguments]].flatten.join(' ')}` will now be run on startup!"
  end

end