Class: ThreddedCreateApp::Tasks::SetupAppSkeleton

Inherits:
Base
  • Object
show all
Defined in:
lib/thredded_create_app/tasks/setup_app_skeleton.rb

Overview

rubocop:disable Metrics/ClassLength

Constant Summary collapse

MATERIAL_700_COLORS =
[
  '#7B1FA2', # purple
  '#512DA8', # deep purple
  '#303F9F', # indigo
  '#1976D2', # blue
  '#0288D1', # light blue
  '#0097A7', # cyan
  '#00796B', # teal
  '#F57C00' # orange
].freeze

Instance Attribute Summary

Attributes inherited from Base

#app_hostname, #app_name, #app_path, #gems

Instance Method Summary collapse

Methods inherited from Base

#devise_form_fields_begin_pattern, #initialize, #webpack_js?

Methods included from RunCommand

#run

Methods included from Logging

#log_command, #log_error, #log_info, #log_stderr, #log_verbose, #log_warn, #program_name

Constructor Details

This class inherits a constructor from ThreddedCreateApp::Tasks::Base

Instance Method Details

#add_app_layoutObject

rubocop:disable Metrics/AbcSize,Metrics/MethodLength



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/thredded_create_app/tasks/setup_app_skeleton.rb', line 165

def add_app_layout
  copy 'setup_app_skeleton/_header.html.erb',
       'app/views/shared/_header.html.erb'
  copy 'setup_app_skeleton/_flash_messages.html.erb',
       'app/views/shared/_flash_messages.html.erb'
  app_helper_src = File.read(
    expand_src_path('setup_app_skeleton/application_helper_methods.rb')
  )
  inject_into_file 'app/helpers/application_helper.rb',
                   before: /end\n\z/,
                   content: indent(2, app_helper_src)
  copy 'setup_app_skeleton/themes_helper.rb',
       'app/helpers/themes_helper.rb'
  replace 'app/views/layouts/application.html.erb',
          %r{<title>.*?</title>},
          '<title><%= page_title %></title>'
  replace 'app/views/layouts/application.html.erb',
          /[ ]*<%= stylesheet_link_tag.*?%>/,
          indent(4, "            <%= stylesheet_link_tag current_theme, media: 'all', 'data-turbolinks-track': 'reload' %>\n          ERB\n  if webpack_js?\n    replace 'app/views/layouts/application.html.erb',\n            <<-'ERB',\n    <%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>\n            ERB\n            <<-'ERB'\n    <%= javascript_pack_tag 'application',\n                      async: true,\n                      defer: true,\n                      'data-turbolinks-track': 'reload' %>\n            ERB\n  else\n    inject_into_file 'app/views/layouts/application.html.erb',\n                     before: %r{\\s*</head>},\n                     content: <<-'ERB'\n    <%= javascript_include_tag 'application',\n                          async: !Rails.application.config.assets.debug,\n                          defer: true,\n                          'data-turbolinks-track': 'reload' %>\n                     ERB\n  end\n\n  inject_into_file 'app/views/layouts/application.html.erb',\n                   before: %r{\\s*</head>},\n                   content: <<-'ERB'\n\n    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1, user-scalable=no\">\n                   ERB\n\n  body = File.read(\n    expand_src_path('setup_app_skeleton/application.body.html.erb')\n  )\n  replace 'app/views/layouts/application.html.erb',\n          %r{<body>.*?</body>}m,\n          body\nend\n")

#add_config_varsObject



144
145
146
147
148
149
150
# File 'lib/thredded_create_app/tasks/setup_app_skeleton.rb', line 144

def add_config_vars
  copy_template 'setup_app_skeleton/config/settings.yml.erb',
                'config/settings.yml'
  replace 'app/mailers/application_mailer.rb',
          /default from: .*/,
          'default from: Settings.email_sender'
end

#add_favicon_and_touch_iconsObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/thredded_create_app/tasks/setup_app_skeleton.rb', line 51

def add_favicon_and_touch_icons
  FileUtils.mv 'public/favicon.ico',
               'app/assets/images/favicon.ico'
  FileUtils.mv 'public/apple-touch-icon.png',
               'app/assets/images/apple-touch-icon.png'
  # The `-precomposed.png` touch icon is only used by iOS < 7, remove it.
  # Do not raise if the file does not exist, as Rails will stop generating
  # it one of these days.
  FileUtils.rm 'public/apple-touch-icon-precomposed.png', force: true

  inject_into_file 'app/views/layouts/application.html.erb',
                   before: '    <%= csrf_meta_tags %>',
                   content: indent(4, "                     <%= favicon_link_tag 'favicon.ico' %>\n                     <%= favicon_link_tag 'apple-touch-icon.png',\n                                          rel: 'apple-touch-icon', type: 'image/png' %>\n                   ERB\nend\n")

#add_home_pageObject



245
246
247
248
249
250
251
252
253
254
# File 'lib/thredded_create_app/tasks/setup_app_skeleton.rb', line 245

def add_home_page
  run_generator 'controller home show' \
                ' --no-assets --no-helper --skip-routes' \
                ' --no-test-framework'
  add_route "    root to: 'home#show'\n  RUBY\n  copy_template 'setup_app_skeleton/home_show.html.erb.erb',\n                'app/views/home/show.html.erb'\nend\n", prepend: true

#add_i18nObject



152
153
154
155
156
157
158
159
160
161
162
# File 'lib/thredded_create_app/tasks/setup_app_skeleton.rb', line 152

def add_i18n
  copy_template 'setup_app_skeleton/config/locales/en.yml.erb',
                'config/locales/en.yml'
  copy_template 'setup_app_skeleton/initializers/02_i18n.rb',
                'config/initializers/02_i18n.rb'
  inject_into_file'config/application.rb',
                  before: / *end\nend\n\z/,
                  content: indent(4, "                    config.i18n.available_locales = %w(en)\n                  RUBY\nend\n")

#add_javascripts_sprocketsObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/thredded_create_app/tasks/setup_app_skeleton.rb', line 102

def add_javascripts_sprockets
  inject_into_file 'config/environments/production.rb',
                   after: "  # config.assets.css_compressor = :sass\n",
                   content: "  config.assets.js_compressor = Uglifier.new(harmony: true)\n                   RUBY\n\n  copy 'setup_app_skeleton/javascripts/application.js',\n       'app/assets/javascripts/application.js'\n  copy 'setup_app_skeleton/javascripts/app.js',\n       'app/assets/javascripts/app.js'\n  copy 'setup_app_skeleton/javascripts/app/',\n       'app/assets/javascripts/app/'\n  add_precompile_asset 'application.js'\n  git_commit 'Add app JavaScript'\nend\n"

#add_javascripts_webpackObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/thredded_create_app/tasks/setup_app_skeleton.rb', line 70

def add_javascripts_webpack
  append_to_file 'app/javascript/packs/application.js', "    require('monkey-patch-turbolinks.js');\n    require('app');\n  JS\n  copy 'setup_app_skeleton/javascript_webpack/monkey-patch-turbolinks.js',\n       'app/javascript/monkey-patch-turbolinks.js'\n  %w[app index theme time_ago].each do |file|\n    copy \"setup_app_skeleton/javascript_webpack/app/\#{file}.js\",\n         \"app/javascript/app/\#{file}.js\"\n  end\n  append_to_file 'config/initializers/content_security_policy.rb', <<~RUBY\n    Rails.application.config.content_security_policy do |policy|\n      # Allow connections to bin/webpack-dev-server in development\n      policy.connect_src :self, :https, 'http://localhost:3035', 'ws://localhost:3035' if Rails.env.development?\n    end\n  RUBY\n  git_commit 'Add app JavaScript'\nend\n"

#add_seedsObject



256
257
258
259
# File 'lib/thredded_create_app/tasks/setup_app_skeleton.rb', line 256

def add_seeds
  copy_template 'setup_app_skeleton/seeds.rb.erb',
                'db/seeds.rb'
end

#add_stylesObject



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/thredded_create_app/tasks/setup_app_skeleton.rb', line 119

def add_styles
  copy 'setup_app_skeleton/images/brightness.svg',
       'app/assets/images/brightness.svg'
  copy_template 'setup_app_skeleton/stylesheets/_variables.scss.erb',
                'app/assets/stylesheets/_variables.scss'
  copy 'setup_app_skeleton/stylesheets/_variables-night.scss',
       'app/assets/stylesheets/_variables-night.scss'
  copy 'setup_app_skeleton/stylesheets/_deps.scss',
       'app/assets/stylesheets/_deps.scss',
       mode: 'a'
  copy 'setup_app_skeleton/stylesheets/_app.scss',
       'app/assets/stylesheets/_app.scss'
  copy 'setup_app_skeleton/stylesheets/app/',
       'app/assets/stylesheets/app/'
  if File.file? 'app/assets/stylesheets/application.css'
    File.delete 'app/assets/stylesheets/application.css'
  end
  copy 'setup_app_skeleton/stylesheets/day.scss',
       'app/assets/stylesheets/day.scss'
  add_precompile_asset 'day.css'
  copy 'setup_app_skeleton/stylesheets/night.scss',
       'app/assets/stylesheets/night.scss'
  add_precompile_asset 'night.css'
end

#add_user_pageObject

rubocop:enable Metrics/AbcSize,Metrics/MethodLength



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/thredded_create_app/tasks/setup_app_skeleton.rb', line 224

def add_user_page
  run_generator 'controller users show' \
                ' --no-assets --no-helper --skip-routes' \
                ' --no-test-framework'
  copy 'setup_app_skeleton/spec/controllers/users_controller_spec.rb',
       'spec/controllers/users_controller_spec.rb'
  copy 'setup_app_skeleton/users_show.html.erb',
       'app/views/users/show.html.erb'
  replace 'app/controllers/users_controller.rb', 'def show', "  def show\n    @user = User.find(params[:id])\n  RUBY\n  # The route must be defined after `devise_for`.\n  # The route is injected before Thredded in case Thredded is mount at /.\n  inject_into_file 'config/routes.rb',\n                   before: /^\\s*mount Thredded::Engine/,\n                   content: indent(2, <<~'RUBY')\n                     resources :users, only: [:show]\n                   RUBY\nend\n"

#admin_emailObject



261
262
263
# File 'lib/thredded_create_app/tasks/setup_app_skeleton.rb', line 261

def admin_email
  "admin@#{app_hostname}"
end

#admin_passwordObject



265
266
267
# File 'lib/thredded_create_app/tasks/setup_app_skeleton.rb', line 265

def admin_password
  '123456'
end

#after_bundleObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/thredded_create_app/tasks/setup_app_skeleton.rb', line 31

def after_bundle
  add_config_vars
  add_i18n
  add_seeds
  configure_sprockets
  add_favicon_and_touch_icons
  if webpack_js?
    add_javascripts_webpack
  else
    add_javascripts_sprockets
  end
  add_styles
  add_user_page
  add_home_page
  add_app_layout
  copy 'setup_app_skeleton/spec/features/homepage_spec.rb',
       'spec/features/homepage_spec.rb'
  git_commit 'Set up basic app navigation and styles'
end

#before_bundleObject



22
23
24
25
26
27
28
29
# File 'lib/thredded_create_app/tasks/setup_app_skeleton.rb', line 22

def before_bundle
  unless webpack_js?
    add_gem 'babel-transpiler'
    add_gem 'uglifier'
    add_gem 'turbolinks'
  end
  add_gem 'rails-timeago'
end

#brand_primaryObject



269
270
271
# File 'lib/thredded_create_app/tasks/setup_app_skeleton.rb', line 269

def brand_primary
  @brand_primary ||= MATERIAL_700_COLORS.sample.downcase
end

#configure_sprocketsObject



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/thredded_create_app/tasks/setup_app_skeleton.rb', line 90

def configure_sprockets
  replace 'app/assets/config/manifest.js',
          "//= link_directory ../stylesheets .css\n",
          ''
  append_to_file 'config/initializers/assets.rb', "    # Work around https://github.com/rails/sprockets/issues/581\n    Rails.application.config.assets.configure do |env|\n      env.export_concurrent = false\n    end\n  RUBY\nend\n"

#summaryObject



18
19
20
# File 'lib/thredded_create_app/tasks/setup_app_skeleton.rb', line 18

def summary
  'Setup basic app navigation and styles'
end