launch-agent 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -14,6 +14,10 @@ A library to use launchd easily
14
14
  # unload
15
15
  agent.unload
16
16
 
17
+ == CLI
18
+
19
+ $ launchagent -h
20
+
17
21
  == Note on Patches/Pull Requests
18
22
 
19
23
  * Fork the project.
@@ -0,0 +1,33 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'git-style-binary/command'
4
+
5
+ GitStyleBinary.command do
6
+ short_desc "load/unload a periodic agent"
7
+ banner <<-EOS
8
+ Usage: #{command.full_name} #{all_options_string} {full command}
9
+
10
+ Load/Unload a periodic launchd agent
11
+
12
+
13
+ EOS
14
+ opt :interval, "causes the job to be started every N seconds", :required => true, :type => Integer
15
+ opt :env, "additional environmental variables to be set before running the job. can specify multiple times", :type => String, :multi => true
16
+
17
+ run do |command|
18
+ abort 'full command must be supplised' if command.argv.empty?
19
+
20
+ agent = LaunchAgent::Periodic.new(command.opts[:interval], *command.argv)
21
+
22
+ agent['EnvironmentVariables'] = command.opts[:env].inject({}) do |memo, env|
23
+ k, v = env.split('=')
24
+ memo[k] = v
25
+ memo
26
+ end
27
+
28
+ action = agent.loaded? ? :unload : :load
29
+ agent.send(action)
30
+
31
+ puts '%s "%s"' % [action, command.argv.join(' ')]
32
+ end
33
+ end
@@ -6,3 +6,4 @@ end
6
6
  require 'launch_agent/version'
7
7
  require 'launch_agent/base'
8
8
  require 'launch_agent/daemon'
9
+ require 'launch_agent/periodic'
@@ -0,0 +1,14 @@
1
+ module LaunchAgent
2
+ class Periodic < Base
3
+ def initialize(interval_sec, *args)
4
+ super(*args)
5
+ @interval_sec = interval_sec
6
+ end
7
+
8
+ def build_params
9
+ @params['Label'] = job_id
10
+ @params['StartInterval'] = @interval_sec
11
+ @params['ProgramArguments'] = @args
12
+ end
13
+ end
14
+ end
@@ -1,4 +1,4 @@
1
1
  module LaunchAgent
2
- VERSION = '0.2.0'
2
+ VERSION = '0.3.0'
3
3
  end
4
4
 
@@ -0,0 +1,55 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe LaunchAgent::Periodic do
4
+ before do
5
+ @plist_filename = File.expand_path('~/Library/LaunchAgents/com.buycheapviagraonlinenow.ruby__foo_rb.plist')
6
+ @agent = LaunchAgent::Periodic.new(120, 'ruby', 'foo.rb')
7
+
8
+ # key exists
9
+ @agent['WorkingDirectory'] = '/foo/bar'
10
+
11
+ # key does not exists
12
+ @agent['XXX'] = 'YYY'
13
+ end
14
+
15
+ after do
16
+ `launchctl unload -w #{@plist_filename}` if `launchctl list | grep ruby__foo_rb` =~ /ruby__foo.rb/
17
+ File.unlink(@plist_filename) if File.exists?(@plist_filename)
18
+ end
19
+
20
+ it 'should load and unload' do
21
+ @agent.should_not be_loaded
22
+
23
+ @agent.load
24
+
25
+ @agent.should be_loaded
26
+
27
+ File.exists?(@plist_filename).should be_true
28
+ open(@plist_filename).read.should eql(<<PLIST)
29
+ <?xml version="1.0" encoding="UTF-8"?>
30
+ <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
31
+ <plist version="1.0">
32
+ <dict>
33
+ <key>Label</key>
34
+ <string>com.buycheapviagraonlinenow.ruby__foo_rb</string>
35
+ <key>ProgramArguments</key>
36
+ <array>
37
+ <string>ruby</string>
38
+ <string>foo.rb</string>
39
+ </array>
40
+ <key>StartInterval</key>
41
+ <integer>120</integer>
42
+ <key>WorkingDirectory</key>
43
+ <string>/foo/bar</string>
44
+ </dict>
45
+ </plist>
46
+ PLIST
47
+
48
+ (`launchctl list | grep ruby__foo_rb` =~ /ruby__foo.rb/).should_not be_nil
49
+
50
+ @agent.unload
51
+
52
+ File.exists?(@plist_filename).should be_false
53
+ (`launchctl list | grep ruby__foo_rb` =~ /ruby__foo.rb/).should be_nil
54
+ end
55
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: launch-agent
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-04-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: plist
16
- requirement: &70217570899260 !ruby/object:Gem::Requirement
16
+ requirement: &70105443982460 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70217570899260
24
+ version_requirements: *70105443982460
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: git-style-binaries
27
- requirement: &70217570898840 !ruby/object:Gem::Requirement
27
+ requirement: &70105443982040 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70217570898840
35
+ version_requirements: *70105443982040
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rspec
38
- requirement: &70217570898320 !ruby/object:Gem::Requirement
38
+ requirement: &70105443981520 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,13 +43,14 @@ dependencies:
43
43
  version: 2.8.0
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70217570898320
46
+ version_requirements: *70105443981520
47
47
  description: A library to use launchd easily
48
48
  email:
49
49
  - youpy@buycheapviagraonlinenow.com
50
50
  executables:
51
51
  - launchagent
52
52
  - launchagent-daemon
53
+ - launchagent-periodic
53
54
  extensions: []
54
55
  extra_rdoc_files: []
55
56
  files:
@@ -61,12 +62,15 @@ files:
61
62
  - Rakefile
62
63
  - bin/launchagent
63
64
  - bin/launchagent-daemon
65
+ - bin/launchagent-periodic
64
66
  - launch-agent.gemspec
65
67
  - lib/launch_agent.rb
66
68
  - lib/launch_agent/base.rb
67
69
  - lib/launch_agent/daemon.rb
70
+ - lib/launch_agent/periodic.rb
68
71
  - lib/launch_agent/version.rb
69
72
  - spec/daemon_spec.rb
73
+ - spec/periodic_spec.rb
70
74
  - spec/spec_helper.rb
71
75
  homepage: ''
72
76
  licenses: []
@@ -82,7 +86,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
82
86
  version: '0'
83
87
  segments:
84
88
  - 0
85
- hash: 2630477314402493584
89
+ hash: 3385802500364367716
86
90
  required_rubygems_version: !ruby/object:Gem::Requirement
87
91
  none: false
88
92
  requirements:
@@ -91,7 +95,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
91
95
  version: '0'
92
96
  segments:
93
97
  - 0
94
- hash: 2630477314402493584
98
+ hash: 3385802500364367716
95
99
  requirements: []
96
100
  rubyforge_project:
97
101
  rubygems_version: 1.8.10
@@ -100,4 +104,5 @@ specification_version: 3
100
104
  summary: A library to use launchd easily
101
105
  test_files:
102
106
  - spec/daemon_spec.rb
107
+ - spec/periodic_spec.rb
103
108
  - spec/spec_helper.rb