template.rb 7.5 KB

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