74 lines
1.3 KiB
Ruby
74 lines
1.3 KiB
Ruby
require 'erb'
|
|
|
|
module Penchant
|
|
class Gemfile
|
|
attr_reader :path
|
|
|
|
class << self
|
|
def do_full_env_switch!(env)
|
|
gemfile = Penchant::Gemfile.new
|
|
gemfile.run_dot_penchant!(env)
|
|
|
|
if !gemfile.has_gemfile_erb?
|
|
return false
|
|
end
|
|
|
|
gemfile.switch_to!(env)
|
|
end
|
|
end
|
|
|
|
def initialize(path = Dir.pwd)
|
|
@path = path
|
|
end
|
|
|
|
def gemfile_path
|
|
file_in_path('Gemfile')
|
|
end
|
|
|
|
def has_gemfile?
|
|
File.file?('Gemfile')
|
|
end
|
|
|
|
def has_dot_penchant?
|
|
File.file?('.penchant')
|
|
end
|
|
|
|
def gemfile_erb_path
|
|
file_in_path('Gemfile.erb')
|
|
end
|
|
|
|
def has_gemfile_erb?
|
|
File.file?(gemfile_erb_path)
|
|
end
|
|
|
|
def environment
|
|
File.readlines(gemfile_path).first.strip[%r{environment: (.*)}, 1]
|
|
end
|
|
|
|
def switch_to!(gemfile_env = nil)
|
|
@env = gemfile_env
|
|
template = File.read(gemfile_erb_path)
|
|
|
|
File.open(gemfile_path, 'wb') do |fh|
|
|
fh.puts "# generated by penchant, environment: #{@env}"
|
|
|
|
fh.print ERB.new(template).result(binding)
|
|
end
|
|
end
|
|
|
|
def run_dot_penchant!(env)
|
|
DotPenchant.run(env || environment) if has_dot_penchant?
|
|
end
|
|
|
|
private
|
|
def file_in_path(file)
|
|
File.join(@path, file)
|
|
end
|
|
|
|
def env(check, &block)
|
|
instance_eval(&block) if check.to_s == @env.to_s
|
|
end
|
|
end
|
|
end
|
|
|