Class: LaunchAgent

Inherits:
Object
  • Object
show all
Extended by:
CLI
Defined in:
lib/launch_agent.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CLI

chown_if_required, codesign_exists?, dry_run?, install_launch_agent?, no_colors?, non_interactive?, restore_xcode?, separator, uninstall_launch_agent?, unsign_xcode?

Constructor Details

#initialize(bin_path = nil) ⇒ LaunchAgent

Returns a new instance of LaunchAgent.



57
58
59
# File 'lib/launch_agent.rb', line 57

def initialize(bin_path = nil)
  self.bin_path = bin_path
end

Instance Attribute Details

#bin_pathObject

Returns the value of attribute bin_path.



4
5
6
# File 'lib/launch_agent.rb', line 4

def bin_path
  @bin_path
end

Class Method Details

.install(bin_path) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/launch_agent.rb', line 6

def self.install(bin_path)
  if !installed?
    LaunchAgent.new(File.expand_path(bin_path)).install
    success 'Installed! 🎉'
  else
    warning 'Launch agent is already installed!'
  end
end

.installed?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/launch_agent.rb', line 53

def self.installed?
  File.exist?(LaunchAgent.new.launch_agent_path)
end

.stale?Boolean

Returns:

  • (Boolean)


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/launch_agent.rb', line 33

def self.stale?
  if installed?
    path = LaunchAgent.new.launch_agent_path

    agent_xml = ''
    File.open(path, 'r') do |file|
      agent_xml = file.read
    end

    match = agent_xml.match(/update_xcode_plugins-(.*?)\//)
    installed_version = match ? match[1] : nil

    if installed_version && UpdateXcodePlugins::VERSION != installed_version
      return true
    end
  end

  false
end

.uninstallObject



15
16
17
18
19
20
21
22
# File 'lib/launch_agent.rb', line 15

def self.uninstall
  if installed?
    LaunchAgent.new.uninstall
    success 'Uninstalled! 🎉'
  else
    warning 'Launch agent is not installed!'
  end
end

.update_if_stale(bin_path) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/launch_agent.rb', line 24

def self.update_if_stale(bin_path)
  return unless stale?

  launch_agent = LaunchAgent.new(File.expand_path(bin_path))
  launch_agent.uninstall
  launch_agent.install
  success 'Updated launch agent.'
end

Instance Method Details

#identifierObject



75
76
77
# File 'lib/launch_agent.rb', line 75

def identifier
  'jp.mahdi.update_xcode_plugins'
end

#installObject



61
62
63
64
65
66
67
# File 'lib/launch_agent.rb', line 61

def install
  File.open(launch_agent_path, 'w') do |file|
    file.write(xml)
  end

  `launchctl load "#{launch_agent_path}"`
end

#launch_agent_pathObject



79
80
81
# File 'lib/launch_agent.rb', line 79

def launch_agent_path
  "#{Dir.home}/Library/LaunchAgents/#{identifier}.plist"
end

#uninstallObject



69
70
71
72
73
# File 'lib/launch_agent.rb', line 69

def uninstall
  `launchctl unload "#{launch_agent_path}"`

  File.delete(launch_agent_path) if File.exist?(launch_agent_path)
end

#watch_pathsObject



83
84
85
86
87
88
89
# File 'lib/launch_agent.rb', line 83

def watch_paths
  [
    '/Applications/Xcode.app',
    '/Applications/Xcode-beta.app',
    '~/Library/Application Support/Developer/Shared/Xcode/Plug-ins/'
  ]
end

#watch_paths_xmlObject



91
92
93
94
95
# File 'lib/launch_agent.rb', line 91

def watch_paths_xml
  watch_paths.map do |path|
    "<string>#{path}</string>"
  end.join("\n")
end

#xmlObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/launch_agent.rb', line 97

def xml
  "<?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>#{identifier}</string>
      <key>ProgramArguments</key>
      <array>
        <string>/usr/bin/env</string>
        <string>ruby</string>
        <string>#{bin_path}</string>
        <string>--no-colors</string>
        <string>--non-interactive</string>
      </array>
      <key>RunAtLoad</key>
      <false/>
      <key>StandardErrorPath</key>
      <string>/tmp/#{identifier}.err</string>
      <key>StandardOutPath</key>
      <string>/tmp/#{identifier}.out</string>
      <key>WatchPaths</key>
      <array>
        #{watch_paths_xml}
      </array>
    </dict>
  </plist>"
end