From 79cf541ebefc82b47efe1b0839d0a04c6e440c46 Mon Sep 17 00:00:00 2001 From: Chris Continanza Date: Sat, 3 Dec 2011 15:43:07 -0800 Subject: [PATCH 1/5] refactor engine to expose env methods --- lib/foreman/engine.rb | 52 +++++++++++++++++++++++++++---------------- 1 file changed, 33 insertions(+), 19 deletions(-) diff --git a/lib/foreman/engine.rb b/lib/foreman/engine.rb index c74c9cb..b217644 100644 --- a/lib/foreman/engine.rb +++ b/lib/foreman/engine.rb @@ -12,7 +12,6 @@ class Foreman::Engine attr_reader :procfile attr_reader :directory - attr_reader :environment attr_reader :options extend Term::ANSIColor @@ -26,6 +25,11 @@ class Foreman::Engine @environment = read_environment_files(options[:env]) end + def self.load_env(env_file) + @environment = read_environment_files(env_file) + load_env! + end + def start proctitle "ruby: foreman master" termtitle "#{File.basename(@directory)} - foreman (#{processes.size} processes)" @@ -73,7 +77,7 @@ private ###################################################################### end def fork_individual(process, num, port) - @environment.each { |k,v| ENV[k] = v } + load_env! ENV["PORT"] = port.to_s ENV["PS"] = "#{process.name}.#{num}" @@ -175,27 +179,37 @@ private ###################################################################### @current_color >= COLORS.length ? "" : COLORS[@current_color] end - def read_environment_files(filenames) - environment = {} + module Env + attr_reader :environment - (filenames || "").split(",").map(&:strip).each do |filename| - error "No such file: #{filename}" unless File.exists?(filename) - environment.merge!(read_environment(filename)) - end + def read_environment_files(filenames) + environment = {} - environment.merge!(read_environment(".env")) unless filenames - environment - end - - def read_environment(filename) - return {} unless File.exists?(filename) - - File.read(filename).split("\n").inject({}) do |hash, line| - if line =~ /\A([A-Za-z_0-9]+)=(.*)\z/ - hash[$1] = $2 + (filenames || "").split(",").map(&:strip).each do |filename| + error "No such file: #{filename}" unless File.exists?(filename) + environment.merge!(read_environment(filename)) end - hash + + environment.merge!(read_environment(".env")) unless filenames + environment + end + + def read_environment(filename) + return {} unless File.exists?(filename) + + File.read(filename).split("\n").inject({}) do |hash, line| + if line =~ /\A([A-Za-z_0-9]+)=(.*)\z/ + hash[$1] = $2 + end + hash + end + end + + def load_env! + @environment.each { |k,v| ENV[k] = v } end end + include Env + extend Env end From e33921f083bdb2837477d6d424363b3c58d59416 Mon Sep 17 00:00:00 2001 From: Chris Continanza Date: Sat, 3 Dec 2011 15:43:51 -0800 Subject: [PATCH 2/5] load contents from env file --- lib/foreman.rb | 5 +++++ spec/foreman_spec.rb | 14 ++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/lib/foreman.rb b/lib/foreman.rb index a7cfaa9..f9c522a 100644 --- a/lib/foreman.rb +++ b/lib/foreman.rb @@ -4,5 +4,10 @@ module Foreman class AppDoesNotExist < Exception; end + # load contents of env_file into ENV + def self.load!(env_file) + require 'foreman/engine' + Foreman::Engine.load_env(env_file) + end end diff --git a/spec/foreman_spec.rb b/spec/foreman_spec.rb index b0a0286..f2c14cf 100644 --- a/spec/foreman_spec.rb +++ b/spec/foreman_spec.rb @@ -8,4 +8,18 @@ describe Foreman do it { should be_a String } end + describe "::load!(env_file)" do + before do + File.open("/tmp/env1", "w") { |f| f.puts("FOO=bar") } + end + + after do + ENV['FOO'] = nil + end + + it "should load env_file into ENV" do + Foreman.load!("/tmp/env1") + ENV['FOO'].should == 'bar' + end + end end From 44a5dff724bb26fc7e92b0a485bc1d2257c7f76d Mon Sep 17 00:00:00 2001 From: Chris Continanza Date: Mon, 5 Dec 2011 11:37:51 -0800 Subject: [PATCH 3/5] use ./.env as default --- lib/foreman.rb | 2 +- spec/foreman_spec.rb | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/foreman.rb b/lib/foreman.rb index f9c522a..a7d841e 100644 --- a/lib/foreman.rb +++ b/lib/foreman.rb @@ -5,7 +5,7 @@ module Foreman class AppDoesNotExist < Exception; end # load contents of env_file into ENV - def self.load!(env_file) + def self.load!(env_file = './.env') require 'foreman/engine' Foreman::Engine.load_env(env_file) end diff --git a/spec/foreman_spec.rb b/spec/foreman_spec.rb index f2c14cf..9a66d12 100644 --- a/spec/foreman_spec.rb +++ b/spec/foreman_spec.rb @@ -10,16 +10,24 @@ describe Foreman do describe "::load!(env_file)" do before do - File.open("/tmp/env1", "w") { |f| f.puts("FOO=bar") } + FakeFS.activate! end after do + FakeFS.deactivate! ENV['FOO'] = nil end it "should load env_file into ENV" do + File.open("/tmp/env1", "w") { |f| f.puts("FOO=bar") } Foreman.load!("/tmp/env1") ENV['FOO'].should == 'bar' end + + it "should assume env_file in ./.env" do + File.open("./.env", "w") { |f| f.puts("FOO=bar") } + Foreman.load! + ENV['FOO'].should == 'bar' + end end end From 58e4cdafd7bea9739962fa2e0891d9b971d96e00 Mon Sep 17 00:00:00 2001 From: Chris Continanza Date: Mon, 5 Dec 2011 12:27:28 -0800 Subject: [PATCH 4/5] rename load! to load_env! --- lib/foreman.rb | 2 +- spec/foreman_spec.rb | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/foreman.rb b/lib/foreman.rb index a7d841e..77ef69c 100644 --- a/lib/foreman.rb +++ b/lib/foreman.rb @@ -5,7 +5,7 @@ module Foreman class AppDoesNotExist < Exception; end # load contents of env_file into ENV - def self.load!(env_file = './.env') + def self.load_env!(env_file = './.env') require 'foreman/engine' Foreman::Engine.load_env(env_file) end diff --git a/spec/foreman_spec.rb b/spec/foreman_spec.rb index 9a66d12..ef1bbb3 100644 --- a/spec/foreman_spec.rb +++ b/spec/foreman_spec.rb @@ -8,7 +8,7 @@ describe Foreman do it { should be_a String } end - describe "::load!(env_file)" do + describe "::load_env!(env_file)" do before do FakeFS.activate! end @@ -20,13 +20,13 @@ describe Foreman do it "should load env_file into ENV" do File.open("/tmp/env1", "w") { |f| f.puts("FOO=bar") } - Foreman.load!("/tmp/env1") + Foreman.load_env!("/tmp/env1") ENV['FOO'].should == 'bar' end it "should assume env_file in ./.env" do File.open("./.env", "w") { |f| f.puts("FOO=bar") } - Foreman.load! + Foreman.load_env! ENV['FOO'].should == 'bar' end end From 2e8030dbd4fc539b77bd6b854ec40b17b73817b4 Mon Sep 17 00:00:00 2001 From: Chris Continanza Date: Mon, 5 Dec 2011 12:36:23 -0800 Subject: [PATCH 5/5] refactor load_env to apply_environment --- lib/foreman.rb | 2 +- lib/foreman/engine.rb | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/foreman.rb b/lib/foreman.rb index 77ef69c..422bdd4 100644 --- a/lib/foreman.rb +++ b/lib/foreman.rb @@ -7,7 +7,7 @@ module Foreman # load contents of env_file into ENV def self.load_env!(env_file = './.env') require 'foreman/engine' - Foreman::Engine.load_env(env_file) + Foreman::Engine.load_env!(env_file) end end diff --git a/lib/foreman/engine.rb b/lib/foreman/engine.rb index b217644..b4140d5 100644 --- a/lib/foreman/engine.rb +++ b/lib/foreman/engine.rb @@ -25,9 +25,9 @@ class Foreman::Engine @environment = read_environment_files(options[:env]) end - def self.load_env(env_file) + def self.load_env!(env_file) @environment = read_environment_files(env_file) - load_env! + apply_environment! end def start @@ -77,7 +77,7 @@ private ###################################################################### end def fork_individual(process, num, port) - load_env! + apply_environment! ENV["PORT"] = port.to_s ENV["PS"] = "#{process.name}.#{num}" @@ -205,7 +205,7 @@ private ###################################################################### end end - def load_env! + def apply_environment! @environment.each { |k,v| ENV[k] = v } end end