module FileUtils

Public Instance Methods

sh(*cmd, &block) click to toggle source

code “borrowed” directly from Rake

# File lib/buildr/core/util.rb, line 362
def sh(*cmd, &block)
  options = (Hash === cmd.last) ? cmd.pop : {}
  unless block_given?
    show_command = cmd.join(" ")
    show_command = show_command[0,42] + "..."

    block = lambda { |ok, status|
      ok or fail "Command failed with status (#{status.exitstatus}): [#{show_command}]"
    }
  end
  if RakeFileUtils.verbose_flag == Rake::FileUtilsExt::DEFAULT
    options[:verbose] = false
  else
    options[:verbose] ||= RakeFileUtils.verbose_flag
  end
  options[:noop]    ||= RakeFileUtils.nowrite_flag
  rake_check_options options, :noop, :verbose
  rake_output_message cmd.join(" ") if options[:verbose]
  unless options[:noop]
    if Buildr::Util.win_os?
      # Ruby uses forward slashes regardless of platform,
      # unfortunately cd c:/some/path fails on Windows
      pwd = Dir.pwd.gsub(%r{/}, '\')
      cd = "cd /d \"#{pwd}\" && "
    else
      cd = "cd '#{Dir.pwd}' && "
    end
    args = if cmd.size > 1 then cmd[1..cmd.size] else [] end

    res = if Buildr::Util.win_os? && cmd.size == 1
      __native_system__("#{cd} call #{cmd.first}")
    else
      arg_str = args.map { |a| "'#{a}'" }
      __native_system__(cd + cmd.first + ' ' + arg_str.join(' '))
    end
    status = Buildr::ProcessStatus.new(0, res == 0, res)    # KLUDGE
    block.call(res == 0, status)
  end
end