Class: Kowl::Overrides::AppGenerator

Inherits:
Rails::Generators::AppGenerator
  • Object
show all
Includes:
Actions, Docker
Defined in:
lib/kowl/generators/overrides/app_generator.rb

Constant Summary collapse

WEBPACKS =

If the person wants to use webpack in their application (vuew, react etc)

%w[react vue angular elm stimulus].freeze

Instance Method Summary collapse

Methods included from Docker

#alpine_docker_dependencies, #app_js_volumes, #app_volumes, #db_volumes, #debian_database_dependencies, #debian_docker_dependencies, #docker_app_command, #docker_compose_database_string, #docker_databases, #docker_depends_on, #docker_port_watcher, #docker_redis_service, #docker_sidekiq_service, #docker_variables, #docker_volumes, #docker_webpacker_service, #dockerfile_database_args, #dockerfile_migration_snip, #js_volumes, #mysql_volumes, #postgresql_volumes, #redis_volumes

Methods included from Actions

#add_extension_routes, #add_package, #append_to_file, #database_route, #dev_config, #dup_file, #file_exists?, #mailer_gems, #mailer_route, #mk_dir, #move_file, #pry_gems, #rails_cmd, #remove_dir, #remove_file, #remove_gem, #replace_string_in_file, #robocop_test_engine, #sidekiq_route, #template_linter_gems

Instance Method Details

#add_uuids_to_migrationsObject

If UUIDs are to be used, add id: :uuid to all migrations



206
207
208
209
210
211
212
213
214
# File 'lib/kowl/generators/overrides/app_generator.rb', line 206

def add_uuids_to_migrations
  return nil unless options[:database] == 'postgresql' && options[:uuid]

  Dir.foreach('db/migrate/') do |migration|
    next if %w[. ..].include?(migration) || migration.match(/_enable_pgcrypto_extension.rb$/)

    inject_into_file("db/migrate/#{migration}", ', id: :uuid', after: /create_table \:[a-z\_]+/)
  end
end

#initial_commitObject

Create a git initial commit



199
200
201
202
203
# File 'lib/kowl/generators/overrides/app_generator.rb', line 199

def initial_commit
  git :init
  git add: '.'
  git commit: "-aqm 'Initial Commit' --quiet"
end

#install_webpack_assetsObject

Rails6 has issues when genereting a new app using Oracle/sqlserver right away, this ensures webpack is installed if it previously failed because of the missing ruby-oci8/sqlserver gems In addition his also prevents a Psych error as the application is generated



236
237
238
# File 'lib/kowl/generators/overrides/app_generator.rb', line 236

def install_webpack_assets
  rails_command('webpacker:install') unless options[:skip_javascript]
end

#kowl_generatorsObject

Begin calling all the Kowl application generators to override the default rails application based on the options supplied



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/kowl/generators/overrides/app_generator.rb', line 127

def kowl_generators
  build :replace_gemfile
  build :procfile
  bundle_command 'install --jobs=4 --retry=3 --quiet'
  bundle_command 'exec spring stop' unless options[:skip_spring]
  invoke :install_webpack_assets
  invoke :setup_staging # Ran afterwards webpack is setup for oracle, because it also createas a staging webpacker file

  generate("kowl:config --database=#{options[:database]} --encrypt=#{options[:encrypt]} --framework=#{options[:framework]} --noauth=#{options[:noauth]} --skip_docker=#{options[:skip_docker]} --skip_erd=#{options[:skip_erd]} --skip_pagination=#{options[:skip_pagination]} --skip_sidekiq=#{options[:skip_sidekiq]} --skip_tests=#{options[:skip_tests]} --template_engine=#{options[:template_engine]} --simpleform=#{options[:simpleform]} --test_engine=#{options[:test_engine]} --uuid=#{options[:uuid]}")
  generate('kowl:uuid') if options[:database] == 'postgresql' && options[:uuid]
  generate("kowl:dotfiles --database=#{options[:database]} --encrypt=#{options[:encrypt]} --skip_docker=#{options[:skip_docker]} --skip_erd=#{options[:skip_erd]} --skip_javascript=#{options[:skip_javascript]} --skip_mailer=#{options[:skip_mailer]} --skip_pry=#{options[:skip_pry]} --skip_sidekiq=#{options[:skip_sidekiq]} --skip_tests=#{options[:skip_tests]} --test_engine=#{options[:test_engine]} --template_engine=#{options[:template_engine]} ")
  generate("kowl:assets --framework=#{options[:framework]} --noauth=#{options[:noauth]} --skip_javascript=#{options[:skip_javascript]} --skip_mailer=#{options[:skip_mailer]} --skip_turbolinks=#{options[:skip_turbolinks]}")
  generate("kowl:views_and_helpers --framework=#{options[:framework]} --noauth=#{options[:noauth]} --simpleform=#{options[:simpleform]} --skip_javascript=#{options[:skip_javascript]} --skip_pagination=#{options[:skip_pagination]} --skip_turbolinks=#{options[:skip_turbolinks]} --template_engine=#{options[:template_engine]}")
  generate("kowl:users_and_auth --database=#{options[:database]} --encrypt=#{options[:encrypt]} --framework=#{options[:framework]} --skip_javascript=#{options[:skip_javascript]} --skip_mailer=#{options[:skip_mailer]} --skip_pagination=#{options[:skip_pagination]}  --skip_sidekiq=#{options[:skip_sidekiq]} --template_engine=#{options[:template_engine]}") unless options[:noauth]
  # tests are generated after users, because devise migrations tries to create empty minitest and rspec migration tests
  generate("kowl:test --noauth=#{options[:noauth]} --test_engine=#{options[:test_engine]}") unless options[:test_engine] == :none || options[:skip_tests]
  generate('kowl:staging') unless options[:database] == 'sqlite3'
  generate("kowl:controller --noauth=#{options[:noauth]}")
  generate("kowl:database --database=#{options[:database]} --noauth=#{options[:noauth]}")
  generate("kowl:action --database=#{options[:database]} --simpleform=#{options[:simpleform]} --skip_javascript=#{options[:skip_javascript]}")
  generate("kowl:mailer --noauth=#{options[:noauth]} --mailer=#{options[:mailer]}") unless options[:skip_mailer]
  generate("kowl:sidekiq --database=#{options[:database]} --noauth=#{options[:noauth]}") unless options[:skip_sidekiq]
  generate('kowl:misc') # Generator misc application directores such as serializers, services, workers, etc.
  generate("kowl:decorators --noauth=#{options[:noauth]}")
  generate("kowl:text_files --framework=#{options[:framework]} --noauth=#{options[:noauth]}")
  generate("kowl:pages --framework=#{options[:framework]} --noauth=#{options[:noauth]} --template_engine=#{options[:template_engine]}") # Generate welcome pages and routes for the application
  generate("kowl:routes --database=#{options[:database]} --noauth=#{options[:noauth]} --skip_mailer=#{options[:skip_mailer]} --skip_sidekiq=#{options[:skip_sidekiq]}")
  generate('kowl:libs')

  # Generate admin dashboard if user authentication is enabled
  generate("kowl:docker --database=#{options[:database]} --docker_distro=#{options[:docker_distro]} --encrypt=#{options[:encrypt]} --skip_erd=#{options[:skip_erd]} --skip_javascript=#{options[:skip_javascript]} --skip_sidekiq=#{options[:skip_sidekiq]}") unless options[:skip_docker]

  rake 'db:migrate' unless (options[:database] == 'postgresql' && options[:uuid]) || options[:noauth]
  generate("kowl:admin --database=#{options[:database]} --skip_javascript=#{options[:skip_javascript]} --skip_turbolinks=#{options[:skip_turbolinks]} --uuid=#{options[:uuid]} ") unless options[:noauth]
  invoke :add_uuids_to_migrations if options[:database] == 'postgresql' && options[:uuid]
  invoke :remove_sqlite_yaml unless options[:database] == 'sqlite3'
  invoke :setup_spring unless options[:skip_spring]

  # Remove the kowl gem, it is initially required to run the rails generators
  # => Removing the gem removes the generators from application
  remove_gem('kowl')

  # This should be ran last, thus there will only be 1 commit when application is started
  # This is a method tather than a generate, because generations can't be called after the gem has been removed
  invoke :initial_commit unless options[:skip_git]
end

#remove_sqlite_yamlObject

While generating the application the config/database.yml is defaulted to using sqlite3.

This is because we can't run migrations, and various tasks without a valid database.yml
but this file should be remove and replace with the config/database[#{database_type}].yml file once the application is ready


219
220
221
222
223
224
225
# File 'lib/kowl/generators/overrides/app_generator.rb', line 219

def remove_sqlite_yaml
  return nil if options[:database] == 'sqlite3'

  db_file = "config/database[#{options[:database]}].yml"
  remove_file('config/database.yml')
  move_file(db_file, 'config/database.yml')
end

#run_after_bundle_callbacksObject

Used with Rails6 v6, because this has the application generate.

> We want it after the applicaton config and assets are generated, otherwise it generates a large number of errors



117
118
119
120
# File 'lib/kowl/generators/overrides/app_generator.rb', line 117

def run_after_bundle_callbacks
  super
  invoke :kowl_generators unless options[:skip_javascript]
end

#setup_springObject

Setup spring for the application unless specified otherwise



228
229
230
231
# File 'lib/kowl/generators/overrides/app_generator.rb', line 228

def setup_spring
  say 'Springifying binstubs'
  build :setup_spring unless options[:skip_spring]
end

#setup_stagingObject

Setup a staging envirnment for application config and webpacker



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/kowl/generators/overrides/app_generator.rb', line 175

def setup_staging
  template 'config/environments/production.rb.tt', 'config/environments/staging.rb'
  # Enable displaying error messages in staging
  replace_string_in_file('config/environments/staging.rb',
                         '^[\s]*?(config.consider_all_requests_local)[\s]*= false[\s]?$',
                         '  config.consider_all_requests_local       = true')
  # create a webpacker staging environment
  return if options[:skip_javascript]

  dup_file('config/webpack/production.js', 'config/webpack/staging.js')
  webpacker_str = <<~STAGING
    staging:
      <<: *default
      # Production depends on precompilation of packs prior to booting for performance.
      compile: false
      # Extract and emit a css file
      extract_css: true
      # Cache manifest.json for performance
      cache_manifest: true
  STAGING
  append_to_file('config/webpacker.yml', "\n#{webpacker_str}")
end