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