Module: Pwrake::FileUtils

Included in:
Rake::DSL
Defined in:
lib/pwrake/branch/file_utils.rb

Class Method Summary collapse

Class Method Details

.bq(*cmd, &block) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/pwrake/branch/file_utils.rb', line 30

def bq(*cmd, &block)
  options = (Hash === cmd.last) ? cmd.pop : {}
  unless block_given?
    show_command = cmd.join(" ")
    show_command = show_command[0,42] + "..."
    block = lambda { |ok, status|
      ok or fail "Command failed with status (#{status.exitstatus}): [#{show_command}]"
    }
  end
  if RakeFileUtils.verbose_flag == :default
    options[:verbose] = true
  else
    options[:verbose] ||= RakeFileUtils.verbose_flag
  end
  options[:noop]    ||= RakeFileUtils.nowrite_flag
  Rake.rake_check_options options, :noop, :verbose
  Rake.rake_output_message cmd.join(" ") if options[:verbose]
  #Pwrake::Log.stderr_puts cmd.join(" ") if options[:verbose]
  unless options[:noop]
    res,status = Pwrake::FileUtils.pwrake_backquote(*cmd)
    block.call(res, status)
  end
  res
end

.pwrake_backquote(cmd) ⇒ Object

Pwrake version of backquote command



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/pwrake/branch/file_utils.rb', line 74

def pwrake_backquote(cmd)
  cmd_log = cmd.inspect
  #tm = Pwrake::Timer.new("bq",cmd_log)

  conn = Pwrake::Shell.current
  if conn.kind_of?(Pwrake::Shell)
    res    = conn.backquote(*cmd)
    status = Rake::PseudoStatus.new(conn.status)
  else
    res    = `#{cmd}`
    status = $?
    status = Rake::PseudoStatus.new(1) if status.nil?
  end

  #tm.finish("status=%s cmd=%s"%[status.exitstatus,cmd_log])
  [res,status]
end

.pwrake_system(*cmd) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/pwrake/branch/file_utils.rb', line 55

def pwrake_system(*cmd)
  cmd_log = cmd.join(" ").inspect
  #tm = Pwrake::Timer.new("sh",cmd_log)

  conn = Pwrake::Shell.current
  if conn.kind_of?(Pwrake::Shell)
    res    = conn.system(*cmd)
    status = Rake::PseudoStatus.new(conn.status)
  else
    res    = system(*cmd)
    status = $?
    status = Rake::PseudoStatus.new(1) if !res && status.nil?
  end

  #tm.finish("status=%s cmd=%s"%[status.exitstatus,cmd_log])
  [res,status]
end

.sh(*cmd, &block) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/pwrake/branch/file_utils.rb', line 6

def sh(*cmd, &block)
  options = (Hash === cmd.last) ? cmd.pop : {}
  unless block_given?
    show_command = cmd.join(" ")
    show_command = show_command[0,42] + "..."
    block = lambda { |ok, status|
      ok or fail "Command failed with status (#{status.exitstatus}): [#{show_command}]"
    }
  end
  if RakeFileUtils.verbose_flag == :default
    options[:verbose] = true
  else
    options[:verbose] ||= RakeFileUtils.verbose_flag
  end
  options[:noop]    ||= RakeFileUtils.nowrite_flag
  Rake.rake_check_options options, :noop, :verbose
  Rake.rake_output_message cmd.join(" ") if options[:verbose]
  #Pwrake::Log.stderr_puts cmd.join(" ") if options[:verbose]
  unless options[:noop]
    res,status = Pwrake::FileUtils.pwrake_system(*cmd)
    block.call(res, status)
  end
end