class Nucleon::Command::Bash

Public Instance Methods

build(components = {}, overrides = nil, override_key = false) click to toggle source
    # File lib/nucleon/command/bash.rb
 18 def build(components = {}, overrides = nil, override_key = false)
 19   command            = string(components[:command])
 20   flags              = array( components.has_key?(:flags) ? components[:flags] : [] )
 21   data               = string_map(hash( components.has_key?(:data) ? components[:data] : {} ))
 22   args               = array( components.has_key?(:args) ? components[:args] : [] )
 23   subcommand         = hash( components.has_key?(:subcommand) ? components[:subcommand] : {} )
 24   
 25   override_key       = command unless override_key
 26   override_key       = override_key.to_sym
 27   
 28   command_string     = command.dup
 29   subcommand_string  = ''
 30   
 31   escape_characters  = /[\'\"]+/
 32   escape_replacement = '\"'
 33   
 34   dash_pattern       = /^([\-]+)/
 35   assignment_pattern = /\=$/
 36   
 37   logger.info("Building command #{command}")
 38   logger.debug("Command flags: #{flags.inspect}")
 39   logger.debug("Command options: #{data.inspect}")
 40   logger.debug("Command arguments: #{args.inspect}")
 41   logger.debug("Command has sub command") unless subcommand.empty?
 42   
 43   logger.debug("Overrides: #{overrides.inspect}")
 44   logger.debug("Override key: #{override_key}")
 45   
 46   # Flags
 47   if overrides && overrides.has_key?(:flags)
 48     if overrides[:flags].is_a?(Hash)
 49       if overrides[:flags].has_key?(override_key)
 50         flags = array(overrides[:flags][override_key])
 51       end
 52     else
 53       flags = array(overrides[:flags])
 54     end
 55   end
 56   flags.each do |flag|
 57     flag = string(flag)
 58     if ! flag.empty?        
 59       if flag.match(dash_pattern)
 60         dashes = $1
 61       else
 62         dashes = ( flag.size == 1 ? '-' : '--' )  
 63       end
 64       command_string << " #{dashes}#{flag}"
 65     end
 66   end
 67   
 68   # Data
 69   if overrides && overrides.has_key?(:data)
 70     if overrides[:data].has_key?(override_key)
 71       data = hash(overrides[:data][override_key])
 72     else
 73       override = true
 74       overrides[:data].each do |key, value|
 75         if ! value.is_a?(String)
 76           override = false
 77         end
 78       end
 79       data = hash(overrides[:data]) if override
 80     end
 81   end
 82   data.each do |key, value|
 83     key   = string(key)
 84     value = string(value).strip.sub(escape_characters, escape_replacement)
 85     
 86     if key.match(dash_pattern)
 87       dashes = $1
 88     else
 89       dashes = ( key.size == 1 ? '-' : '--' )  
 90     end      
 91     space = ( key.match(assignment_pattern) ? '' : ' ' )  
 92     
 93     command_string << " #{dashes}#{key}#{space}\"#{value}\""
 94   end
 95   
 96   # Arguments
 97   if overrides && overrides.has_key?(:args)
 98     unless overrides[:args].empty?
 99       if overrides[:args].is_a?(Hash)
100         if overrides[:args].has_key?(override_key)
101           args = array(overrides[:args][override_key])
102         end
103       else
104         args = array(overrides[:args])
105       end
106     end
107   end
108   args.each do |arg|
109     arg = string(arg).sub(escape_characters, escape_replacement)
110     
111     unless arg.empty?
112       command_string << " \"#{arg}\""
113     end
114   end
115   
116   # Subcommand
117   subcommand_overrides = ( overrides ? overrides[:subcommand] : nil )
118   if subcommand && subcommand.is_a?(Hash) && ! subcommand.empty?
119     subcommand_string = build(subcommand, subcommand_overrides)
120   end
121   
122   command_string = (command_string + ' ' + subcommand_string).strip
123   
124   logger.debug("Rendered command: #{command_string}")
125   return command_string
126 end
exec(options = {}, overrides = nil, &code) click to toggle source
    # File lib/nucleon/command/bash.rb
130 def exec(options = {}, overrides = nil, &code)
131   config = Config.ensure(options)
132   result = Nucleon.cli_run(build(export, overrides), config.import({ :ui => @ui }), &code)
133   
134   if result
135     logger.debug("Command status: #{result.status}")
136     logger.debug("Command output:\n#{result.output}")
137     logger.debug("Command errors:\n#{result.errors}")
138   else
139     logger.debug("Command returned no result")
140   end
141   result
142 end
executable(options) click to toggle source
    # File lib/nucleon/command/bash.rb
147 def executable(options)
148   config = Config.ensure(options)
149   
150   return 'nucleon ' + config[:nucleon] if config.get(:nucleon, false)
151   config[:command]
152 end
normalize(reload) click to toggle source
Calls superclass method
   # File lib/nucleon/command/bash.rb
 9 def normalize(reload)
10   super
11   myself.command = executable(myself)    
12   logger.info("Setting command executable to #{command}")
13 end