dotenv 3.1.4 → 3.1.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bbefe05cecd5a12cdd9a9dc72b3ff2feee883c10f84c40c9105d3d9c1c5ffc3c
4
- data.tar.gz: a61066d9119426fa542acf6a17d7134033d17cef8bdf71fe87e964a5d4b5e925
3
+ metadata.gz: 9ef0c37e3854d57f689650f8dd106baf862506689c27a3151f19d1e56c8c205d
4
+ data.tar.gz: 933c17a9b3974de22f762da60230f449a8b7b75b94949995301905d146289591
5
5
  SHA512:
6
- metadata.gz: 9941e0374f811e9048db77c77731106b1b3f75eff0358d6c2a8ef24265857ccfb02d76409f733f1f930815f4b032e5939520ee6c0a48d3eaa00d734be036eb4b
7
- data.tar.gz: 83b856b11c9817f51151e4249a1fb5fd38b057720a049270ffa7fcf05a20547ce2db29e6690feb8f536478b0995549eaa52a90b5d1a019af5aa73ce6def3e561
6
+ metadata.gz: 9a8fed1ea223636257222b690ca9320328d382671fe4023015ddd3aebbd0c778b8f904dab59e80c89309fc01c97e7bb0981c3c2b61833a4884b0c3744608a54b
7
+ data.tar.gz: 1cbe9de7f7587cb500464130cc1069f064630eff8ba905fc438080830aec6d7bd193dd5b97b22f71446f78313e1840c4fd147677be2b134e00853f58dafe8ec2
data/README.md CHANGED
@@ -213,7 +213,7 @@ DATABASE_URL="postgres://$(whoami)@localhost/my_database"
213
213
 
214
214
  ### Variable Substitution
215
215
 
216
- You need to add the value of another variable in one of your variables? You can reference the variable with `${VAR}` or often just `$VAR` in unqoted or double-quoted values.
216
+ You need to add the value of another variable in one of your variables? You can reference the variable with `${VAR}` or often just `$VAR` in unquoted or double-quoted values.
217
217
 
218
218
  ```shell
219
219
  DATABASE_URL="postgres://${USER}@localhost/my_database"
data/lib/dotenv/parser.rb CHANGED
@@ -8,25 +8,29 @@ module Dotenv
8
8
  # Parses the `.env` file format into key/value pairs.
9
9
  # It allows for variable substitutions, command substitutions, and exporting of variables.
10
10
  class Parser
11
- @substitutions =
12
- [Dotenv::Substitutions::Variable, Dotenv::Substitutions::Command]
11
+ @substitutions = [
12
+ Dotenv::Substitutions::Variable,
13
+ Dotenv::Substitutions::Command
14
+ ]
13
15
 
14
16
  LINE = /
15
- (?:^|\A) # beginning of line
16
- \s* # leading whitespace
17
- (?:export\s+)? # optional export
18
- ([\w.]+) # key
19
- (?:\s*=\s*?|:\s+?) # separator
20
- ( # optional value begin
21
- \s*'(?:\\'|[^'])*' # single quoted value
22
- | # or
23
- \s*"(?:\\"|[^"])*" # double quoted value
24
- | # or
25
- [^\#\r\n]+ # unquoted value
26
- )? # value end
27
- \s* # trailing whitespace
28
- (?:\#.*)? # optional comment
29
- (?:$|\z) # end of line
17
+ (?:^|\A) # beginning of line
18
+ \s* # leading whitespace
19
+ (?<export>export\s+)? # optional export
20
+ (?<key>[\w.]+) # key
21
+ (?: # optional separator and value
22
+ (?:\s*=\s*?|:\s+?) # separator
23
+ (?<value> # optional value begin
24
+ \s*'(?:\\'|[^'])*' # single quoted value
25
+ | # or
26
+ \s*"(?:\\"|[^"])*" # double quoted value
27
+ | # or
28
+ [^\#\n]+ # unquoted value
29
+ )? # value end
30
+ )? # separator and value end
31
+ \s* # trailing whitespace
32
+ (?:\#.*)? # optional comment
33
+ (?:$|\z) # end of line
30
34
  /x
31
35
 
32
36
  class << self
@@ -38,41 +42,55 @@ module Dotenv
38
42
  end
39
43
 
40
44
  def initialize(string, overwrite: false)
41
- @string = string
45
+ # Convert line breaks to same format
46
+ @string = string.gsub(/[\n\r]+/, "\n")
42
47
  @hash = {}
43
48
  @overwrite = overwrite
44
49
  end
45
50
 
46
51
  def call
47
- # Convert line breaks to same format
48
- lines = @string.gsub(/\r\n?/, "\n")
49
- # Process matches
50
- lines.scan(LINE).each do |key, value|
51
- @hash[key] = parse_value(value || "")
52
- end
53
- # Process non-matches
54
- lines.gsub(LINE, "").split(/[\n\r]+/).each do |line|
55
- parse_line(line)
52
+ @string.scan(LINE) do
53
+ match = $LAST_MATCH_INFO
54
+
55
+ if existing?(match[:key])
56
+ # Use value from already defined variable
57
+ @hash[match[:key]] = ENV[match[:key]]
58
+ elsif match[:export] && !match[:value]
59
+ # Check for exported variable with no value
60
+ if !@hash.member?(match[:key])
61
+ raise FormatError, "Line #{match.to_s.inspect} has an unset variable"
62
+ end
63
+ else
64
+ @hash[match[:key]] = parse_value(match[:value] || "")
65
+ end
56
66
  end
67
+
57
68
  @hash
58
69
  end
59
70
 
60
71
  private
61
72
 
62
- def parse_line(line)
63
- if line.split.first == "export"
64
- if variable_not_set?(line)
65
- raise FormatError, "Line #{line.inspect} has an unset variable"
66
- end
67
- end
73
+ # Determine if a variable is already defined and should not be overwritten.
74
+ def existing?(key)
75
+ !@overwrite && key != "DOTENV_LINEBREAK_MODE" && ENV.key?(key)
68
76
  end
69
77
 
78
+ QUOTED_STRING = /\A(['"])(.*)\1\z/m
70
79
  def parse_value(value)
71
80
  # Remove surrounding quotes
72
- value = value.strip.sub(/\A(['"])(.*)\1\z/m, '\2')
81
+ value = value.strip.sub(QUOTED_STRING, '\2')
73
82
  maybe_quote = Regexp.last_match(1)
74
- value = unescape_value(value, maybe_quote)
75
- perform_substitutions(value, maybe_quote)
83
+
84
+ # Expand new lines in double quoted values
85
+ value = expand_newlines(value) if maybe_quote == '"'
86
+
87
+ # Unescape characters and performs substitutions unless value is single quoted
88
+ if maybe_quote != "'"
89
+ value = unescape_characters(value)
90
+ self.class.substitutions.each { |proc| value = proc.call(value, @hash) }
91
+ end
92
+
93
+ value
76
94
  end
77
95
 
78
96
  def unescape_characters(value)
@@ -86,28 +104,5 @@ module Dotenv
86
104
  value.gsub('\n', "\\\\\\n").gsub('\r', "\\\\\\r")
87
105
  end
88
106
  end
89
-
90
- def variable_not_set?(line)
91
- !line.split[1..].all? { |var| @hash.member?(var) }
92
- end
93
-
94
- def unescape_value(value, maybe_quote)
95
- if maybe_quote == '"'
96
- unescape_characters(expand_newlines(value))
97
- elsif maybe_quote.nil?
98
- unescape_characters(value)
99
- else
100
- value
101
- end
102
- end
103
-
104
- def perform_substitutions(value, maybe_quote)
105
- if maybe_quote != "'"
106
- self.class.substitutions.each do |proc|
107
- value = proc.call(value, @hash, overwrite: @overwrite)
108
- end
109
- end
110
- value
111
- end
112
107
  end
113
108
  end
@@ -20,7 +20,7 @@ module Dotenv
20
20
  )
21
21
  /x
22
22
 
23
- def call(value, _env, overwrite: false)
23
+ def call(value, _env)
24
24
  # Process interpolated shell commands
25
25
  value.gsub(INTERPOLATED_SHELL_COMMAND) do |*|
26
26
  # Eliminate opening and closing parentheses
@@ -12,29 +12,23 @@ module Dotenv
12
12
  VARIABLE = /
13
13
  (\\)? # is it escaped with a backslash?
14
14
  (\$) # literal $
15
- (?!\() # shouldnt be followed by paranthesis
15
+ (?!\() # shouldn't be followed by parenthesis
16
16
  \{? # allow brace wrapping
17
17
  ([A-Z0-9_]+)? # optional alpha nums
18
18
  \}? # closing brace
19
19
  /xi
20
20
 
21
- def call(value, env, overwrite: false)
22
- combined_env = overwrite ? ENV.to_h.merge(env) : env.merge(ENV)
21
+ def call(value, env)
23
22
  value.gsub(VARIABLE) do |variable|
24
23
  match = $LAST_MATCH_INFO
25
- substitute(match, variable, combined_env)
26
- end
27
- end
28
-
29
- private
30
24
 
31
- def substitute(match, variable, env)
32
- if match[1] == "\\"
33
- variable[1..]
34
- elsif match[3]
35
- env.fetch(match[3], "")
36
- else
37
- variable
25
+ if match[1] == "\\"
26
+ variable[1..]
27
+ elsif match[3]
28
+ env[match[3]] || ENV[match[3]] || ""
29
+ else
30
+ variable
31
+ end
38
32
  end
39
33
  end
40
34
  end
@@ -34,7 +34,7 @@ module Dotenv
34
34
 
35
35
  def var_defined?(line)
36
36
  match = Dotenv::Parser::LINE.match(line)
37
- match && match[1]
37
+ match && match[:key]
38
38
  end
39
39
 
40
40
  def line_blank?(line)
@@ -1,3 +1,3 @@
1
1
  module Dotenv
2
- VERSION = "3.1.4".freeze
2
+ VERSION = "3.1.6".freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dotenv
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.1.4
4
+ version: 3.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brandon Keepers
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-09-18 00:00:00.000000000 Z
11
+ date: 2024-12-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -85,7 +85,7 @@ licenses:
85
85
  - MIT
86
86
  metadata:
87
87
  changelog_uri: https://github.com/bkeepers/dotenv/releases
88
- post_install_message:
88
+ post_install_message:
89
89
  rdoc_options: []
90
90
  require_paths:
91
91
  - lib
@@ -100,8 +100,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
100
100
  - !ruby/object:Gem::Version
101
101
  version: '0'
102
102
  requirements: []
103
- rubygems_version: 3.5.16
104
- signing_key:
103
+ rubygems_version: 3.5.22
104
+ signing_key:
105
105
  specification_version: 4
106
106
  summary: Loads environment variables from `.env`.
107
107
  test_files: []