/tools/Ruby/lib/ruby/gems/1.8/gems/rake-0.9.2/lib/rake/file_utils.rb

http://github.com/agross/netopenspace · Ruby · 112 lines · 69 code · 10 blank · 33 comment · 12 complexity · ea877f4e89e90eac374c10d64ed3ddf3 MD5 · raw file

  1. require 'rbconfig'
  2. require 'fileutils'
  3. # ###########################################################################
  4. # This a FileUtils extension that defines several additional commands to be
  5. # added to the FileUtils utility functions.
  6. #
  7. module FileUtils
  8. # Path to the currently running Ruby program
  9. RUBY = File.join(
  10. RbConfig::CONFIG['bindir'],
  11. RbConfig::CONFIG['ruby_install_name'] + RbConfig::CONFIG['EXEEXT']).
  12. sub(/.*\s.*/m, '"\&"')
  13. OPT_TABLE['sh'] = %w(noop verbose)
  14. OPT_TABLE['ruby'] = %w(noop verbose)
  15. # Run the system command +cmd+. If multiple arguments are given the command
  16. # is not run with the shell (same semantics as Kernel::exec and
  17. # Kernel::system).
  18. #
  19. # Example:
  20. # sh %{ls -ltr}
  21. #
  22. # sh 'ls', 'file with spaces'
  23. #
  24. # # check exit status after command runs
  25. # sh %{grep pattern file} do |ok, res|
  26. # if ! ok
  27. # puts "pattern not found (status = #{res.exitstatus})"
  28. # end
  29. # end
  30. #
  31. def sh(*cmd, &block)
  32. options = (Hash === cmd.last) ? cmd.pop : {}
  33. shell_runner = block_given? ? block : create_shell_runner(cmd)
  34. set_verbose_option(options)
  35. options[:noop] ||= Rake::FileUtilsExt.nowrite_flag
  36. Rake.rake_check_options options, :noop, :verbose
  37. Rake.rake_output_message cmd.join(" ") if options[:verbose]
  38. unless options[:noop]
  39. res = rake_system(*cmd)
  40. status = $?
  41. status = PseudoStatus.new(1) if !res && status.nil?
  42. shell_runner.call(res, status)
  43. end
  44. end
  45. def create_shell_runner(cmd)
  46. show_command = cmd.join(" ")
  47. show_command = show_command[0,42] + "..." unless $trace
  48. lambda { |ok, status|
  49. ok or fail "Command failed with status (#{status.exitstatus}): [#{show_command}]"
  50. }
  51. end
  52. private :create_shell_runner
  53. def set_verbose_option(options)
  54. if options[:verbose].nil?
  55. options[:verbose] = Rake::FileUtilsExt.verbose_flag.nil? || Rake::FileUtilsExt.verbose_flag
  56. end
  57. end
  58. private :set_verbose_option
  59. def rake_system(*cmd)
  60. Rake::AltSystem.system(*cmd)
  61. end
  62. private :rake_system
  63. # Run a Ruby interpreter with the given arguments.
  64. #
  65. # Example:
  66. # ruby %{-pe '$_.upcase!' <README}
  67. #
  68. def ruby(*args,&block)
  69. options = (Hash === args.last) ? args.pop : {}
  70. if args.length > 1 then
  71. sh(*([RUBY] + args + [options]), &block)
  72. else
  73. sh("#{RUBY} #{args.first}", options, &block)
  74. end
  75. end
  76. LN_SUPPORTED = [true]
  77. # Attempt to do a normal file link, but fall back to a copy if the link
  78. # fails.
  79. def safe_ln(*args)
  80. unless LN_SUPPORTED[0]
  81. cp(*args)
  82. else
  83. begin
  84. ln(*args)
  85. rescue StandardError, NotImplementedError
  86. LN_SUPPORTED[0] = false
  87. cp(*args)
  88. end
  89. end
  90. end
  91. # Split a file path into individual directory names.
  92. #
  93. # Example:
  94. # split_all("a/b/c") => ['a', 'b', 'c']
  95. #
  96. def split_all(path)
  97. head, tail = File.split(path)
  98. return [tail] if head == '.' || tail == '/'
  99. return [head, tail] if head == '/'
  100. return split_all(head) + [tail]
  101. end
  102. end