class RCoLi::SystemExecutor

Public Class Methods

new() click to toggle source
# File lib/rcoli/utils.rb, line 57
def initialize
end

Public Instance Methods

execute(command, *args) click to toggle source
# File lib/rcoli/utils.rb, line 66
def execute(command, *args)
  cmnd = @commands[command.to_s]
  if cmnd
    cmnd.scan(/\$\{([^}\s]+)\}/).each do |s|
      context = args[0]
      (s[0].split('.').each{|key| context = (context.is_a? Hash) ? context[key] : nil})
      cmnd = cmnd.gsub("${#{s[0]}}", ((context.is_a? Array) ? context.join(',') : context.to_s)) if context
    end
    
    # ALTERNATIVE SOLUTION
    # cmnd.to_enum(:scan, /\$\{([^\s]+)\}/).map {[Regexp.last_match[0], Regexp.last_match[1].split('.')]}.each do |m|
    #   context = (context.nil? ? config : context)[m[1].delete_at(0)] until m[1].size == 0
    #   cmnd = cmnd.sub(m[0], context) if context.is_a? String
    # end
    
    
    log.debug("EXEC: #{cmnd}")
    system(cmnd) unless ApplicationContext.instance.devmode
  else
    raise ApplicationError, "The command #{command} isn't configured. Check the file #{@source}"
  end
end
register(file) click to toggle source
# File lib/rcoli/utils.rb, line 60
def register(file)
  @source = file
  log.debug("Loading commands from file #{file}")
  @commands = YAML::load(File.open(file))
end