-
Notifications
You must be signed in to change notification settings - Fork 0
/
ruby.go
95 lines (85 loc) · 2.46 KB
/
ruby.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
package internal
import (
"errors"
"path"
"strings"
"github.com/CircleCI-Public/circleci-config/labeling/codebase"
"github.com/CircleCI-Public/circleci-config/labeling/labels"
)
var RubyRules = []labels.Rule{
func(c codebase.Codebase, ls labels.LabelSet) (label labels.Label, err error) {
label.Key = labels.DepsRuby
label.Dependencies = make(map[string]string)
gemfilePath, err := c.FindFile("Gemfile")
if err != nil && !errors.Is(err, codebase.NotFoundError) {
return label, err
}
if gemfilePath != "" {
label.Valid = true
label.BasePath = path.Dir(gemfilePath)
err = readDepsFile(c, label.Dependencies, gemfilePath)
if err != nil {
return label, err
}
label.HasLockFile = hasPath(c, path.Join(path.Dir(gemfilePath), "Gemfile.lock"))
}
return label, nil
},
func(c codebase.Codebase, ls labels.LabelSet) (label labels.Label, err error) {
label.Key = labels.PackageManagerGemspec
label.Dependencies = make(map[string]string)
gemspecPath, err := c.FindFile("*.gemspec")
if err != nil && !errors.Is(err, codebase.NotFoundError) {
return label, err
}
if gemspecPath != "" {
label.Valid = true
label.BasePath = path.Dir(gemspecPath)
err = readDepsFile(c, label.Dependencies, gemspecPath)
if err != nil {
return label, err
}
}
return label, nil
},
}
// Parse the Gemfile to add dependencies to the label
func readDepsFile(c codebase.Codebase, deps map[string]string, filePath string) error {
fileContents, err := c.ReadFile(filePath)
if err != nil {
return err
}
for _, line := range strings.Split(string(fileContents), "\n") {
line = strings.ReplaceAll(line, "\"", "'")
if strings.HasPrefix(line, "ruby ") {
version := strings.Split(line, ",")[0]
version = strings.SplitAfter(version, "ruby ")[1]
version = strings.ReplaceAll(version, "'", "")
version = strings.TrimSpace(version)
deps["ruby"] = version
}
gems := map[string]string{
"rails": "rails",
"rspec-rails": "rspec",
"rspec": "rspec",
"rspec_junit_formatter": "rspec_junit_formatter",
"rake": "rake",
"pg": "pg",
}
for k, v := range gems {
if hasGem(line, k) {
deps[v] = "true"
}
}
}
return nil
}
func hasGem(line string, gem string) bool {
forms := []string{"gem '", "development_dependency '", "development_dependency('"}
for _, f := range forms {
if strings.Contains(line, f+gem) {
return true
}
}
return false
}