Module: FileUtils

Included in:
RakeFileUtils
Defined in:
lib/rake.rb

Overview

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

Constant Summary collapse

RUBY =
LN_SUPPORTED =
[true]

Instance Method Summary collapse

Instance Method Details

#ruby(*args, &block) ⇒ Object

Run a Ruby interpreter with the given arguments.

Example:

ruby %{-pe '$_.upcase!' <README}


409
410
411
412
413
414
415
416
417
418
419
420
# File 'lib/rake.rb', line 409

def ruby(*args,&block)
  if Hash === args.last
    options = args.pop
  else
    options = {}
  end
  if args.length > 1 then
    sh *([RUBY] + args + [options]), &block
  else
    sh "#{RUBY} #{args}", options, &block
  end
end

#safe_ln(*args) ⇒ Object

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

link fails.


426
427
428
429
430
431
432
433
434
435
436
437
# File 'lib/rake.rb', line 426

def safe_ln(*args)
  unless LN_SUPPORTED[0]
    cp(*args)
  else
    begin
	ln(*args)
    rescue Errno::EOPNOTSUPP
	LN_SUPPORTED[0] = false
	cp(*args)
    end
  end
end

#sh(*cmd, &block) ⇒ Object

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


385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
# File 'lib/rake.rb', line 385

def sh(*cmd, &block)
  if Hash === cmd.last then
    options = cmd.pop
  else
    options = {}
  end
  unless block_given?
    block = lambda { |ok, status|
	ok or fail "Command failed with status (#{status.exitstatus}): [#{cmd.join(" ")}]"
    }
  end
  fu_check_options options, :noop, :verbose
  fu_output_message cmd.join(" ") if options[:verbose]
  unless options[:noop]
    res = system(*cmd)      
    block.call(res, $?)
  end
end

#split_all(path) ⇒ Object

Split a file path into individual directory names.

Example:

split_all("a/b/c") =>  ['a', 'b', 'c']


444
445
446
447
448
449
# File 'lib/rake.rb', line 444

def split_all(path)
  head, tail = File.split(path)
  return [tail] if head == '.' || tail == '/'
  return [head, tail] if head == '/'
  return split_all(head) + [tail]
end