Class: Henchman::LaunchdHandler

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

Class Method Summary collapse

Class Method Details

.internet_connection?Boolean

Returns:

  • (Boolean)


62
63
64
65
66
67
68
# File 'lib/launchd_handler.rb', line 62

def self.internet_connection?
  begin
    true if open("https://www.dropbox.com/")
  rescue
    false
  end
end

.start(args) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/launchd_handler.rb', line 7

def self.start args
  if !internet_connection?
    puts "No internet connection detected - unable to verify correct configuration."
    return if !agree("Launch henchman anyways? (y/n) ")
  else
    puts "Checking configuration"
    return if !Henchman.connect
  end

  puts "Creating agents"
  plist_main       = Henchman::Templates.plist_main
  plist_path_main  = File.expand_path("~/Library/LaunchAgents/com.henchman.plist")
  plist_clean      = Henchman::Templates.plist_clean
  plist_path_clean = File.expand_path("~/Library/LaunchAgents/com.henchman.clean.plist")
  shell_script_path_main  = File.expand_path("~/.henchman/run.sh")
  shell_script_path_clean = File.expand_path("~/.henchman/clean.sh")
  cache_path        = File.expand_path("~/.henchman/cache")
  File.write(plist_path_main, plist_main)
  File.write(shell_script_path_main, Henchman::Templates.shell_script('run', args))
  File.write(plist_path_clean, plist_clean)
  File.write(shell_script_path_clean, Henchman::Templates.shell_script('clean'))
  File.open(cache_path, "w") { |f| f.write( Henchman::Templates.cache.to_yaml ) }

  puts "Launching agent"
  `chmod +x #{shell_script_path_main}`
  `chmod +x #{shell_script_path_clean}`
  `launchctl load #{plist_path_main}`
  `launchctl load #{plist_path_clean}`

  puts "Launched successfully! You are now running henchman."
  open(File.expand_path("~/.henchman/stdout.log"), 'a') do |f|
    f.puts "#{DateTime.now.strftime('%m-%d-%Y %H:%M:%S')}|Henchman Started"
  end
end

.stopObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/launchd_handler.rb', line 42

def self.stop
  puts "Stopping agents"
  plist_path_main = File.expand_path("~/Library/LaunchAgents/com.henchman.plist")
  plist_path_clean = File.expand_path("~/Library/LaunchAgents/com.henchman.clean.plist")
  `launchctl unload #{plist_path_main} 2> /dev/null`
  `launchctl unload #{plist_path_clean} 2> /dev/null`
  `rm #{plist_path_main} 2> /dev/null`
  `rm #{plist_path_clean} 2> /dev/null`
  `rm #{File.expand_path("~/.henchman/run.sh")} 2> /dev/null`
  `rm #{File.expand_path("~/.henchman/clean.sh")} 2> /dev/null`
  `rm #{File.expand_path("~/.henchman/cache")} 2> /dev/null`
  # `rm #{File.expand_path("~/.henchman/stdout.log")} 2> /dev/null`
  # `rm #{File.expand_path("~/.henchman/stderr.log")} 2> /dev/null`

  puts "Successfully stopped henchman"
  open(File.expand_path("~/.henchman/stdout.log"), 'a') do |f|
    f.puts "#{DateTime.now.strftime('%m-%d-%Y %H:%M:%S')}|Henchman Stopped"
  end
end