Class: Linecook::Commands::Env

Inherits:
Command
  • Object
show all
Defined in:
lib/linecook/commands/env.rb

Overview

:startdoc::desc prints a package env

Prints the env for the current project directory. Specifically the cookbook file is loaded and used to determine all resources that are current available. The full build env for a package can be viewed by specifying the package file as an option.

A specific env value can be printed by specifying the key path to it.

Constant Summary collapse

ORIGINAL_TO_YAML =

:stopdoc: Evaluate to replace the to_yaml function on Hash so that it will serialize keys in order. Evaluate the OFF code to turn this hack off (and thereby ease up on the code pollution)

Modified from: snippets.dzone.com/posts/show/5811 Original func: /usr/lib/ruby/1.8/yaml/rubytypes.rb

'linecook_original_to_yaml'
SORTED_HASH_ON_LINE =
__LINE__ + 1
SORTED_HASH_ON =
%{
class Hash
  unless instance_methods.include?('#{ORIGINAL_TO_YAML}')
    alias #{ORIGINAL_TO_YAML} to_yaml
    undef_method :to_yaml
    def to_yaml( opts = {} )
      YAML::quick_emit( object_id, opts ) do |out|
        out.map( taguri, to_yaml_style ) do |map|
          keys.sort_by do |k|
            k.to_s
          end.each do |k|
            map.add( k, fetch(k) )
          end
        end
      end
    end
  end
end}
SORTED_HASH_OFF_LINE =
__LINE__ + 1
SORTED_HASH_OFF =
%{
class Hash
  if instance_methods.include?('#{ORIGINAL_TO_YAML}')
    undef_method :to_yaml
    alias to_yaml #{ORIGINAL_TO_YAML}
    undef_method :#{ORIGINAL_TO_YAML}
  end
end}

Constants inherited from Command

Command::REGISTRY

Instance Attribute Summary

Attributes inherited from Command

#quiet

Instance Method Summary collapse

Methods inherited from Command

#call, inherited, #initialize, #log, registry, #sh, #sh!

Constructor Details

This class inherits a constructor from Linecook::Commands::Command

Instance Method Details

#process(*keys) ⇒ Object



82
83
84
85
86
# File 'lib/linecook/commands/env.rb', line 82

def process(*keys)
  package  = Linecook::Package.init(package_file, project_dir)
  env = select(package.env, *keys)
  serialize(env, $stdout)
end

#select(current, *keys) ⇒ Object

:startdoc:



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/linecook/commands/env.rb', line 59

def select(current, *keys)
  keys.each do |key|
    unless current.kind_of?(Hash)
      return nil
    end
    
    current = current[key]
  end
  
  current
end

#serialize(env, target = "") ⇒ Object

Serializes the env to the target as YAML. Ensures hashes are serialized with their keys sorted by their to_s value.



73
74
75
76
77
78
79
80
# File 'lib/linecook/commands/env.rb', line 73

def serialize(env, target="")
  begin
    eval SORTED_HASH_ON, TOPLEVEL_BINDING, __FILE__, SORTED_HASH_ON_LINE
    YAML.dump(env, target)
  ensure
    eval SORTED_HASH_OFF, TOPLEVEL_BINDING, __FILE__, SORTED_HASH_OFF_LINE
  end
end