Permalink
Browse files

Load ENV from .env

  • Loading branch information...
0 parents commit c3568a06b341f1182bd4e8b0d6e58a594cac7966 @bkeepers bkeepers committed Jul 24, 2012
Showing with 188 additions and 0 deletions.
  1. +6 −0 .gitignore
  2. +2 −0 Gemfile
  3. +22 −0 LICENSE
  4. +54 −0 README.md
  5. +2 −0 Rakefile
  6. +18 −0 dotenv.gemspec
  7. +9 −0 lib/dotenv.rb
  8. +22 −0 lib/dotenv/environment.rb
  9. +11 −0 lib/dotenv/railtie.rb
  10. +7 −0 lib/dotenv/tasks.rb
  11. +32 −0 spec/dotenv_spec.rb
  12. +2 −0 spec/fixtures/plain.env
  13. +1 −0 spec/spec_helper.rb
6 .gitignore
@@ -0,0 +1,6 @@
+*.gem
+*.rbc
+.bundle
+.config
+.yardoc
+Gemfile.lock
2 Gemfile
@@ -0,0 +1,2 @@
+source 'https://rubygems.org'
+gemspec
22 LICENSE
@@ -0,0 +1,22 @@
+Copyright (c) 2012 Brandon Keepers
+
+MIT License
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
54 README.md
@@ -0,0 +1,54 @@
+# dotenv
+
+Loads environment settings for your application from `.env`.
+
+The emerging practice of storing application configuration in environment variables is a great idea, but it's not always practical to set all of those environment variables in your development or continuous integration environments. [Foreman](https://github.com/ddollar/foreman) provides this handy feature of loading settings from `.env`, which works great for anything that you want to put in your `Procfile`. But it makes things difficult when you want to run a console or rake task. `dotenv` solves that problem.
+
+## Installation
+
+### Rails
+
+Add this line to your application's Gemfile:
+
+ gem 'dotenv', :groups => [:development, :test]
+
+And then execute:
+
+ $ bundle
+
+### Sinatra or Plain ol' Ruby
+
+Install the gem:
+
+ $ gem install dotenv
+
+As early as possible in your application bootstrap process, load `.env`:
+
+ Dotenv.load
+
+To ensure `.env` is loaded in rake, load the tasks:
+
+ require 'dotenv/tasks'
+
+ task :mytask => :dotenv do
+ # things that require .env
+ end
+
+## Usage
+
+Add your application configuration to `.env`.
+
+ S3_BUCKET=dotenv
+ SECRET_KEY=sssshhh!
+
+Whenever your application loads, these variables will be available in `ENV`:
+
+ config.fog_directory = ENV['S3_BUCKET']
+
+## Contributing
+
+1. Fork it
+2. Create your feature branch (`git checkout -b my-new-feature`)
+3. Commit your changes (`git commit -am 'Added some feature'`)
+4. Push to the branch (`git push origin my-new-feature`)
+5. Create new Pull Request
2 Rakefile
@@ -0,0 +1,2 @@
+#!/usr/bin/env rake
+require "bundler/gem_tasks"
18 dotenv.gemspec
@@ -0,0 +1,18 @@
+# -*- encoding: utf-8 -*-
+
+Gem::Specification.new do |gem|
+ gem.authors = ["Brandon Keepers"]
+ gem.email = ["brandon@opensoul.org"]
+ gem.description = %q{Loads environment variables from `.env`.}
+ gem.summary = %q{Loads environment variables from `.env`.}
+ gem.homepage = "https://github.com/bkeepers/dotenv"
+
+ gem.files = `git ls-files`.split($\)
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
+ gem.name = "dotenv"
+ gem.require_paths = ["lib"]
+ gem.version = '0.0.1'
+
+ gem.add_development_dependency 'rspec'
+end
9 lib/dotenv.rb
@@ -0,0 +1,9 @@
+require 'dotenv/environment'
+
+module Dotenv
+ def self.load(filename = '.env')
+ Dotenv::Environment.new(filename).apply
+ end
+end
+
+require 'dotenv/railtie' if defined?(Rails)
22 lib/dotenv/environment.rb
@@ -0,0 +1,22 @@
+module Dotenv
+ class Environment < Hash
+ def initialize(filename)
+ @filename = filename
+ load
+ end
+
+ def load
+ read.each do |line|
+ self[$1] = $2 if line =~ /\A([\w_]+)=(.*)\z/
+ end
+ end
+
+ def read
+ File.read(@filename).split("\n")
+ end
+
+ def apply
+ each { |k,v| ENV[k] = v }
+ end
+ end
+end
11 lib/dotenv/railtie.rb
@@ -0,0 +1,11 @@
+module Dotenv
+ class Railtie < Rails::Railtie
+ rake_tasks do
+ load "dotenv/tasks.rb"
+ end
+
+ initializer 'dotenv', :group => :all do
+ Dotenv.load
+ end
+ end
+end
7 lib/dotenv/tasks.rb
@@ -0,0 +1,7 @@
+desc 'Load environment settings from .env'
+task :dotenv do
+ require 'dotenv'
+ Dotenv.load
+end
+
+task :environment => :dotenv
32 spec/dotenv_spec.rb
@@ -0,0 +1,32 @@
+require 'spec_helper'
+
+describe Dotenv::Environment do
+ let(:env_path) { fixture_path('plain.env') }
+ let(:dotenv) { Dotenv::Environment.new(env_path) }
+
+ before do
+ @env_keys = ENV.keys
+ end
+
+ after do
+ ENV.delete_if { |k,v| !@env_keys.include?(k) }
+ end
+
+ describe 'initialize' do
+ it 'reads environment config' do
+ expect(dotenv['OPTION_A']).to eq('1')
+ expect(dotenv['OPTION_B']).to eq('2')
+ end
+ end
+
+ describe 'apply' do
+ it 'sets variables in ENV' do
+ dotenv.apply
+ expect(ENV['OPTION_A']).to eq('1')
+ end
+ end
+
+ def fixture_path(name)
+ File.join(File.expand_path('../fixtures', __FILE__), name)
+ end
+end
2 spec/fixtures/plain.env
@@ -0,0 +1,2 @@
+OPTION_A=1
+OPTION_B=2
1 spec/spec_helper.rb
@@ -0,0 +1 @@
+require 'dotenv'

0 comments on commit c3568a0

Please sign in to comment.