dotenv 0.8.0 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,12 +1,30 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.9.0 - Aug 29, 2013
4
+
5
+ * Add support for variable expansion.
6
+
7
+ HOST="example.com"
8
+ URL="http://${USER}@${HOST}"
9
+ ESCAPED_VARIABLE="this is \$NOT replaced"
10
+
11
+ * Allow setting variables without a value.
12
+
13
+ BLANK=
14
+
15
+ * Add `dotenv` executable to load `.env` for other scripts.
16
+
17
+ $ dotenv ./script.py
18
+
19
+ [Full Changelog](https://github.com/bkeepers/dotenv/compare/v0.8.0...v0.9.0)
20
+
3
21
  ## 0.8.0 - June 12, 2013
4
22
 
5
23
  * Added a capistrano recipe to symlink in `.env` on deploy.
6
24
 
7
25
  * Allow inline comments
8
26
 
9
- VARIABLE=value # this is a comment
27
+ VARIABLE=value # this is a comment
10
28
 
11
29
  * Raises Dotenv::FormatError when parsing fails
12
30
 
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "dotenv"
4
+
5
+ begin
6
+ Dotenv.load!
7
+ rescue Errno::ENOENT => e
8
+ warn e.message
9
+ exit 1
10
+ else
11
+ exec *ARGV
12
+ end
@@ -7,16 +7,25 @@ module Dotenv
7
7
  (?:export\s+)? # optional export
8
8
  ([\w\.]+) # key
9
9
  (?:\s*=\s*|:\s+?) # separator
10
- ( # value begin
10
+ ( # optional value begin
11
11
  '(?:\'|[^'])*' # single quoted value
12
12
  | # or
13
13
  "(?:\"|[^"])*" # double quoted value
14
14
  | # or
15
15
  [^#\n]+ # unquoted value
16
- ) # value end
16
+ )? # value end
17
17
  (?:\s*\#.*)? # optional comment
18
18
  \z
19
19
  /x
20
+ VARIABLE = /
21
+ (\\)?
22
+ (\$)
23
+ ( # collect braces with var for sub
24
+ \{? # allow brace wrapping
25
+ ([A-Z0-9_]+) # match the variable
26
+ \}? # closing brace
27
+ )
28
+ /xi
20
29
 
21
30
  def initialize(filename)
22
31
  @filename = filename
@@ -27,8 +36,28 @@ module Dotenv
27
36
  read.each do |line|
28
37
  if match = line.match(LINE)
29
38
  key, value = match.captures
39
+
40
+ value ||= ''
41
+ # Remove surrounding quotes
30
42
  value = value.strip.sub(/\A(['"])(.*)\1\z/, '\2')
31
- value = value.gsub('\n', "\n").gsub(/\\(.)/, '\1') if $1 == '"'
43
+
44
+ if $1 == '"'
45
+ value = value.gsub('\n', "\n")
46
+ # Unescape all characters except $ so variables can be escaped properly
47
+ value = value.gsub(/\\([^$])/, '\1')
48
+ end
49
+
50
+ # Process embedded variables
51
+ value.scan(VARIABLE).each do |parts|
52
+ if parts.first == '\\'
53
+ replace = parts[1...-1].join('')
54
+ else
55
+ replace = self.fetch(parts.last) { ENV[parts.last] }
56
+ end
57
+
58
+ value = value.sub(parts[0...-1].join(''), replace || '')
59
+ end
60
+
32
61
  self[key] = value
33
62
  elsif line !~ /\A\s*(?:#.*)?\z/ # not comment or blank line
34
63
  raise FormatError, "Line #{line.inspect} doesn't match format"
@@ -1,3 +1,3 @@
1
1
  module Dotenv
2
- VERSION = '0.8.0'
2
+ VERSION = '0.9.0'
3
3
  end
@@ -50,6 +50,36 @@ describe Dotenv::Environment do
50
50
  expect(env('FOO="escaped\"bar"')).to eql('FOO' => 'escaped"bar')
51
51
  end
52
52
 
53
+ it 'parses empty values' do
54
+ expect(env('FOO=')).to eql('FOO' => '')
55
+ end
56
+
57
+ it 'expands variables found in values' do
58
+ expect(env("FOO=test\nBAR=$FOO")).to eql('FOO' => 'test', 'BAR' => 'test')
59
+ end
60
+
61
+ it 'parses variables wrapped in brackets' do
62
+ expect(env("FOO=test\nBAR=${FOO}bar")).to eql('FOO' => 'test', 'BAR' => 'testbar')
63
+ end
64
+
65
+ it 'reads variables from ENV when expanding if not found in local env' do
66
+ ENV['FOO'] = 'test'
67
+ expect(env('BAR=$FOO')).to eql('BAR' => 'test')
68
+ end
69
+
70
+ it 'expands undefined variables to an empty string' do
71
+ expect(env('BAR=$FOO')).to eql('BAR' => '')
72
+ end
73
+
74
+ it 'expands variables in quoted strings' do
75
+ expect(env("FOO=test\nBAR='quote $FOO'")).to eql('FOO' => 'test', 'BAR' => 'quote test')
76
+ end
77
+
78
+ it 'does not expand escaped variables' do
79
+ expect(env('FOO="foo\$BAR"')).to eql('FOO' => 'foo$BAR')
80
+ expect(env('FOO="foo\${BAR}"')).to eql('FOO' => 'foo${BAR}')
81
+ end
82
+
53
83
  it 'parses yaml style options' do
54
84
  expect(env('OPTION_A: 1')).to eql('OPTION_A' => '1')
55
85
  end
@@ -7,7 +7,7 @@ describe Dotenv do
7
7
 
8
8
  it 'defaults to .env' do
9
9
  Dotenv::Environment.should_receive(:new).with(expand('.env')).
10
- and_return(mock(:apply => {}))
10
+ and_return(double(:apply => {}))
11
11
  subject
12
12
  end
13
13
  end
@@ -19,7 +19,7 @@ describe Dotenv do
19
19
  expected = expand("~/.env")
20
20
  File.stub(:exists?){ |arg| arg == expected }
21
21
  Dotenv::Environment.should_receive(:new).with(expected).
22
- and_return(mock(:apply => {}))
22
+ and_return(double(:apply => {}))
23
23
  subject
24
24
  end
25
25
  end
metadata CHANGED
@@ -1,18 +1,20 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dotenv
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.0
4
+ version: 0.9.0
5
+ prerelease:
5
6
  platform: ruby
6
7
  authors:
7
8
  - Brandon Keepers
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2013-06-12 00:00:00.000000000 Z
12
+ date: 2013-08-29 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: rake
15
16
  requirement: !ruby/object:Gem::Requirement
17
+ none: false
16
18
  requirements:
17
19
  - - ! '>='
18
20
  - !ruby/object:Gem::Version
@@ -20,6 +22,7 @@ dependencies:
20
22
  type: :development
21
23
  prerelease: false
22
24
  version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
23
26
  requirements:
24
27
  - - ! '>='
25
28
  - !ruby/object:Gem::Version
@@ -27,6 +30,7 @@ dependencies:
27
30
  - !ruby/object:Gem::Dependency
28
31
  name: rspec
29
32
  requirement: !ruby/object:Gem::Requirement
33
+ none: false
30
34
  requirements:
31
35
  - - ! '>='
32
36
  - !ruby/object:Gem::Version
@@ -34,6 +38,7 @@ dependencies:
34
38
  type: :development
35
39
  prerelease: false
36
40
  version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
37
42
  requirements:
38
43
  - - ! '>='
39
44
  - !ruby/object:Gem::Version
@@ -41,7 +46,8 @@ dependencies:
41
46
  description: Loads environment variables from `.env`.
42
47
  email:
43
48
  - brandon@opensoul.org
44
- executables: []
49
+ executables:
50
+ - dotenv
45
51
  extensions: []
46
52
  extra_rdoc_files: []
47
53
  files:
@@ -54,6 +60,7 @@ files:
54
60
  - LICENSE
55
61
  - README.md
56
62
  - Rakefile
63
+ - bin/dotenv
57
64
  - dotenv-rails.gemspec
58
65
  - dotenv.gemspec
59
66
  - lib/dotenv-rails.rb
@@ -74,26 +81,27 @@ files:
74
81
  - spec/spec_helper.rb
75
82
  homepage: https://github.com/bkeepers/dotenv
76
83
  licenses: []
77
- metadata: {}
78
84
  post_install_message:
79
85
  rdoc_options: []
80
86
  require_paths:
81
87
  - lib
82
88
  required_ruby_version: !ruby/object:Gem::Requirement
89
+ none: false
83
90
  requirements:
84
91
  - - ! '>='
85
92
  - !ruby/object:Gem::Version
86
93
  version: '0'
87
94
  required_rubygems_version: !ruby/object:Gem::Requirement
95
+ none: false
88
96
  requirements:
89
97
  - - ! '>='
90
98
  - !ruby/object:Gem::Version
91
99
  version: '0'
92
100
  requirements: []
93
101
  rubyforge_project:
94
- rubygems_version: 2.0.3
102
+ rubygems_version: 1.8.23
95
103
  signing_key:
96
- specification_version: 4
104
+ specification_version: 3
97
105
  summary: Loads environment variables from `.env`.
98
106
  test_files:
99
107
  - spec/dotenv/environment_spec.rb
@@ -103,4 +111,3 @@ test_files:
103
111
  - spec/fixtures/quoted.env
104
112
  - spec/fixtures/yaml.env
105
113
  - spec/spec_helper.rb
106
- has_rdoc:
checksums.yaml DELETED
@@ -1,15 +0,0 @@
1
- ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- OGMzMjQ2MDFjM2IyMDFiOGVmYzU3ZmJjNWZhYmRlNTFlYWU1MTVjYg==
5
- data.tar.gz: !binary |-
6
- MzIwNjhlMGQ3NGFlNWQxMTQzMDM1MDRiNjAwMTQ4Y2UzOTFhMDBjNA==
7
- !binary "U0hBNTEy":
8
- metadata.gz: !binary |-
9
- ZDQ1MzM1YTlhMGVmYjgyYTc3ZDY1ZDExMjY3NTRkNzhjMmNhMmY0OWViNTVj
10
- YmRkMWZlNTJlMDMyOGEwMDdkNjg2OTJjZTA2MmFlMzRjMzY5ZmQ5M2QwMjdj
11
- MGQ5NDdlMDNmY2M4ZWZhMGNjNGYxZDc3YmUzYTMwOWRmZWMzYzE=
12
- data.tar.gz: !binary |-
13
- ZDVjZGMyMWJmYmNkZmYyZDE0YWZiNTc2YzllMzc4OTcyZDk5MjM2ZDE4YWJh
14
- YjhiZTZlZDJlYzAwMGEzYzMzMDhkZmMzZDMzMDkwNzNkN2E3OGEyMjMxZDE0
15
- MzUzZjRjMDlmZGY0MTlhMWY5MDJmOTE3ZDVmNGI4NDJiZjI0NzA=