├── spec ├── spec_helper.rb ├── fixtures │ └── plain.env └── dotenv_spec.rb ├── Gemfile ├── .gitignore ├── lib ├── dotenv │ ├── tasks.rb │ ├── railtie.rb │ └── environment.rb └── dotenv.rb ├── Rakefile ├── dotenv.gemspec ├── LICENSE └── README.md /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | require 'dotenv' -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | gemspec 3 | -------------------------------------------------------------------------------- /spec/fixtures/plain.env: -------------------------------------------------------------------------------- 1 | OPTION_A=1 2 | OPTION_B=2 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.gem 2 | *.rbc 3 | .bundle 4 | .config 5 | .yardoc 6 | Gemfile.lock 7 | -------------------------------------------------------------------------------- /lib/dotenv/tasks.rb: -------------------------------------------------------------------------------- 1 | desc 'Load environment settings from .env' 2 | task :dotenv do 3 | require 'dotenv' 4 | Dotenv.load 5 | end 6 | 7 | task :environment => :dotenv -------------------------------------------------------------------------------- /lib/dotenv.rb: -------------------------------------------------------------------------------- 1 | require 'dotenv/environment' 2 | 3 | module Dotenv 4 | def self.load(filename = '.env') 5 | Dotenv::Environment.new(filename).apply 6 | end 7 | end 8 | 9 | require 'dotenv/railtie' if defined?(Rails) 10 | -------------------------------------------------------------------------------- /lib/dotenv/railtie.rb: -------------------------------------------------------------------------------- 1 | module Dotenv 2 | class Railtie < Rails::Railtie 3 | rake_tasks do 4 | load "dotenv/tasks.rb" 5 | end 6 | 7 | initializer 'dotenv', :group => :all do 8 | Dotenv.load 9 | end 10 | end 11 | end -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env rake 2 | require "bundler/gem_tasks" 3 | require 'rspec/core/rake_task' 4 | 5 | desc "Run all specs" 6 | RSpec::Core::RakeTask.new(:spec) do |t| 7 | t.rspec_opts = %w[--color] 8 | t.verbose = false 9 | end 10 | 11 | 12 | task :default => :spec -------------------------------------------------------------------------------- /lib/dotenv/environment.rb: -------------------------------------------------------------------------------- 1 | module Dotenv 2 | class Environment < Hash 3 | def initialize(filename) 4 | @filename = filename 5 | load 6 | end 7 | 8 | def load 9 | read.each do |line| 10 | self[$1] = $2 if line =~ /\A([\w_]+)=(.*)\z/ 11 | end 12 | end 13 | 14 | def read 15 | File.read(@filename).split("\n") 16 | end 17 | 18 | def apply 19 | each { |k,v| ENV[k] = v } 20 | end 21 | end 22 | end 23 | -------------------------------------------------------------------------------- /dotenv.gemspec: -------------------------------------------------------------------------------- 1 | # -*- encoding: utf-8 -*- 2 | 3 | Gem::Specification.new do |gem| 4 | gem.authors = ["Brandon Keepers"] 5 | gem.email = ["brandon@opensoul.org"] 6 | gem.description = %q{Loads environment variables from `.env`.} 7 | gem.summary = %q{Loads environment variables from `.env`.} 8 | gem.homepage = "https://github.com/bkeepers/dotenv" 9 | 10 | gem.files = `git ls-files`.split($\) 11 | gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) } 12 | gem.test_files = gem.files.grep(%r{^(test|spec|features)/}) 13 | gem.name = "dotenv" 14 | gem.require_paths = ["lib"] 15 | gem.version = '0.1.0' 16 | 17 | gem.add_development_dependency 'rake' 18 | gem.add_development_dependency 'rspec' 19 | end 20 | -------------------------------------------------------------------------------- /spec/dotenv_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | describe Dotenv::Environment do 4 | let(:env_path) { fixture_path('plain.env') } 5 | let(:dotenv) { Dotenv::Environment.new(env_path) } 6 | 7 | before do 8 | @env_keys = ENV.keys 9 | end 10 | 11 | after do 12 | ENV.delete_if { |k,v| !@env_keys.include?(k) } 13 | end 14 | 15 | describe 'initialize' do 16 | it 'reads environment config' do 17 | expect(dotenv['OPTION_A']).to eq('1') 18 | expect(dotenv['OPTION_B']).to eq('2') 19 | end 20 | end 21 | 22 | describe 'apply' do 23 | it 'sets variables in ENV' do 24 | dotenv.apply 25 | expect(ENV['OPTION_A']).to eq('1') 26 | end 27 | end 28 | 29 | def fixture_path(name) 30 | File.join(File.expand_path('../fixtures', __FILE__), name) 31 | end 32 | end -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2012 Brandon Keepers 2 | 3 | MIT License 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 20 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 22 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # dotenv 2 | 3 | Loads environment variables from `.env` into `ENV`, automagically. 4 | 5 | Read more about the [motivation for dotenv at opensoul.org](http://opensoul.org/blog/archives/2012/07/24/dotenv/). 6 | 7 | ## Installation 8 | 9 | ### Rails 10 | 11 | Add this line to your application's Gemfile: 12 | 13 | gem 'dotenv', :groups => [:development, :test] 14 | 15 | And then execute: 16 | 17 | $ bundle 18 | 19 | ### Sinatra or Plain ol' Ruby 20 | 21 | Install the gem: 22 | 23 | $ gem install dotenv 24 | 25 | As early as possible in your application bootstrap process, load `.env`: 26 | 27 | Dotenv.load 28 | 29 | To ensure `.env` is loaded in rake, load the tasks: 30 | 31 | require 'dotenv/tasks' 32 | 33 | task :mytask => :dotenv do 34 | # things that require .env 35 | end 36 | 37 | ## Usage 38 | 39 | Add your application configuration to `.env`. 40 | 41 | S3_BUCKET=dotenv 42 | SECRET_KEY=sssshhh! 43 | 44 | Whenever your application loads, these variables will be available in `ENV`: 45 | 46 | config.fog_directory = ENV['S3_BUCKET'] 47 | 48 | ## Contributing 49 | 50 | 1. Fork it 51 | 2. Create your feature branch (`git checkout -b my-new-feature`) 52 | 3. Commit your changes (`git commit -am 'Added some feature'`) 53 | 4. Push to the branch (`git push origin my-new-feature`) 54 | 5. Create new Pull Request 55 | --------------------------------------------------------------------------------