Class: Gel::LockParser

Inherits:
Object
  • Object
show all
Defined in:
lib/gel/lock_parser.rb

Instance Method Summary collapse

Instance Method Details

#parse(content) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/gel/lock_parser.rb', line 6

def parse(content)
  sections = []

  scanner = StringScanner.new(content)
  while section = scanner.scan(/^(\w.+)\n/)
    section.chomp!
    case section
    when "GIT", "PATH", "GEM"
      content = {}
      while scanner.skip(/^  \b/)
        label = scanner.scan(/[^:]+:/).chop
        if scanner.skip(/ /)
          value = scanner.scan(/.*/)
          scanner.skip(/\n/)
          (content[label] ||= []) << value
        else
          scanner.skip(/\n/)
          value = []
          while scanner.skip(/^    \b/)
            entry = scanner.scan(/.*/)
            scanner.skip(/\n/)
            children = []
            while scanner.skip(/^      \b/)
              child = scanner.scan(/.*/)
              children << child
              scanner.skip(/\n/)
            end
            if children.empty?
              value << [entry]
            else
              value << [entry, children]
            end
          end
          content[label] = value
        end
      end
    when "PLATFORMS", "DEPENDENCIES"
      content = []
      while scanner.skip(/^  \b/)
        entry = scanner.scan(/.*/)
        content << entry
        scanner.skip(/\n/)
      end
    when "BUNDLED WITH"
      content = []
      while scanner.skip(/^   \b/)
        entry = scanner.scan(/.*/)
        content << entry
        scanner.skip(/\n/)
      end
    end
    scanner.skip(/\n+/)

    sections << [section, content]
  end

  sections
end