template.rb 7.3 KB

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