template.rb 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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 rails_version
  26. @rails_version ||= Gem::Version.new(Rails::VERSION::STRING)
  27. end
  28. def rails_5?
  29. Gem::Requirement.new(">= 5.2.0", "< 6.0.0.beta1").satisfied_by? rails_version
  30. end
  31. def rails_6?
  32. Gem::Requirement.new(">= 6.0.0.beta1", "< 7").satisfied_by? rails_version
  33. end
  34. def add_gems
  35. gem 'administrate', github: "excid3/administrate", branch: "zeitwerk"
  36. gem 'bootstrap', '~> 4.3', '>= 4.3.1'
  37. gem 'devise', '~> 4.6', '>= 4.6.1'
  38. gem 'devise-bootstrapped', github: 'excid3/devise-bootstrapped', branch: 'bootstrap4'
  39. gem 'devise_masquerade', '~> 0.6.2'
  40. gem 'font-awesome-sass', '~> 5.6', '>= 5.6.1'
  41. gem 'friendly_id', '~> 5.2', '>= 5.2.5'
  42. gem 'gravatar_image_tag', github: 'mdeering/gravatar_image_tag'
  43. gem 'mini_magick', '~> 4.9', '>= 4.9.2'
  44. gem 'name_of_person', '~> 1.1'
  45. gem 'omniauth-facebook', '~> 5.0'
  46. gem 'omniauth-github', '~> 1.3'
  47. gem 'omniauth-twitter', '~> 1.4'
  48. gem 'sidekiq', '~> 5.2', '>= 5.2.5'
  49. gem 'sitemap_generator', '~> 6.0', '>= 6.0.1'
  50. gem 'whenever', require: false
  51. if rails_5?
  52. gem 'webpacker', '~> 4.0.0.rc.7'
  53. end
  54. end
  55. def set_application_name
  56. # Add Application Name to Config
  57. environment "config.application_name = Rails.application.class.parent_name"
  58. # Announce the user where he can change the application name in the future.
  59. puts "You can change application name inside: ./config/application.rb"
  60. end
  61. def add_users
  62. # Install Devise
  63. generate "devise:install"
  64. # Configure Devise
  65. environment "config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }",
  66. env: 'development'
  67. route "root to: 'home#index'"
  68. # Devise notices are installed via Bootstrap
  69. generate "devise:views:bootstrapped"
  70. # Create Devise User
  71. generate :devise, "User",
  72. "first_name",
  73. "last_name",
  74. "announcements_last_read_at:datetime",
  75. "admin:boolean"
  76. # Set admin default to false
  77. in_root do
  78. migration = Dir.glob("db/migrate/*").max_by{ |f| File.mtime(f) }
  79. gsub_file migration, /:admin/, ":admin, default: false"
  80. end
  81. if Gem::Requirement.new("> 5.2").satisfied_by? rails_version
  82. gsub_file "config/initializers/devise.rb",
  83. / # config.secret_key = .+/,
  84. " config.secret_key = Rails.application.credentials.secret_key_base"
  85. end
  86. # Add Devise masqueradable to users
  87. inject_into_file("app/models/user.rb", "omniauthable, :masqueradable, :", after: "devise :")
  88. end
  89. def add_webpack
  90. # Rails 6+ comes with webpacker by default, so we can skip this step
  91. return if rails_6?
  92. # Our application layout already includes the javascript_pack_tag,
  93. # so we don't need to inject it
  94. rails_command 'webpacker:install'
  95. end
  96. def add_javascript
  97. run "yarn add expose-loader jquery popper.js bootstrap data-confirm-modal local-time"
  98. content = <<-JS
  99. require("local-time").start()
  100. window.Rails = Rails
  101. import 'bootstrap'
  102. import 'data-confirm-modal'
  103. $(document).on("turbolinks:load", () => {
  104. $('[data-toggle="tooltip"]').tooltip()
  105. $('[data-toggle="popover"]').popover()
  106. })
  107. JS
  108. append_to_file 'app/javascript/packs/application.js', content
  109. content = <<-JS
  110. const webpack = require('webpack')
  111. environment.plugins.append('Provide', new webpack.ProvidePlugin({
  112. $: 'jquery',
  113. jQuery: 'jquery',
  114. Rails: '@rails/ujs'
  115. }))
  116. JS
  117. insert_into_file 'config/webpack/environment.js', content + "\n\n", before: "module.exports = environment"
  118. end
  119. def copy_templates
  120. # Remove default application CSS
  121. # Our stylesheets get copied over from the app folder
  122. run "rm app/assets/stylesheets/application.css"
  123. copy_file "Procfile"
  124. copy_file "Procfile.dev"
  125. copy_file ".foreman"
  126. directory "app", force: true
  127. directory "config", force: true
  128. directory "lib", force: true
  129. route "get '/terms', to: 'home#terms'"
  130. route "get '/privacy', to: 'home#privacy'"
  131. end
  132. def add_sidekiq
  133. environment "config.active_job.queue_adapter = :sidekiq"
  134. insert_into_file "config/routes.rb",
  135. "require 'sidekiq/web'\n\n",
  136. before: "Rails.application.routes.draw do"
  137. content = <<-RUBY
  138. authenticate :user, lambda { |u| u.admin? } do
  139. mount Sidekiq::Web => '/sidekiq'
  140. end
  141. RUBY
  142. insert_into_file "config/routes.rb", "#{content}\n\n", after: "Rails.application.routes.draw do\n"
  143. end
  144. def add_announcements
  145. generate "model Announcement published_at:datetime announcement_type name description:text"
  146. route "resources :announcements, only: [:index]"
  147. end
  148. def add_notifications
  149. generate "model Notification recipient_id:bigint actor_id:bigint read_at:datetime action:string notifiable_id:bigint notifiable_type:string"
  150. route "resources :notifications, only: [:index]"
  151. end
  152. def add_administrate
  153. generate "administrate:install"
  154. gsub_file "app/dashboards/announcement_dashboard.rb",
  155. /announcement_type: Field::String/,
  156. "announcement_type: Field::Select.with_options(collection: Announcement::TYPES)"
  157. gsub_file "app/dashboards/user_dashboard.rb",
  158. /email: Field::String/,
  159. "email: Field::String,\n password: Field::String.with_options(searchable: false)"
  160. gsub_file "app/dashboards/user_dashboard.rb",
  161. /FORM_ATTRIBUTES = \[/,
  162. "FORM_ATTRIBUTES = [\n :password,"
  163. gsub_file "app/controllers/admin/application_controller.rb",
  164. /# TODO Add authentication logic here\./,
  165. "redirect_to '/', alert: 'Not authorized.' unless user_signed_in? && current_user.admin?"
  166. environment do <<-RUBY
  167. # Expose our application's helpers to Administrate
  168. config.to_prepare do
  169. Administrate::ApplicationController.helper #{@app_name.camelize}::Application.helpers
  170. end
  171. RUBY
  172. end
  173. end
  174. def add_multiple_authentication
  175. insert_into_file "config/routes.rb",
  176. ', controllers: { omniauth_callbacks: "users/omniauth_callbacks" }',
  177. after: " devise_for :users"
  178. generate "model Service user:references provider uid access_token access_token_secret refresh_token expires_at:datetime auth:text"
  179. template = """
  180. env_creds = Rails.application.credentials[Rails.env.to_sym] || {}
  181. %w{ facebook twitter github }.each do |provider|
  182. if options = env_creds[provider]
  183. config.omniauth provider, options[:app_id], options[:app_secret], options.fetch(:options, {})
  184. end
  185. end
  186. """.strip
  187. insert_into_file "config/initializers/devise.rb", " " + template + "\n\n",
  188. before: " # ==> Warden configuration"
  189. end
  190. def add_whenever
  191. run "wheneverize ."
  192. end
  193. def add_friendly_id
  194. generate "friendly_id"
  195. insert_into_file(
  196. Dir["db/migrate/**/*friendly_id_slugs.rb"].first,
  197. "[5.2]",
  198. after: "ActiveRecord::Migration"
  199. )
  200. end
  201. def stop_spring
  202. run "spring stop"
  203. end
  204. def add_sitemap
  205. rails_command "sitemap:install"
  206. end
  207. # Main setup
  208. add_template_repository_to_source_path
  209. add_gems
  210. after_bundle do
  211. set_application_name
  212. stop_spring
  213. add_users
  214. add_webpack
  215. add_javascript
  216. add_announcements
  217. add_notifications
  218. add_multiple_authentication
  219. add_sidekiq
  220. add_friendly_id
  221. copy_templates
  222. add_whenever
  223. add_sitemap
  224. # Migrate
  225. rails_command "db:create"
  226. rails_command "db:migrate"
  227. # Migrations must be done before this
  228. add_administrate
  229. # Commit everything to git
  230. git :init
  231. git add: "."
  232. git commit: %Q{ -m 'Initial commit' }
  233. say
  234. say "Jumpstart app successfully created! 🙌", :blue
  235. say
  236. say "To get started with your new app:", :green
  237. say "`cd #{app_name}` - Switch to your new app's directory."
  238. say "`foreman start` - Run Rails, sidekiq, and webpack-dev-server."
  239. end