dotenv-rails 2.7.6 → 3.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 63a5d656ee0a5beec3233432fe5132ddcaae19276ddb1bf57eeb0303e7533dd0
4
- data.tar.gz: 42d7b7894c089250b36e14263c175c6ed0dd3952076bc2008a9b2312ba4f36c1
3
+ metadata.gz: 04d36b9b6116f28f935965844d3fd5916ad780fda54f3cc652ed9f32ab229e4b
4
+ data.tar.gz: 56189def0feb6f6199cf633b4098f13fe71f7704b9d70fcfcacce32980d7a24b
5
5
  SHA512:
6
- metadata.gz: a469e40e9eb8a977108a5c00ab6b6d3178892a4b06eb2c9e5157a04bb5219d05df934d51d6b09ffaeedacacbfe9e05441a890311895b24d6bf687c671e55bac9
7
- data.tar.gz: 01fdb41bcd5eeaaa41fdfe14df7fec1e1632aaac050e00f46089635d28aad1598b28b44fa30b737bc09821d6fcacceef913e3298f5f4ba238a19445c9a0a9c27
6
+ metadata.gz: fde31902ef88b2cccfb6e20f6c68ae5f0331d2b0c5222c58b3b2a6b28a82f325686c55cdf1dc6b90fc44669af687f090349b7a710c2c53bd2fbcef420acf65d9
7
+ data.tar.gz: cab09cf88978eddf7e1f0f0477a68e87cd48e036f0e1ba354884cee7a190f45ff775df66c1839ad2f23e5c7359615acda78c6314bf890f82de3d355765c96344
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # dotenv [![Build Status](https://secure.travis-ci.org/bkeepers/dotenv.svg?branch=master)](https://travis-ci.org/bkeepers/dotenv) [![Gem Version](https://badge.fury.io/rb/dotenv.svg)](https://badge.fury.io/rb/dotenv) [![Join the chat at https://gitter.im/bkeepers/dotenv](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/bkeepers/dotenv?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
1
+ # dotenv [![Gem Version](https://badge.fury.io/rb/dotenv.svg)](https://badge.fury.io/rb/dotenv)
2
2
 
3
3
  Shim to load environment variables from `.env` into `ENV` in *development*.
4
4
 
@@ -8,49 +8,36 @@ But it is not always practical to set environment variables on development machi
8
8
 
9
9
  ## Installation
10
10
 
11
- ### Rails
12
-
13
- Add this line to the top of your application's Gemfile:
11
+ Add this line to the top of your application's Gemfile and run `bundle install`:
14
12
 
15
13
  ```ruby
16
- gem 'dotenv-rails', groups: [:development, :test]
14
+ gem 'dotenv', groups: [:development, :test]
17
15
  ```
18
16
 
19
- And then execute:
17
+ ## Usage
18
+
19
+ Add your application configuration to your `.env` file in the root of your project:
20
20
 
21
21
  ```shell
22
- $ bundle
22
+ S3_BUCKET=YOURS3BUCKET
23
+ SECRET_KEY=YOURSECRETKEYGOESHERE
23
24
  ```
24
25
 
25
- #### Note on load order
26
-
27
- dotenv is initialized in your Rails app during the `before_configuration` callback, which is fired when the `Application` constant is defined in `config/application.rb` with `class Application < Rails::Application`. If you need it to be initialized sooner, you can manually call `Dotenv::Railtie.load`.
26
+ Whenever your application loads, these variables will be available in `ENV`:
28
27
 
29
28
  ```ruby
30
- # config/application.rb
31
- Bundler.require(*Rails.groups)
32
-
33
- Dotenv::Railtie.load
34
-
35
- HOSTNAME = ENV['HOSTNAME']
29
+ config.fog_directory = ENV['S3_BUCKET']
36
30
  ```
37
31
 
38
- If you use gems that require environment variables to be set before they are loaded, then list `dotenv-rails` in the `Gemfile` before those other gems and require `dotenv/rails-now`.
32
+ See the [API Docs](https://rubydoc.info/github/bkeepers/dotenv/main) for more.
39
33
 
40
- ```ruby
41
- gem 'dotenv-rails', require: 'dotenv/rails-now'
42
- gem 'gem-that-requires-env-variables'
43
- ```
34
+ ### Rails
44
35
 
45
- ### Sinatra or Plain ol' Ruby
36
+ Dotenv will automatically load when your Rails app boots. See [Customizing Rails](#customizing-rails) to change which files are loaded and when.
46
37
 
47
- Install the gem:
38
+ ### Sinatra / Ruby
48
39
 
49
- ```shell
50
- $ gem install dotenv
51
- ```
52
-
53
- As early as possible in your application bootstrap process, load `.env`:
40
+ Load Dotenv as early as possible in your application bootstrap process:
54
41
 
55
42
  ```ruby
56
43
  require 'dotenv/load'
@@ -60,24 +47,27 @@ require 'dotenv'
60
47
  Dotenv.load
61
48
  ```
62
49
 
63
- By default, `load` will look for a file called `.env` in the current working directory. Pass in multiple files and they will be loaded in order. The first value set for a variable will win.
50
+ By default, `load` will look for a file called `.env` in the current working directory.
51
+ Pass in multiple files and they will be loaded in order.
52
+ The first value set for a variable will win.
53
+ Existing environment variables will not be overwritten unless you set `overwrite: true`.
64
54
 
65
55
  ```ruby
66
56
  require 'dotenv'
67
57
  Dotenv.load('file1.env', 'file2.env')
68
58
  ```
69
59
 
70
- Alternatively, you can use the `dotenv` executable to launch your application:
60
+ ### Autorestore in tests
71
61
 
72
- ```shell
73
- $ dotenv ./script.rb
74
- ```
62
+ Since 3.0, dotenv in a Rails app will automatically restore `ENV` after each test. This means you can modify `ENV` in your tests without fear of leaking state to other tests. It works with both `ActiveSupport::TestCase` and `Rspec`.
75
63
 
76
- The `dotenv` executable also accepts a single flag, `-f`. Its value should be a comma-separated list of configuration files, in the order of most important to least. All of the files must exist. There _must_ be a space between the flag and its value.
64
+ To disable this behavior, set `config.dotenv.autorestore = false` in `config/application.rb` or `config/environments/test.rb`. It is disabled by default if your app uses [climate_control](https://github.com/thoughtbot/climate_control) or [ice_age](https://github.com/dpep/ice_age_rb).
77
65
 
78
- ```
79
- $ dotenv -f ".env.local,.env" ./script.rb
80
- ```
66
+ To use this behavior outside of a Rails app, just `require "dotenv/autorestore"` in your test suite.
67
+
68
+ See [`Dotenv.save`](https://rubydoc.info/github/bkeepers/dotenv/main/Dotenv:save), [Dotenv.restore](https://rubydoc.info/github/bkeepers/dotenv/main/Dotenv:restore), and [`Dotenv.modify(hash) { ... }`](https://rubydoc.info/github/bkeepers/dotenv/main/Dotenv:modify) for manual usage.
69
+
70
+ ### Rake
81
71
 
82
72
  To ensure `.env` is loaded in rake, load the tasks:
83
73
 
@@ -85,41 +75,121 @@ To ensure `.env` is loaded in rake, load the tasks:
85
75
  require 'dotenv/tasks'
86
76
 
87
77
  task mytask: :dotenv do
88
- # things that require .env
78
+ # things that require .env
89
79
  end
90
80
  ```
91
81
 
92
- ## Usage
82
+ ### CLI
93
83
 
94
- Add your application configuration to your `.env` file in the root of your project:
84
+ You can use the `dotenv` executable load `.env` before launching your application:
95
85
 
96
- ```shell
97
- S3_BUCKET=YOURS3BUCKET
98
- SECRET_KEY=YOURSECRETKEYGOESHERE
86
+ ```console
87
+ $ dotenv ./script.rb
99
88
  ```
100
89
 
101
- Whenever your application loads, these variables will be available in `ENV`:
90
+ The `dotenv` executable also accepts the flag `-f`. Its value should be a comma-separated list of configuration files, in the order of the most important to the least important. All of the files must exist. There _must_ be a space between the flag and its value.
102
91
 
103
- ```ruby
104
- config.fog_directory = ENV['S3_BUCKET']
92
+ ```console
93
+ $ dotenv -f ".env.local,.env" ./script.rb
105
94
  ```
106
95
 
107
- You may also add `export` in front of each line so you can `source` the file in bash:
96
+ The `dotenv` executable can optionally ignore missing files with the `-i` or `--ignore` flag. For example, if the `.env.local` file does not exist, the following will ignore the missing file and only load the `.env` file.
108
97
 
109
- ```shell
110
- export S3_BUCKET=YOURS3BUCKET
111
- export SECRET_KEY=YOURSECRETKEYGOESHERE
98
+ ```console
99
+ $ dotenv -i -f ".env.local,.env" ./script.rb
112
100
  ```
113
101
 
114
- ### Multi-line values
102
+ ### Load Order
115
103
 
116
- If you need multiline variables, for example private keys, you can double quote strings and use the `\n` character for newlines:
104
+ If you use gems that require environment variables to be set before they are loaded, then list `dotenv` in the `Gemfile` before those other gems and require `dotenv/load`.
117
105
 
118
- ```shell
119
- PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----\nHkVN9...\n-----END DSA PRIVATE KEY-----\n"
106
+ ```ruby
107
+ gem 'dotenv', require: 'dotenv/load'
108
+ gem 'gem-that-requires-env-variables'
109
+ ```
110
+
111
+ ### Customizing Rails
112
+
113
+ Dotenv will load the following files depending on `RAILS_ENV`, with the first file having the highest precedence, and `.env` having the lowest precedence:
114
+
115
+ <table>
116
+ <thead>
117
+ <tr>
118
+ <th>Priority</th>
119
+ <th colspan="3">Environment</th>
120
+ <th><code>.gitignore</code>it?</th>
121
+ <th>Notes</th>
122
+ </tr>
123
+ <tr>
124
+ <th></th>
125
+ <th>development</th>
126
+ <th>test</th>
127
+ <th>production</th>
128
+ <th></th>
129
+ <th></th>
130
+ </tr>
131
+ </thead>
132
+ <tr>
133
+ <td>highest</td>
134
+ <td><code>.env.development.local</code></td>
135
+ <td><code>.env.test.local</code></td>
136
+ <td><code>.env.production.local</code></td>
137
+ <td>Yes</td>
138
+ <td>Environment-specific local overrides</td>
139
+ </tr>
140
+ <tr>
141
+ <td>2nd</td>
142
+ <td><code>.env.local</code></td>
143
+ <td><strong>N/A</strong></td>
144
+ <td><code>.env.local</code></td>
145
+ <td>Yes</td>
146
+ <td>Local overrides</td>
147
+ </tr>
148
+ <tr>
149
+ <td>3rd</td>
150
+ <td><code>.env.development</code></td>
151
+ <td><code>.env.test</code></td>
152
+ <td><code>.env.production</code></td>
153
+ <td>No</td>
154
+ <td>Shared environment-specific variables</td>
155
+ </tr>
156
+ <tr>
157
+ <td>last</td>
158
+ <td><code>.env</code></td>
159
+ <td><code>.env</code></td>
160
+ <td><code>.env</code></td>
161
+ <td><a href="#should-i-commit-my-env-file">Maybe</a></td>
162
+ <td>Shared for all environments</td>
163
+ </tr>
164
+ </table>
165
+
166
+
167
+ These files are loaded during the `before_configuration` callback, which is fired when the `Application` constant is defined in `config/application.rb` with `class Application < Rails::Application`. If you need it to be initialized sooner, or need to customize the loading process, you can do so at the top of `application.rb`
168
+
169
+ ```ruby
170
+ # config/application.rb
171
+ Bundler.require(*Rails.groups)
172
+
173
+ # Load .env.local in test
174
+ Dotenv::Rails.files.unshift(".env.local") if ENV["RAILS_ENV"] == "test"
175
+
176
+ module YourApp
177
+ class Application < Rails::Application
178
+ # ...
179
+ end
180
+ end
120
181
  ```
121
182
 
122
- Alternatively, multi-line values with line breaks are now supported for quoted values.
183
+ Available options:
184
+
185
+ * `Dotenv::Rails.files` - list of files to be loaded, in order of precedence.
186
+ * `Dotenv::Rails.overwrite` - Overwrite existing `ENV` variables with contents of `.env*` files
187
+ * `Dotenv::Rails.logger` - The logger to use for dotenv's logging. Defaults to `Rails.logger`
188
+ * `Dotenv::Rails.autorestore` - Enable or disable [autorestore](#autorestore-in-tests)
189
+
190
+ ### Multi-line values
191
+
192
+ Multi-line values with line breaks must be surrounded with double quotes.
123
193
 
124
194
  ```shell
125
195
  PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----
@@ -129,7 +199,12 @@ HkVN9...
129
199
  -----END DSA PRIVATE KEY-----"
130
200
  ```
131
201
 
132
- This is particularly helpful when using the Heroku command line plugin [`heroku-config`](https://github.com/xavdid/heroku-config) to pull configuration variables down that may have line breaks.
202
+ Prior to 3.0, dotenv would replace `\n` in quoted strings with a newline, but that behavior is deprecated. To use the old behavior, set `DOTENV_LINEBREAK_MODE=legacy` before any variables that include `\n`:
203
+
204
+ ```shell
205
+ DOTENV_LINEBREAK_MODE=legacy
206
+ PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----\nHkVN9...\n-----END DSA PRIVATE KEY-----\n"
207
+ ```
133
208
 
134
209
  ### Command Substitution
135
210
 
@@ -141,7 +216,7 @@ DATABASE_URL="postgres://$(whoami)@localhost/my_database"
141
216
 
142
217
  ### Variable Substitution
143
218
 
144
- 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.
219
+ 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.
145
220
 
146
221
  ```shell
147
222
  DATABASE_URL="postgres://${USER}@localhost/my_database"
@@ -163,6 +238,15 @@ SECRET_KEY=YOURSECRETKEYGOESHERE # comment
163
238
  SECRET_HASH="something-with-a-#-hash"
164
239
  ```
165
240
 
241
+ ### Exports
242
+
243
+ For compatability, you may also add `export` in front of each line so you can `source` the file in bash:
244
+
245
+ ```shell
246
+ export S3_BUCKET=YOURS3BUCKET
247
+ export SECRET_KEY=YOURSECRETKEYGOESHERE
248
+ ```
249
+
166
250
  ### Required Keys
167
251
 
168
252
  If a particular configuration value is required but not set, it's appropriate to raise an error.
@@ -188,42 +272,14 @@ Dotenv.parse(".env.local", ".env")
188
272
 
189
273
  This method returns a hash of the ENV var name/value pairs.
190
274
 
191
- ## Frequently Answered Questions
192
-
193
- ### Can I use dotenv in production?
194
-
195
- dotenv was originally created to load configuration variables into `ENV` in *development*. There are typically better ways to manage configuration in production environments - such as `/etc/environment` managed by [Puppet](https://github.com/puppetlabs/puppet) or [Chef](https://github.com/chef/chef), `heroku config`, etc.
196
-
197
- However, some find dotenv to be a convenient way to configure Rails applications in staging and production environments, and you can do that by defining environment-specific files like `.env.production` or `.env.test`.
198
-
199
- If you use this gem to handle env vars for multiple Rails environments (development, test, production, etc.), please note that env vars that are general to all environments should be stored in `.env`. Then, environment specific env vars should be stored in `.env.<that environment's name>`.
200
-
201
- ### What other .env* files can I use?
202
-
203
- `dotenv-rails` will override in the following order (highest defined variable overrides lower):
204
-
205
- | Hierarchy Priority | Filename | Environment | Should I `.gitignore`it? | Notes |
206
- | ------------------ | ------------------------ | -------------------- | --------------------------------------------------- | ------------------------------------------------------------ |
207
- | 1st (highest) | `.env.development.local` | Development | Yes! | Local overrides of environment-specific settings. |
208
- | 1st | `.env.test.local` | Test | Yes! | Local overrides of environment-specific settings. |
209
- | 1st | `.env.production.local` | Production | Yes! | Local overrides of environment-specific settings. |
210
- | 2nd | `.env.local` | Wherever the file is | Definitely. | Local overrides. This file is loaded for all environments _except_ `test`. |
211
- | 3rd | `.env.development` | Development | No. | Shared environment-specific settings |
212
- | 3rd | `.env.test` | Test | No. | Shared environment-specific settings |
213
- | 3rd | `.env.production` | Production | No. | Shared environment-specific settings |
214
- | Last | `.env` | All Environments | Depends (See [below](#should-i-commit-my-env-file)) | The Original® |
215
-
216
-
217
- ### Should I commit my .env file?
218
-
219
- Credentials should only be accessible on the machines that need access to them. Never commit sensitive information to a repository that is not needed by every development machine and server.
220
-
275
+ ### Templates
221
276
 
222
277
  You can use the `-t` or `--template` flag on the dotenv cli to create a template of your `.env` file.
223
- ```shell
278
+
279
+ ```console
224
280
  $ dotenv -t .env
225
281
  ```
226
- A template will be created in your working directory named `{FINAME}.template`. So in the above example, it would create a `.env.template` file.
282
+ A template will be created in your working directory named `{FILENAME}.template`. So in the above example, it would create a `.env.template` file.
227
283
 
228
284
  The template will contain all the environment variables in your `.env` file but with their values set to the variable names.
229
285
 
@@ -233,7 +289,7 @@ S3_BUCKET=YOURS3BUCKET
233
289
  SECRET_KEY=YOURSECRETKEYGOESHERE
234
290
  ```
235
291
 
236
- Would become
292
+ Would become
237
293
 
238
294
  ```shell
239
295
  # .env.template
@@ -241,11 +297,34 @@ S3_BUCKET=S3_BUCKET
241
297
  SECRET_KEY=SECRET_KEY
242
298
  ```
243
299
 
300
+ ## Frequently Answered Questions
301
+
302
+ ### Can I use dotenv in production?
303
+
304
+ dotenv was originally created to load configuration variables into `ENV` in *development*. There are typically better ways to manage configuration in production environments - such as `/etc/environment` managed by [Puppet](https://github.com/puppetlabs/puppet) or [Chef](https://github.com/chef/chef), `heroku config`, etc.
305
+
306
+ However, some find dotenv to be a convenient way to configure Rails applications in staging and production environments, and you can do that by defining environment-specific files like `.env.production` or `.env.test`.
307
+
308
+ If you use this gem to handle env vars for multiple Rails environments (development, test, production, etc.), please note that env vars that are general to all environments should be stored in `.env`. Then, environment specific env vars should be stored in `.env.<that environment's name>`.
309
+
310
+ ### Should I commit my .env file?
311
+
312
+ Credentials should only be accessible on the machines that need access to them. Never commit sensitive information to a repository that is not needed by every development machine and server.
313
+
244
314
  Personally, I prefer to commit the `.env` file with development-only settings. This makes it easy for other developers to get started on the project without compromising credentials for other environments. If you follow this advice, make sure that all the credentials for your development environment are different from your other deployments and that the development credentials do not have access to any confidential data.
245
315
 
246
- ### Why is it not overriding existing `ENV` variables?
316
+ ### Why is it not overwriting existing `ENV` variables?
317
+
318
+ By default, it **won't** overwrite existing environment variables as dotenv assumes the deployment environment has more knowledge about configuration than the application does. To overwrite existing environment variables you can use `Dotenv.load files, overwrite: true`.
247
319
 
248
- By default, it **won't** overwrite existing environment variables as dotenv assumes the deployment environment has more knowledge about configuration than the application does. To overwrite existing environment variables you can use `Dotenv.overload`.
320
+ To warn when a value was not overwritten (e.g. to make users aware of this gotcha),
321
+ use `Dotenv.load files, overwrite: :warn`.
322
+
323
+ You can also use the `-o` or `--overwrite` flag on the dotenv cli to overwrite existing `ENV` variables.
324
+
325
+ ```console
326
+ $ dotenv -o -f ".env.local,.env"
327
+ ```
249
328
 
250
329
  ## Contributing
251
330
 
data/lib/dotenv-rails.rb CHANGED
@@ -1 +1 @@
1
- require "dotenv/rails"
1
+ require "dotenv"
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dotenv-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.7.6
4
+ version: 3.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brandon Keepers
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2020-07-11 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: dotenv
@@ -16,28 +15,28 @@ dependencies:
16
15
  requirements:
17
16
  - - '='
18
17
  - !ruby/object:Gem::Version
19
- version: 2.7.6
18
+ version: 3.2.0
20
19
  type: :runtime
21
20
  prerelease: false
22
21
  version_requirements: !ruby/object:Gem::Requirement
23
22
  requirements:
24
23
  - - '='
25
24
  - !ruby/object:Gem::Version
26
- version: 2.7.6
25
+ version: 3.2.0
27
26
  - !ruby/object:Gem::Dependency
28
27
  name: railties
29
28
  requirement: !ruby/object:Gem::Requirement
30
29
  requirements:
31
30
  - - ">="
32
31
  - !ruby/object:Gem::Version
33
- version: '3.2'
32
+ version: '6.1'
34
33
  type: :runtime
35
34
  prerelease: false
36
35
  version_requirements: !ruby/object:Gem::Requirement
37
36
  requirements:
38
37
  - - ">="
39
38
  - !ruby/object:Gem::Version
40
- version: '3.2'
39
+ version: '6.1'
41
40
  - !ruby/object:Gem::Dependency
42
41
  name: spring
43
42
  requirement: !ruby/object:Gem::Requirement
@@ -62,13 +61,12 @@ files:
62
61
  - LICENSE
63
62
  - README.md
64
63
  - lib/dotenv-rails.rb
65
- - lib/dotenv/rails-now.rb
66
- - lib/dotenv/rails.rb
67
64
  homepage: https://github.com/bkeepers/dotenv
68
65
  licenses:
69
66
  - MIT
70
- metadata: {}
71
- post_install_message:
67
+ metadata:
68
+ changelog_uri: https://github.com/bkeepers/dotenv/releases
69
+ funding_uri: https://github.com/sponsors/bkeepers
72
70
  rdoc_options: []
73
71
  require_paths:
74
72
  - lib
@@ -83,8 +81,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
83
81
  - !ruby/object:Gem::Version
84
82
  version: '0'
85
83
  requirements: []
86
- rubygems_version: 3.0.3
87
- signing_key:
84
+ rubygems_version: 3.6.9
88
85
  specification_version: 4
89
86
  summary: Autoload dotenv in Rails.
90
87
  test_files: []
@@ -1,10 +0,0 @@
1
- # If you use gems that require environment variables to be set before they are
2
- # loaded, then list `dotenv-rails` in the `Gemfile` before those other gems and
3
- # require `dotenv/rails-now`.
4
- #
5
- # gem "dotenv-rails", require: "dotenv/rails-now"
6
- # gem "gem-that-requires-env-variables"
7
- #
8
-
9
- require "dotenv/rails"
10
- Dotenv::Railtie.load
data/lib/dotenv/rails.rb DELETED
@@ -1,77 +0,0 @@
1
- require "dotenv"
2
-
3
- # Fix for rake tasks loading in development
4
- #
5
- # Dotenv loads environment variables when the Rails application is initialized.
6
- # When running `rake`, the Rails application is initialized in development.
7
- # Rails includes some hacks to set `RAILS_ENV=test` when running `rake test`,
8
- # but rspec does not include the same hacks.
9
- #
10
- # See https://github.com/bkeepers/dotenv/issues/219
11
- if defined?(Rake.application)
12
- task_regular_expression = /^(default$|parallel:spec|spec(:|$))/
13
- if Rake.application.top_level_tasks.grep(task_regular_expression).any?
14
- environment = Rake.application.options.show_tasks ? "development" : "test"
15
- Rails.env = ENV["RAILS_ENV"] ||= environment
16
- end
17
- end
18
-
19
- Dotenv.instrumenter = ActiveSupport::Notifications
20
-
21
- # Watch all loaded env files with Spring
22
- begin
23
- require "spring/commands"
24
- ActiveSupport::Notifications.subscribe(/^dotenv/) do |*args|
25
- event = ActiveSupport::Notifications::Event.new(*args)
26
- Spring.watch event.payload[:env].filename if Rails.application
27
- end
28
- rescue LoadError, ArgumentError
29
- # Spring is not available
30
- end
31
-
32
- module Dotenv
33
- # Dotenv Railtie for using Dotenv to load environment from a file into
34
- # Rails applications
35
- class Railtie < Rails::Railtie
36
- # Public: Load dotenv
37
- #
38
- # This will get called during the `before_configuration` callback, but you
39
- # can manually call `Dotenv::Railtie.load` if you needed it sooner.
40
- def load
41
- Dotenv.load(*dotenv_files)
42
- end
43
-
44
- # Public: Reload dotenv
45
- #
46
- # Same as `load`, but will override existing values in `ENV`
47
- def overload
48
- Dotenv.overload(*dotenv_files)
49
- end
50
-
51
- # Internal: `Rails.root` is nil in Rails 4.1 before the application is
52
- # initialized, so this falls back to the `RAILS_ROOT` environment variable,
53
- # or the current working directory.
54
- def root
55
- Rails.root || Pathname.new(ENV["RAILS_ROOT"] || Dir.pwd)
56
- end
57
-
58
- # Rails uses `#method_missing` to delegate all class methods to the
59
- # instance, which means `Kernel#load` gets called here. We don't want that.
60
- def self.load
61
- instance.load
62
- end
63
-
64
- private
65
-
66
- def dotenv_files
67
- [
68
- root.join(".env.#{Rails.env}.local"),
69
- (root.join(".env.local") unless Rails.env.test?),
70
- root.join(".env.#{Rails.env}"),
71
- root.join(".env")
72
- ].compact
73
- end
74
-
75
- config.before_configuration { load }
76
- end
77
- end