# frozen_string_literal: true

require 'rubygems'
require 'date'
require 'open3'

task :build do
  sh('cargo build --release')
end

begin
  require 'rspec/core/rake_task'
  RSpec::Core::RakeTask.new(:spec)

  require 'rake/testtask'

  Rake::TestTask.new(minitests: :build) do |t|
    t.test_files = FileList['minitests/**/test_*.rb']
    t.verbose = true
  end

  task test: %i[build spec minitests]
  task rspec: %i[build spec]
  task minitest: %i[build without_rspec] do
    begin
      Rake::Task['minitests'].execute
    ensure
      Rake::Task['with_rspec'].execute
    end
  end
rescue LoadError
end

task :without_rspec do
  sh('bundle config set without rspec')
end

task :with_rspec do
  sh('bundle config unset without')
end

def rubocop(fix:)
  sh "bundle exec rubocop #{'-a' if fix} lib spec minitests " \
  'Rakefile hypothesis-specs.gemspec'
end

task :checkformat do
  rubocop(fix: false)
end

task :format do
  rubocop(fix: true)
end

begin
  require 'yard'

  YARD::Rake::YardocTask.new(:runyard) do |t|
    t.files = [
      'lib/hypothesis.rb', 'lib/hypothesis/errors.rb',
      'lib/hypothesis/possible.rb'
    ]
    t.options = ['--markup=markdown', '--no-private']
  end

  task doc: :runyard do
    YARD::Registry.load

    objs = YARD::Registry.select do |o|
      is_private = false
      t = o
      until t.root?
        if t.visibility != :public
          is_private = true
          break
        end
        t = t.parent
      end

      !is_private && o.docstring.blank?
    end

    objs.sort_by! { |o| o.name.to_s }

    unless objs.empty?
      abort "Undocumented objects: #{objs.map(&:name).join(', ')}"
    end
  end
rescue LoadError
end

GEMSPEC = 'hypothesis-specs.gemspec'

RELEASE_FILE = 'RELEASE.md'
CHANGELOG = 'CHANGELOG.md'

def run_for_output(*args)
  out, result = Open3.capture2(*args)
  abort if result.exitstatus != 0
  out.strip
end

task :clean do
  sh 'git clean -fdx lib'
  sh 'rm -rf hypothesis-specs*.gem'
  sh 'rm -rf ../target'
end

task gem: :clean do
  sh 'gem build hypothesis-specs.gemspec'
end
