Class: Lunchy

Inherits:
Object
  • Object
show all
Defined in:
lib/lunchy.rb

Constant Summary collapse

VERSION =
'0.10.4'

Instance Method Summary collapse

Instance Method Details

#edit(params) ⇒ Object

Raises:

  • (ArgumentError)

93
94
95
96
97
98
99
100
101
102
103
104
# 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) ⇒ Object

Raises:

  • (ArgumentError)

55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# 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

#ls(params) ⇒ Object Also known as: list


43
44
45
46
47
48
49
50
51
52
# 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

#restart(params) ⇒ Object


24
25
26
27
# File 'lib/lunchy.rb', line 24

def restart(params)
  stop(params.dup)
  start(params.dup)
end

#show(params) ⇒ Object

Raises:

  • (ArgumentError)

85
86
87
88
89
90
91
# 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) ⇒ Object

Raises:

  • (ArgumentError)

6
7
8
9
10
11
12
13
# 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) ⇒ Object


29
30
31
32
33
34
35
36
37
38
39
40
41
# 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) ⇒ Object

Raises:

  • (ArgumentError)

15
16
17
18
19
20
21
22
# 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) ⇒ Object Also known as: rm

Raises:

  • (ArgumentError)

71
72
73
74
75
76
77
78
79
80
81
82
# 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