require 'pathname'
require 'bundler/gem_tasks'

__current__ = Pathname( File.expand_path('..', __FILE__) )

task(:default) { system "rake --tasks" }
task  :test    => 'test:unit'

# ----- Test tasks ------------------------------------------------------------

require 'rake/testtask'
namespace :test do
  Rake::TestTask.new(:unit) do |test|
    test.libs << 'lib' << 'test'
    test.test_files = FileList["test/unit/**/*_test.rb"]
    # test.verbose = true
    # test.warning = true
  end

  desc "Run integration tests"
  task :integration do
    unless ENV['TEST_REST_API_SPEC']
      puts "[!] Please export the TEST_REST_API_SPEC variable with a path to the Watcher YAML tests"
      exit(1)
    end

    # Define the task
    t = Rake::TestTask.new(:integration) do |test|
      test.libs << 'lib' << 'test'
      test.test_files = FileList["../elasticsearch-api/test/integration/yaml_test_runner.rb", "test/integration/**/*_test.rb"]
    end

    # Run the task
    args = [t.ruby_opts_string, t.run_code, t.file_list_string, t.option_list].join(' ')

    ruby args do |ok, status|
      if !ok && status.respond_to?(:signaled?) && status.signaled?
        raise SignalException.new(status.termsig)
      elsif !ok
        fail "Command failed with status (#{status.exitstatus}): " + "[ruby #{args}]"
      end
    end
  end

  desc "Run unit and integration tests"
  task :all do
    Rake::Task['test:ci_reporter'].invoke if ENV['CI']
    Rake::Task['test:unit'].invoke
    Rake::Task['test:integration'].invoke
  end

  desc "Initialize or update the repository with integration tests"
  task :update do
    sh "git --git-dir=#{__current__.join('tmp/elasticsearch-watcher/.git')} --work-tree=#{__current__.join('tmp/elasticsearch-watcher')} fetch origin --verbose"
    begin
      puts %x[git --git-dir=#{__current__.join('tmp/elasticsearch-watcher/.git')} --work-tree=#{__current__.join('tmp/elasticsearch-watcher')} pull --verbose]
    rescue Exception => @exception
      @failed = true
    end

    if @failed || !$?.success?
      STDERR.puts "", "[!] Error while pulling -- #{@exception}"
    end

    puts "\n", "CHANGES:", '-'*80
    sh "git --git-dir=#{__current__.join('tmp/elasticsearch-watcher/.git')} --work-tree=#{__current__.join('tmp/elasticsearch-watcher')} log --oneline ORIG_HEAD..HEAD | cat", :verbose => false
  end

  namespace :cluster do
    desc "Start Elasticsearch nodes for tests"
    task :start do
      $LOAD_PATH << File.expand_path('../../elasticsearch-transport/lib', __FILE__) << File.expand_path('../test', __FILE__)
      require 'elasticsearch/extensions/test/cluster'
      Elasticsearch::Extensions::Test::Cluster.start
    end

    desc "Stop Elasticsearch nodes for tests"
    task :stop do
      $LOAD_PATH << File.expand_path('../../elasticsearch-transport/lib', __FILE__) << File.expand_path('../test', __FILE__)
      require 'elasticsearch/extensions/test/cluster'
      Elasticsearch::Extensions::Test::Cluster.stop
    end
  end
end

# ----- Documentation tasks ---------------------------------------------------

require 'yard'
YARD::Rake::YardocTask.new(:doc) do |t|
  t.options = %w| --embed-mixins --markup=markdown |
end

# ----- Code analysis tasks ---------------------------------------------------

require 'cane/rake_task'
Cane::RakeTask.new(:quality) do |cane|
  cane.abc_max = 15
  cane.no_style = true
end
