Module: Unifig::Env::Parser

Defined in:
lib/unifig/env/parser.rb

Constant Summary collapse

Error =
Class.new(Unifig::Error)
WHITESPACE =
/\s*/.freeze
COMMENT =
/\#.*$/.freeze
EXPORT =
/export\s+/.freeze
KEY =
/[^=\s]+/.freeze
ASSIGNMENT =
/\s*=\s*/.freeze
SINGLE_LINE_VALUE =
/\S+/.freeze
QUOTE =
/['"]/.freeze
TO_END_OF_LINE =
/\s*(?:\#.*)?$/.freeze

Class Method Summary collapse

Class Method Details

.call(file_path) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/unifig/env/parser.rb', line 21

def call(file_path)
  content = File.read(file_path)

  vars = {}
  parser = StringScanner.new(content.strip)
  until parser.eos?
    parser.skip(WHITESPACE)

    next if parser.skip(COMMENT)

    parser.skip(EXPORT)
    key = extract!(parser, KEY, 'Missing env key on line %i')
    extract!(parser, ASSIGNMENT, 'Missing env var assignment on line %i')
    quote = parser.scan(QUOTE)
    value =
      if quote
        parser.scan(/.*?#{quote}/m).chop
      else
        parser.scan(SINGLE_LINE_VALUE)
      end

    extract!(parser, TO_END_OF_LINE, 'Invalid env var value on line %i')

    vars[key] = value
  end

  vars
end