template.rb 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. require "fileutils"
  2. require "shellwords"
  3. # Copied from: https://github.com/mattbrictson/rails-template
  4. # Add this template directory to source_paths so that Thor actions like
  5. # copy_file and template resolve against our source files. If this file was
  6. # invoked remotely via HTTP, that means the files are not present locally.
  7. # In that case, use `git clone` to download them to a local temporary dir.
  8. def add_template_repository_to_source_path
  9. if __FILE__ =~ %r{\Ahttps?://}
  10. require "tmpdir"
  11. source_paths.unshift(tempdir = Dir.mktmpdir("jumpstart-"))
  12. at_exit { FileUtils.remove_entry(tempdir) }
  13. git clone: [
  14. "--quiet",
  15. "https://github.com/excid3/jumpstart.git",
  16. tempdir
  17. ].map(&:shellescape).join(" ")
  18. if (branch = __FILE__[%r{jumpstart/(.+)/template.rb}, 1])
  19. Dir.chdir(tempdir) { git checkout: branch }
  20. end
  21. else
  22. source_paths.unshift(File.dirname(__FILE__))
  23. end
  24. end
  25. def add_gems
  26. gem 'bootstrap', '~> 4.1', '>= 4.1.1'
  27. gem 'devise', '~> 4.5', '>= 4.4.3'
  28. gem 'devise-bootstrapped', github: 'excid3/devise-bootstrapped', branch: 'bootstrap4'
  29. gem 'font-awesome-sass', '~> 5.0', '>= 5.0.13'
  30. gem 'gravatar_image_tag', github: 'mdeering/gravatar_image_tag'
  31. gem 'jquery-rails', '~> 4.3.1'
  32. gem 'mini_magick', '~> 4.8'
  33. gem 'omniauth-facebook', '~> 5.0'
  34. gem 'omniauth-github', '~> 1.3'
  35. gem 'omniauth-twitter', '~> 1.4'
  36. gem 'sidekiq', '~> 5.1', '>= 5.1.3'
  37. gem 'webpacker', '~> 3.5', '>= 3.5.3'
  38. gem 'procodile', '~> 1.0', '>= 1.0.16'
  39. end
  40. def set_application_name
  41. # Add Application Name to Config
  42. environment "config.application_name = Rails.application.class.parent_name"
  43. # Announce the user where he can change the application name in the future.
  44. puts "You can change application name inside: ./config/application.rb"
  45. end
  46. def add_users
  47. # Install Devise
  48. generate "devise:install"
  49. # Configure Devise
  50. environment "config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }",
  51. env: 'development'
  52. route "root to: 'home#index'"
  53. # Devise notices are installed via Bootstrap
  54. generate "devise:views:bootstrapped"
  55. # Create Devise User
  56. generate :devise, "User",
  57. "first_name",
  58. "last_name",
  59. "announcements_last_read_at:datetime",
  60. "admin:boolean"
  61. # Set admin default to false
  62. in_root do
  63. migration = Dir.glob("db/migrate/*").max_by{ |f| File.mtime(f) }
  64. gsub_file migration, /:admin/, ":admin, default: false"
  65. end
  66. requirement = Gem::Requirement.new("> 5.2")
  67. rails_version = Gem::Version.new(Rails::VERSION::STRING)
  68. if requirement.satisfied_by? rails_version
  69. gsub_file "config/initializers/devise.rb",
  70. / # config.secret_key = .+/,
  71. " config.secret_key = Rails.application.credentials.secret_key_base"
  72. end
  73. # Add Devise masqueradable to users
  74. inject_into_file("app/models/user.rb", "omniauthable, :masqueradable, :", after: "devise :")
  75. end
  76. def add_bootstrap
  77. # Remove Application CSS
  78. run "rm app/assets/stylesheets/application.css"
  79. # Add Bootstrap JS
  80. insert_into_file(
  81. "app/assets/javascripts/application.js",
  82. "\n//= require jquery\n//= require popper\n//= require bootstrap\n//= require data-confirm-modal\n//= require local-time",
  83. after: "//= require rails-ujs"
  84. )
  85. end
  86. def copy_templates
  87. directory "app", force: true
  88. directory "config", force: true
  89. directory "lib", force: true
  90. route "get '/terms', to: 'home#terms'"
  91. route "get '/privacy', to: 'home#privacy'"
  92. end
  93. def add_webpack
  94. rails_command 'webpacker:install'
  95. rails_command 'webpacker:install:stimulus'
  96. end
  97. def add_sidekiq
  98. environment "config.active_job.queue_adapter = :sidekiq"
  99. insert_into_file "config/routes.rb",
  100. "require 'sidekiq/web'\n\n",
  101. before: "Rails.application.routes.draw do"
  102. insert_into_file "config/routes.rb",
  103. " authenticate :user, lambda { |u| u.admin? } do\n mount Sidekiq::Web => '/sidekiq'\n end\n\n",
  104. after: "Rails.application.routes.draw do\n"
  105. end
  106. def add_procodile
  107. copy_file "Procfile"
  108. end
  109. def add_multiple_authentication
  110. insert_into_file "config/routes.rb",
  111. ', controllers: { omniauth_callbacks: "users/omniauth_callbacks" }',
  112. after: " devise_for :users"
  113. generate "model Service user:references provider uid access_token access_token_secret refresh_token expires_at:datetime auth:text"
  114. template = """
  115. if Rails.application.secrets.facebook_app_id.present? && Rails.application.secrets.facebook_app_secret.present?
  116. config.omniauth :facebook, Rails.application.secrets.facebook_app_id, Rails.application.secrets.facebook_app_secret, scope: 'email,user_posts'
  117. end
  118. if Rails.application.secrets.twitter_app_id.present? && Rails.application.secrets.twitter_app_secret.present?
  119. config.omniauth :twitter, Rails.application.secrets.twitter_app_id, Rails.application.secrets.twitter_app_secret
  120. end
  121. if Rails.application.secrets.github_app_id.present? && Rails.application.secrets.github_app_secret.present?
  122. config.omniauth :github, Rails.application.secrets.github_app_id, Rails.application.secrets.github_app_secret
  123. end
  124. """.strip
  125. insert_into_file "config/initializers/devise.rb", " " + template + "\n\n",
  126. before: " # ==> Warden configuration"
  127. end
  128. def stop_spring
  129. run "spring stop"
  130. end
  131. # Main setup
  132. add_template_repository_to_source_path
  133. add_gems
  134. after_bundle do
  135. set_application_name
  136. stop_spring
  137. add_users
  138. add_bootstrap
  139. add_sidekiq
  140. add_procodile
  141. add_webpack
  142. add_multiple_authentication
  143. copy_templates
  144. # Migrate
  145. rails_command "db:create"
  146. rails_command "db:migrate"
  147. # Migrations must be done before this
  148. git :init
  149. git add: "."
  150. git commit: %Q{ -m 'Initial commit' }
  151. end