Sha256: 395a6750f65fee62c781b973f24f714474307696498da9a2378988a392e44e61

Contents?: true

Size: 1.63 KB

Versions: 4

Compression:

Stored size: 1.63 KB

Contents

require "bundler"

module RailsConfig
  module Tasks
    class Heroku < Struct.new(:app)
      def invoke
        puts 'Setting vars...'
        heroku_command = "config:set #{vars}"
        heroku(heroku_command)
        puts 'Vars set:'
        puts heroku_command
      end

      def vars
        # Load only local options to Heroku
        RailsConfig.load_and_set_settings(
            Rails.root.join("config", "settings.local.yml").to_s,
            Rails.root.join("config", "settings", "#{environment}.local.yml").to_s,
            Rails.root.join("config", "environments", "#{environment}.local.yml").to_s
        )

        out = ''
        dotted_hash = to_dotted_hash Kernel.const_get(RailsConfig.const_name).to_hash, {}, RailsConfig.const_name
        dotted_hash.each {|key, value| out += " #{key}=#{value} "}
        out
      end

      def environment
        heroku("run 'echo $RAILS_ENV'").chomp[/(\w+)\z/]
      end

      def heroku(command)
        with_app = app ? " --app #{app}" : ""
        `heroku #{command}#{with_app}`
      end

      def `(command)
        Bundler.with_clean_env { super }
      end

      def to_dotted_hash(source, target = {}, namespace = nil)
        prefix = "#{namespace}." if namespace
        case source
          when Hash
            source.each do |key, value|
              to_dotted_hash(value, target, "#{prefix}#{key}")
            end
          when Array
            source.each_with_index do |value, index|
              to_dotted_hash(value, target, "#{prefix}#{index}")
            end
          else
            target[namespace] = source
        end
        target
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
drtom_rails_config-0.5.0.beta1 lib/rails_config/tasks.rb
rails_config-0.5.0.beta1 lib/rails_config/tasks.rb
rails_config-0.4.2 lib/rails_config/tasks.rb
rails_config-0.4.1 lib/tasks.rb