template.rb 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 'administrate', '~> 0.8.1'
  27. gem 'data-confirm-modal', '~> 1.6.2'
  28. gem 'devise', '~> 4.4.3'
  29. gem 'devise-bootstrapped', github: 'excid3/devise-bootstrapped', branch: 'bootstrap4'
  30. gem 'devise_masquerade', '~> 0.6.0'
  31. gem 'font-awesome-sass', '~> 4.7'
  32. gem 'gravatar_image_tag', github: 'mdeering/gravatar_image_tag'
  33. gem 'jquery-rails', '~> 4.3.1'
  34. gem 'bootstrap', '~> 4.0.0.beta'
  35. gem 'mini_magick', '~> 4.8'
  36. gem 'webpacker', '~> 3.4'
  37. gem 'sidekiq', '~> 5.0'
  38. gem 'foreman', '~> 0.84.0'
  39. gem 'omniauth-facebook', '~> 4.0'
  40. gem 'omniauth-twitter', '~> 1.4'
  41. gem 'omniauth-github', '~> 1.3'
  42. gem 'whenever', require: false
  43. end
  44. def set_application_name
  45. # Add Application Name to Config
  46. environment "config.application_name = Rails.application.class.parent_name"
  47. # Announce the user where he can change the application name in the future.
  48. puts "You can change application name inside: ./config/application.rb"
  49. end
  50. def add_users
  51. # Install Devise
  52. generate "devise:install"
  53. # Configure Devise
  54. environment "config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }",
  55. env: 'development'
  56. route "root to: 'home#index'"
  57. # Devise notices are installed via Bootstrap
  58. generate "devise:views:bootstrapped"
  59. # Create Devise User
  60. generate :devise, "User",
  61. "name",
  62. "announcements_last_read_at:datetime",
  63. "admin:boolean"
  64. # Set admin default to false
  65. in_root do
  66. migration = Dir.glob("db/migrate/*").max_by{ |f| File.mtime(f) }
  67. gsub_file migration, /:admin/, ":admin, default: false"
  68. end
  69. requirement = Gem::Requirement.new("> 5.2")
  70. rails_version = Gem::Version.new(Rails::VERSION::STRING)
  71. if requirement.satisfied_by? rails_version
  72. gsub_file "config/initializers/devise.rb",
  73. / # config.secret_key = .+/,
  74. " config.secret_key = Rails.application.credentials.secret_key_base"
  75. end
  76. # Add Devise masqueradable to users
  77. inject_into_file("app/models/user.rb", "omniauthable, :masqueradable, :", after: "devise :")
  78. end
  79. def add_bootstrap
  80. # Remove Application CSS
  81. run "rm app/assets/stylesheets/application.css"
  82. # Add Bootstrap JS
  83. insert_into_file(
  84. "app/assets/javascripts/application.js",
  85. "\n//= require jquery\n//= require popper\n//= require bootstrap\n//= require data-confirm-modal",
  86. after: "//= require rails-ujs"
  87. )
  88. end
  89. def copy_templates
  90. directory "app", force: true
  91. directory "config", force: true
  92. directory "lib", force: true
  93. route "get '/terms', to: 'home#terms'"
  94. route "get '/privacy', to: 'home#privacy'"
  95. end
  96. def add_webpack
  97. rails_command 'webpacker:install'
  98. end
  99. def add_sidekiq
  100. environment "config.active_job.queue_adapter = :sidekiq"
  101. insert_into_file "config/routes.rb",
  102. "require 'sidekiq/web'\n\n",
  103. before: "Rails.application.routes.draw do"
  104. insert_into_file "config/routes.rb",
  105. " authenticate :user, lambda { |u| u.admin? } do\n mount Sidekiq::Web => '/sidekiq'\n end\n\n",
  106. after: "Rails.application.routes.draw do\n"
  107. end
  108. def add_foreman
  109. copy_file "Procfile"
  110. end
  111. def add_announcements
  112. generate "model Announcement published_at:datetime announcement_type name description:text"
  113. route "resources :announcements, only: [:index]"
  114. end
  115. def add_notifications
  116. generate "model Notification recipient_id:integer actor_id:integer read_at:datetime action:string notifiable_id:integer notifiable_type:string"
  117. route "resources :notifications, only: [:index]"
  118. end
  119. def add_administrate
  120. generate "administrate:install"
  121. gsub_file "app/dashboards/announcement_dashboard.rb",
  122. /announcement_type: Field::String/,
  123. "announcement_type: Field::Select.with_options(collection: Announcement::TYPES)"
  124. gsub_file "app/controllers/admin/application_controller.rb",
  125. /# TODO Add authentication logic here\./,
  126. "redirect_to '/', alert: 'Not authorized.' unless user_signed_in? && current_user.admin?"
  127. end
  128. def add_multiple_authentication
  129. insert_into_file "config/routes.rb",
  130. ', controllers: { omniauth_callbacks: "users/omniauth_callbacks" }',
  131. after: " devise_for :users"
  132. generate "model Service user:references provider uid access_token access_token_secret refresh_token expires_at:datetime auth:text"
  133. template = """
  134. if Rails.application.secrets.facebook_app_id.present? && Rails.application.secrets.facebook_app_secret.present?
  135. config.omniauth :facebook, Rails.application.secrets.facebook_app_id, Rails.application.secrets.facebook_app_secret, scope: 'email,user_posts'
  136. end
  137. if Rails.application.secrets.twitter_app_id.present? && Rails.application.secrets.twitter_app_secret.present?
  138. config.omniauth :twitter, Rails.application.secrets.twitter_app_id, Rails.application.secrets.twitter_app_secret
  139. end
  140. if Rails.application.secrets.github_app_id.present? && Rails.application.secrets.github_app_secret.present?
  141. config.omniauth :github, Rails.application.secrets.github_app_id, Rails.application.secrets.github_app_secret
  142. end
  143. """.strip
  144. insert_into_file "config/initializers/devise.rb", " " + template + "\n\n",
  145. before: " # ==> Warden configuration"
  146. end
  147. def add_whenever
  148. run "wheneverize ."
  149. end
  150. # Main setup
  151. add_template_repository_to_source_path
  152. add_gems
  153. after_bundle do
  154. set_application_name
  155. add_users
  156. add_bootstrap
  157. add_sidekiq
  158. add_foreman
  159. add_webpack
  160. add_announcements
  161. add_notifications
  162. add_multiple_authentication
  163. copy_templates
  164. # Migrate
  165. rails_command "db:create"
  166. rails_command "db:migrate"
  167. # Migrations must be done before this
  168. add_administrate
  169. add_whenever
  170. git :init
  171. git add: "."
  172. git commit: %Q{ -m 'Initial commit' }
  173. end