Class: RailsSiteEngine::Generators::InstallGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/rails_site_engine/install/install_generator.rb

Constant Summary collapse

REQUIRED_HOST_GEMS =
%w[importmap-rails stimulus-rails].freeze
POSTGRES_ENV_DEFAULTS =
{
  "host" => '<%= ENV.fetch("POSTGRES_HOST", "127.0.0.1") %>',
  "port" => '<%= ENV.fetch("POSTGRES_PORT", 54325) %>',
  "username" => '<%= ENV.fetch("POSTGRES_USER", "postgres") %>',
  "password" => '<%= ENV.fetch("POSTGRES_PASSWORD", "postgres") %>'
}.freeze

Instance Method Summary collapse

Instance Method Details

#configure_bin_dev ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/generators/rails_site_engine/install/install_generator.rb', line 122

def configure_bin_dev
  write_if_changed(
    "bin/dev",
    <<~SH,
      #!/usr/bin/env bash
      set -euo pipefail

      cd "$(dirname "$0")/.."

      export POSTGRES_HOST="${POSTGRES_HOST:-127.0.0.1}"
      export POSTGRES_PORT="${POSTGRES_PORT:-54325}"
      export POSTGRES_USER="${POSTGRES_USER:-postgres}"
      export POSTGRES_PASSWORD="${POSTGRES_PASSWORD:-postgres}"

      exec bin/rails server "$@"
    SH
    mode: 0o755
  )
end

#configure_database_defaults ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/generators/rails_site_engine/install/install_generator.rb', line 142

def configure_database_defaults
  database_path = host_path("config/database.yml")
  return unless database_path.file?

  lines = database_path.read.lines
  default_start = lines.index { |line| line.match?(/^default:\s*&default/) }
  return if default_start.nil?

  default_end =
    ((default_start + 1)...lines.length).find { |index| lines[index].match?(/^[a-zA-Z0-9_]+:/) } || lines.length
  section = lines[default_start...default_end]

  section.reject! { |line| line.match?(/^\s*(host|port|username|password):/) }

  insert_after_pool = section.index { |line| line.match?(/^\s*pool:/) || line.match?(/^\s*max_connections:/) }
  insertion_index = insert_after_pool ? insert_after_pool + 1 : section.length

  env_lines = POSTGRES_ENV_DEFAULTS.map { |key, value| "  #{key}: #{value}\n" }
  section.insert(insertion_index, *env_lines)

  updated = lines[0...default_start] + section + lines[default_end..]
  write_if_changed("config/database.yml", updated.join)
end

#configure_development_action_mailer ⇒ Object



70
71
72
73
74
75
76
77
78
79
# File 'lib/generators/rails_site_engine/install/install_generator.rb', line 70

def configure_development_action_mailer
  development_path = host_path("config/environments/development.rb")
  return unless development_path.file?

  content = development_path.read
  updated = with_development_action_mailer_defaults(content)
  return if updated == content

  write_if_changed("config/environments/development.rb", updated)
end

#create_override_markers ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
# File 'lib/generators/rails_site_engine/install/install_generator.rb', line 110

def create_override_markers
  scaffold_profile_site_config
  scaffold_profile_theme_config
  empty_directory "content/pages"
  scaffold_profile_pages

  overrides_doc_path = host_path("content/OVERRIDES.md")
  return if overrides_doc_path.file?

  write_if_changed("content/OVERRIDES.md", overrides_markdown)
end

#create_site_engine_config ⇒ Object



103
104
105
106
107
108
# File 'lib/generators/rails_site_engine/install/install_generator.rb', line 103

def create_site_engine_config
  destination = host_path("config/site_engine.yml")
  return if destination.file?

  write_if_changed("config/site_engine.yml", site_engine_config_content)
end

#ensure_action_mailer_railtie ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/generators/rails_site_engine/install/install_generator.rb', line 48

def ensure_action_mailer_railtie
  application_path = host_path("config/application.rb")
  return unless application_path.file?

  content = application_path.read
  updated = with_action_mailer_railtie(content)
  return if updated == content

  write_if_changed("config/application.rb", updated)
end

#ensure_development_mail_preview_gem ⇒ Object



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

def ensure_development_mail_preview_gem
  gemfile_path = host_path("Gemfile")
  return unless gemfile_path.file?

  content = gemfile_path.read
  updated = with_letter_opener_web_gem(content)
  return if updated == content

  write_if_changed("Gemfile", updated)
end

#ensure_required_javascript_gems ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/generators/rails_site_engine/install/install_generator.rb', line 31

def ensure_required_javascript_gems
  gemfile_path = host_path("Gemfile")
  return unless gemfile_path.file?

  content = gemfile_path.read
  additions = REQUIRED_HOST_GEMS.filter_map do |gem_name|
    pattern = /^\s*gem ["']#{Regexp.escape(gem_name)}["']/
    next if content.match?(pattern)

    %(gem "#{gem_name}")
  end
  return if additions.empty?

  append_to_file "Gemfile", "\n#{additions.join("\n")}\n"
  say_status :warning, "Added #{additions.join(', ')} to Gemfile. Run bundle install."
end

#mount_engine ⇒ Object



92
93
94
95
96
97
98
99
100
101
# File 'lib/generators/rails_site_engine/install/install_generator.rb', line 92

def mount_engine
  routes_path = host_path("config/routes.rb")
  return unless routes_path.file?

  content = routes_path.read
  updated = with_engine_route(content)
  return if updated == content

  write_if_changed("config/routes.rb", updated)
end

#mount_letter_opener_web ⇒ Object



81
82
83
84
85
86
87
88
89
90
# File 'lib/generators/rails_site_engine/install/install_generator.rb', line 81

def mount_letter_opener_web
  routes_path = host_path("config/routes.rb")
  return unless routes_path.file?

  content = routes_path.read
  updated = with_letter_opener_web_route(content)
  return if updated == content

  write_if_changed("config/routes.rb", updated)
end