bootstrap.rake 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. module BootstrapRakeHelper
  2. APP_CACHE = Dir.glob(Rails.root.join('tmp', 'cache*'))
  3. SERVER_LOG = Rails.root.join('log', "#{Rails.env}.log")
  4. AUTO_WIZARD = { source: Rails.root.join('contrib', 'auto_wizard_test.json'),
  5. dest: Rails.root.join('auto_wizard.json') }.freeze
  6. DB_CONFIG = { source: Rails.root.join('config', 'database', 'database.yml'),
  7. dest: Rails.root.join('config', 'database.yml') }.freeze
  8. def flush_cache_and_logs
  9. FileUtils.rm_rf(APP_CACHE)
  10. File.write(SERVER_LOG, '')
  11. end
  12. def run_auto_wizard
  13. FileUtils.ln(AUTO_WIZARD[:source], AUTO_WIZARD[:dest], force: true)
  14. AutoWizard.setup
  15. # set system init to done
  16. UserInfo.current_user_id = 1
  17. Setting.set('system_init_done', true)
  18. end
  19. def add_database_config
  20. raise Errno::ENOENT, 'config/database.yml not found' unless File.exist?(DB_CONFIG[:source])
  21. if File.exist?(DB_CONFIG[:dest])
  22. return if FileUtils.identical?(DB_CONFIG[:source], DB_CONFIG[:dest])
  23. printf 'config/database.yml: File exists. Overwrite? [y/N] '
  24. return if STDIN.gets.chomp.downcase != 'y'
  25. end
  26. FileUtils.cp(DB_CONFIG[:source], DB_CONFIG[:dest])
  27. end
  28. end
  29. namespace :bs do
  30. desc 'Bootstrap the application'
  31. task :init => %i[db_config db:create db:migrate db:seed] do
  32. include BootstrapRakeHelper
  33. run_auto_wizard
  34. end
  35. desc 'Reset the application to its initial state'
  36. task :reset => %i[db:reset] do
  37. include BootstrapRakeHelper
  38. run_auto_wizard
  39. flush_cache_and_logs
  40. end
  41. task :db_config do
  42. include BootstrapRakeHelper
  43. add_database_config
  44. end
  45. end