-
Notifications
You must be signed in to change notification settings - Fork 9
/
parse-gemfile-lock.go
179 lines (141 loc) · 4.42 KB
/
parse-gemfile-lock.go
1
2
3
4
5
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package lockfile
import (
"fmt"
"log"
"os"
"strings"
"github.com/g-rath/osv-detector/internal/cachedregexp"
)
const BundlerEcosystem Ecosystem = "RubyGems"
const lockfileSectionBUNDLED = "BUNDLED WITH"
const lockfileSectionDEPENDENCIES = "DEPENDENCIES"
const lockfileSectionPLATFORMS = "PLATFORMS"
const lockfileSectionRUBY = "RUBY VERSION"
const lockfileSectionGIT = "GIT"
const lockfileSectionGEM = "GEM"
const lockfileSectionPATH = "PATH"
const lockfileSectionPLUGIN = "PLUGIN SOURCE"
type parserState string
const parserStateSource parserState = "source"
const parserStateDependency parserState = "dependency"
const parserStatePlatform parserState = "platform"
const parserStateRuby parserState = "ruby"
const parserStateBundledWith parserState = "bundled_with"
func isSourceSection(line string) bool {
return strings.Contains(line, lockfileSectionGIT) ||
strings.Contains(line, lockfileSectionGEM) ||
strings.Contains(line, lockfileSectionPATH) ||
strings.Contains(line, lockfileSectionPLUGIN)
}
type gemfileLockfileParser struct {
state parserState
dependencies []PackageDetails
bundlerVersion string
rubyVersion string
// holds the commit of the gem that is currently being parsed, if found
currentGemCommit string
}
func (parser *gemfileLockfileParser) addDependency(name string, version string) {
parser.dependencies = append(parser.dependencies, PackageDetails{
Name: name,
Version: version,
Ecosystem: BundlerEcosystem,
CompareAs: BundlerEcosystem,
Commit: parser.currentGemCommit,
})
}
func (parser *gemfileLockfileParser) parseSpec(line string) {
// nameVersionReg := cachedregexp.MustCompile(`^( {2}| {4}| {6})(?! )(.*?)(?: \(([^-]*)(?:-(.*))?\))?(!)?$`)
nameVersionReg := cachedregexp.MustCompile(`^( +)(.*?)(?: \(([^-]*)(?:-(.*))?\))?(!)?$`)
results := nameVersionReg.FindStringSubmatch(line)
if results == nil {
return
}
spaces := results[1]
if spaces == "" {
log.Fatal("Weird error when parsing spec in Gemfile.lock (unexpectedly had no spaces) - please report this")
}
if len(spaces) == 4 {
parser.addDependency(results[2], results[3])
}
}
func (parser *gemfileLockfileParser) parseSource(line string) {
if line == " specs" {
// todo: skip for now
return
}
// OPTIONS = /^ ([a-z]+): (.*)$/i.freeze
optionsRegexp := cachedregexp.MustCompile(`(?i)^ {2}([a-z]+): (.*)$`)
// todo: support
options := optionsRegexp.FindStringSubmatch(line)
if options != nil {
commit := strings.TrimPrefix(options[0], " revision: ")
// if the prefix was removed then the gem being parsed is git based, so
// we store the commit to be included later
if commit != options[0] {
parser.currentGemCommit = commit
}
return
}
// todo: source check
parser.parseSpec(line)
}
func isNotIndented(line string) bool {
re := cachedregexp.MustCompile(`^\S`)
return re.MatchString(line)
}
func (parser *gemfileLockfileParser) parseLineBasedOnState(line string) {
switch parser.state {
case parserStateDependency:
case parserStatePlatform:
break
case parserStateRuby:
parser.rubyVersion = strings.TrimSpace(line)
case parserStateBundledWith:
parser.bundlerVersion = strings.TrimSpace(line)
case parserStateSource:
parser.parseSource(line)
default:
log.Fatalf("Unknown supported '%s'\n", parser.state)
}
}
func (parser *gemfileLockfileParser) parse(contents string) {
lineMatcher := cachedregexp.MustCompile(`(?:\r?\n)+`)
lines := lineMatcher.Split(contents, -1)
for _, line := range lines {
if isSourceSection(line) {
// clear the stateful package details,
// since we're now parsing a new group
parser.currentGemCommit = ""
parser.state = parserStateSource
parser.parseSource(line)
continue
}
switch line {
case lockfileSectionDEPENDENCIES:
parser.state = parserStateDependency
case lockfileSectionPLATFORMS:
parser.state = parserStatePlatform
case lockfileSectionRUBY:
parser.state = parserStateRuby
case lockfileSectionBUNDLED:
parser.state = parserStateBundledWith
default:
if isNotIndented(line) {
parser.state = ""
}
if parser.state != "" {
parser.parseLineBasedOnState(line)
}
}
}
}
func ParseGemfileLock(pathToLockfile string) ([]PackageDetails, error) {
var parser gemfileLockfileParser
bytes, err := os.ReadFile(pathToLockfile)
if err != nil {
return []PackageDetails{}, fmt.Errorf("could not read %s: %w", pathToLockfile, err)
}
parser.parse(string(bytes))
return parser.dependencies, nil
}