2
# Copyright (c) 2008 James M. Lawrence
4
# Permission is hereby granted, free of charge, to any person
5
# obtaining a copy of this software and associated documentation files
6
# (the "Software"), to deal in the Software without restriction,
7
# including without limitation the rights to use, copy, modify, merge,
8
# publish, distribute, sublicense, and/or sell copies of the Software,
9
# and to permit persons to whom the Software is furnished to do so,
10
# subject to the following conditions:
12
# The above copyright notice and this permission notice shall be
13
# included in all copies or substantial portions of the Software.
15
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
# Alternate implementations of system() and backticks `` on Windows
29
# for ruby-1.8 and earlier.
31
module Rake::AltSystem
32
WINDOWS = RbConfig::CONFIG["host_os"] =~
33
%r!(msdos|mswin|djgpp|mingw|[Ww]indows)!
36
def define_module_function(name, &block)
37
define_method(name, &block)
42
if WINDOWS and RUBY_VERSION < "1.9.0"
43
RUNNABLE_EXTS = %w[com exe bat cmd]
44
RUNNABLE_PATTERN = %r!\.(#{RUNNABLE_EXTS.join('|')})\Z!i
46
define_module_function :kernel_system, &Kernel.method(:system)
47
define_module_function :kernel_backticks, &Kernel.method(:'`')
51
def repair_command(cmd)
53
if cmd =~ %r!\A\s*\".*?\"!
56
elsif match = cmd.match(%r!\A\s*(\S+)!)
58
# avoid x/y.bat interpretation as x with option /y
59
%Q!"#{match[1]}"! + match.post_match
61
# a shell command will fail if quoted
71
def find_runnable(file)
72
if file =~ RUNNABLE_PATTERN
75
RUNNABLE_EXTS.each { |ext|
76
if File.exist?(test = "#{file}.#{ext}")
84
def system(cmd, *args)
88
elsif runnable = find_runnable(cmd)
89
[File.expand_path(runnable), *args]
95
kernel_system(*repaired)
99
kernel_backticks(repair_command(cmd))
102
define_module_function :'`', &method(:backticks)
104
# Non-Windows or ruby-1.9+: same as Kernel versions
105
define_module_function :system, &Kernel.method(:system)
106
define_module_function :backticks, &Kernel.method(:'`')
107
define_module_function :'`', &Kernel.method(:'`')