Class: Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/go_run/parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_file_path) ⇒ Parser

Returns a new instance of Parser.



6
7
8
9
# File 'lib/go_run/parser.rb', line 6

def initialize(config_file_path)
  @config = {}
  parse(YAML.load_file(config_file_path))
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



4
5
6
# File 'lib/go_run/parser.rb', line 4

def config
  @config
end

Instance Method Details

#add_call(target, call) ⇒ Object



68
69
70
71
72
# 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) ⇒ Object



53
54
55
56
# 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) ⇒ Object



63
64
65
66
# 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) ⇒ Object



74
75
76
77
# 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) ⇒ Object



58
59
60
61
# 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) ⇒ Object



110
111
112
113
114
# 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) ⇒ Object



104
105
106
107
108
# File 'lib/go_run/parser.rb', line 104

def initialise_target(target)
  if not @config.has_key? target
    @config[target] = {}
  end
end

#parse(config) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 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) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# 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) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
# 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) ⇒ Object



79
80
81
82
83
84
85
86
87
# 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