# Rakefile
require 'awesome_print'
require 'pathname'
require 'dotenv'
require 'dotenv/tasks'

WINDOWS = (RUBY_PLATFORM =~ /mswin|win32|mingw|bccwin|cygwin/) rescue false

# These environment variables must be set outside of a .env* file.
# If they are not set, default to the development environment.
ENV['RACK_ENV']     = 'development'   unless ENV.has_key?('RACK_ENV')
ENV['RAILS_ENV']    = 'development'   unless ENV.has_key?('RAILS_ENV')
ENV['DEPLOY_TYPE']  = 'localhost'     unless ENV.has_key?('DEPLOY_TYPE')

RAILS_ROOT = Pathname.new(__FILE__).parent

require RAILS_ROOT + 'config/application'
require RAILS_ROOT + 'lib/truth_or_consequences'


# NOTE: This is also done in config/initializers/aarp.rb
#       These SEV's are used in the rake task prep:db
#       located in lib/tasks/prep.rake
%w[ DBRESET DBMIGRATE DBSEED ].each { |sev| truth_or_consequences(sev) }


# Load all the Rails default tasks and the application specific tasks
DrTurboTax::Application.load_tasks

# Set the default task to run all of the RSpec tests
task default: :spec



# Remove Rails default reset task.
Rake::Task["db:reset"].clear

namespace :db do
  ################################################################
  ## rake db:reset
  #
  # NOTE: In order for this to work, the default Rails db:reset task must
  #       be cleared in the Rakefile.
  desc "Reset #{DATABASE_NAME} with default values"
  task reset: :environment do
    if DBRESET
      Rake::Task["db:drop"].invoke
      Rake::Task["db:setup"].invoke
    else
      puts <<~EOS

        ERROR: the "rake db:reset" task is only allowed when the environment
               variable DBRESET is set to a truthy value.
               Your RAILS_ENV=#{Rails.env}
               Your DBRESET=#{ENV['DBRESET']}

      EOS
    end
  end
end # namespace :db do
