template.rb 7.1 KB

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