require 'bundler' module JsonVoorhees class SetupAppGenerator < Rails::Generators::Base source_root File.expand_path('../templates', __FILE__) class_option :arcadex, :type => :boolean, :default => true, :description => "Include arcadex gem and authentication in api_controller" class_option :user, :type => :boolean, :default => true, :description => "Create an engine for the user class" class_option :test, :type => :boolean, :default => false, :description => "Set true if running tests to skip things like bundle update and git" class_option :admin, :type => :boolean, :default => true, :description => "Create an admin page" class_option :active_admin, :type => :boolean, :default => true, :description => "Setup the active_admin gem" def sprint create_file_structure all_gems run_bundle rspec api_controller include_middleware cors_route if options.admin? make_admin end if options.user? make_user end run "rake railties:install:migrations" run_db_migrations routes make_controllers if options.arcadex? && options.active_admin? #run "rails generate active_admin:resource arcadex::token" custom_active_admin end if options.active_admin? seed_database end run "wheneverize" run_git end private def seed_database #db/seeds.rb inject_into_file "db/seeds.rb", after: "# Mayor.create(name: 'Emanuel', city: cities.first)\n" do <<-'RUBY' ::Defcon::AdminUser.create({username: "admin", password: "password", password_confirmation: "password"}) RUBY end run "rake db:seed" end def custom_active_admin template "active_admin_token_register.rb.erb", "app/admin/arcadex_token.rb" end def make_admin if !options.active_admin? if !options.arcadex? && !options.user? run "rails g json_voorhees:app_make_admin --skip-arcadex --skip-user --skip-active_admin" end if !options.arcadex? && options.user? run "rails g json_voorhees:app_make_admin --skip-arcadex --skip-active_admin" end if !options.user? && options.arcadex? run "rails g json_voorhees:app_make_admin --skip-user --skip-active_admin" end if options.user? && options.arcadex? run "rails g json_voorhees:app_make_admin --skip-active_admin" end else if !options.arcadex? && !options.user? run "rails g json_voorhees:app_make_admin --skip-arcadex --skip-user" end if !options.arcadex? && options.user? run "rails g json_voorhees:app_make_admin --skip-arcadex" end if !options.user? && options.arcadex? run "rails g json_voorhees:app_make_admin --skip-user" end if options.user? && options.arcadex? run "rails g json_voorhees:app_make_admin" end end end def make_user if !options.active_admin? if !options.arcadex? run "rails g json_voorhees:app_make_user --skip-arcadex --skip-active_admin" else run "rails g json_voorhees:app_make_user --skip-active_admin" end else if !options.arcadex? run "rails g json_voorhees:app_make_user --skip-arcadex" else run "rails g json_voorhees:app_make_user" end end run_bundle end def include_middleware inject_into_file 'config/application.rb', after: "class Application < Rails::Application\n" do <<-'RUBY' #So Rails-Api doesn't remove all of the middleware config.api_only = false RUBY end end def routes route "mount Defcon::Engine, at: \'/\'" route "root to: \"app_index#app\"" if !options.active_admin? && options.admin? route "get \'admin\' => \'main#admin\', as: \"admin\" #admin_path" end end def cors_route route "#This is to handle the CORS preflight request, it only catches the options action. controller \'api/v1/api\' do match \'*unmatched_route\', :to => \'api/v1/api#route_options\', via: [:options] end" end def make_controllers generate "controller", "app_index app" run "rm -f app/controllers/app_index_controller.rb" copy_file "app_index_controller.rb", "app/controllers/app_index_controller.rb" if !options.active_admin? && options.admin? generate "controller", "main admin" run "rm -f spec/controllers/main_controller_spec.rb" end #Copy the views over copy_file "views/app", "app/views/app_index/app.html.erb" copy_file "views/app_index", "app/views/layouts/app_index.html.erb" end def api_controller if options.arcadex? copy_file "api_controller_with_arcadex.rb", "app/controllers/api/v1/api_controller.rb" else copy_file "api_controller_no_arcadex.rb", "app/controllers/api/v1/api_controller.rb" end end def rspec generate "rspec:install" run "rm -f .rspec" copy_file "hidden_rspec.rb", ".rspec" copy_file "rspec_factory_girl.rb", "spec/support/factory_girl.rb" inject_into_file 'spec/rails_helper.rb', after: "RSpec.configure do |config|\n" do <<-'RUBY' config.include ::Requests::JsonHelpers, :type => :request config.include ::Requests::JsonHelpers, :type => :controller RUBY end copy_file "json_helpers.rb", "spec/support/request_helpers.rb" end def all_gems # Dev gems gem_group :development, :test do #gem 'sqlite3', '~> 1.3.9' # dev & test database gem 'figaro', '~> 0.7.0' # env variables gem 'annotate', ">=2.6.0" gem 'rspec-rails', '~> 3.0.0' gem 'byebug', '~> 3.2.0' gem "factory_girl_rails", "~> 4.0" gem "database_cleaner", '~> 1.3.0' end gem_group :development do gem "better_errors", '~> 1.1.0' gem "binding_of_caller", "~> 0.7.1" # needed by better_errors or variable inspection end # Other gems # This ties in nicely with Ember, 0.8.0 is the stable version gem "active_model_serializers", "~> 0.8.0" # This is the api of choice now gem "rails-api" gem 'kaminari' gem 'bcrypt', '~> 3.1.7' gem 'type_cartographer' gem 'devise', "~> 3.2.4" if options.arcadex? gem 'arcadex', '~> 1.1.3' end if options.admin? gem 'autoprefixer-rails' gem 'bootstrap-sass', '~> 3.2.0' end if options.active_admin? gem 'activeadmin', :git => 'https://github.com/activeadmin/activeadmin.git', :branch => "master" gem 'defcon', '~> 1.2.3' end gem 'authorization', :path => "gems/authorization" gem 'whenever', :require => false #gem 'websocket-rails' end def create_file_structure run "rm -rf test" run "rm -f README.rdoc" run "echo '# Describe your application here' > README.md" run "mkdir engines && touch engines/.gitkeep" run "mkdir gems && touch gems/.gitkeep" run "mkdir notes && touch notes/.gitkeep" run "rails plugin new gems/authorization" end def run_bundle if !options.test? Bundler.with_clean_env do run "bundle install" end end end def run_db_migrations run "rake db:migrate RAILS_ENV=development" run "rake db:migrate RAILS_ENV=test" run "rake db:migrate RAILS_ENV=production" end def run_git if !options.test? git :init git add: "." run "git commit -m \"Initial commit\"" end end end end