Class: Hephaestus::ConfigGenerator

Inherits:
Generators::Base show all
Defined in:
lib/hephaestus/generators/config_generator.rb

Instance Method Summary collapse

Methods inherited from Generators::Base

default_source_root, inherited

Methods included from ExitOnFailure

#bundle_command

Methods included from Actions

#action_mailer_asset_host, #action_mailer_host, #configure_environment, #expand_json, #gem, #replace_in_file, #replace_in_files

Instance Method Details

#applicationObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/hephaestus/generators/config_generator.rb', line 8

def application
  config = <<~CONFIG


    if ENV.fetch("DEBUG", false) && defined?(Rails::Server) && Rails.env.development?
      require "debug/open_nonstop"
    end

  CONFIG

  inject_into_file(
    "config/application.rb",
    config,
    after: "require \"rails/test_unit/railtie\"",
  )
  inject_into_file(
    "config/application.rb",
    "\n  CURRENT_VERSION = \"2023-03-06\"\n\n",
    before: "class Application < Rails::Application",
  )

  raltie_overload = <<~CONFIG

    # Load dotenv only in development environment
    Dotenv::Rails.overwrite = true if Rails.env.development?
  CONFIG
  inject_into_file(
    "config/application.rb",
    raltie_overload,
    after: "Bundler.require(*Rails.groups)",
  )

  config = <<~CONFIG

    # Only loads a smaller set of middleware suitable for API-only Rails.
    # Middleware like session, flash, cookies can be added back manually.
    # Skip views, helpers and assets when generating a new resource.
    config.api_only = true

    Rails.root.glob("app/lib/#{app_name.underscore}/middleware/*.{rb}").each { |file| require_relative file }

    config.middleware.insert(0, PlugApp::Middleware::TracingAttributes)
    config.middleware.insert(0, PlugApp::Middleware::MalformedRequest)

    config.middleware.use(PlugApp::Middleware::OpenapiValidation)

    GIT_SHA = %x(git rev-parse HEAD).chomp
  CONFIG

  inject_into_file(
    "config/application.rb",
    config,
    before: "\n  end",
  )

  replace_in_file(
    "config/application.rb",
    "require \"rails\"\n",
    "",
  )

  comment_lines("config/application.rb", "require \"active_model/railtie\"")
  uncomment_lines("config/application.rb", "require \"active_job/railtie\"")
end

#bootObject



73
74
75
76
77
78
79
# File 'lib/hephaestus/generators/config_generator.rb', line 73

def boot
  boot = "require \"bootsnap/setup\" # Speed up boot time by caching expensive operations."
  inject_into_file(
    "config/boot.rb",
    boot,
  )
end

#configure_routesObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/hephaestus/generators/config_generator.rb', line 86

def configure_routes
  routes = <<~ROUTES
    root "root#index"

    # events into the plug, usually from yetto
    get "/api/2023-03-06/settings", to: "settings#new"
    post "/api/2023-03-06/:record_type/:event", to: "yetto#event"

    # inbound message
    post "/$app/2023-03-06/webhook/inbound", to: "app#webhook"

    # Staff pages
    get "staff", to: "staff#index"
    require "sidekiq/web"
    constraints ->(request) { StaffController.staff_request?(request) } do
      mount Sidekiq::Web => "staff/sidekiq"
    end

    #############################################
    # error pages -- these MUST be at the end! ##
    #############################################

    get "/500", to: "application#render500" if Rails.env.production? || Rails.env.staging?

    match "/", to: "application#not_found", via: :all
    match "/*unmatched_route", to: "application#not_found", via: :all
  ROUTES

  replace_in_file(
    "config/routes.rb",
    "# root \"articles#index\"",
    routes,
  )
end

#credentialsObject



125
126
127
# File 'lib/hephaestus/generators/config_generator.rb', line 125

def credentials
  remove_file("config/credentials.yml.enc")
end

#initializersObject



81
82
83
84
# File 'lib/hephaestus/generators/config_generator.rb', line 81

def initializers
  remove_dir("config/initializers")
  directory("config/initializers", "config/initializers")
end

#sidekiqObject



121
122
123
# File 'lib/hephaestus/generators/config_generator.rb', line 121

def sidekiq
  copy_file("config/sidekiq.yml", "config/sidekiq.yml")
end