Sha256: 57b55d7098b90b32d361cc4258379ea116001beda117943d47413c3170471ae5

Contents?: true

Size: 1.22 KB

Versions: 2

Compression:

Stored size: 1.22 KB

Contents

require 'erb'

module Penchant
  class Gemfile
    attr_reader :path

    class << self
      def do_full_env_switch!(env)
        gemfile = Penchant::Gemfile.new
        if !gemfile.has_gemfile_erb?
          puts "Not using Gemfile.erb, exiting."
          return false
        end

        gemfile.switch_to!(env)
        system %{bundle}
      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 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

    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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
penchant-0.0.2 lib/penchant/gemfile.rb
penchant-0.0.1 lib/penchant/gemfile.rb