Class: RrxApi::Generators::InstallGenerator

Inherits:
Base
  • Object
show all
Defined in:
lib/generators/rrx_api/install_generator.rb

Instance Method Summary collapse

Methods inherited from Base

init!

Instance Method Details

#asdf_versionsObject



83
84
85
86
87
# File 'lib/generators/rrx_api/install_generator.rb', line 83

def asdf_versions
  create_file '.tool-versions', <<~VERSIONS
    ruby #{ruby_version}
  VERSIONS
end

#routesObject



70
71
72
73
74
75
76
77
# File 'lib/generators/rrx_api/install_generator.rb', line 70

def routes
  inject_into_file 'config/routes.rb',
                   after: "Rails.application.routes.draw do\n" do
    <<~RUBY
      mount RrxApi::Engine => '/'
    RUBY
  end
end

#rrx_devObject



79
80
81
# File 'lib/generators/rrx_api/install_generator.rb', line 79

def rrx_dev
  generate 'rrx_dev:install' unless options[:skip_rrx_dev]
end

#update_applicationvoid

This method returns an undefined value.

Updates the application configuration file with specific dependencies and settings. The method performs the following operations:

  • Reads the application configuration file located at ‘config/application.rb’.

  • Removes existing comments and unnecessary ‘require` statements from the file.

  • Includes necessary ‘require` directives for the application’s gem dependencies.

  • Injects or updates the Bundler require statement to include the necessary gems.

  • Cleans up unwanted whitespace in the file content.

  • Rewrites the configuration file with the updated content.

  • Appends additional configuration settings for time zone, schema format, and session management.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/generators/rrx_api/install_generator.rb', line 23

def update_application
  # @type [Pathname]
  app_file = Pathname(destination_root).join('config', 'application.rb')
  app_code = app_file.read

  # Assume full replace if we've never modified before.
  # Otherwise, create_file will prompt to replace it.
  remove_file app_file unless app_code =~ /rrx_api/

  app_code.gsub!(/^\s*#.*\r?\n/, '')
  app_code.gsub!(/^(?:#\s+)?require ["'].*\r?\n/, '')

  requires = application_gems.map do |gem|
    "require '#{gem}'"
  end.join("\n")

  app_code.sub!(/^(Bundler.require.*)$/) do |str|
    <<~REQ
      #{requires}

      #{str}
    REQ
  end

  # Remove existing application config lines
  APPLICATION_CONFIG.each do |line|
    app_code.gsub!(/^\s*#{line}\W*.*\n/, '')
  end

  # Remove unnecessary whitespace
  app_code.lstrip!
  app_code.gsub!(/^\s*\r?\n(\s*\r?\n)+/, "\n")

  # puts app_code
  create_file app_file, app_code
end

#update_base_classesObject



60
61
62
63
64
65
66
67
68
# File 'lib/generators/rrx_api/install_generator.rb', line 60

def update_base_classes
  gsub_file 'app/models/application_record.rb',
            /ApplicationRecord.*/,
            'ApplicationRecord < RrxApi::Record'

  gsub_file 'app/controllers/application_controller.rb',
            /ApplicationController.*/,
            'ApplicationController < RrxApi::Controller'
end