class RubyProf::ProfileTask

def clean_output_directory

def clean_output_directory
  if File.exist?(output_directory)
    files = Dir.glob(output_directory + '/*')
    FileUtils.rm(files)
  end
end

def create_output_directory

def create_output_directory
  if not File.exist?(output_directory)
    Dir.mkdir(output_directory)
  end
end

def define

Create the tasks defined by this task lib.
def define
  lib_path = @libs.join(File::PATH_SEPARATOR)
  desc "Profile" + (@name==:profile ? "" : " for #{@name}")
  task @name do
    create_output_directory
    @ruby_opts.unshift( "-I#{lib_path}" )
    @ruby_opts.unshift( "-w" ) if @warning
    @ruby_opts.push("-S ruby-prof")
    @ruby_opts.push("--printer #{@printer}")
    @ruby_opts.push("--min_percent #{@min_percent}")
    file_list.each do |file_path|
      run_script(file_path)
    end
  end
  self
end

def initialize(name = :profile)

def initialize(name = :profile)
  super(name)
end

def option_list # :nodoc:

:nodoc:
def option_list # :nodoc:

  ENV['OPTIONS'] || @options.join(" ") || ""
end

def output_directory

def output_directory
  File.expand_path(@output_dir)
end

def run_script(script_path)

Run script
def run_script(script_path)
  run_code = ''
  RakeFileUtils.verbose(@verbose) do
    file_name = File.basename(script_path, File.extname(script_path))
    case @printer
      when :flat, :graph, :call_tree
        file_name += ".txt"
      when :graph_html
        file_name += ".html"
      else
        file_name += ".txt"
    end
    output_file_path = File.join(output_directory, file_name)
    command_line = @ruby_opts.join(" ") +
                  " --file=" + output_file_path +
                  " " + script_path
    puts "ruby " + command_line
    # We have to catch the exeption to continue on.  However,

    # the error message will have been output to STDERR

    # already by the time we get here so we don't have to

    # do that again

    begin
      ruby command_line
    rescue => e
      STDOUT << e << "\n"
      STDOUT.flush
    end
    puts ""
    puts ""
  end
end