Sha256: 0931971644332f149f7f8bc1b40455972b6fc6e12a11be5848ab268c5e6b394d

Contents?: true

Size: 1.7 KB

Versions: 1

Compression:

Stored size: 1.7 KB

Contents

require 'erb'

module Penchant
  class Gemfile
    attr_reader :path

    class << self
      def do_full_env_switch!(env, deployment = false)
        gemfile = Penchant::Gemfile.new
        gemfile.run_dot_penchant!(env, deployment)

        if !gemfile.has_gemfile_erb?
          return false
        end

        gemfile.switch_to!(env, deployment)
      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 deployment?
      File.readlines(gemfile_path).first['deployment mode'] != nil
    end

    def switch_to!(gemfile_env = nil, deployment = false)
      @env, @is_deployment = gemfile_env, deployment
      template = File.read(gemfile_erb_path)

      File.open(gemfile_path, 'wb') do |fh|
        fh.puts "# generated by penchant, environment: #{@env || "none"}#{@is_deployment ? " , deployment mode" : ""}"

        fh.print ERB.new(template).result(binding)
      end
    end

    def run_dot_penchant!(env, deployment)
      DotPenchant.run(env || environment, deployment) 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

    def no_deployment(&block)
      instance_eval(&block) if !@is_deployment
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
penchant-0.0.5 lib/penchant/gemfile.rb