dotenv 0.9.0 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: bd2cbf743caa9a52aa0be269a6d85623a429e63f
4
+ data.tar.gz: 8dbb6133dbb68670820f783469d680052b7ad6b9
5
+ SHA512:
6
+ metadata.gz: e214586a73a311cf7544a1e2141f2653fba3bcead5a43b5dadd865732a16d9aaed54c0f063f5bade068971191c915fe8917d8da7bcf16eaaf8c8b843642e404c
7
+ data.tar.gz: 4b689ee34f098e79858a84ec03944d1a365905e9d896ccabbe41bb4181b6272466d97dded85ff58b4ddbdc86412090170ac853a6529ead2088a03ae4fa1e9ab0
@@ -1,6 +1,10 @@
1
1
  language: ruby
2
2
  rvm:
3
+ - 2.1.0
3
4
  - 2.0.0
4
5
  - 1.9.3
5
6
  - 1.8.7
6
7
  - ree
8
+ - jruby-19mode
9
+ - rbx
10
+ bundler_args: --without=guard
@@ -1,5 +1,17 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.10.0 - Feb 22, 2014
4
+
5
+ * Add support for executing interpolated commands. (Ruby >= 1.9 only)
6
+
7
+ HEAD_SHA=$(git rev-parse HEAD)
8
+
9
+ * Add `dotenv_role` option in Capistrano.
10
+
11
+ set :dotenv_role, [:app, web]
12
+
13
+ * Add `Dotenv.overload` to overwrite existing environment values.
14
+
3
15
  ## 0.9.0 - Aug 29, 2013
4
16
 
5
17
  * Add support for variable expansion.
data/Gemfile CHANGED
@@ -1,6 +1,12 @@
1
1
  source 'https://rubygems.org'
2
2
  gemspec :name => 'dotenv'
3
3
 
4
- gem 'guard-rspec'
5
- gem 'guard-bundler'
6
- gem 'rb-fsevent'
4
+ group :guard do
5
+ gem 'guard-rspec'
6
+ gem 'guard-bundler'
7
+ gem 'rb-fsevent'
8
+ end
9
+
10
+ platforms :rbx do
11
+ gem 'rubysl', '~> 2.0' # if using anything in the ruby standard library
12
+ end
data/README.md CHANGED
@@ -1,16 +1,16 @@
1
- # dotenv [![Build Status](https://secure.travis-ci.org/bkeepers/dotenv.png)](https://travis-ci.org/bkeepers/dotenv)
1
+ # dotenv [![Build Status](https://secure.travis-ci.org/bkeepers/dotenv.png?branch=master)](https://travis-ci.org/bkeepers/dotenv)
2
2
 
3
3
  Dotenv loads environment variables from `.env` into `ENV`.
4
4
 
5
5
  Storing [configuration in the environment](http://www.12factor.net/config) is one of the tenets of a [twelve-factor app](http://www.12factor.net/). Anything that is likely to change between deployment environments–such as resource handles for databases or credentials for external services–should be extracted from the code into environment variables.
6
6
 
7
- But it is not always practical to set environment variables on development machines or continuous integration servers where multiple projects are run. Dotenv load variables from a `.env` file into ENV when the environment is bootstrapped.
7
+ But it is not always practical to set environment variables on development machines or continuous integration servers where multiple projects are run. Dotenv load variables from a `.env` file into `ENV` when the environment is bootstrapped.
8
8
 
9
9
  ## Installation
10
10
 
11
11
  ### Rails
12
12
 
13
- Add this line to your application's Gemfile:
13
+ Add this line to the top of your application's Gemfile:
14
14
 
15
15
  ```ruby
16
16
  gem 'dotenv-rails', :groups => [:development, :test]
@@ -18,13 +18,19 @@ gem 'dotenv-rails', :groups => [:development, :test]
18
18
 
19
19
  And then execute:
20
20
 
21
- $ bundle
21
+ ```shell
22
+ $ bundle
23
+ ```
24
+
25
+ It should be listed in the Gemfile before any other gems that use environment variables, otherwise those gems will get initialized with the wrong values.
22
26
 
23
27
  ### Sinatra or Plain ol' Ruby
24
28
 
25
29
  Install the gem:
26
30
 
27
- $ gem install dotenv
31
+ ```shell
32
+ $ gem install dotenv
33
+ ```
28
34
 
29
35
  As early as possible in your application bootstrap process, load `.env`:
30
36
 
@@ -33,6 +39,12 @@ require 'dotenv'
33
39
  Dotenv.load
34
40
  ```
35
41
 
42
+ Alternatively, you can use the `dotenv` executable to launch your application:
43
+
44
+ ```shell
45
+ $ dotenv ./script.py
46
+ ```
47
+
36
48
  To ensure `.env` is loaded in rake, load the tasks:
37
49
 
38
50
  ```ruby
@@ -74,13 +86,26 @@ config.fog_directory = ENV['S3_BUCKET']
74
86
 
75
87
  ## Capistrano integration
76
88
 
77
- In your `config/deploy.rb` file:
89
+ If you want to use Dotenv with Capistrano in your production environment, make sure the dotenv gem is included in your Gemfile `:production` group.
90
+
91
+ ### Capistrano version 2.x.x
92
+
93
+ Add the gem to your `config/deploy.rb` file:
78
94
 
79
95
  ```ruby
80
96
  require "dotenv/capistrano"
81
97
  ```
82
98
 
83
- It will symlink the `.env` located in `/path/to/shared` in the new release.
99
+ It will symlink the `.env` located in `/path/to/shared` in the new release.
100
+
101
+
102
+ ### Capistrano version 3.x.x
103
+
104
+ Just add `.env` to the list of linked files, for example:
105
+
106
+ ```ruby
107
+ set :linked_files, %w{config/database.yml .env}
108
+ ```
84
109
 
85
110
  ## Should I commit my .env file?
86
111
 
data/bin/dotenv CHANGED
@@ -1,12 +1,11 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require "dotenv"
3
+ require 'dotenv'
4
4
 
5
5
  begin
6
6
  Dotenv.load!
7
7
  rescue Errno::ENOENT => e
8
- warn e.message
9
- exit 1
8
+ abort e.message
10
9
  else
11
- exec *ARGV
10
+ exec *ARGV unless ARGV.empty?
12
11
  end
@@ -8,6 +8,7 @@ Gem::Specification.new do |gem|
8
8
  gem.description = %q{Autoload dotenv in Rails.}
9
9
  gem.summary = %q{Autoload dotenv in Rails.}
10
10
  gem.homepage = "https://github.com/bkeepers/dotenv"
11
+ gem.license = 'MIT'
11
12
 
12
13
  gem.files = ["lib/dotenv-rails.rb"]
13
14
  gem.name = "dotenv-rails"
@@ -8,6 +8,7 @@ Gem::Specification.new do |gem|
8
8
  gem.description = %q{Loads environment variables from `.env`.}
9
9
  gem.summary = %q{Loads environment variables from `.env`.}
10
10
  gem.homepage = "https://github.com/bkeepers/dotenv"
11
+ gem.license = 'MIT'
11
12
 
12
13
  gem.files = `git ls-files`.split($\)
13
14
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
@@ -8,6 +8,14 @@ module Dotenv
8
8
  end
9
9
  end
10
10
 
11
+ # same as `load`, but will override existing values in `ENV`
12
+ def self.overload(*filenames)
13
+ default_if_empty(filenames).inject({}) do |hash, filename|
14
+ filename = File.expand_path filename
15
+ hash.merge(File.exists?(filename) ? Environment.new(filename).apply! : {})
16
+ end
17
+ end
18
+
11
19
  # same as `load`, but raises Errno::ENOENT if any files don't exist
12
20
  def self.load!(*filenames)
13
21
  load(
@@ -1,5 +1,11 @@
1
- require 'dotenv/capistrano/recipes'
1
+ require 'capistrano/version'
2
2
 
3
- Capistrano::Configuration.instance(:must_exist).load do
4
- before "deploy:finalize_update", "dotenv:symlink"
3
+ if defined?(Capistrano::VERSION) && Capistrano::VERSION >= '3.0'
4
+ raise 'Please read https://github.com/bkeepers/dotenv#capistrano-integration to update your dotenv configuration for new Capistrano version'
5
+ else
6
+ require 'dotenv/capistrano/recipes'
7
+
8
+ Capistrano::Configuration.instance(:must_exist).load do
9
+ before "deploy:finalize_update", "dotenv:symlink"
10
+ end
5
11
  end
@@ -1,9 +1,11 @@
1
1
  Capistrano::Configuration.instance(:must_exist).load do
2
2
  _cset(:dotenv_path){ "#{shared_path}/.env" }
3
3
 
4
+ symlink_args = (role = fetch(:dotenv_role, nil) ? {:roles => role} : {})
5
+
4
6
  namespace :dotenv do
5
7
  desc "Symlink shared .env to current release"
6
- task :symlink, roles: :app do
8
+ task :symlink, symlink_args do
7
9
  run "ln -nfs #{dotenv_path} #{release_path}/.env"
8
10
  end
9
11
  end
@@ -1,7 +1,13 @@
1
1
  require 'dotenv/format_error'
2
+ require 'dotenv/substitutions/variable'
3
+ if RUBY_VERSION > '1.8.7'
4
+ require 'dotenv/substitutions/command'
5
+ end
2
6
 
3
7
  module Dotenv
4
8
  class Environment < Hash
9
+ @@substitutions = Substitutions.constants.map { |const| Substitutions.const_get(const) }
10
+
5
11
  LINE = /
6
12
  \A
7
13
  (?:export\s+)? # optional export
@@ -17,15 +23,6 @@ module Dotenv
17
23
  (?:\s*\#.*)? # optional comment
18
24
  \z
19
25
  /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
29
26
 
30
27
  def initialize(filename)
31
28
  @filename = filename
@@ -47,15 +44,8 @@ module Dotenv
47
44
  value = value.gsub(/\\([^$])/, '\1')
48
45
  end
49
46
 
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 || '')
47
+ @@substitutions.each do |proc|
48
+ value = proc.call(value, self)
59
49
  end
60
50
 
61
51
  self[key] = value
@@ -72,5 +62,9 @@ module Dotenv
72
62
  def apply
73
63
  each { |k,v| ENV[k] ||= v }
74
64
  end
65
+
66
+ def apply!
67
+ each { |k,v| ENV[k] = v }
68
+ end
75
69
  end
76
70
  end
@@ -0,0 +1,32 @@
1
+ module Dotenv
2
+ module Substitutions
3
+ module Command
4
+ class << self
5
+
6
+ INTERPOLATED_SHELL_COMMAND = /
7
+ (?<backslash>\\)?
8
+ \$
9
+ (?<cmd> # collect command content for eval
10
+ \( # require opening paren
11
+ ([^()]|\g<cmd>)+ # allow any number of non-parens, or balanced parens (by nesting the <cmd> expression recursively)
12
+ \) # require closing paren
13
+ )
14
+ /x
15
+
16
+ def call(value, env)
17
+ # Process interpolated shell commands
18
+ value.gsub(INTERPOLATED_SHELL_COMMAND) do |*|
19
+ command = $~[:cmd][1..-2] # Eliminate opening and closing parentheses
20
+
21
+ if $~[:backslash]
22
+ $~[0][1..-1]
23
+ else
24
+ `#{command}`.chomp
25
+ end
26
+ end
27
+ end
28
+ end
29
+
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,35 @@
1
+ module Dotenv
2
+ module Substitutions
3
+ module Variable
4
+ class << self
5
+
6
+ VARIABLE = /
7
+ (\\)?
8
+ (\$)
9
+ ( # collect braces with var for sub
10
+ \{? # allow brace wrapping
11
+ ([A-Z0-9_]+) # match the variable
12
+ \}? # closing brace
13
+ )
14
+ /xi
15
+
16
+ def call(value, env)
17
+ # Process embedded variables
18
+ value.scan(VARIABLE).each do |parts|
19
+ if parts.first == '\\'
20
+ replace = parts[1...-1].join('')
21
+ else
22
+ replace = env.fetch(parts.last) { ENV[parts.last] }
23
+ end
24
+
25
+ value = value.sub(parts[0...-1].join(''), replace || '')
26
+ end
27
+
28
+ value
29
+ end
30
+ end
31
+
32
+ end
33
+
34
+ end
35
+ end
@@ -1,3 +1,3 @@
1
1
  module Dotenv
2
- VERSION = '0.9.0'
2
+ VERSION = '0.10.0'
3
3
  end
@@ -29,6 +29,19 @@ describe Dotenv::Environment do
29
29
  end
30
30
  end
31
31
 
32
+ describe 'apply!' do
33
+ it 'sets variables in the ENV' do
34
+ subject.apply
35
+ expect(ENV['OPTION_A']).to eq('1')
36
+ end
37
+
38
+ it 'overrides defined variables' do
39
+ ENV['OPTION_A'] = 'predefined'
40
+ subject.apply!
41
+ expect(ENV['OPTION_A']).to eq('1')
42
+ end
43
+ end
44
+
32
45
  it 'parses unquoted values' do
33
46
  expect(env('FOO=bar')).to eql('FOO' => 'bar')
34
47
  end
@@ -92,7 +105,7 @@ describe Dotenv::Environment do
92
105
  expect(env('FOO="bar\nbaz"')).to eql('FOO' => "bar\nbaz")
93
106
  end
94
107
 
95
- it 'parses varibales with "." in the name' do
108
+ it 'parses variables with "." in the name' do
96
109
  expect(env('FOO.BAR=foobar')).to eql('FOO.BAR' => 'foobar')
97
110
  end
98
111
 
@@ -125,6 +138,31 @@ describe Dotenv::Environment do
125
138
  expect(env("foo='ba#r'")).to eql('foo' => 'ba#r')
126
139
  end
127
140
 
141
+ if RUBY_VERSION > '1.8.7'
142
+ it 'parses shell commands interpolated in $()' do
143
+ expect(env('ruby_v=$(ruby -v)')).to eql('ruby_v' => RUBY_DESCRIPTION)
144
+ end
145
+
146
+ it 'allows balanced parentheses within interpolated shell commands' do
147
+ expect(env('ruby_v=$(echo "$(echo "$(echo "$(ruby -v)")")")')).to eql('ruby_v' => RUBY_DESCRIPTION)
148
+ end
149
+
150
+ it "doesn't interpolate shell commands when escape says not to" do
151
+ expect(env('ruby_v=escaped-\$(ruby -v)')).to eql('ruby_v' => 'escaped-$(ruby -v)')
152
+ end
153
+
154
+ it 'is not thrown off by quotes in interpolated shell commands' do
155
+ expect(env('interp=$(echo "Quotes won\'t be a problem")')['interp']).to eql("Quotes won't be a problem")
156
+ end
157
+
158
+ # This functionality is not supported on JRuby or Rubinius
159
+ if (!defined?(RUBY_ENGINE) || RUBY_ENGINE != 'jruby') && !defined?(Rubinius)
160
+ it 'substitutes shell variables within interpolated shell commands' do
161
+ expect(env(%(VAR1=var1\ninterp=$(echo "VAR1 is $VAR1")))['interp']).to eql("VAR1 is var1")
162
+ end
163
+ end
164
+ end
165
+
128
166
  require 'tempfile'
129
167
  def env(text)
130
168
  file = Tempfile.new('dotenv')
@@ -6,7 +6,7 @@ describe Dotenv do
6
6
  let(:env_files) { [] }
7
7
 
8
8
  it 'defaults to .env' do
9
- Dotenv::Environment.should_receive(:new).with(expand('.env')).
9
+ expect(Dotenv::Environment).to receive(:new).with(expand('.env')).
10
10
  and_return(double(:apply => {}))
11
11
  subject
12
12
  end
@@ -17,8 +17,8 @@ describe Dotenv do
17
17
 
18
18
  it 'expands the path' do
19
19
  expected = expand("~/.env")
20
- File.stub(:exists?){ |arg| arg == expected }
21
- Dotenv::Environment.should_receive(:new).with(expected).
20
+ allow(File).to receive(:exists?){ |arg| arg == expected }
21
+ expect(Dotenv::Environment).to receive(:new).with(expected).
22
22
  and_return(double(:apply => {}))
23
23
  subject
24
24
  end
@@ -82,6 +82,17 @@ describe Dotenv do
82
82
  end
83
83
  end
84
84
 
85
+ describe 'overload' do
86
+ it 'overrides any existing ENV variables' do
87
+ ENV['OPTION_A'] = 'predefined'
88
+ path = fixture_path 'plain.env'
89
+
90
+ Dotenv.overload(path)
91
+
92
+ expect(ENV['OPTION_A']).to eq('1')
93
+ end
94
+ end
95
+
85
96
  def fixture_path(name)
86
97
  File.join(File.expand_path('../fixtures', __FILE__), name)
87
98
  end
metadata CHANGED
@@ -1,46 +1,41 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dotenv
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
5
- prerelease:
4
+ version: 0.10.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Brandon Keepers
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-08-29 00:00:00.000000000 Z
11
+ date: 2014-02-22 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: rake
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ! '>='
17
+ - - '>='
20
18
  - !ruby/object:Gem::Version
21
19
  version: '0'
22
20
  type: :development
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ! '>='
24
+ - - '>='
28
25
  - !ruby/object:Gem::Version
29
26
  version: '0'
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: rspec
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ! '>='
31
+ - - '>='
36
32
  - !ruby/object:Gem::Version
37
33
  version: '0'
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ! '>='
38
+ - - '>='
44
39
  - !ruby/object:Gem::Version
45
40
  version: '0'
46
41
  description: Loads environment variables from `.env`.
@@ -70,6 +65,8 @@ files:
70
65
  - lib/dotenv/environment.rb
71
66
  - lib/dotenv/format_error.rb
72
67
  - lib/dotenv/railtie.rb
68
+ - lib/dotenv/substitutions/command.rb
69
+ - lib/dotenv/substitutions/variable.rb
73
70
  - lib/dotenv/tasks.rb
74
71
  - lib/dotenv/version.rb
75
72
  - spec/dotenv/environment_spec.rb
@@ -80,28 +77,28 @@ files:
80
77
  - spec/fixtures/yaml.env
81
78
  - spec/spec_helper.rb
82
79
  homepage: https://github.com/bkeepers/dotenv
83
- licenses: []
80
+ licenses:
81
+ - MIT
82
+ metadata: {}
84
83
  post_install_message:
85
84
  rdoc_options: []
86
85
  require_paths:
87
86
  - lib
88
87
  required_ruby_version: !ruby/object:Gem::Requirement
89
- none: false
90
88
  requirements:
91
- - - ! '>='
89
+ - - '>='
92
90
  - !ruby/object:Gem::Version
93
91
  version: '0'
94
92
  required_rubygems_version: !ruby/object:Gem::Requirement
95
- none: false
96
93
  requirements:
97
- - - ! '>='
94
+ - - '>='
98
95
  - !ruby/object:Gem::Version
99
96
  version: '0'
100
97
  requirements: []
101
98
  rubyforge_project:
102
- rubygems_version: 1.8.23
99
+ rubygems_version: 2.0.3
103
100
  signing_key:
104
- specification_version: 3
101
+ specification_version: 4
105
102
  summary: Loads environment variables from `.env`.
106
103
  test_files:
107
104
  - spec/dotenv/environment_spec.rb