childprocess 0.0.7 → 0.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/VERSION +1 -1
- data/childprocess.gemspec +2 -2
- data/lib/childprocess.rb +45 -21
- data/lib/childprocess/abstract_process.rb +14 -1
- data/lib/childprocess/jruby/process.rb +12 -1
- data/lib/childprocess/unix/process.rb +2 -0
- data/lib/childprocess/windows/constants.rb +1 -0
- data/lib/childprocess/windows/functions.rb +18 -13
- data/lib/childprocess/windows/process.rb +5 -1
- data/spec/childprocess_spec.rb +4 -0
- data/spec/spec_helper.rb +4 -0
- metadata +4 -4
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
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.
|
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-
|
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
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
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
|
-
|
24
|
+
alias_method :build, :new
|
22
25
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
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
|
@@ -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,
|
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
|
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
|
data/spec/childprocess_spec.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
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:
|
4
|
+
hash: 13
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
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-
|
18
|
+
date: 2010-10-16 00:00:00 +02:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|