envied 0.7.2 → 0.8.0

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
  SHA1:
3
- metadata.gz: edcedc4d8c20ce9daebc7a4850197705ea88e0d1
4
- data.tar.gz: 76db142dc7ae98934704145a788617b2c62948f0
3
+ metadata.gz: 3da0d6e67da927a774c498a1042d5ee27b3ee3f9
4
+ data.tar.gz: 9dadd80c4f668ddb1990d0f8837b730db4ecd54c
5
5
  SHA512:
6
- metadata.gz: cd8ca76d1dda0a489be8b5c97ddf2d4c75a3e10a8eafe9d64a14c00432b40025948f62f782069228b64029494fb8544ea9b9c9e11ac02c96f032feb51132949d
7
- data.tar.gz: 3e523db66a464e6b3a236908f5439d38d877aa6cf867ad5754e7555b1e20edb99e2f6f15f3757026c999d8cc7e43b550212a4ea0fa940bb1b9734fc1ea7cfdb7
6
+ metadata.gz: c2ef13db6f79e0590da7a86217c5b5b50b4b72b993f4a6b7711feffbccf8a285e9ec20743b2fd7859e901bc495dc13623b8068446ac0664e954687eba1b6f0fd
7
+ data.tar.gz: d5eaf03debc9c15d30360d8ee7c2fd75dae3cbac6d92d8cd72e9adc82c8be477a8a661532e24a54d435998da5de128a013c8c1a2af38f9cd49f9f8813b97623d
data/CHANGELOG.md CHANGED
@@ -1,98 +1,136 @@
1
- # 0.7.2 / 2014-9-7
1
+ ## 0.8.0 / 2014-10-04
2
2
 
3
- ## Added:
4
- * extract-task: see all ENV-variables used in your project.
3
+ ### Added
5
4
 
6
- ```bash
7
- $ bin/envied extract
8
- Found 63 occurrences of 45 variables:
9
- BUNDLE_GEMFILE
10
- * config/boot.rb:4
11
- * config/boot.rb:6
12
- ...
13
- ```
5
+ * the `check:heroku:binstub` task uses ENV['HEROKU_APP'] as app-option if present
6
+ * the `check` and `check:heroku` task now have a `--quiet` option
14
7
 
15
- * version-task (i.e. bin/envied --version)
8
+ This prevents output on STDOUT for a successful check.
9
+ Handy for wrapper-scripts:
16
10
 
17
- # 0.7.1 / 2014-08-29
11
+ # some bash-script 'ensure-env'
12
+ #!/usr/bin/env bash
13
+ bundle exec envied check --quiet --groups default && $@
18
14
 
19
- * Total refactor (TM).
15
+ $ ./ensure-env echo 'works!'
16
+ works!
20
17
 
21
- * Fix bug in Heroku binstub.
18
+ * ENVied.require accepts string with groups, e.g. 'default,production'
22
19
 
23
- It checked for group 'default,production' instead of 'default' and 'production'.
20
+ This way it's possible to easily require groups using the ENV:
24
21
 
25
- # 0.7.0 / 2014-08-24
22
+ # config/initializers/envied.rb
23
+ ENVied.require(*ENV['ENVIED_GROUPS'] || Rails.groups)
26
24
 
27
- * Add init:rails-task for setup in Rails applications.
25
+ $ ENVIED_GROUPS='default,production' bin/rails server
28
26
 
29
- # 0.6.3 / 2014-08-22
27
+ ### Fixed
30
28
 
31
- * Fix bug: 'false' was not a coercible value.
29
+ * extract-task would report at most one variable per line of code.
30
+ * extract-task would not extract variable names with digits
32
31
 
33
- # 0.6.2 / 2014-08-20
32
+ ### Deprecated
34
33
 
35
- * Add `envied check:heroku` to do a check on your Heroku app.
34
+ * titlecase variable types. Use downcased instead: `variable :PORT, :integer`
36
35
 
37
- * Add `envied check:heroku:binstub` to generate script for convenient 'check:heroku'
36
+ ### Removed
38
37
 
39
- # 0.6.1 / 2014-08-13
38
+ * extract: test/spec-folder are no longer part of the default globs.
40
39
 
41
- * Add `envied check` to check whether defined variables are present and valid.
40
+ Use the option `--tests` to include it:
42
41
 
43
- # 0.6.0 / 2014-08-13
42
+ $ bundle exec envied extract --tests
44
43
 
45
- * The configuration now lives in `Envfile` by default.
44
+ ## 0.7.2 / 2014-09-07
46
45
 
47
- # 0.5.0 / 2014-07-02
46
+ ### Added
48
47
 
49
- * add Array Hash types
48
+ * extract-task: see all ENV-variables used in your project.
50
49
 
51
- ```ruby
52
- # in env.rb
53
- ENVied.configure { variable :TAGS, :Array; variable :HASH, :Hash }
54
- ENVied.require
50
+ $ envied extract
51
+ Found 63 occurrences of 45 variables:
52
+ BUNDLE_GEMFILE
53
+ * config/boot.rb:4
54
+ * config/boot.rb:6
55
+ ...
55
56
 
56
- $ HASH=a=1&b=2 TAGS=tag1,tag2 ruby -renvied -r./env.rb -e 'p ENVied.TAGS'
57
- # ["tag1", "tag2"]
58
- $ HASH='a=1&b=2' TAGS=tag1,tag2 ruby -renvied -r./env.rb -e 'p ENVied.HASH'
59
- # {'a' => '1', 'b' => '2'}
60
- ```
57
+ * version-task (i.e. bin/envied --version)
61
58
 
62
- # 0.4.0 / 2014-05-16
59
+ ## 0.7.1 / 2014-08-29
63
60
 
64
- * groups added
61
+ * Total refactor (TM).
65
62
 
66
- This allows for more fine-grained requiring.
67
- See the section in the [README](https://github.com/eval/envied/tree/v0.4.0#groups).
63
+ * Fix bug in Heroku binstub.
68
64
 
69
- * configuring is now simpler:
65
+ It checked for group 'default,production' instead of 'default' and 'production'.
70
66
 
71
- ```ruby
72
- ENVied.configure { variable :RACK_ENV }
73
- # vs
74
- ENVied.configure {|env| env.variable :RACK_ENV }
75
- ```
67
+ ## 0.7.0 / 2014-08-24
76
68
 
77
- * Deprecate `require!`. Use `require` instead.
69
+ * Add init:rails-task for setup in Rails applications.
78
70
 
79
- Just like requiring groups with Bundler.
71
+ ## 0.6.3 / 2014-08-22
80
72
 
81
- * Deprecate lowercase methods for uppercase ENV-variables.
73
+ * Fix bug: 'false' was not a coercible value.
82
74
 
83
- `ENV['RACK_ENV']` is no longer accessible as `ENVied.rack_env`, only as `ENVied.RACK_ENV`.
84
- This is not only what you would expect, but it also reduces the chance of clashing with existing class-methods.
75
+ ## 0.6.2 / 2014-08-20
85
76
 
86
- # 0.3.0 / 2014-03-14
77
+ * Add `envied check:heroku` to do a check on your Heroku app.
87
78
 
88
- * defaults need to be enabled explicitly:
79
+ * Add `envied check:heroku:binstub` to generate script for convenient 'check:heroku'
89
80
 
90
- `ENVied.configure(enable_defaults: Rails.env.development?) { ... }`
81
+ ## 0.6.1 / 2014-08-13
91
82
 
92
- # 0.2.0 / 2014-03-14
83
+ * Add `envied check` to check whether defined variables are present and valid.
93
84
 
94
- * add defaults
85
+ ## 0.6.0 / 2014-08-13
95
86
 
96
- # 0.1.0 / 2014-03-13
87
+ * The configuration now lives in `Envfile` by default.
97
88
 
98
- * add defaults
89
+ ## 0.5.0 / 2014-07-02
90
+
91
+ * add Array Hash types
92
+
93
+ # in env.rb
94
+ ENVied.configure { variable :TAGS, :Array; variable :HASH, :Hash }
95
+ ENVied.require
96
+
97
+ $ HASH=a=1&b=2 TAGS=tag1,tag2 ruby -renvied -r./env.rb -e 'p ENVied.TAGS'
98
+ # ["tag1", "tag2"]
99
+ $ HASH='a=1&b=2' TAGS=tag1,tag2 ruby -renvied -r./env.rb -e 'p ENVied.HASH'
100
+ # {'a' => '1', 'b' => '2'}
101
+
102
+ ## 0.4.0 / 2014-05-16
103
+
104
+ * groups added
105
+
106
+ This allows for more fine-grained requiring.
107
+ See the section in the [README](https://github.com/eval/envied/tree/v0.4.0#groups).
108
+
109
+ * configuring is now simpler:
110
+
111
+ ENVied.configure { variable :RACK_ENV }
112
+ # vs
113
+ ENVied.configure {|env| env.variable :RACK_ENV }
114
+
115
+ * Deprecate `require!`. Use `require` instead.
116
+
117
+ Just like requiring groups with Bundler.
118
+
119
+ * Deprecate lowercase methods for uppercase ENV-variables.
120
+
121
+ `ENV['RACK_ENV']` is no longer accessible as `ENVied.rack_env`, only as `ENVied.RACK_ENV`.
122
+ This is not only what you would expect, but it also reduces the chance of clashing with existing class-methods.
123
+
124
+ ## 0.3.0 / 2014-03-14
125
+
126
+ * defaults need to be enabled explicitly:
127
+
128
+ `ENVied.configure(enable_defaults: Rails.env.development?) { ... }`
129
+
130
+ ## 0.2.0 / 2014-03-14
131
+
132
+ * add defaults
133
+
134
+ ## 0.1.0 / 2014-03-13
135
+
136
+ * add defaults
data/README.md CHANGED
@@ -17,7 +17,7 @@
17
17
  * [Groups](#groups)
18
18
  * [Defaults](#defaults)
19
19
  * [More examples](#more-examples)
20
- * [Rails](#rails)
20
+ * [Rails](#rails--spring)
21
21
  * [Command-line interface](#command-line-interface)
22
22
  * [Testing](#testing)
23
23
  * [Developing](#developing)
@@ -31,8 +31,8 @@ After [successful installation](#installation), define some variables in `Envfil
31
31
 
32
32
  ```ruby
33
33
  # file: Envfile
34
- variable :FORCE_SSL, :Boolean
35
- variable :PORT, :Integer
34
+ variable :FORCE_SSL, :boolean
35
+ variable :PORT, :integer
36
36
  ```
37
37
 
38
38
  ### 2) Check for presence and coercibility
@@ -79,14 +79,14 @@ Add this line to your application's Gemfile:
79
79
 
80
80
  The following types are supported:
81
81
 
82
- * `:String` (implied)
83
- * `:Boolean` (e.g. '0'/'1', 'f'/'t', 'false'/'true', 'off'/'on', 'no'/'yes' for resp. false and true)
84
- * `:Integer`
85
- * `:Symbol`
86
- * `:Date` (e.g. '2014-3-26')
87
- * `:Time` (e.g. '14:00')
88
- * `:Hash` (e.g. 'a=1&b=2' becomes `{'a' => '1', 'b' => '2'}`)
89
- * `:Array` (e.g. 'tag1,tag2' becomes `['tag1', 'tag2']`)
82
+ * `:string` (implied)
83
+ * `:boolean` (e.g. '0'/'1', 'f'/'t', 'false'/'true', 'off'/'on', 'no'/'yes' for resp. false and true)
84
+ * `:integer`
85
+ * `:symbol`
86
+ * `:date` (e.g. '2014-3-26')
87
+ * `:time` (e.g. '14:00')
88
+ * `:hash` (e.g. 'a=1&b=2' becomes `{'a' => '1', 'b' => '2'}`)
89
+ * `:array` (e.g. 'tag1,tag2' becomes `['tag1', 'tag2']`)
90
90
 
91
91
  ### Groups
92
92
 
@@ -95,10 +95,10 @@ It's similar to groups in a Gemfile:
95
95
 
96
96
  ```ruby
97
97
  # file: Envfile
98
- variable :FORCE_SSL, :Boolean
98
+ variable :FORCE_SSL, :boolean
99
99
 
100
100
  group :production do
101
- variable :NEW_RELIC_LICENSE_KEY
101
+ variable :SECRET_KEY_BASE
102
102
  end
103
103
  ```
104
104
 
@@ -106,7 +106,15 @@ end
106
106
  # For local development you would typically do:
107
107
  ENVied.require(:default) #=> Only ENV['FORCE_SSL'] is required
108
108
  # On the production server:
109
- ENVied.require(:default, :production) #=> ...also ENV['NEW_RELIC_LICENSE_KEY'] is required
109
+ ENVied.require(:default, :production) #=> ...also ENV['SECRET_KEY_BASE'] is required
110
+
111
+ # You can also pass it a string with the groups separated by comma's:
112
+ ENVied.require('default, production')
113
+
114
+ # This allows for easily requiring groups using the ENV:
115
+ ENVied.require(ENV['ENVIED_GROUPS'])
116
+ # ...then from the prompt:
117
+ $ ENVIED_GROUPS='default,production' bin/rails server
110
118
 
111
119
  # BTW the following are equivalent:
112
120
  ENVied.require
@@ -134,16 +142,17 @@ Please remember that ENVied only **reads** from ENV; it doesn't mutate ENV.
134
142
  Don't let setting a default for, say `RAILS_ENV`, give you the impression that `ENV['RAILS_ENV']` is set.
135
143
  As a rule of thumb you should only use defaults:
136
144
  * for local development
137
- * for ENV-variables that your application introduces (i.e. for `ENV['STAFF_EMAILS']` not for `ENV['REDIS_URL']`)
145
+ * for ENV-variables that are solely used by your application (i.e. for `ENV['STAFF_EMAILS']`, not for `ENV['RAILS_ENV']`)
138
146
 
139
147
  ### More examples
140
148
 
141
149
  * See the [examples](/examples)-folder for a more extensive Envfile
142
- * See [the Envfile](https://github.com/eval/bunny_drain/blob/c54d7d977afb5e23a92da7a2fd0d39f6a7e29bf1/Envfile) for the bunndy_drain application
150
+ * See [the Envfile](https://github.com/eval/bunny_drain/blob/c54d7d977afb5e23a92da7a2fd0d39f6a7e29bf1/Envfile) for the bunny_drain application
143
151
 
144
- ## Rails
152
+ ## Rails & Spring
145
153
 
146
- **tl;dr** use the `init:rails`-task to generate the necessary files for a Rails app (see [installation](#installation)).
154
+ **tl;dr** use the `init:rails`-task to generate the necessary files for a Rails app (see [installation](#installation)).
155
+ Remember: the environment as seen by Spring is not up-to-date until *after* initialization - this is good enough for checking the environment, but too late to use it to configure your app.
147
156
 
148
157
  ---
149
158
 
@@ -173,9 +182,10 @@ If you want to change Rails' config based on ENV-variables you should put this i
173
182
  ```ruby
174
183
  # config/initializers/envied.rb as generated by 'bundle exec envied init:rails'
175
184
  ENVied.springify do
176
- ENVied.require(:default, ENV['RAILS_ENV'])
185
+ ENVied.require(*ENV['ENVIED_GROUPS'] || Rails.groups)
177
186
 
178
- Rails.configuration.force_ssl = ENVied.FORCE_SSL
187
+ # Put any initialization based on ENV-variables below, e.g.:
188
+ # Rails.configuration.force_ssl = ENVied.FORCE_SSL
179
189
  end
180
190
  ```
181
191
 
@@ -184,30 +194,30 @@ end
184
194
  ```bash
185
195
  $ envied help
186
196
  Commands:
187
- envied --version # Shows version number
188
- envied check # Checks whether you environment contains the defined variables
189
- envied check:heroku # Checks whether a Heroku config contains the defined variables
190
- envied check:heroku:binstub # Generates a shell script for the check:heroku-task
191
- envied extract # Shows candidate variables (i.e. occurences of ENV['X'])
192
- envied help [COMMAND] # Describe available commands or one specific command
193
- envied init # Generates a default Envfile in the current working directory
194
- envied init:rails # Generate all files needed for a Rails project
197
+ envied check # Checks whether you environment contains required variables
198
+ envied check:heroku # Checks whether a Heroku config contains required variables
199
+ envied check:heroku:binstub # Generates a shell script for the check:heroku-task
200
+ envied extract # Grep code to find ENV-variables
201
+ envied help [COMMAND] # Describe available commands or one specific command
202
+ envied init # Generates a default Envfile in the current working directory
203
+ envied init:rails # Generate all files needed for a Rails project
204
+ envied version, --version, -v # Shows version number
195
205
  ```
196
206
 
197
207
  ## Testing
198
208
 
199
209
  ```bash
200
- bundle install --binstubs
210
+ bundle install
201
211
 
202
- bin/rspec
212
+ bundle exec rspec
203
213
  # or
204
- bin/rake
214
+ bundle exec rake
205
215
  ```
206
216
 
207
217
  ## Developing
208
218
 
209
219
  ```bash
210
- bin/pry --gem
220
+ bundle exec pry --gem
211
221
  ```
212
222
 
213
223
  ## Contributing
@@ -10,10 +10,10 @@ enable_defaults!(&not_production_nor_ci)
10
10
  # we want it to be present though; heck, we're using it in this file!
11
11
  variable :RACK_ENV
12
12
 
13
- variable :FORCE_SSL, :Boolean, default: false
14
- variable :PORT, :Integer, default: 3000
13
+ variable :FORCE_SSL, :boolean, default: false
14
+ variable :PORT, :integer, default: 3000
15
15
  # generate the default value using the value of PORT:
16
- variable :PUBLIC_HOST_WITH_PORT, :String, default: proc {|envied| "localhost:#{envied.PORT}" }
16
+ variable :PUBLIC_HOST_WITH_PORT, :string, default: proc {|envied| "localhost:#{envied.PORT}" }
17
17
 
18
18
  group :production do
19
19
  variable :MAIL_PAAS_USERNAME
@@ -27,7 +27,7 @@ end
27
27
  group :not_ci do
28
28
  # CI needs no puma-threads, and sidekiq-stuff etc.
29
29
  # Define that here:
30
- variable :MIN_THREADS, :Integer, default: 1
30
+ variable :MIN_THREADS, :integer, default: 1
31
31
  # more...
32
32
  end
33
33
 
data/lib/envied.rb CHANGED
@@ -38,7 +38,8 @@ class ENVied
38
38
  end
39
39
 
40
40
  def self.required_groups(*groups)
41
- result = groups.compact
41
+ splitter = ->(group){ group.is_a?(String) ? group.split(/ *, */) : group }
42
+ result = groups.compact.map(&splitter).flatten
42
43
  result.any? ? result.map(&:to_sym) : [:default]
43
44
  end
44
45
 
data/lib/envied/cli.rb CHANGED
@@ -6,16 +6,26 @@ class ENVied
6
6
  include Thor::Actions
7
7
  source_root File.expand_path('../templates', __FILE__)
8
8
 
9
- desc "--version", "Shows version number"
9
+ desc "version, --version, -v", "Shows version number"
10
10
  def version
11
11
  puts ENVied::VERSION
12
12
  end
13
13
  map %w(-v --version) => :version
14
14
 
15
- desc "extract", "Shows candidate variables (i.e. occurences of ENV['X'])"
15
+ desc "extract", "Grep code to find ENV-variables"
16
+ long_desc <<-LONG
17
+ Greps source-files to find all ENV-variables your code is using.
18
+
19
+ This task helps you find variables to put in your Envfile.
20
+
21
+ By default the test/spec-folders are excluded. Use `--tests` to include them.
22
+ LONG
16
23
  option :globs, type: :array, default: ENVied::EnvVarExtractor.defaults[:globs], banner: "*.* lib/*"
24
+ option :tests, type: :boolean, default: false, desc: "include tests/specs"
17
25
  def extract
18
- var_occurences = ENVied::EnvVarExtractor.new(globs: options[:globs]).extract
26
+ globs = options[:globs]
27
+ globs << "{test,spec}/*" if options[:tests]
28
+ var_occurences = ENVied::EnvVarExtractor.new(globs: globs).extract
19
29
 
20
30
  puts "Found %d occurrences of %d variables:" % [var_occurences.values.flatten.size, var_occurences.size]
21
31
  var_occurences.sort.each do |var, occs|
@@ -29,13 +39,20 @@ class ENVied
29
39
 
30
40
  desc "init", "Generates a default Envfile in the current working directory"
31
41
  def init
32
- puts "Writing new Envfile to #{File.expand_path('Envfile')}"
42
+ puts "Writing Envfile to #{File.expand_path('Envfile')}"
33
43
  template("Envfile.tt")
44
+
45
+ puts <<-INIT
46
+ Add the following snippet (or similar) to your app's initialization:
47
+ ENVied.require(*ENV['ENVIED_GROUPS'] || [:default, ENV['RACK_ENV']])
48
+ INIT
34
49
  end
35
50
 
36
51
  desc "init:rails", "Generate all files needed for a Rails project"
37
52
  define_method "init:rails" do
38
- init
53
+ puts "Writing Envfile to #{File.expand_path('Envfile')}"
54
+ template("Envfile.tt")
55
+
39
56
  template("rails-initializer.tt", 'config/initializers/envied.rb')
40
57
  end
41
58
 
@@ -47,9 +64,12 @@ class ENVied
47
64
  Else the missing/invalid variables will be shown, and the process will exit with status 1.
48
65
  LONG
49
66
  option :groups, type: :array, default: %w(default), banner: 'default production'
67
+ option :quiet, type: :boolean, desc: 'Communicate success of the check only via the exit status.'
50
68
  def check
51
69
  ENVied.require(*options[:groups])
52
- puts "All variables for group(s) #{options[:groups]} are present and valid"
70
+ unless options[:quiet]
71
+ puts "All variables for group(s) #{options[:groups]} are present and valid"
72
+ end
53
73
  end
54
74
 
55
75
  desc "check:heroku", "Checks whether a Heroku config contains required variables"
@@ -61,12 +81,13 @@ class ENVied
61
81
 
62
82
  heroku config | bundle exec envied check:heroku
63
83
 
64
- It's more convenient to generate a shell script using the check:heroku:binstub-task.
84
+ Use the check:heroku:binstub-task to turn this into a bash-script.
65
85
 
66
86
  On success the process will exit with status 0.
67
87
  Else the missing/invalid variables will be shown, and the process will exit with status 1.
68
88
  LONG
69
89
  option :groups, type: :array, default: %w(default production), banner: 'default production'
90
+ option :quiet, type: :boolean, desc: 'Communicate success of the check only via the exit status.'
70
91
  define_method "check:heroku" do
71
92
  if STDIN.tty?
72
93
  error <<-ERR
@@ -81,7 +102,9 @@ ERR
81
102
  end]
82
103
  ENV.replace({}).update(heroku_env)
83
104
  ENVied.require(*options[:groups])
84
- puts "All variables for group(s) #{options[:groups]} are present and valid in your Heroku app"
105
+ unless options[:quiet]
106
+ puts "All variables for group(s) #{options[:groups]} are present and valid in your Heroku app"
107
+ end
85
108
  end
86
109
 
87
110
  desc "check:heroku:binstub", "Generates a shell script for the check:heroku-task"
@@ -91,14 +114,16 @@ ERR
91
114
  The same as the check:heroku-task, but all in one script (no need to pipe `heroku config` to it etc.).
92
115
 
93
116
  LONG
94
- option :dest, banner: "where to put the script", default: 'bin/heroku-env-check'
95
- option :app, banner: "name of Heroku app"
117
+ option :dest, banner: "where to put the script", desc: "Default: bin/<app>-env-check or bin/heroku-env-check"
118
+ option :app, banner: "name of Heroku app", desc: "uses ENV['HEROKU_APP'] as default if present", default: ENV['HEROKU_APP']
96
119
  option :groups, type: :array, default: %w(default production), banner: 'default production'
97
120
  define_method "check:heroku:binstub" do
98
121
  require 'fileutils'
99
122
  @app = options[:app]
100
- @dest = @app ? File.join(*%W(bin #{@app}-env-check)) : options[:dest]
123
+ @dest = options[:dest]
124
+ @dest ||= File.join(*%W(bin #{(@app || 'heroku')}-env-check))
101
125
  @groups = options[:groups]
126
+
102
127
  full_dest = File.expand_path(@dest)
103
128
  template("heroku-env-check.tt", full_dest)
104
129
  FileUtils.chmod 0755, full_dest
@@ -4,7 +4,7 @@ class ENVied
4
4
  @defaults ||= begin
5
5
  {
6
6
  extensions: %w(ru thor rake rb yml ruby yaml erb builder markerb haml),
7
- globs: %w(*.* Thorfile Rakefile {app,config,db,lib,script,test,spec}/*)
7
+ globs: %w(*.* Thorfile Rakefile {app,config,db,lib,script}/*)
8
8
  }
9
9
  end
10
10
  end
@@ -13,19 +13,6 @@ class ENVied
13
13
  self.class.defaults
14
14
  end
15
15
 
16
- def self.env_var_re
17
- @env_var_re ||= begin
18
- /^[^\#]* # not matching comments
19
- ENV
20
- (?: # non-capture...
21
- \[['"] | # either ENV['
22
- \.fetch\(['"] # or ENV.fetch('
23
- )
24
- ([a-zA-Z_]+) # capture variable name
25
- /x
26
- end
27
- end
28
-
29
16
  attr_reader :globs, :extensions
30
17
 
31
18
  def initialize(options = {})
@@ -37,6 +24,22 @@ class ENVied
37
24
  new(options.merge(globs: Array(globs))).extract
38
25
  end
39
26
 
27
+ # Greps all ENV-variables from non-comment-parts of line.
28
+ # Captures 'A' in lines like `ENV['A']`, but also `ENV.fetch('A')`.
29
+ #
30
+ # @param line [String] the line to grep
31
+ #
32
+ # @example
33
+ # extractor.new.capture_variables("config.force_ssl = ENV['FORCE_SSL']")
34
+ # # => ["FORCE_SSL"]
35
+ # extractor.new.capture_variables("# comment about ENV['FORCE_SSL']")
36
+ # # => []
37
+ #
38
+ # @return [Array<String>] the names o
39
+ def capture_variables(line)
40
+ noncomment, _ = line.split("#", 2)
41
+ noncomment.scan(/ENV(?:\[|\.fetch\()['"]([^'"]+)['"]/).flatten
42
+ end
40
43
 
41
44
  # Extract all keys recursively from files found via `globs`.
42
45
  # Any occurence of `ENV['A']` or `ENV.fetch('A')` in code (not in comments), will result
@@ -61,8 +64,8 @@ class ENVied
61
64
  results.merge!(extract("#{item}/*"))
62
65
  else
63
66
  next unless extensions.detect {|ext| File.extname(item)[ext] }
64
- File.readlines(item, :encoding=>"UTF-8").each_with_index do |line, ix|
65
- if variable = line[self.class.env_var_re, 1]
67
+ File.readlines(item).each_with_index do |line, ix|
68
+ capture_variables(line).each do |variable|
66
69
  results[variable] << { :path => item, :line => ix.succ }
67
70
  end
68
71
  end
@@ -1,8 +1,8 @@
1
1
  # A sample Envfile
2
2
  # enable_defaults! { ENV['RACK_ENV'] != 'production' }
3
3
 
4
- # variable :REDIS_URL, :String, default: 'redis://localhost:6379'
4
+ # variable :REDIS_URL, :string, default: 'redis://localhost:6379'
5
5
  #
6
6
  # group :production do
7
- # variable :MAIL_PASS_USERNAME, :String
7
+ # variable :SECRET_KEY_BASE
8
8
  # end
@@ -1,5 +1,6 @@
1
1
  ENVied.springify do
2
- ENVied.require(:default, ENV['RAILS_ENV'])
2
+ ENVied.require(*ENV['ENVIED_GROUPS'] || Rails.groups)
3
+
3
4
  # Put any initialization based on ENV-variables below, e.g.:
4
5
  # Rails.configuration.force_ssl = ENVied.FORCE_SSL
5
6
  end
@@ -1,3 +1,3 @@
1
1
  class ENVied
2
- VERSION = '0.7.2'
2
+ VERSION = '0.8.0'
3
3
  end
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+
3
+ describe ENVied::EnvVarExtractor do
4
+
5
+ describe "#capture_variables" do
6
+ def capture_variables(text)
7
+ described_class.new.capture_variables(text)
8
+ end
9
+
10
+ {
11
+ %{self.a = ENV['A']} => %w(A),
12
+ %{self.a = ENV["A"]} => %w(A),
13
+ %{self.a = ENV.fetch("A")]} => %w(A),
14
+ %{self.a = ENV.fetch("A")]} => %w(A),
15
+ %{# self.a = ENV["A"]} => %w(),
16
+ %{self.a = ENV["A"] && self.b = ENV["B"]} => %w(A B),
17
+ %{self.a = ENV["A3"]} => %w(A3)
18
+ }.each do |line, expected|
19
+ it "captures #{expected} from #{line.inspect}" do
20
+ expect(capture_variables(line)).to contain_exactly(*expected)
21
+ end
22
+ end
23
+ end
24
+ end
data/spec/envied_spec.rb CHANGED
@@ -198,6 +198,28 @@ describe ENVied do
198
198
  end
199
199
 
200
200
  describe "groups" do
201
+ describe 'requiring' do
202
+ it 'yields :default when nothing passed to require' do
203
+ envied_require
204
+ expect(ENVied.env.groups).to eq [:default]
205
+ end
206
+
207
+ it 'yields groupnames passed to it as string' do
208
+ envied_require('bar')
209
+ expect(ENVied.env.groups).to eq [:bar]
210
+ end
211
+
212
+ it 'yields groupnames passed to it as symbols' do
213
+ envied_require(:foo)
214
+ expect(ENVied.env.groups).to eq [:foo]
215
+ end
216
+
217
+ it 'yields the groups passed via a string with groupnames' do
218
+ envied_require('foo,bar')
219
+ expect(ENVied.env.groups).to eq [:foo, :bar]
220
+ end
221
+ end
222
+
201
223
  context 'a variable in a group' do
202
224
  before do
203
225
  configure do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: envied
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.2
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gert Goet
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-09-07 00:00:00.000000000 Z
11
+ date: 2014-10-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: coercible
@@ -126,6 +126,7 @@ files:
126
126
  - lib/envied/version.rb
127
127
  - spec/coercer_spec.rb
128
128
  - spec/configuration_spec.rb
129
+ - spec/env_var_extractor_spec.rb
129
130
  - spec/envied_spec.rb
130
131
  - spec/spec_helper.rb
131
132
  - spec/variable_spec.rb
@@ -149,13 +150,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
149
150
  version: '0'
150
151
  requirements: []
151
152
  rubyforge_project:
152
- rubygems_version: 2.4.0
153
+ rubygems_version: 2.4.2
153
154
  signing_key:
154
155
  specification_version: 4
155
156
  summary: Ensure presence and type of ENV-variables
156
157
  test_files:
157
158
  - spec/coercer_spec.rb
158
159
  - spec/configuration_spec.rb
160
+ - spec/env_var_extractor_spec.rb
159
161
  - spec/envied_spec.rb
160
162
  - spec/spec_helper.rb
161
163
  - spec/variable_spec.rb