class Parser

Attributes

config[R]

Public Class Methods

new(config_file_path) click to toggle source
# File lib/go_run/parser.rb, line 6
def initialize(config_file_path)
  @config = {}
  parse(YAML.load_file(config_file_path))
end

Public Instance Methods

add_call(target, call) click to toggle source
# File lib/go_run/parser.rb, line 68
def add_call(target, call)
  initialise_action(target, "call")
  c = {"target" => call}
  @config[target]["call"] << c
end
add_group(target, groups) click to toggle source
# File lib/go_run/parser.rb, line 53
def add_group(target, groups)
  initialise_action(target, "group")
  @config[target]["group"] += groups.split(' ')
end
add_require(target, capture) click to toggle source
# File lib/go_run/parser.rb, line 63
def add_require(target, capture)
  initialise_action(target, "requires")
  @config[target]["requires"] << parse_require(capture)
end
add_run(target, command) click to toggle source
# File lib/go_run/parser.rb, line 74
def add_run(target, command)
  initialise_action(target, "commands")
  @config[target]["commands"] << command
end
add_set(target, capture) click to toggle source
# File lib/go_run/parser.rb, line 58
def add_set(target, capture)
  initialise_action(target, "set")
  @config[target]["set"] << parse_set(capture)
end
initialise_action(target, action) click to toggle source
# File lib/go_run/parser.rb, line 110
def initialise_action(target, action)
  if not @config[target].has_key? action
    @config[target][action] = []
  end
end
initialise_target(target) click to toggle source
# File lib/go_run/parser.rb, line 104
def initialise_target(target)
  if not @config.has_key? target
    @config[target] = {}
  end
end
parse(config) click to toggle source
# File lib/go_run/parser.rb, line 11
def parse(config)
  if config.is_a? Hash
    config.each { |target, lines|
      initialise_target(target)
      if lines.is_a? Array
        lines.each { |line|
          if line.is_a? String
            parse_line(target, line)
          else
            raise "Configuration error: each line of a target should be a string"
          end
        }
      else
        raise "Configuration error: each target should be an array of strings"
      end
    }
  else
    raise "Configuration error: config is not a hash"
  end
end
parse_line(target, line) click to toggle source
# File lib/go_run/parser.rb, line 32
def parse_line(target, line)
  match = /^(GROUP|SET|REQUIRE|CALL|RUN)(.*)/.match line
  if not match.nil?
    capture = match.captures[1].strip
    case match.captures[0]
    when 'GROUP'
      add_group(target, capture)
    when 'SET'
      add_set(target, capture)
    when 'REQUIRE'
      add_require(target, capture)
    when 'CALL'
      add_call(target, capture)
    when 'RUN'
      add_run(target, capture)
    end
  else
    raise "Configuration error: target lines should match /^(GROUP|SET|REQUIRE|CALL|RUN)/"
  end
end
parse_require(requirement) click to toggle source
# File lib/go_run/parser.rb, line 89
def parse_require(requirement)
  match = /^([a-zA-Z][a-zA-Z0-9_]*)$/.match requirement
  if not match.nil?
    r = {"key" => match.captures[0]}
  else
    match = /^([a-zA-Z][a-zA-Z0-9_]*) default (.*)$/.match requirement
    if not match.nil?
      r = {"key" => match.captures[0], "default" => match.captures[1]}
    else
      raise "Configuration error: REQUIRE syntax is 'REQUIRE KEY' or 'REQUIRE KEY default value'"
    end
  end
  return r
end
parse_set(set) click to toggle source
# File lib/go_run/parser.rb, line 79
def parse_set(set)
  match = /^([a-zA-Z][a-zA-Z0-9_]*) (.*)$/.match set
  if not match.nil?
    s = {"key" => match.captures[0], "value" => match.captures[1]}
  else
    raise "Configuration error: SET syntax is 'SET KEY value'"
  end
  return s
end