template.rb 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. "http://gogs.anxgang.com/Hiskio/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 'bootstrap', '~> 4.1', '>= 4.1.1'
  27. gem 'devise', '~> 4.5', '>= 4.4.3'
  28. gem 'devise-bootstrapped', github: 'excid3/devise-bootstrapped', branch: 'bootstrap4'
  29. gem 'font-awesome-sass', '~> 5.0', '>= 5.0.13'
  30. gem 'gravatar_image_tag', github: 'mdeering/gravatar_image_tag'
  31. gem 'jquery-rails', '~> 4.3.1'
  32. gem 'mini_magick', '~> 4.8'
  33. gem 'omniauth-facebook', '~> 5.0'
  34. gem 'omniauth-github', '~> 1.3'
  35. gem 'omniauth-twitter', '~> 1.4'
  36. gem 'sidekiq', '~> 5.1', '>= 5.1.3'
  37. gem 'webpacker', '~> 3.5', '>= 3.5.3'
  38. gem 'procodile', '~> 1.0', '>= 1.0.16'
  39. end
  40. def set_application_name
  41. # Add Application Name to Config
  42. environment "config.application_name = Rails.application.class.parent_name"
  43. # Announce the user where he can change the application name in the future.
  44. puts "You can change application name inside: ./config/application.rb"
  45. end
  46. def add_users
  47. # Install Devise
  48. generate "devise:install"
  49. # Configure Devise
  50. environment "config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }",
  51. env: 'development'
  52. route "root to: 'home#index'"
  53. # Devise notices are installed via Bootstrap
  54. generate "devise:views:bootstrapped"
  55. # Create Devise User
  56. generate :devise, "User",
  57. "first_name",
  58. "last_name",
  59. "announcements_last_read_at:datetime",
  60. "admin:boolean"
  61. # Set admin default to false
  62. in_root do
  63. migration = Dir.glob("db/migrate/*").max_by{ |f| File.mtime(f) }
  64. gsub_file migration, /:admin/, ":admin, default: false"
  65. end
  66. requirement = Gem::Requirement.new("> 5.2")
  67. rails_version = Gem::Version.new(Rails::VERSION::STRING)
  68. if requirement.satisfied_by? rails_version
  69. gsub_file "config/initializers/devise.rb",
  70. / # config.secret_key = .+/,
  71. " config.secret_key = Rails.application.credentials.secret_key_base"
  72. end
  73. end
  74. def add_bootstrap
  75. # Remove Application CSS
  76. run "rm app/assets/stylesheets/application.css"
  77. # Add Bootstrap JS
  78. insert_into_file(
  79. "app/assets/javascripts/application.js",
  80. "\n//= require jquery\n//= require popper\n//= require bootstrap\n//= require data-confirm-modal\n//= require local-time",
  81. after: "//= require rails-ujs"
  82. )
  83. end
  84. def copy_templates
  85. directory "app", force: true
  86. directory "config", force: true
  87. directory "lib", force: true
  88. route "get '/terms', to: 'home#terms'"
  89. route "get '/privacy', to: 'home#privacy'"
  90. end
  91. def add_webpack
  92. rails_command 'webpacker:install'
  93. rails_command 'webpacker:install:stimulus'
  94. end
  95. def add_sidekiq
  96. environment "config.active_job.queue_adapter = :sidekiq"
  97. insert_into_file "config/routes.rb",
  98. "require 'sidekiq/web'\n\n",
  99. before: "Rails.application.routes.draw do"
  100. insert_into_file "config/routes.rb",
  101. " authenticate :user, lambda { |u| u.admin? } do\n mount Sidekiq::Web => '/sidekiq'\n end\n\n",
  102. after: "Rails.application.routes.draw do\n"
  103. end
  104. def add_procodile
  105. copy_file "Procfile"
  106. end
  107. def add_multiple_authentication
  108. insert_into_file "config/routes.rb",
  109. ', controllers: { omniauth_callbacks: "users/omniauth_callbacks" }',
  110. after: " devise_for :users"
  111. generate "model Service user:references provider uid access_token access_token_secret refresh_token expires_at:datetime auth:text"
  112. template = """
  113. if Rails.application.secrets.facebook_app_id.present? && Rails.application.secrets.facebook_app_secret.present?
  114. config.omniauth :facebook, Rails.application.secrets.facebook_app_id, Rails.application.secrets.facebook_app_secret, scope: 'email,user_posts'
  115. end
  116. if Rails.application.secrets.twitter_app_id.present? && Rails.application.secrets.twitter_app_secret.present?
  117. config.omniauth :twitter, Rails.application.secrets.twitter_app_id, Rails.application.secrets.twitter_app_secret
  118. end
  119. if Rails.application.secrets.github_app_id.present? && Rails.application.secrets.github_app_secret.present?
  120. config.omniauth :github, Rails.application.secrets.github_app_id, Rails.application.secrets.github_app_secret
  121. end
  122. """.strip
  123. insert_into_file "config/initializers/devise.rb", " " + template + "\n\n",
  124. before: " # ==> Warden configuration"
  125. end
  126. def stop_spring
  127. run "spring stop"
  128. end
  129. # Main setup
  130. add_template_repository_to_source_path
  131. add_gems
  132. after_bundle do
  133. set_application_name
  134. stop_spring
  135. add_users
  136. add_bootstrap
  137. add_sidekiq
  138. add_procodile
  139. # add_webpack
  140. add_multiple_authentication
  141. copy_templates
  142. # Migrate
  143. rails_command "db:create"
  144. rails_command "db:migrate"
  145. # Migrations must be done before this
  146. git :init
  147. git add: "."
  148. git commit: %Q{ -m 'Initial commit' }
  149. end