launch-agent 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,24 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
22
+
23
+ .bundle
24
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mac-event-monitor.gemspec
4
+ gemspec
data/Rakefile CHANGED
@@ -1,46 +1,11 @@
1
- require 'rubygems'
1
+ #!/usr/bin/env rake
2
2
  require 'rake'
3
+ require "bundler/gem_tasks"
3
4
 
4
- begin
5
- require 'jeweler'
6
- Jeweler::Tasks.new do |gem|
7
- gem.name = "launch-agent"
8
- gem.summary = %Q{A library to use launchd easily}
9
- gem.description = %Q{A library to use launchd easily}
10
- gem.email = "youpy@buycheapviagraonlinenow.com"
11
- gem.homepage = "http://github.com/youpy/ruby-launch-agent"
12
- gem.authors = ["youpy"]
13
- gem.add_development_dependency "rspec", ">= 1.2.9"
14
- # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
- gem.executables = ["launch-agent-daemonize"]
16
- end
17
- Jeweler::GemcutterTasks.new
18
- rescue LoadError
19
- puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
5
+ require 'rspec/core'
6
+ require 'rspec/core/rake_task'
7
+ RSpec::Core::RakeTask.new(:spec) do |spec|
8
+ spec.pattern = FileList['spec/**/*_spec.rb']
20
9
  end
21
10
 
22
- require 'spec/rake/spectask'
23
- Spec::Rake::SpecTask.new(:spec) do |spec|
24
- spec.libs << 'lib' << 'spec'
25
- spec.spec_files = FileList['spec/**/*_spec.rb']
26
- end
27
-
28
- Spec::Rake::SpecTask.new(:rcov) do |spec|
29
- spec.libs << 'lib' << 'spec'
30
- spec.pattern = 'spec/**/*_spec.rb'
31
- spec.rcov = true
32
- end
33
-
34
- task :spec => :check_dependencies
35
-
36
11
  task :default => :spec
37
-
38
- require 'rdoc/task'
39
- Rake::RDocTask.new do |rdoc|
40
- version = File.exist?('VERSION') ? File.read('VERSION') : ""
41
-
42
- rdoc.rdoc_dir = 'rdoc'
43
- rdoc.title = "ruby-launch-agent #{version}"
44
- rdoc.rdoc_files.include('README*')
45
- rdoc.rdoc_files.include('lib/**/*.rb')
46
- end
data/bin/launchagent ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'launch_agent'
4
+ require 'git-style-binary/command'
5
+
6
+ GitStyleBinary.primary do
7
+ version "launch-agent #{LaunchAgent::VERSION}"
8
+ end
@@ -0,0 +1,32 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'git-style-binary/command'
4
+
5
+ GitStyleBinary.command do
6
+ short_desc "load/unload a daemon-like agent"
7
+ banner <<-EOS
8
+ Usage: #{command.full_name} #{all_options_string} {full command}
9
+
10
+ Load/Unload a daemon-like launchd agent
11
+
12
+
13
+ EOS
14
+ opt :env, "additional environmental variables to be set before running the job. can specify multiple times", :type => String, :multi => true
15
+
16
+ run do |command|
17
+ abort 'full command must be supplised' if command.argv.empty?
18
+
19
+ agent = LaunchAgent::Daemon.new(*command.argv)
20
+
21
+ agent['EnvironmentVariables'] = command.opts[:env].inject({}) do |memo, env|
22
+ k, v = env.split('=')
23
+ memo[k] = v
24
+ memo
25
+ end
26
+
27
+ action = agent.loaded? ? :unload : :load
28
+ agent.send(action)
29
+
30
+ puts '%s "%s"' % [action, command.argv.join(' ')]
31
+ end
32
+ end
data/launch-agent.gemspec CHANGED
@@ -1,52 +1,21 @@
1
- # Generated by jeweler
2
- # DO NOT EDIT THIS FILE DIRECTLY
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
1
  # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/launch_agent/version', __FILE__)
5
3
 
6
- Gem::Specification.new do |s|
7
- s.name = %q{launch-agent}
8
- s.version = "0.0.1"
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["youpy"]
6
+ gem.email = ["youpy@buycheapviagraonlinenow.com"]
7
+ gem.description = %q{A library to use launchd easily}
8
+ gem.summary = %q{A library to use launchd easily}
9
+ gem.homepage = ""
9
10
 
10
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = ["youpy"]
12
- s.date = %q{2011-12-21}
13
- s.default_executable = %q{launch-agent-daemonize}
14
- s.description = %q{A library to use launchd easily}
15
- s.email = %q{youpy@buycheapviagraonlinenow.com}
16
- s.executables = ["launch-agent-daemonize"]
17
- s.extra_rdoc_files = [
18
- "LICENSE",
19
- "README.rdoc"
20
- ]
21
- s.files = [
22
- ".document",
23
- "LICENSE",
24
- "README.rdoc",
25
- "Rakefile",
26
- "VERSION",
27
- "launch-agent.gemspec",
28
- "lib/launch_agent.rb",
29
- "lib/launch_agent/daemon.rb",
30
- "spec/daemon_spec.rb",
31
- "spec/spec.opts",
32
- "spec/spec_helper.rb"
33
- ]
34
- s.homepage = %q{http://github.com/youpy/ruby-launch-agent}
35
- s.require_paths = ["lib"]
36
- s.rubygems_version = %q{1.3.6}
37
- s.summary = %q{A library to use launchd easily}
11
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
12
+ gem.files = `git ls-files`.split("\n")
13
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
+ gem.name = %q{launch-agent}
15
+ gem.require_paths = ["lib"]
16
+ gem.version = LaunchAgent::VERSION
38
17
 
39
- if s.respond_to? :specification_version then
40
- current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
41
- s.specification_version = 3
42
-
43
- if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
44
- s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
45
- else
46
- s.add_dependency(%q<rspec>, [">= 1.2.9"])
47
- end
48
- else
49
- s.add_dependency(%q<rspec>, [">= 1.2.9"])
50
- end
18
+ gem.add_dependency('plist')
19
+ gem.add_dependency('git-style-binaries')
20
+ gem.add_development_dependency('rspec', ['~> 2.8.0'])
51
21
  end
52
-
data/lib/launch_agent.rb CHANGED
@@ -1,4 +1,8 @@
1
+ require 'plist'
2
+
1
3
  module LaunchAgent
2
4
  end
3
5
 
6
+ require 'launch_agent/version'
7
+ require 'launch_agent/base'
4
8
  require 'launch_agent/daemon'
@@ -0,0 +1,97 @@
1
+ module LaunchAgent
2
+ class Base
3
+ DOMAIN = 'com.buycheapviagraonlinenow'
4
+ KEYS = [
5
+ 'Label',
6
+ 'Disabled',
7
+ 'UserName',
8
+ 'GroupName',
9
+ 'inetdCompatibility',
10
+ 'LimitLoadToHosts',
11
+ 'LimitLoadFromHosts',
12
+ 'LimitLoadToSessionType',
13
+ 'Program',
14
+ 'ProgramArguments',
15
+ 'EnableGlobbing',
16
+ 'EnableTransactions',
17
+ 'OnDemand',
18
+ 'KeepAlive',
19
+ 'RunAtLoad',
20
+ 'RootDirectory',
21
+ 'WorkingDirectory',
22
+ 'EnvironmentVariables',
23
+ 'Umask',
24
+ 'TimeOut',
25
+ 'ExitTimeOut',
26
+ 'ThrottleInterval',
27
+ 'InitGroups',
28
+ 'WatchPaths',
29
+ 'QueueDirectories',
30
+ 'StartOnMount',
31
+ 'StartInterval',
32
+ 'StartCalendarInterval',
33
+ 'StandardInPath',
34
+ 'StandardOutPath',
35
+ 'StandardErrorPath',
36
+ 'Debug',
37
+ 'WaitForDebugger',
38
+ 'SoftResourceLimits',
39
+ 'HardResourceLimits',
40
+ 'Nice',
41
+ 'AbandonProcessGroup',
42
+ 'LowPriorityIO',
43
+ 'LaunchOnlyOnce',
44
+ 'MachServices',
45
+ 'Sockets'
46
+ ]
47
+
48
+ def initialize(*args)
49
+ @args = args
50
+ @params = {}
51
+ @user_params = {}
52
+ end
53
+
54
+ def load
55
+ open(plist_filename, 'w') do |file|
56
+ file.write(plist_content)
57
+ end
58
+
59
+ `launchctl load -w #{plist_filename}`
60
+ end
61
+
62
+ def unload
63
+ `launchctl unload -w #{plist_filename}`
64
+
65
+ File.unlink(plist_filename)
66
+ end
67
+
68
+ def loaded?
69
+ `launchctl list | grep #{job_id}` =~ /#{job_id}/
70
+ end
71
+
72
+ def []=(key, value)
73
+ if KEYS.include?(key)
74
+ @user_params[key] = value
75
+ end
76
+ end
77
+
78
+ def plist_filename
79
+ File.expand_path('~/Library/LaunchAgents/' + job_id + '.plist')
80
+ end
81
+
82
+ def build_params
83
+ end
84
+
85
+ def plist_content
86
+ build_params
87
+
88
+ @params.merge(@user_params).to_plist
89
+ end
90
+
91
+ def job_id
92
+ DOMAIN + '.' + @args.inject([]) do |m, arg|
93
+ m << arg.gsub(/\W/, '_')
94
+ end.join('__')
95
+ end
96
+ end
97
+ end
@@ -1,67 +1,12 @@
1
1
  module LaunchAgent
2
- class Daemon
3
- DOMAIN = 'com.buycheapviagraonlinenow'
4
-
5
- def initialize(*args)
6
- @args = args
7
- end
8
-
9
- def load
10
- open(plist_filename, 'w') do |file|
11
- file.write(plist_content)
12
- end
13
-
14
- `launchctl load -w #{plist_filename}`
15
- end
16
-
17
- def unload
18
- `launchctl unload -w #{plist_filename}`
19
- File.unlink(plist_filename)
20
- end
21
-
22
- def loaded?
23
- `launchctl list | grep #{job_id}` =~ /#{job_id}/
24
- end
25
-
26
- def plist_filename
27
- File.expand_path('~/Library/LaunchAgents/' + job_id + '.plist')
28
- end
29
-
30
- def plist_content
31
- template = <<PLIST
32
- <?xml version="1.0" encoding="UTF-8"?>
33
- <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
34
- <plist version="1.0">
35
- <dict>
36
- <key>KeepAlive</key>
37
- <dict>
38
- <key>SuccessfulExit</key>
39
- <false/>
40
- </dict>
41
- <key>Label</key>
42
- <string>%s</string>
43
- <key>ProgramArguments</key>
44
- <array>
45
- %s
46
- </array>
47
- <key>RunAtLoad</key>
48
- <true/>
49
- </dict>
50
- </plist>
51
- PLIST
52
- template % [job_id, xmlized_args]
53
- end
54
-
55
- def job_id
56
- DOMAIN + '.' + @args.inject([]) do |m, arg|
57
- m << arg.gsub(/\W/, '_')
58
- end.join('__')
59
- end
60
-
61
- def xmlized_args
62
- @args.inject([]) do |m, arg|
63
- m << "\t\t<string>#{arg}</string>"
64
- end.join("\n")
2
+ class Daemon < Base
3
+ def build_params
4
+ @params['Label'] = job_id
5
+ @params['KeepAlive'] = {
6
+ 'SuccessfulExit' => false
7
+ }
8
+ @params['ProgramArguments'] = @args
9
+ @params['RunAtLoad'] = true
65
10
  end
66
11
  end
67
12
  end
@@ -0,0 +1,4 @@
1
+ module LaunchAgent
2
+ VERSION = '0.2.0'
3
+ end
4
+
data/spec/daemon_spec.rb CHANGED
@@ -4,6 +4,12 @@ describe LaunchAgent::Daemon do
4
4
  before do
5
5
  @plist_filename = File.expand_path('~/Library/LaunchAgents/com.buycheapviagraonlinenow.ruby__foo_rb.plist')
6
6
  @agent = LaunchAgent::Daemon.new('ruby', 'foo.rb')
7
+
8
+ # key exists
9
+ @agent['WorkingDirectory'] = '/foo/bar'
10
+
11
+ # key does not exists
12
+ @agent['XXX'] = 'YYY'
7
13
  end
8
14
 
9
15
  after do
@@ -21,7 +27,7 @@ describe LaunchAgent::Daemon do
21
27
  File.exists?(@plist_filename).should be_true
22
28
  open(@plist_filename).read.should eql(<<PLIST)
23
29
  <?xml version="1.0" encoding="UTF-8"?>
24
- <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
30
+ <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
25
31
  <plist version="1.0">
26
32
  <dict>
27
33
  <key>KeepAlive</key>
@@ -38,6 +44,8 @@ describe LaunchAgent::Daemon do
38
44
  </array>
39
45
  <key>RunAtLoad</key>
40
46
  <true/>
47
+ <key>WorkingDirectory</key>
48
+ <string>/foo/bar</string>
41
49
  </dict>
42
50
  </plist>
43
51
  PLIST
data/spec/spec_helper.rb CHANGED
@@ -1,9 +1,16 @@
1
- $LOAD_PATH.unshift(File.dirname(__FILE__))
2
1
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+
3
+ require 'rubygems'
3
4
  require 'launch_agent'
4
- require 'spec'
5
- require 'spec/autorun'
6
5
 
7
- Spec::Runner.configure do |config|
8
-
6
+ # This file was generated by the `rspec --init` command. Conventionally, all
7
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
8
+ # Require this file using `require "spec_helper.rb"` to ensure that it is only
9
+ # loaded once.
10
+ #
11
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
12
+ RSpec.configure do |config|
13
+ config.treat_symbols_as_metadata_keys_with_true_values = true
14
+ config.run_all_when_everything_filtered = true
15
+ config.filter_run :focus
9
16
  end
metadata CHANGED
@@ -1,87 +1,103 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: launch-agent
3
- version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 1
8
- - 0
9
- version: 0.1.0
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ prerelease:
10
6
  platform: ruby
11
- authors:
7
+ authors:
12
8
  - youpy
13
9
  autorequire:
14
10
  bindir: bin
15
11
  cert_chain: []
16
-
17
- date: 2011-12-21 00:00:00 +09:00
18
- default_executable: launch-agent-daemonize
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
21
- name: rspec
12
+ date: 2012-04-17 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: plist
16
+ requirement: &70217570899260 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70217570899260
25
+ - !ruby/object:Gem::Dependency
26
+ name: git-style-binaries
27
+ requirement: &70217570898840 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
22
34
  prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
24
- requirements:
25
- - - ">="
26
- - !ruby/object:Gem::Version
27
- segments:
28
- - 1
29
- - 2
30
- - 9
31
- version: 1.2.9
35
+ version_requirements: *70217570898840
36
+ - !ruby/object:Gem::Dependency
37
+ name: rspec
38
+ requirement: &70217570898320 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: 2.8.0
32
44
  type: :development
33
- version_requirements: *id001
45
+ prerelease: false
46
+ version_requirements: *70217570898320
34
47
  description: A library to use launchd easily
35
- email: youpy@buycheapviagraonlinenow.com
36
- executables:
37
- - launch-agent-daemonize
48
+ email:
49
+ - youpy@buycheapviagraonlinenow.com
50
+ executables:
51
+ - launchagent
52
+ - launchagent-daemon
38
53
  extensions: []
39
-
40
- extra_rdoc_files:
41
- - LICENSE
42
- - README.rdoc
43
- files:
44
- - .document
54
+ extra_rdoc_files: []
55
+ files:
56
+ - .gitignore
57
+ - .rspec
58
+ - Gemfile
45
59
  - LICENSE
46
60
  - README.rdoc
47
61
  - Rakefile
48
- - VERSION
49
- - bin/launch-agent-daemonize
62
+ - bin/launchagent
63
+ - bin/launchagent-daemon
50
64
  - launch-agent.gemspec
51
65
  - lib/launch_agent.rb
66
+ - lib/launch_agent/base.rb
52
67
  - lib/launch_agent/daemon.rb
68
+ - lib/launch_agent/version.rb
53
69
  - spec/daemon_spec.rb
54
- - spec/spec.opts
55
70
  - spec/spec_helper.rb
56
- has_rdoc: true
57
- homepage: http://github.com/youpy/ruby-launch-agent
71
+ homepage: ''
58
72
  licenses: []
59
-
60
73
  post_install_message:
61
74
  rdoc_options: []
62
-
63
- require_paths:
75
+ require_paths:
64
76
  - lib
65
- required_ruby_version: !ruby/object:Gem::Requirement
66
- requirements:
67
- - - ">="
68
- - !ruby/object:Gem::Version
69
- segments:
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ segments:
70
84
  - 0
71
- version: "0"
72
- required_rubygems_version: !ruby/object:Gem::Requirement
73
- requirements:
74
- - - ">="
75
- - !ruby/object:Gem::Version
76
- segments:
85
+ hash: 2630477314402493584
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ! '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ segments:
77
93
  - 0
78
- version: "0"
94
+ hash: 2630477314402493584
79
95
  requirements: []
80
-
81
96
  rubyforge_project:
82
- rubygems_version: 1.3.6
97
+ rubygems_version: 1.8.10
83
98
  signing_key:
84
99
  specification_version: 3
85
100
  summary: A library to use launchd easily
86
- test_files: []
87
-
101
+ test_files:
102
+ - spec/daemon_spec.rb
103
+ - spec/spec_helper.rb
data/.document DELETED
@@ -1,5 +0,0 @@
1
- README.rdoc
2
- lib/**/*.rb
3
- bin/*
4
- features/**/*.feature
5
- LICENSE
data/VERSION DELETED
@@ -1 +0,0 @@
1
- 0.1.0
@@ -1,17 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- %w/rubygems launch_agent/.each {|g| require g }
4
-
5
- def usage
6
- print <<EOM
7
- usage: #{File.basename($0)} command [arg ...]
8
- EOM
9
- exit 0
10
- end
11
-
12
- usage if ARGV.empty?
13
-
14
- agent = LaunchAgent::Daemon.new(*ARGV)
15
- action = agent.loaded? ? :unload : :load
16
- agent.send(action)
17
- warn '%s "%s"' % [action, ARGV.join(' ')]
data/spec/spec.opts DELETED
@@ -1 +0,0 @@
1
- --color