Skip to content

Commit

Permalink
support .env
Browse files Browse the repository at this point in the history
  • Loading branch information
masarakki committed Sep 22, 2014
1 parent e1131aa commit 0270548
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 9 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
# Capistrano::Env
Capistrano with Env via file

## Notice!

`ruby_formatter` will deprecated at `0.2.x` and it will be removed at next version (`>= 0.3`).
Now `dotenv_formatter` is avaiable, it create `.env` file instead of `capenv.rb` file,
and use `dotenv` or `dotenv-rails` gem to read `.env` file.

## Installation

Add this line to your application's Gemfile:
Expand Down Expand Up @@ -32,6 +38,7 @@ capenv.use do |env|
end
env.add 'UNICORN_PROCESSES'
env.add 'HOGE', 'hage'
env.formatter = :dotenv #=> default is :ruby, but it is deprecated now.
end
```

Expand All @@ -41,8 +48,8 @@ end
bundle exec cap production deploy
```

- automaticaly create #{current_path}/capenv.rb
- automaticaly load #{current_path}/capenv.rb in rails boot
- automaticaly create #{current_path}/.env
- automaticaly load #{current_path}/.env if you use dotenv-rails
- you should load manualy in other framework
- you can use ENV['ENV_NAME'] in application

Expand Down
2 changes: 1 addition & 1 deletion capistrano-env.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Gem::Specification.new do |spec|
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ['lib']

spec.post_install_message = 'formatter :ruby is deprecated! use :dotenv instead of :ruby.'
spec.add_dependency 'rails', '>= 3.0', '< 5.0'

spec.add_development_dependency 'bundler', '~> 1.3'
Expand Down
5 changes: 5 additions & 0 deletions lib/capistrano/env.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
module Capistrano
module Env
module Formatter
class Base
def self.filename
"capenv.#{extname}"
end
end
end
end
end
Expand Down
15 changes: 12 additions & 3 deletions lib/capistrano/env/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,20 @@ class Config
attr_accessor :formatter

def initialize
@formatter = :ruby
@values = {}
@keys = []
end

def formatter
self.formatter = :ruby unless @formatter
@formatter
end

def formatter=(value)
warn 'formatter :ruby is deprecated! use :dotenv with dotenv(-rails).gem' if value == :ruby
@formatter = value
end

def add(name_or_regexp, val = nil, &block)
if val && name_or_regexp.is_a?(String)
@values[name_or_regexp] = val
Expand All @@ -19,7 +28,7 @@ def add(name_or_regexp, val = nil, &block)

def formatter_class
@formatter_class ||= begin
require "capistrano/env/formatter/#{@formatter}_formatter"
require "capistrano/env/formatter/#{formatter}_formatter"
Capistrano::Env::Formatter.const_get "#{formatter.capitalize}Formatter"
end
end
Expand All @@ -35,7 +44,7 @@ def envs
end

def capenv_file
"capenv.#{formatter_class.file_ext}"
formatter_class.filename
end

def capenv_content
Expand Down
15 changes: 15 additions & 0 deletions lib/capistrano/env/formatter/dotenv_formatter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Capistrano
module Env
module Formatter
class DotenvFormatter
def self.filename
'.env'
end

def self.format(envs)
envs.map { |k, v| "#{k}=\"#{v}\"\n" }.join
end
end
end
end
end
4 changes: 2 additions & 2 deletions lib/capistrano/env/formatter/ruby_formatter.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module Capistrano
module Env
module Formatter
class RubyFormatter
def self.file_ext
class RubyFormatter < Base
def self.extname
'rb'
end

Expand Down
2 changes: 1 addition & 1 deletion lib/capistrano/env/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Capistrano
module Env
VERSION = '0.1.2'
VERSION = '0.2.0'
end
end
12 changes: 12 additions & 0 deletions spec/capistrano/env/formatter/dotenv_formatter_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require 'spec_helper'
require 'capistrano/env/formatter/dotenv_formatter'

describe Capistrano::Env::Formatter::DotenvFormatter do
let(:envs) { { 'HELLO' => 'WORLD', 'HOGE' => '1,2,3' } }
expect_string = <<EOF
HELLO="WORLD"
HOGE="1,2,3"
EOF
it { expect(described_class.format(envs)).to eq expect_string }
it { expect(described_class.filename).to eq '.env' }
end
1 change: 1 addition & 0 deletions spec/capistrano/env/formatter/ruby_formatter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
ENV["HOGE"] = "1,2,3"
EOF
it { expect(described_class.format(envs)).to eq expect_string }
it { expect(described_class.filename).to eq 'capenv.rb' }
end

0 comments on commit 0270548

Please sign in to comment.