Class: Anvil::EnvLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/anvil/env_loader.rb

Overview

Simple .env file loader (no dependencies required!)

Class Method Summary collapse

Class Method Details

.load(path = '.env') ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/anvil/env_loader.rb', line 6

def self.load(path = '.env')
  return unless File.exist?(path)

  File.readlines(path).each do |line|
    line = line.chomp # Remove newline

    # Skip comments and empty lines
    next if line.strip.empty? || line.strip.start_with?('#')

    # Parse KEY=value format
    next unless line =~ /\A([A-Z_][A-Z0-9_]*)\s*=\s*(.*)\z/

    key = ::Regexp.last_match(1)
    value = ::Regexp.last_match(2).strip

    # Remove quotes if present
    value = value[1..-2] if (value.start_with?('"') && value.end_with?('"')) ||
                            (value.start_with?("'") && value.end_with?("'"))

    # Set environment variable
    ENV[key] = value
  end
end