childprocess 0.0.7 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.7
1
+ 0.0.9
data/childprocess.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{childprocess}
8
- s.version = "0.0.7"
8
+ s.version = "0.0.9"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Jari Bakken"]
12
- s.date = %q{2010-10-09}
12
+ s.date = %q{2010-10-16}
13
13
  s.description = %q{This gem aims at being a simple and reliable solution for controlling external programs running in the background on any Ruby / OS combination.}
14
14
  s.email = %q{jari.bakken@gmail.com}
15
15
  s.extra_rdoc_files = [
data/lib/childprocess.rb CHANGED
@@ -7,29 +7,53 @@ module ChildProcess
7
7
  autoload :JRuby, 'childprocess/jruby'
8
8
  autoload :IronRuby, 'childprocess/ironruby'
9
9
 
10
- def self.build(*args)
11
- case platform
12
- when :jruby
13
- JRuby::Process.new(args)
14
- when :ironruby
15
- IronRuby::Process.new(args)
16
- when :windows
17
- Windows::Process.new(args)
18
- else
19
- Unix::Process.new(args)
10
+ class << self
11
+
12
+ def new(*args)
13
+ case platform
14
+ when :jruby
15
+ JRuby::Process.new(args)
16
+ when :ironruby
17
+ IronRuby::Process.new(args)
18
+ when :windows
19
+ Windows::Process.new(args)
20
+ else
21
+ Unix::Process.new(args)
22
+ end
20
23
  end
21
- end
24
+ alias_method :build, :new
22
25
 
23
- def self.platform
24
- if RUBY_PLATFORM == "java"
25
- :jruby
26
- elsif defined?(RUBY_ENGINE) && RUBY_ENGINE == "ironruby"
27
- :ironruby
28
- elsif RUBY_PLATFORM =~ /mswin|msys|mingw32/
29
- :windows
30
- else
31
- :unix
26
+ def platform
27
+ if RUBY_PLATFORM == "java"
28
+ :jruby
29
+ elsif defined?(RUBY_ENGINE) && RUBY_ENGINE == "ironruby"
30
+ :ironruby
31
+ elsif RUBY_PLATFORM =~ /mswin|msys|mingw32/
32
+ :windows
33
+ else
34
+ os
35
+ end
32
36
  end
33
- end
34
37
 
38
+ def os
39
+ @os ||= (
40
+ require "rbconfig"
41
+ host_os = RbConfig::CONFIG['host_os']
42
+
43
+ case host_os
44
+ when /mswin|msys|mingw32/
45
+ :windows
46
+ when /darwin|mac os/
47
+ :macosx
48
+ when /linux/
49
+ :linux
50
+ when /solaris|bsd/
51
+ :unix
52
+ else
53
+ raise Error, "unknown os: #{host_os.inspect}"
54
+ end
55
+ )
56
+ end
57
+
58
+ end
35
59
  end
@@ -2,6 +2,19 @@ module ChildProcess
2
2
  class AbstractProcess
3
3
  attr_reader :exit_code
4
4
 
5
+ #
6
+ # Set this to true if you do not care about when or if the process quits.
7
+ #
8
+ attr_accessor :detach
9
+
10
+
11
+ #
12
+ # Create a new process with the given args.
13
+ #
14
+ # @api private
15
+ # @see ChildProcess.build
16
+ #
17
+
5
18
  def initialize(args)
6
19
  @args = args
7
20
  @started = false
@@ -48,7 +61,7 @@ module ChildProcess
48
61
  def alive?
49
62
  started? && !exited?
50
63
  end
51
-
64
+
52
65
  def crashed?
53
66
  exited? && @exit_code != 0
54
67
  end
@@ -3,7 +3,6 @@ require "java"
3
3
  module ChildProcess
4
4
  module JRuby
5
5
  class Process < AbstractProcess
6
-
7
6
  def exited?
8
7
  return true if @exit_code
9
8
 
@@ -27,6 +26,8 @@ module ChildProcess
27
26
  private
28
27
 
29
28
  def launch_process
29
+ backround_args! if @detach
30
+
30
31
  pb = java.lang.ProcessBuilder.new(@args)
31
32
 
32
33
  # not sure why this is necessary
@@ -44,6 +45,16 @@ module ChildProcess
44
45
  @process.getInputStream.close
45
46
  end
46
47
 
48
+ def background_args!
49
+ case ChildProcess.os
50
+ when :windows
51
+ args = %w[start /wait /b]
52
+ @args.unshift(*args) unless @args[0] == start
53
+ else
54
+ @args.push "&" unless @args.last == "&"
55
+ end
56
+ end
57
+
47
58
  end # Process
48
59
  end # JRuby
49
60
  end # ChildProcess
@@ -70,6 +70,8 @@ module ChildProcess
70
70
 
71
71
  exec(*@args)
72
72
  }
73
+
74
+ ::Process.detach(@pid) if @detach
73
75
  end
74
76
 
75
77
  end # Process
@@ -17,6 +17,7 @@ module ChildProcess::Windows
17
17
  CTRL_C_EVENT = 0
18
18
  CTRL_BREAK_EVENT = 1
19
19
 
20
+ DETACHED_PROCESS = 0x00000008
20
21
 
21
22
  module Lib
22
23
  enum :wait_status, [ :wait_object_0, 0,
@@ -2,25 +2,18 @@ module ChildProcess
2
2
  module Windows
3
3
  module Lib
4
4
 
5
- def self.last_error_message
6
- errnum = get_last_error
7
- buf = FFI::MemoryPointer.new :char, 512
8
-
9
- size = format_message(
10
- FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ARGUMENT_ARRAY,
11
- nil, errnum, 0, buf, buf.size, nil
12
- )
13
-
14
- buf.read_string(size).strip
15
- end
16
-
17
5
  def self.create_proc(cmd, opts = {})
18
6
  cmd_ptr = FFI::MemoryPointer.from_string cmd
19
7
 
8
+ flags = 0
9
+ inherit = !!opts[:inherit]
10
+
11
+ flags |= DETACHED_PROCESS if opts[:detach]
12
+
20
13
  si = StartupInfo.new
21
14
  pi = ProcessInfo.new
22
15
 
23
- ok = create_process(nil, cmd_ptr, nil, nil, !!opts[:inherit], 0, nil, nil, si, pi)
16
+ ok = create_process(nil, cmd_ptr, nil, nil, inherit, flags, nil, nil, si, pi)
24
17
  ok or raise Error, last_error_message
25
18
 
26
19
  close_handle pi[:hProcess]
@@ -29,6 +22,18 @@ module ChildProcess
29
22
  pi[:dwProcessId]
30
23
  end
31
24
 
25
+ def self.last_error_message
26
+ errnum = get_last_error
27
+ buf = FFI::MemoryPointer.new :char, 512
28
+
29
+ size = format_message(
30
+ FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ARGUMENT_ARRAY,
31
+ nil, errnum, 0, buf, buf.size, nil
32
+ )
33
+
34
+ buf.read_string(size).strip
35
+ end
36
+
32
37
  #
33
38
  # BOOL WINAPI CreateProcess(
34
39
  # __in_opt LPCTSTR lpApplicationName,
@@ -33,7 +33,11 @@ module ChildProcess
33
33
  private
34
34
 
35
35
  def launch_process
36
- @pid = Lib.create_proc(@args.join(' '), :inherit => false)
36
+ @pid = Lib.create_proc(
37
+ @args.join(' '),
38
+ :inherit => false,
39
+ :detach => @detach
40
+ )
37
41
  @handle = Handle.open(@pid)
38
42
 
39
43
  self
@@ -62,5 +62,9 @@ describe ChildProcess do
62
62
  end
63
63
  end
64
64
 
65
+ it "lets a detached child live on" do
66
+ # hmm. how do we spec this?
67
+ end
68
+
65
69
  it_should_behave_like "unix process" if ChildProcess.platform == :unix
66
70
  end
data/spec/spec_helper.rb CHANGED
@@ -79,6 +79,10 @@ module ChildProcessSpecHelper
79
79
  raise last_error unless ok
80
80
  end
81
81
 
82
+ def ruby(code)
83
+ ruby_process(tmp_script(code))
84
+ end
85
+
82
86
  end # ChildProcessSpecHelper
83
87
 
84
88
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: childprocess
3
3
  version: !ruby/object:Gem::Version
4
- hash: 17
4
+ hash: 13
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 7
10
- version: 0.0.7
9
+ - 9
10
+ version: 0.0.9
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jari Bakken
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-10-09 00:00:00 +02:00
18
+ date: 2010-10-16 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency