Class: EasyCaddy::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/easy_caddy/parser.rb

Overview

Minimal Caddyfile parser — extracts site blocks, domains, and reverse_proxy ports. Not a full grammar; covers the patterns ecaddy generates.

Defined Under Namespace

Classes: ParsedConfig

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content) ⇒ Parser

Returns a new instance of Parser.



13
14
15
# File 'lib/easy_caddy/parser.rb', line 13

def initialize(content)
  @content = content
end

Class Method Details

.infer_name(content) ⇒ Object

Derive a project name from the first non-vite domain. “fishme.localhost” → “fishme”, “vite.fishme.localhost” is skipped.



31
32
33
34
35
36
37
# File 'lib/easy_caddy/parser.rb', line 31

def self.infer_name(content)
  domains = parse(content).domains
  primary = domains.reject { |d| d.start_with?('vite.') }.first
  return unless primary

  primary.sub(/\.localhost$/, '')
end

.parse(content) ⇒ Object



9
10
11
# File 'lib/easy_caddy/parser.rb', line 9

def self.parse(content)
  new(content).parse
end

Instance Method Details

#parseObject



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/easy_caddy/parser.rb', line 17

def parse
  domains   = []
  ports     = []
  log_paths = []

  @content.scan(/^([\w.*-]+\.localhost)\s*\{/) { domains << Regexp.last_match(1) }
  @content.scan(/reverse_proxy\s+localhost:(\d+)/) { ports << Regexp.last_match(1).to_i }
  @content.scan(/\boutput\s+file\s+(\S+)/) { log_paths << Regexp.last_match(1) }

  ParsedConfig.new(domains: domains.uniq, ports: ports.uniq, log_paths: log_paths.uniq)
end