Module: Hammer::Dotenv
- Defined in:
- lib/hammer/dotenv.rb
Overview
Tiny built-in .env loader. Kept dependency-free on purpose - the gemspec advertises zero runtime deps.
Hammer.cli calls Dotenv.load(Dir.pwd) after evaluating the
Hammerfile (unless the Hammerfile said dotenv false), so vars
from .env are available to every command handler.
Semantics:
.envis loaded, then.env.local(latter overrides former).- Missing files are silently skipped - auto-load must not raise.
- Existing
ENV[k]always wins (ENV[k] ||= v); shell-set values are never clobbered. - Supported lines:
KEY=value,KEY="value",KEY='value', optional leadingexport,#comments, blank lines. - NOT supported:
${VAR}interpolation, multiline values, inline comments after a value. Reach for thedotenvgem from abeforehook if you need those.
Class Method Summary collapse
Class Method Details
.load(dir) ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/hammer/dotenv.rb', line 22 def self.load(dir) FILES.each do |name| path = File.join(dir, name) next unless File.file?(path) parse(File.read(path)).each { |k, v| ENV[k] ||= v } end end |
.parse(text) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/hammer/dotenv.rb', line 30 def self.parse(text) out = {} text.each_line do |line| line = line.strip next if line.empty? || line.start_with?('#') line = line.sub(/\Aexport\s+/, '') key, sep, val = line.partition('=') next if sep.empty? key = key.strip next if key.empty? val = val.strip if val.length >= 2 && ((val.start_with?('"') && val.end_with?('"')) || (val.start_with?("'") && val.end_with?("'"))) val = val[1..-2] end out[key] = val end out end |