dotenv-rails 2.8.1 → 3.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +156 -93
- metadata +10 -12
- data/lib/dotenv/rails-now.rb +0 -10
- data/lib/dotenv/rails.rb +0 -77
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e29526945ace92347bf5840bcfbc79a548c02ab2ed5622476e8799f92db8d88f
|
4
|
+
data.tar.gz: dfb6e1fb96fa95908ed74aa4d3f3bfdbd65a7cb64ac817919ad4cf3507c4f53f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9accefcf2609b10c629aef41a742f72857c60fbdf2fad1da17b68c3b5703d87507f6b238aa653644a6635d60f650285d750f7405fd2dd5a82179ea6107b0b2b1
|
7
|
+
data.tar.gz: a726eb63136679a215998a1534bd01b62bd56a9ea539e8f3e7196fab15b3b9512d606164e7ec5d65e6f39b9bdc5e5ecd2d8b89a9947d13a5e2828cd0b4892071
|
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# dotenv [![Gem Version](https://badge.fury.io/rb/dotenv.svg)](https://badge.fury.io/rb/dotenv)
|
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,52 +8,36 @@ But it is not always practical to set environment variables on development machi
|
|
8
8
|
|
9
9
|
## Installation
|
10
10
|
|
11
|
-
|
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
|
14
|
+
gem 'dotenv', groups: [:development, :test]
|
17
15
|
```
|
18
16
|
|
19
|
-
|
17
|
+
## Usage
|
18
|
+
|
19
|
+
Add your application configuration to your `.env` file in the root of your project:
|
20
20
|
|
21
21
|
```shell
|
22
|
-
|
22
|
+
S3_BUCKET=YOURS3BUCKET
|
23
|
+
SECRET_KEY=YOURSECRETKEYGOESHERE
|
23
24
|
```
|
24
25
|
|
25
|
-
|
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
|
-
|
31
|
-
Bundler.require(*Rails.groups)
|
32
|
-
|
33
|
-
# Load dotenv only in development or test environment
|
34
|
-
if ['development', 'test'].include? ENV['RAILS_ENV']
|
35
|
-
Dotenv::Railtie.load
|
36
|
-
end
|
37
|
-
|
38
|
-
HOSTNAME = ENV['HOSTNAME']
|
29
|
+
config.fog_directory = ENV['S3_BUCKET']
|
39
30
|
```
|
40
31
|
|
41
|
-
|
32
|
+
See the [API Docs](https://rubydoc.info/github/bkeepers/dotenv/main) for more.
|
42
33
|
|
43
|
-
|
44
|
-
gem 'dotenv-rails', require: 'dotenv/rails-now'
|
45
|
-
gem 'gem-that-requires-env-variables'
|
46
|
-
```
|
47
|
-
|
48
|
-
### Sinatra or Plain ol' Ruby
|
34
|
+
### Rails
|
49
35
|
|
50
|
-
|
36
|
+
Dotenv will automatically load when your Rails app boots. See [Customizing Rails](#customizing-rails) to change which files are loaded and when.
|
51
37
|
|
52
|
-
|
53
|
-
$ gem install dotenv
|
54
|
-
```
|
38
|
+
### Sinatra / Ruby
|
55
39
|
|
56
|
-
|
40
|
+
Load Dotenv as early as possible in your application bootstrap process:
|
57
41
|
|
58
42
|
```ruby
|
59
43
|
require 'dotenv/load'
|
@@ -70,17 +54,17 @@ require 'dotenv'
|
|
70
54
|
Dotenv.load('file1.env', 'file2.env')
|
71
55
|
```
|
72
56
|
|
73
|
-
|
57
|
+
## Autorestore in tests
|
74
58
|
|
75
|
-
|
76
|
-
$ dotenv ./script.rb
|
77
|
-
```
|
59
|
+
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`.
|
78
60
|
|
79
|
-
|
61
|
+
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).
|
80
62
|
|
81
|
-
|
82
|
-
|
83
|
-
|
63
|
+
To use this behavior outside of a Rails app, just `require "dotenv/autorestore"` in your test suite.
|
64
|
+
|
65
|
+
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.
|
66
|
+
|
67
|
+
### Rake
|
84
68
|
|
85
69
|
To ensure `.env` is loaded in rake, load the tasks:
|
86
70
|
|
@@ -88,41 +72,119 @@ To ensure `.env` is loaded in rake, load the tasks:
|
|
88
72
|
require 'dotenv/tasks'
|
89
73
|
|
90
74
|
task mytask: :dotenv do
|
91
|
-
|
75
|
+
# things that require .env
|
92
76
|
end
|
93
77
|
```
|
94
78
|
|
95
|
-
|
79
|
+
### CLI
|
96
80
|
|
97
|
-
|
81
|
+
You can use the `dotenv` executable load `.env` before launching your application:
|
98
82
|
|
99
|
-
```
|
100
|
-
|
101
|
-
SECRET_KEY=YOURSECRETKEYGOESHERE
|
83
|
+
```console
|
84
|
+
$ dotenv ./script.rb
|
102
85
|
```
|
103
86
|
|
104
|
-
|
87
|
+
The `dotenv` executable also accepts the 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.
|
105
88
|
|
106
|
-
```
|
107
|
-
|
89
|
+
```console
|
90
|
+
$ dotenv -f ".env.local,.env" ./script.rb
|
108
91
|
```
|
109
92
|
|
110
|
-
|
93
|
+
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.
|
111
94
|
|
112
|
-
```
|
113
|
-
|
114
|
-
export SECRET_KEY=YOURSECRETKEYGOESHERE
|
95
|
+
```console
|
96
|
+
$ dotenv -i -f ".env.local,.env" ./script.rb
|
115
97
|
```
|
116
98
|
|
117
|
-
###
|
99
|
+
### Load Order
|
118
100
|
|
119
|
-
If you
|
101
|
+
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`.
|
120
102
|
|
121
|
-
```
|
122
|
-
|
103
|
+
```ruby
|
104
|
+
gem 'dotenv', require: 'dotenv/load'
|
105
|
+
gem 'gem-that-requires-env-variables'
|
106
|
+
```
|
107
|
+
|
108
|
+
### Customizing Rails
|
109
|
+
|
110
|
+
Dotenv will load the following files depending on `RAILS_ENV`, with the first file having the highest precedence, and `.env` having the lowest precedence:
|
111
|
+
|
112
|
+
<table>
|
113
|
+
<thead>
|
114
|
+
<tr>
|
115
|
+
<th>Priority</th>
|
116
|
+
<th colspan="3">Environment</th>
|
117
|
+
<th><code>.gitignore</code>it?</th>
|
118
|
+
<th>Notes</th>
|
119
|
+
</tr>
|
120
|
+
<tr>
|
121
|
+
<th></th>
|
122
|
+
<th>development</th>
|
123
|
+
<th>test</th>
|
124
|
+
<th>production</th>
|
125
|
+
<th></th>
|
126
|
+
<th></th>
|
127
|
+
</tr>
|
128
|
+
</thead>
|
129
|
+
<tr>
|
130
|
+
<td>highest</td>
|
131
|
+
<td><code>.env.development.local</code></td>
|
132
|
+
<td><code>.env.test.local</code></td>
|
133
|
+
<td><code>.env.production.local</code></td>
|
134
|
+
<td>Yes</td>
|
135
|
+
<td>Environment-specific local overrides</td>
|
136
|
+
</tr>
|
137
|
+
<tr>
|
138
|
+
<td>2nd</td>
|
139
|
+
<td><code>.env.local</code></td>
|
140
|
+
<td><strong>N/A</strong></td>
|
141
|
+
<td><code>.env.local</code></td>
|
142
|
+
<td>Yes</td>
|
143
|
+
<td>Local overrides</td>
|
144
|
+
</tr>
|
145
|
+
<tr>
|
146
|
+
<td>3rd</td>
|
147
|
+
<td><code>.env.development</code></td>
|
148
|
+
<td><code>.env.test</code></td>
|
149
|
+
<td><code>.env.production</code></td>
|
150
|
+
<td>No</td>
|
151
|
+
<td>Shared environment-specific variables</td>
|
152
|
+
</tr>
|
153
|
+
<tr>
|
154
|
+
<td>last</td>
|
155
|
+
<td><code>.env</code></td>
|
156
|
+
<td><code>.env</code></td>
|
157
|
+
<td><code>.env</code></td>
|
158
|
+
<td><a href="#should-i-commit-my-env-file">Maybe</a></td>
|
159
|
+
<td>Shared for all environments</td>
|
160
|
+
</tr>
|
161
|
+
</table>
|
162
|
+
|
163
|
+
|
164
|
+
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`
|
165
|
+
|
166
|
+
```ruby
|
167
|
+
# config/application.rb
|
168
|
+
Bundler.require(*Rails.groups)
|
169
|
+
|
170
|
+
# Load .env.local in test
|
171
|
+
Dotenv::Rails.files.unshift(".env.local") if ENV["RAILS_ENV"] == "test"
|
172
|
+
|
173
|
+
module YourApp
|
174
|
+
class Application < Rails::Application
|
175
|
+
# ...
|
176
|
+
end
|
177
|
+
end
|
123
178
|
```
|
124
179
|
|
125
|
-
|
180
|
+
Available options:
|
181
|
+
|
182
|
+
* `Dotenv::Rails.files` - list of files to be loaded, in order of precedence.
|
183
|
+
* `Dotenv::Rails.overwrite` - Overwrite exiting `ENV` variables with contents of `.env*` files
|
184
|
+
|
185
|
+
### Multi-line values
|
186
|
+
|
187
|
+
Multi-line values with line breaks must be surrounded with double quotes.
|
126
188
|
|
127
189
|
```shell
|
128
190
|
PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----
|
@@ -132,7 +194,12 @@ HkVN9...
|
|
132
194
|
-----END DSA PRIVATE KEY-----"
|
133
195
|
```
|
134
196
|
|
135
|
-
|
197
|
+
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`:
|
198
|
+
|
199
|
+
```shell
|
200
|
+
DOTENV_LINEBREAK_MODE=legacy
|
201
|
+
PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----\nHkVN9...\n-----END DSA PRIVATE KEY-----\n"
|
202
|
+
```
|
136
203
|
|
137
204
|
### Command Substitution
|
138
205
|
|
@@ -166,6 +233,15 @@ SECRET_KEY=YOURSECRETKEYGOESHERE # comment
|
|
166
233
|
SECRET_HASH="something-with-a-#-hash"
|
167
234
|
```
|
168
235
|
|
236
|
+
### Exports
|
237
|
+
|
238
|
+
For compatability, you may also add `export` in front of each line so you can `source` the file in bash:
|
239
|
+
|
240
|
+
```shell
|
241
|
+
export S3_BUCKET=YOURS3BUCKET
|
242
|
+
export SECRET_KEY=YOURSECRETKEYGOESHERE
|
243
|
+
```
|
244
|
+
|
169
245
|
### Required Keys
|
170
246
|
|
171
247
|
If a particular configuration value is required but not set, it's appropriate to raise an error.
|
@@ -191,39 +267,11 @@ Dotenv.parse(".env.local", ".env")
|
|
191
267
|
|
192
268
|
This method returns a hash of the ENV var name/value pairs.
|
193
269
|
|
194
|
-
|
195
|
-
|
196
|
-
### Can I use dotenv in production?
|
197
|
-
|
198
|
-
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.
|
199
|
-
|
200
|
-
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`.
|
201
|
-
|
202
|
-
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>`.
|
203
|
-
|
204
|
-
### What other .env* files can I use?
|
205
|
-
|
206
|
-
`dotenv-rails` will override in the following order (highest defined variable overrides lower):
|
207
|
-
|
208
|
-
| Hierarchy Priority | Filename | Environment | Should I `.gitignore`it? | Notes |
|
209
|
-
| ------------------ | ------------------------ | -------------------- | --------------------------------------------------- | ------------------------------------------------------------ |
|
210
|
-
| 1st (highest) | `.env.development.local` | Development | Yes! | Local overrides of environment-specific settings. |
|
211
|
-
| 1st | `.env.test.local` | Test | Yes! | Local overrides of environment-specific settings. |
|
212
|
-
| 1st | `.env.production.local` | Production | Yes! | Local overrides of environment-specific settings. |
|
213
|
-
| 2nd | `.env.local` | Wherever the file is | Definitely. | Local overrides. This file is loaded for all environments _except_ `test`. |
|
214
|
-
| 3rd | `.env.development` | Development | No. | Shared environment-specific settings |
|
215
|
-
| 3rd | `.env.test` | Test | No. | Shared environment-specific settings |
|
216
|
-
| 3rd | `.env.production` | Production | No. | Shared environment-specific settings |
|
217
|
-
| Last | `.env` | All Environments | Depends (See [below](#should-i-commit-my-env-file)) | The Original® |
|
218
|
-
|
219
|
-
|
220
|
-
### Should I commit my .env file?
|
221
|
-
|
222
|
-
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.
|
223
|
-
|
270
|
+
### Templates
|
224
271
|
|
225
272
|
You can use the `-t` or `--template` flag on the dotenv cli to create a template of your `.env` file.
|
226
|
-
|
273
|
+
|
274
|
+
```console
|
227
275
|
$ dotenv -t .env
|
228
276
|
```
|
229
277
|
A template will be created in your working directory named `{FINAME}.template`. So in the above example, it would create a `.env.template` file.
|
@@ -244,14 +292,29 @@ S3_BUCKET=S3_BUCKET
|
|
244
292
|
SECRET_KEY=SECRET_KEY
|
245
293
|
```
|
246
294
|
|
295
|
+
## Frequently Answered Questions
|
296
|
+
|
297
|
+
### Can I use dotenv in production?
|
298
|
+
|
299
|
+
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.
|
300
|
+
|
301
|
+
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`.
|
302
|
+
|
303
|
+
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>`.
|
304
|
+
|
305
|
+
### Should I commit my .env file?
|
306
|
+
|
307
|
+
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.
|
308
|
+
|
247
309
|
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.
|
248
310
|
|
249
|
-
### Why is it not
|
311
|
+
### Why is it not overwriting existing `ENV` variables?
|
250
312
|
|
251
|
-
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.
|
313
|
+
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`.
|
252
314
|
|
253
|
-
You can also use the `-o` or `--
|
254
|
-
|
315
|
+
You can also use the `-o` or `--overwrite` flag on the dotenv cli to overwrite existing `ENV` variables.
|
316
|
+
|
317
|
+
```console
|
255
318
|
$ dotenv -o -f ".env.local,.env"
|
256
319
|
```
|
257
320
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dotenv-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 3.0.2
|
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:
|
11
|
+
date: 2024-02-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: dotenv
|
@@ -16,28 +16,28 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - '='
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: 3.0.2
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - '='
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
26
|
+
version: 3.0.2
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: railties
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
33
|
+
version: '6.1'
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
40
|
+
version: '6.1'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: spring
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -62,13 +62,11 @@ files:
|
|
62
62
|
- LICENSE
|
63
63
|
- README.md
|
64
64
|
- lib/dotenv-rails.rb
|
65
|
-
- lib/dotenv/rails-now.rb
|
66
|
-
- lib/dotenv/rails.rb
|
67
65
|
homepage: https://github.com/bkeepers/dotenv
|
68
66
|
licenses:
|
69
67
|
- MIT
|
70
68
|
metadata: {}
|
71
|
-
post_install_message:
|
69
|
+
post_install_message:
|
72
70
|
rdoc_options: []
|
73
71
|
require_paths:
|
74
72
|
- lib
|
@@ -83,8 +81,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
83
81
|
- !ruby/object:Gem::Version
|
84
82
|
version: '0'
|
85
83
|
requirements: []
|
86
|
-
rubygems_version: 3.
|
87
|
-
signing_key:
|
84
|
+
rubygems_version: 3.5.3
|
85
|
+
signing_key:
|
88
86
|
specification_version: 4
|
89
87
|
summary: Autoload dotenv in Rails.
|
90
88
|
test_files: []
|
data/lib/dotenv/rails-now.rb
DELETED
@@ -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
|