# encoding: utf-8
require 'rubygems'
require 'bundler'

begin
  Bundler.setup(:default, :development)
rescue Bundler::BundlerError => e
  $stderr.puts e.message
  $stderr.puts "Run `bundle install` to install missing gems"
  exit e.status_code
end
require 'rake'

require 'jeweler'
Jeweler::Tasks.new do |gem|
  # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
  gem.name = "rubyXL"
  gem.homepage = "http://github.com/gilt/rubyXL"
  gem.license = "MIT"
  gem.summary = %Q{rubyXL is a gem which allows the parsing, creation, and manipulation of Microsoft Excel (.xlsx/.xlsm) Documents}
  gem.description = %Q{rubyXL is a gem which allows the parsing, creation, and manipulation of Microsoft Excel (.xlsx/.xlsm) Documents}
  gem.email = "bhagwat.vivek@gmail.com"
  gem.authors = ["Vivek Bhagwat"]
  # dependencies defined in Gemfile
end
Jeweler::RubygemsDotOrgTasks.new

require 'rake/testtask'
Rake::TestTask.new(:test) do |test|
  test.libs << 'lib' << 'test'
  test.pattern = 'test/**/test_*.rb'
  test.verbose = true
end

=begin
require 'rcov/rcovtask'
Rcov::RcovTask.new do |test|
  test.libs << 'test'
  test.pattern = 'test/**/test_*.rb'
  test.verbose = true
  test.rcov_opts << '--exclude "gems/*"'
end
=end

require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec)
task :default => :spec

require 'rdoc/task'
Rake::RDocTask.new do |rdoc|
  version = File.exist?('VERSION') ? File.read('VERSION') : ""

  rdoc.rdoc_dir = 'rdoc'
  rdoc.title = "rubyXL #{version}"
  rdoc.rdoc_files.include('README*')
  rdoc.rdoc_files.include('lib/**/*.rb')
end

desc "Dump profiling data"
task :profile do
  require 'benchmark'
  require 'stackprof'

  $:.unshift File.dirname(__FILE__) + '/lib'  # Make Ruby aware of load path
  require './lib/rubyXL'

  spreadsheets = Dir.glob(File.join("test", "input", "*.xls?")).sort!

  spreadsheets.each { |input|
    puts "<<<--- Profiling parsing #{input}..."
    doc = nil
    StackProf.run(:mode => :cpu, :interval => 100, 
                  :out  => "tmp/stackprof-cpu-parse-#{File.basename(input)}.dump") { 
      doc = RubyXL::Parser.parse(input)
    }

    output = File.join("test", "output", File.basename(input))
    puts  "--->>> Profiling writing of #{output}..."
    StackProf.run(:mode => :cpu, :interval => 100, 
                  :out  => "tmp/stackprof-cpu-write-#{File.basename(input)}.dump") {
      doc.write(output)
    }
  }
end