class Lunchy

Constants

VERSION

Public Instance Methods

edit(params) click to toggle source
# File lib/lunchy.rb, line 93
def edit(params)
  raise ArgumentError, "edit [name]" if params.empty?

  with_match params[0] do |_, path|
    editor = ENV['EDITOR']
    if editor.nil?
      raise 'EDITOR environment variable is not set'
    else
      execute("#{editor} #{path.inspect} > `tty`")
    end
  end
end
install(params) click to toggle source
# File lib/lunchy.rb, line 55
def install(params)
  raise ArgumentError, "install [-s] [file]" if params.empty?
  filename = params[0]
  %w(~/Library/LaunchAgents /Library/LaunchAgents).each do |dir|
    if File.exist?(File.expand_path(dir))
      if symlink
        FileUtils.ln_s filename, File.join(File.expand_path(dir), File.basename(filename)), force: true
        return puts "#{filename} installed to #{dir}"
      else
        FileUtils.cp filename, File.join(File.expand_path(dir), File.basename(filename))
        return puts "#{filename} installed to #{dir}"
      end
    end
  end
end
list(params)
Alias for: ls
ls(params) click to toggle source
# File lib/lunchy.rb, line 43
def ls(params)
  pattern = pattern_regex params[0]
  agents = plists.keys
  agents = agents.grep(pattern) if !params.empty?
  if long
    puts agents.map { |agent| plists[agent] }.sort.join("\n")
  else
    puts agents.sort.join("\n")
  end
end
Also aliased as: list
restart(params) click to toggle source
# File lib/lunchy.rb, line 24
def restart(params)
  stop(params.dup)
  start(params.dup)
end
rm(params)
Alias for: uninstall
show(params) click to toggle source
# File lib/lunchy.rb, line 85
def show(params)
  raise ArgumentError, "show [name]" if params.empty?

  with_match params[0] do |_, path|
    puts IO.read(path)
  end
end
start(params) click to toggle source
# File lib/lunchy.rb, line 6
def start(params)
  raise ArgumentError, "start [-wF] [name]" if params.empty?

  with_match params[0] do |name, path|
    execute("launchctl load #{force}#{write}#{path.inspect}")
    puts "started #{name}"
  end
end
status(params) click to toggle source
# File lib/lunchy.rb, line 29
def status(params)
  pattern = pattern_for_grep params[0]
  cmd = "launchctl list"
  unless verbose?
    agents = plists.keys.map { |k| "-e \"#{k}\"" }.join(" ")
    cmd << " | grep -i #{agents}"
  end

  cmd.gsub!('.','\.')
  cmd << " | grep -i \"#{pattern}\"" if pattern
  print "PID\tStatus\tLabel\n"
  execute(cmd)
end
stop(params) click to toggle source
# File lib/lunchy.rb, line 15
def stop(params)
  raise ArgumentError, "stop [-w] [name]" if params.empty?

  with_match params[0] do |name, path|
    execute("launchctl unload #{write}#{path.inspect}")
    puts "stopped #{name}"
  end
end
uninstall(params) click to toggle source
# File lib/lunchy.rb, line 71
def uninstall(params)
  raise ArgumentError, "uninstall [name]" if params.empty?

  stop(params.dup)

  with_match params[0] do |name, path|
    if File.exist?(path)
      FileUtils.rm(path)
      puts "uninstalled #{name}"
    end
  end
end
Also aliased as: rm

Private Instance Methods

dirs() click to toggle source
# File lib/lunchy.rb, line 167
def dirs
  result = %w(/Library/LaunchAgents ~/Library/LaunchAgents)
  result.push('/Library/LaunchDaemons', '/System/Library/LaunchDaemons') if root?
  result
end
exact() click to toggle source
# File lib/lunchy.rb, line 108
def exact
  CONFIG[:exact]
end
execute(cmd) click to toggle source
# File lib/lunchy.rb, line 149
def execute(cmd)
  puts "Executing: #{cmd}" if verbose?
  emitted = `#{cmd}`
  puts emitted unless emitted.empty?
end
force() click to toggle source
# File lib/lunchy.rb, line 112
def force
  CONFIG[:force] and '-F '
end
long() click to toggle source
# File lib/lunchy.rb, line 120
def long
  CONFIG[:long]
end
pattern_for_grep(s) click to toggle source
# File lib/lunchy.rb, line 128
def pattern_for_grep(s)
  exact ? "^#{s}$" : s if s
end
pattern_regex(s) click to toggle source
# File lib/lunchy.rb, line 132
def pattern_regex(s)
  /#{pattern_for_grep(s)}/i
end
plists() click to toggle source
# File lib/lunchy.rb, line 155
def plists
  @plists ||= begin
    plists = {}
    dirs.each do |dir|
      Dir["#{File.expand_path(dir)}/*.plist"].inject(plists) do |memo, filename|
        memo[File.basename(filename, ".plist")] = filename; memo
      end
    end
    plists
  end
end
root?() click to toggle source
# File lib/lunchy.rb, line 173
def root?
  Process.euid == 0
end
verbose?() click to toggle source
# File lib/lunchy.rb, line 177
def verbose?
  CONFIG[:verbose]
end
with_match(name) { |*to_a.first| ... } click to toggle source
# File lib/lunchy.rb, line 136
def with_match(name)
  files = plists.select {|k,_| k =~ pattern_regex(name) }
  files = Hash[files] if files.is_a?(Array) # ruby 1.8

  if files.size > 1
    puts "Multiple daemons found matching '#{name}'. You need to be more specific. Matches found are:\n#{files.keys.join("\n")}"
  elsif files.empty?
    puts "No daemon found matching '#{name}' #{exact ? 'exactly' : nil}" if name
  else
    yield(*files.to_a.first)
  end
end
write() click to toggle source
# File lib/lunchy.rb, line 116
def write
  CONFIG[:write] and '-w '
end