# frozen_string_literal: true

############################################################################## # Documentation ##############################################################################

namespace :documentation do

namespace :api do
  desc 'Generate API documentation markdown'
  task :blueprint do
    require 'rspec/core/rake_task'

    RSpec::Core::RakeTask.new(:api_docs) do |t|
      t.pattern    = 'spec/controllers/**/v1/**/*_spec.rb'
      t.rspec_opts = '--format="Dox::Formatter" --out="./docs/api/index.apib" --order="defined" --tag="dox"'
    end

    ::Rake::Task['api_docs'].invoke
  end

  task :json => :blueprint do
    `apib2swagger --input='./docs/api/index.apib' --output='./docs/api/index.json'`
  end

  task :yaml => :blueprint do
    `apib2swagger --yaml --input='./docs/api/index.apib' --output='./docs/api/index.yaml'`
  end

  task :html => :json do
    `redoc-cli bundle --output='./docs/api/index.html' './docs/api/index.json'`
  end

  task :preview => :html do
    `open './docs/api/index.html'`
  end

  task :publish => :blueprint do
    `apiary publish --path='./docs/api/apispec.apib' --api-name='#{::Rails.application.name}'`
  end
end

end