Class: Decidim::Generators::AppGenerator

Inherits:
Rails::Generators::AppGenerator
  • Object
show all
Defined in:
lib/decidim/generators/app_generator.rb

Overview

Generates a Rails app and installs decidim to it. Uses the default Rails generator for most of the work.

Remember that, for how generators work, actions are executed based on the definition order of the public methods.

Instance Method Summary collapse

Instance Method Details

#add_ignore_tailwind_configurationObject



303
304
305
# File 'lib/decidim/generators/app_generator.rb', line 303

def add_ignore_tailwind_configuration
  append_file ".gitignore", "\n\n# Ignore Tailwind configuration\ntailwind.config.js" unless options["skip_git"]
end

#add_production_gems(&block) ⇒ Object



235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/decidim/generators/app_generator.rb', line 235

def add_production_gems(&block)
  return if options[:skip_gemfile]

  if block
    @production_gems ||= []
    @production_gems << block
  elsif @production_gems.present?
    gem_group :production do
      @production_gems.map(&:call)
    end
  end
end

#add_queue_adapterObject



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/decidim/generators/app_generator.rb', line 210

def add_queue_adapter
  adapter = options[:queue]

  abort("#{adapter} is not supported as a queue adapter, please use sidekiq for the moment") unless adapter.in?(["", "sidekiq"])

  return unless adapter == "sidekiq"

  template "sidekiq.yml.erb", "config/sidekiq.yml", force: true

  gsub_file "config/environments/production.rb",
            /# config.active_job.queue_adapter     = :resque/,
            "config.active_job.queue_adapter = ENV['QUEUE_ADAPTER'] if ENV['QUEUE_ADAPTER'].present?"

  prepend_file "config/routes.rb", "require \"sidekiq/web\"\n\n"
  route <<~RUBY
    authenticate :user, ->(u) { u.admin? } do
      mount Sidekiq::Web => "/sidekiq"
    end
  RUBY

  add_production_gems do
    gem "sidekiq"
  end
end

#add_storage_providerObject



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/decidim/generators/app_generator.rb', line 193

def add_storage_provider
  template "storage.yml.erb", "config/storage.yml", force: true

  providers = options[:storage].split(",")

  abort("#{providers} is not supported as storage provider, please use local, s3, gcs or azure") unless (providers - %w(local s3 gcs azure)).empty?
  gsub_file "config/environments/production.rb",
            /config.active_storage.service = :local/,
            "config.active_storage.service = Rails.application.secrets.dig(:storage, :provider) || :local"

  add_production_gems do
    gem "aws-sdk-s3", require: false if providers.include?("s3")
    gem "azure-storage-blob", require: false if providers.include?("azure")
    gem "google-cloud-storage", "~> 1.11", require: false if providers.include?("gcs")
  end
end

#authorization_handlerObject



362
363
364
365
366
367
368
# File 'lib/decidim/generators/app_generator.rb', line 362

def authorization_handler
  return unless options[:demo]

  copy_file "dummy_authorization_handler.rb", "app/services/dummy_authorization_handler.rb"
  copy_file "another_dummy_authorization_handler.rb", "app/services/another_dummy_authorization_handler.rb"
  copy_file "verifications_initializer.rb", "config/initializers/decidim_verifications.rb"
end

#budgets_workflowsObject



378
379
380
381
382
383
384
385
# File 'lib/decidim/generators/app_generator.rb', line 378

def budgets_workflows
  return unless options[:demo]

  copy_file "budgets_workflow_random.rb", "lib/budgets_workflow_random.rb"
  copy_file "budgets_workflow_random.en.yml", "config/locales/budgets_workflow_random.en.yml"

  copy_file "budgets_initializer.rb", "config/initializers/decidim_budgets.rb"
end

#cable_ymlObject



140
141
142
# File 'lib/decidim/generators/app_generator.rb', line 140

def cable_yml
  template "cable.yml.erb", "config/cable.yml", force: true
end

#database_ymlObject



123
124
125
# File 'lib/decidim/generators/app_generator.rb', line 123

def database_yml
  template "database.yml.erb", "config/database.yml", force: true
end

#decidim_controllerObject



127
128
129
# File 'lib/decidim/generators/app_generator.rb', line 127

def decidim_controller
  template "decidim_controller.rb.erb", "app/controllers/decidim_controller.rb", force: true
end

#decidim_initializerObject



316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# File 'lib/decidim/generators/app_generator.rb', line 316

def decidim_initializer
  copy_file "initializer.rb", "config/initializers/decidim.rb"

  gsub_file "config/environments/production.rb",
            /config.log_level = :info/,
            "config.log_level = %w(debug info warn error fatal).include?(ENV['RAILS_LOG_LEVEL']) ? ENV['RAILS_LOG_LEVEL'] : :info"

  gsub_file "config/environments/production.rb",
            %r{# config.asset_host = "http://assets.example.com"},
            "config.asset_host = ENV['RAILS_ASSET_HOST'] if ENV['RAILS_ASSET_HOST'].present?"

  if options[:force_ssl] == "false"
    gsub_file "config/initializers/decidim.rb",
              /# config.force_ssl = true/,
              "config.force_ssl = false"
  end
  return if options[:locales].blank?

  gsub_file "config/initializers/decidim.rb",
            /#{Regexp.escape("# config.available_locales = %w(en ca es)")}/,
            "config.available_locales = %w(#{options[:locales].gsub(",", " ")})"
  gsub_file "config/initializers/decidim.rb",
            /#{Regexp.escape("config.available_locales = Rails.application.secrets.decidim[:available_locales].presence || [:en]")}/,
            "# config.available_locales = Rails.application.secrets.decidim[:available_locales].presence || [:en]"
end

#dev_performance_configObject



342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# File 'lib/decidim/generators/app_generator.rb', line 342

def dev_performance_config
  gsub_file "config/environments/development.rb", /^end\n$/, <<~CONFIG

      # Performance configs for local testing
      if ENV.fetch("RAILS_BOOST_PERFORMANCE", false).to_s == "true"
        # Indicate boost performance mode
        config.boost_performance = true
        # Enable caching and eager load
        config.eager_load = true
        config.cache_classes = true
        # Logging
        config.log_level = :info
        config.action_view.logger = nil
        # Compress the HTML responses with gzip
        config.middleware.use Rack::Deflater
      end
    end
  CONFIG
end

#dockerObject



131
132
133
134
# File 'lib/decidim/generators/app_generator.rb', line 131

def docker
  template "Dockerfile.erb", "Dockerfile"
  template "docker-compose.yml.erb", "docker-compose.yml"
end

#etherpadObject



136
137
138
# File 'lib/decidim/generators/app_generator.rb', line 136

def etherpad
  template "docker-compose-etherpad.yml", "docker-compose-etherpad.yml"
end

#gemfileObject



164
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
# File 'lib/decidim/generators/app_generator.rb', line 164

def gemfile
  return if options[:skip_gemfile]

  if branch.present?
    get target_gemfile, "Gemfile", force: true
    append_file "Gemfile", %(\ngem "net-imap", "~> 0.2.3", group: :development)
    append_file "Gemfile", %(\ngem "net-pop", "~> 0.1.1", group: :development)
    append_file "Gemfile", %(\ngem "net-smtp", "~> 0.3.1", group: :development)
    get "#{target_gemfile}.lock", "Gemfile.lock", force: true
  else
    copy_file target_gemfile, "Gemfile", force: true
    copy_file "#{target_gemfile}.lock", "Gemfile.lock", force: true
  end

  gsub_file "Gemfile", /gem "#{current_gem}".*/, "gem \"#{current_gem}\", #{gem_modifier}"

  return unless current_gem == "decidim"

  gsub_file "Gemfile", /gem "decidim-dev".*/, "gem \"decidim-dev\", #{gem_modifier}"

  %w(conferences design initiatives templates).each do |component|
    if options[:demo]
      gsub_file "Gemfile", /gem "decidim-#{component}".*/, "gem \"decidim-#{component}\", #{gem_modifier}"
    else
      gsub_file "Gemfile", /gem "decidim-#{component}".*/, "# gem \"decidim-#{component}\", #{gem_modifier}"
    end
  end
end

#installObject



411
412
413
414
415
416
417
418
419
420
421
# File 'lib/decidim/generators/app_generator.rb', line 411

def install
  Decidim::Generators::InstallGenerator.start(
    [
      "--recreate_db=#{options[:recreate_db]}",
      "--seed_db=#{options[:seed_db]}",
      "--skip_gemfile=#{options[:skip_gemfile]}",
      "--app_name=#{app_name}",
      "--profiling=#{options[:profiling]}"
    ]
  )
end

#licenseObject



148
149
150
# File 'lib/decidim/generators/app_generator.rb', line 148

def license
  template "LICENSE-AGPLv3.txt", "LICENSE-AGPLv3.txt"
end

#load_defaults_rails61Object



248
249
250
251
252
# File 'lib/decidim/generators/app_generator.rb', line 248

def load_defaults_rails61
  gsub_file "config/application.rb",
            /config.load_defaults 7.0/,
            "config.load_defaults 6.1"
end

#machine_translation_serviceObject



403
404
405
406
407
408
409
# File 'lib/decidim/generators/app_generator.rb', line 403

def machine_translation_service
  return unless options[:demo]

  gsub_file "config/initializers/decidim.rb",
            /# config.machine_translation_service = "MyTranslationService"/,
            "config.machine_translation_service = 'Decidim::Dev::DummyTranslator'"
end

#modify_gitignoreObject



282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/decidim/generators/app_generator.rb', line 282

def modify_gitignore
  return if options[:skip_git]

  append_file ".gitignore", <<~GITIGNORE

    # Ignore env configuration files
    .env
    .envrc
    .rbenv-vars

    # Ignore the files and folders generated through Webpack
    /public/decidim-packs
    /public/packs-test
    /public/sw.js
    /public/sw.js.map

    # Ignore node modules
    /node_modules
  GITIGNORE
end

#node_versionObject



160
161
162
# File 'lib/decidim/generators/app_generator.rb', line 160

def node_version
  copy_file ".node-version", ".node-version"
end

#pdf_signature_serviceObject



395
396
397
398
399
400
401
# File 'lib/decidim/generators/app_generator.rb', line 395

def pdf_signature_service
  return unless options[:demo]

  gsub_file "config/initializers/decidim.rb",
            /# config.pdf_signature_service = "MyPDFSignatureService"/,
            "config.pdf_signature_service = \"Decidim::Initiatives::PdfSignatureExample\""
end

#puma_ssl_optionsObject



264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/decidim/generators/app_generator.rb', line 264

def puma_ssl_options
  return unless options[:dev_ssl]

  append_file "config/puma.rb", <<~CONFIG

    # Development SSL
    if ENV["DEV_SSL"] && defined?(Bundler) && (dev_gem = Bundler.load.specs.find { |spec| spec.name == "decidim-dev" })
      cert_dir = ENV.fetch("DEV_SSL_DIR") { "\#{dev_gem.full_gem_path}/lib/decidim/dev/assets" }
      ssl_bind(
        "0.0.0.0",
        ENV.fetch("DEV_SSL_PORT") { 3443 },
        cert_pem: File.read("\#{cert_dir}/ssl-cert.pem"),
        key_pem: File.read("\#{cert_dir}/ssl-key.pem")
      )
    end
  CONFIG
end

#readmeObject



144
145
146
# File 'lib/decidim/generators/app_generator.rb', line 144

def readme
  template "README.md.erb", "README.md", force: true
end

#remove_default_error_pagesObject



307
308
309
310
# File 'lib/decidim/generators/app_generator.rb', line 307

def remove_default_error_pages
  remove_file "public/404.html"
  remove_file "public/500.html"
end

#remove_default_faviconObject



312
313
314
# File 'lib/decidim/generators/app_generator.rb', line 312

def remove_default_favicon
  remove_file "public/favicon.ico"
end

#remove_old_assetsObject



101
102
103
104
105
# File 'lib/decidim/generators/app_generator.rb', line 101

def remove_old_assets
  remove_file "config/initializers/assets.rb"
  remove_dir("app/assets")
  remove_dir("app/javascript")
end

#remove_sprockets_requirementObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/decidim/generators/app_generator.rb', line 107

def remove_sprockets_requirement
  gsub_file "config/application.rb", %r{require ['"]rails/all['"]\R}, <<~RUBY
    require "decidim/rails"

    # Add the frameworks used by your app that are not loaded by Decidim.
    # require "action_mailbox/engine"
    # require "action_text/engine"
    require "action_cable/engine"
    require "rails/test_unit/railtie"
  RUBY

  gsub_file "config/environments/development.rb", /config\.assets.*$/, ""
  gsub_file "config/environments/test.rb", /config\.assets.*$/, ""
  gsub_file "config/environments/production.rb", /config\.assets.*$/, ""
end

#rubocopObject



152
153
154
# File 'lib/decidim/generators/app_generator.rb', line 152

def rubocop
  copy_file ".rubocop.yml", ".rubocop.yml"
end

#ruby_versionObject



156
157
158
# File 'lib/decidim/generators/app_generator.rb', line 156

def ruby_version
  copy_file ".ruby-version", ".ruby-version", force: true
end

#sms_gatewayObject



370
371
372
373
374
375
376
# File 'lib/decidim/generators/app_generator.rb', line 370

def sms_gateway
  return unless options[:demo]

  gsub_file "config/initializers/decidim.rb",
            /# config.sms_gateway_service = "MySMSGatewayService"/,
            "config.sms_gateway_service = 'Decidim::Verifications::Sms::ExampleGateway'"
end

#source_pathsObject



19
20
21
22
23
24
# File 'lib/decidim/generators/app_generator.rb', line 19

def source_paths
  [
    self.class.source_root,
    Rails::Generators::AppGenerator.source_root
  ]
end

#timestamp_serviceObject



387
388
389
390
391
392
393
# File 'lib/decidim/generators/app_generator.rb', line 387

def timestamp_service
  return unless options[:demo]

  gsub_file "config/initializers/decidim.rb",
            /# config.timestamp_service = "MyTimestampService"/,
            "config.timestamp_service = \"Decidim::Initiatives::DummyTimestamp\""
end

#tweak_csp_initializerObject



254
255
256
257
258
259
260
261
262
# File 'lib/decidim/generators/app_generator.rb', line 254

def tweak_csp_initializer
  return unless File.exist?("config/initializers/content_security_policy.rb")

  remove_file("config/initializers/content_security_policy.rb")
  create_file "config/initializers/content_security_policy.rb" do
    %(# For tuning the Content Security Policy, check the Decidim documentation site
# https://docs.decidim.org/develop/en/customize/content_security_policy)
  end
end

#webpacker_gemfile_entryObject

we disable the webpacker installation as we will use shakapacker



97
98
99
# File 'lib/decidim/generators/app_generator.rb', line 97

def webpacker_gemfile_entry
  []
end