require 'pathname'
require 'rubygems'
require 'spec/rake/spectask'
require 'lib/do_postgres/version'


ROOT    = Pathname(__FILE__).dirname.expand_path
JRUBY   = RUBY_PLATFORM =~ /java/
WINDOWS = Gem.win_platform?
SUDO    = (WINDOWS || JRUBY) ? '' : ('sudo' unless ENV['SUDOLESS'])

AUTHOR = "Dirkjan Bussink"
EMAIL  = "d.bussink@gmail.com"
GEM_NAME = "do_postgres"
GEM_VERSION = DataObjects::Postgres::VERSION
GEM_DEPENDENCIES = if JRUBY
  [["data_objects", GEM_VERSION], ["do_jdbc", GEM_VERSION], ["jdbc-postgres", ">=8.2"]]
else
  [["data_objects", GEM_VERSION]]
end
GEM_CLEAN = ['**/*.{o,so,bundle,jar,log,a,gem,dSYM,obj,pdb,lib,def,exp,DS_Store}',
  'ext/Makefile', 'ext-java/target']
GEM_EXTRAS = if JRUBY
  {
    :has_rdoc => false,
    :platform => 'java'
  }
else
  {
    :has_rdoc => false,
    :extensions => 'ext/do_postgres_ext/extconf.rb'
  }
end

PROJECT_NAME = "dorb"
PROJECT_URL  = "http://rubyforge.org/projects/dorb"
PROJECT_DESCRIPTION = PROJECT_SUMMARY = "A DataObject.rb driver for PostgreSQL"


# RCov is run by default, except on the JRuby platform, or if NO_RCOV env is true
RUN_RCOV = JRUBY ? false : (ENV.has_key?('NO_RCOV') ? ENV['NO_RCOV'] != 'true' : true)

if (tasks_dir = ROOT.parent + 'tasks').directory?
  require tasks_dir + 'hoe'
  require tasks_dir + 'ext_helper_java'

  setup_java_extension  "#{GEM_NAME}_ext", HOE.spec
end

if JRUBY
  HOE.spec.files += ['lib/do_postgres_ext.jar']
  HOE.spec.require_paths = Dir['lib']
end

# compile the extension
if JRUBY
  Rake::Task['compile:jruby'].invoke
else
  begin
    gem('rake-compiler')
    require 'rake/extensiontask'
    Rake::ExtensionTask.new('do_postgres_ext', HOE.spec)
  rescue LoadError
    warn "To cross-compile, install rake-compiler (gem install rake-compiler)"
    if tasks_dir.directory?
      require tasks_dir + 'ext_helper'
      setup_c_extension('do_postgres_ext', HOE.spec)
    end
  end
end


def sudo_gem(cmd)
  sh "#{SUDO} #{RUBY} -S gem #{cmd}", :verbose => false
end

# Installation

desc "Install #{GEM_NAME} #{GEM_VERSION}"
task :install => [ :package ] do
  sudo_gem "install pkg/#{GEM_NAME}-#{GEM_VERSION} --no-update-sources"
end

desc "Uninstall #{GEM_NAME} #{GEM_VERSION}"
task :uninstall => [ :clobber ] do
  sudo_gem "uninstall #{GEM_NAME} -v#{GEM_VERSION} -I -x"
end

desc 'Run specifications'
Spec::Rake::SpecTask.new(:spec) do |t|
  t.spec_opts << '--format' << 'specdoc' << '--colour'
  t.spec_opts << '--loadby' << 'random'
  t.spec_files = Pathname.glob(ENV['FILES'] || 'spec/**/*_spec.rb').map { |f| f.to_s }

  begin
    t.rcov = RUN_RCOV
    t.rcov_opts << '--exclude' << 'spec'
    t.rcov_opts << '--text-summary'
    t.rcov_opts << '--sort' << 'coverage' << '--sort-reverse'
  rescue Exception
    # rcov not installed
  end
end

namespace :ci do

  task :prepare do
    rm_rf ROOT + "ci"
    mkdir_p ROOT + "ci"
    mkdir_p ROOT + "ci/doc"
    mkdir_p ROOT + "ci/cyclomatic"
    mkdir_p ROOT + "ci/token"
  end

  task :publish do
    out = ENV['CC_BUILD_ARTIFACTS'] || "out"
    mkdir_p out unless File.directory? out

    mv "ci/rspec_report.html", "#{out}/rspec_report.html"
    mv "ci/coverage", "#{out}/coverage"
  end

  task :spec => :prepare do
    Rake::Task[:spec].invoke
    mv ROOT + "coverage", ROOT + "ci/coverage"
  end

end

task :ci => ["ci:spec"]
