namespace :okei do def app @app ||= ( Rake::Task.task_defined?("railties:install:migrations") ? "" : "app:" ) end desc "Installs the units gem into rails application" task install: %w(dependencies:install db:migrate) do system "rake routes:mount" if app.blank? end namespace :db do desc "Copies and runs db migrations from okei gem" task migrate: %w(install:migrations environment) do system "rake db:migrate SCOPE=okei RAILS_ENV=#{ Rails.env }" end end namespace :install do desc "Copies migrations from okei gem to application" task :migrations do Rake::Task["#{ app }railties:install:migrations"].reenable Rake::Task["#{ app }okei:install:migrations"].invoke end end namespace :dependencies do desc "Installs gem dependencies" task :install do system "rake #{ app }corrector:install" Rake::Task["#{ app }railties:install:migrations"].reenable system "rake #{ app }uuids:install" end end namespace :routes do desc "Mounts units API routes" task :mount do insert_into_file( "config/routes.rb", before: "\nend", line: "\n\n mount Okei::Engine => \"/okei\"", engine: "Okei::Engine" ) end def insert_into_file(name, before:, line:, engine:) text = File.read(name) return if text[engine] File.write name, text.gsub(before, line << before) end end namespace :test do desc "Prepares spec environment" task prepare: %w(cache:prepare) do system "rake db:migrate RAILS_ENV=test" end end namespace :cache do desc "Creates spec/dummy/temp/cache folder" task :prepare do system "mkdir spec/dummy/tmp/cache -p -v" end end end