Class: Rails::Worktrees::PumaConfigUpdater

Inherits:
Object
  • Object
show all
Defined in:
lib/rails/worktrees/puma_config_updater.rb

Overview

Safely updates config/puma.rb to bind Puma to the worktree-local DEV_PORT.

Defined Under Namespace

Classes: Result

Constant Summary collapse

STANDARD_PORT_LINE =
"port ENV['DEV_PORT'] || ENV.fetch('PORT', 3000)".freeze
CURRENT_PORT_PATTERN =
/\A\s*port\s+ENV\.fetch\(["']PORT["'],\s*3000\)\s*(?:#.*)?\z/
LEGACY_PORT_PATTERN =
/\A\s*port\s+ENV\.fetch\(["']PORT["']\)\s*\{\s*3000\s*\}\s*(?:#.*)?\z/
PORT_LINE_PATTERN =
/\A\s*port\s+/
DEV_PORT_ACCESS_PATTERN =
/ENV\[(["'])DEV_PORT\1\]/
DEV_PORT_FETCH_PATTERN =
/ENV\.fetch\((["'])DEV_PORT\1(?:\s*,|\s*\))/

Instance Method Summary collapse

Constructor Details

#initialize(content:) ⇒ PumaConfigUpdater

Returns a new instance of PumaConfigUpdater.



18
19
20
# File 'lib/rails/worktrees/puma_config_updater.rb', line 18

def initialize(content:)
  @content = content
end

Instance Method Details

#callObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rails/worktrees/puma_config_updater.rb', line 22

def call
  lines = @content.lines(chomp: true)
  return identical_result(@content) if dev_port_configured?(lines)

  port_line_indexes = supported_port_line_indexes(lines)
  return skip_result if port_line_indexes.empty?

  updated_content = rebuild_content(
    replace_port_lines(lines, port_line_indexes),
    trailing_newline: @content.end_with?("\n")
  )

  updated_result(updated_content)
end