Sha256: 4531cef817e77eef9bc95a75cb842c542d6ab58599b36ff7f184576860025403

Contents?: true

Size: 1.26 KB

Versions: 6

Compression:

Stored size: 1.26 KB

Contents

require "yaml"
require "doit"

What = Object.new
class << What
  attr_reader :matrix
  attr_reader :where
  attr_reader :env

  def init(config)
    @matrix = nil
    @yml = (config && YAML.load(config)) || {}

    @where = @yml.delete("where")
    @env = @yml.delete("env")
    @env ||= [""]
    @env = [@env].flatten.compact

    remote = Doit.options[:remote]
    @where = remote if remote && remote != "---"
    @where ||= Dir.pwd # default is current directory
    @where = [@where].flatten.compact

    build_matrix
    @matrix ||= []
    @matrix = [@matrix] unless @matrix.first.is_a?(Array)
    @matrix.map! { |m| m.flatten.inject({}) { |hsh, h| hsh.merge(h) } }
    info
  end

  def to_env(hsh)
    arr = hsh.collect { |key, value| "#{key.to_s.upcase}=#{value}" }
    arr.join " "
  end

  def info
    return unless Doit.options[:verbose]

    My.verbose "where", @where
    My.verbose "matrix", @matrix
    My.verbose "env", @env
  end

  private

  def build_matrix
    return if @yml.empty?

    key, value = @yml.first
    @yml.delete(key)
    add_to_matrix(key, value)
    build_matrix
  end

  def add_to_matrix(key, val)
    arr = val.is_a?(Array) ?
      val.collect { |v| [{key => v}] } :
      [{key => val}]
    @matrix = @matrix ? @matrix.product(arr) : arr
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
doit-1.0.9 lib/what.rb
doit-1.0.8 lib/what.rb
doit-1.0.7 lib/what.rb
doit-1.0.5 lib/what.rb
doit-1.0.4 lib/what.rb
doit-1.0.3 lib/what.rb