Debian Codegraph @

rake.d/vendor/drake-0.9.2.0.3.1/lib/rake/file_utils.rb

raw
1 require 'rbconfig'
2 require 'fileutils'
3
4 # ###########################################################################
5 # This a FileUtils extension that defines several additional commands to be
6 # added to the FileUtils utility functions.
7 #
8 module FileUtils
9 # Path to the currently running Ruby program
10 RUBY = File.join(
11 RbConfig::CONFIG['bindir'],
12 RbConfig::CONFIG['ruby_install_name'] + RbConfig::CONFIG['EXEEXT']).
13 sub(/.*\s.*/m, '"\&"')
14
15 OPT_TABLE['sh'] = %w(noop verbose)
16 OPT_TABLE['ruby'] = %w(noop verbose)
17
18 # Run the system command +cmd+. If multiple arguments are given the command
19 # is not run with the shell (same semantics as Kernel::exec and
20 # Kernel::system).
21 #
22 # Example:
23 # sh %{ls -ltr}
24 #
25 # sh 'ls', 'file with spaces'
26 #
27 # # check exit status after command runs
28 # sh %{grep pattern file} do |ok, res|
29 # if ! ok
30 # puts "pattern not found (status = #{res.exitstatus})"
31 # end
32 # end
33 #
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
48
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
56 private :create_shell_runner
57
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
63 private :set_verbose_option
64
65 def rake_system(*cmd)
66 Rake::AltSystem.system(*cmd)
67 end
68 private :rake_system
69
70 # Run a Ruby interpreter with the given arguments.
71 #
72 # Example:
73 # ruby %{-pe '$_.upcase!' <README}
74 #
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
83
84 LN_SUPPORTED = [true]
85
86 # Attempt to do a normal file link, but fall back to a copy if the link
87 # fails.
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
100
101 # Split a file path into individual directory names.
102 #
103 # Example:
104 # split_all("a/b/c") => ['a', 'b', 'c']
105 #
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
112 end