module FileUtils

  1. lib/rake/file_utils.rb
Parent: file_utils.rb

########################################################################### This a FileUtils extension that defines several additional commands to be added to the FileUtils utility functions.

Methods

Public Instance

  1. sh
  2. ruby
  3. safe_ln
  4. split_all

Private Instance

  1. create_shell_runner
  2. set_verbose_option
  3. rake_system

Constants

RUBY = File.join( RbConfig::CONFIG['bindir'], RbConfig::CONFIG['ruby_install_name'] + RbConfig::CONFIG['EXEEXT']). sub(/.*\s.*/m, '"\&"')  

Path to the currently running Ruby program

LN_SUPPORTED = [true]  

Public Instance methods

sh (*cmd, &block)

Run the system command cmd. If multiple arguments are given the command is not run with the shell (same semantics as Kernel::exec and Kernel::system).

Example:

sh %{ls -ltr}

sh 'ls', 'file with spaces'

# check exit status after command runs
sh %{grep pattern file} do |ok, res|
  if ! ok
    puts "pattern not found (status = #{res.exitstatus})"
  end
end
[show source]
    # File lib/rake/file_utils.rb, line 34
34:   def sh(*cmd, &block)
35:     options = (Hash === cmd.last) ? cmd.pop : {}
36:     shell_runner = block_given? ? block : create_shell_runner(cmd)
37:     set_verbose_option(options)
38:     options[:noop] ||= Rake::FileUtilsExt.nowrite_flag
39:     Rake.rake_check_options options, :noop, :verbose
40:     Rake.rake_output_message cmd.join(" ") if options[:verbose]
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
ruby (*args,&block)

Run a Ruby interpreter with the given arguments.

Example:

ruby %{-pe '$_.upcase!' <README}
[show source]
    # File lib/rake/file_utils.rb, line 75
75:   def ruby(*args,&block)
76:     options = (Hash === args.last) ? args.pop : {}
77:     if args.length > 1 then
78:       sh(*([RUBY] + args + [options]), &block)
79:     else
80:       sh("#{RUBY} #{args.first}", options, &block)
81:     end
82:   end
safe_ln (*args)

Attempt to do a normal file link, but fall back to a copy if the link fails.

[show source]
    # File lib/rake/file_utils.rb, line 88
88:   def safe_ln(*args)
89:     unless LN_SUPPORTED[0]
90:       cp(*args)
91:     else
92:       begin
93:         ln(*args)
94:       rescue StandardError, NotImplementedError
95:         LN_SUPPORTED[0] = false
96:         cp(*args)
97:       end
98:     end
99:   end
split_all (path)

Split a file path into individual directory names.

Example:

split_all("a/b/c") =>  ['a', 'b', 'c']
[show source]
     # File lib/rake/file_utils.rb, line 106
106:   def split_all(path)
107:     head, tail = File.split(path)
108:     return [tail] if head == '.' || tail == '/'
109:     return [head, tail] if head == '/'
110:     return split_all(head) + [tail]
111:   end

Private Instance methods

create_shell_runner (cmd)
[show source]
    # File lib/rake/file_utils.rb, line 49
49:   def create_shell_runner(cmd)
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
set_verbose_option (options)
[show source]
    # File lib/rake/file_utils.rb, line 58
58:   def set_verbose_option(options)
59:     if options[:verbose].nil?
60:       options[:verbose] = Rake::FileUtilsExt.verbose_flag.nil? || Rake::FileUtilsExt.verbose_flag
61:     end
62:   end
rake_system (*cmd)
[show source]
    # File lib/rake/file_utils.rb, line 65
65:   def rake_system(*cmd)
66:     Rake::AltSystem.system(*cmd)
67:   end