-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdiscovery_workspace.go
More file actions
186 lines (168 loc) · 4.55 KB
/
Copy pathdiscovery_workspace.go
File metadata and controls
186 lines (168 loc) · 4.55 KB
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
180
181
182
183
184
185
186
package manifests
import (
"bytes"
"encoding/json"
"fmt"
"strings"
"github.com/BurntSushi/toml"
"gopkg.in/yaml.v3"
)
func (d *manifestDiscovery) discoverCargoWorkspace() error {
const parentPath = "Cargo.toml"
content, exists, err := d.readOptional(parentPath)
if err != nil {
return fmt.Errorf("reading Cargo workspace configuration: %w", err)
}
if !exists {
return nil
}
var config struct {
Workspace struct {
Members []string `toml:"members"`
Exclude []string `toml:"exclude"`
} `toml:"workspace"`
}
if _, err := toml.Decode(string(content), &config); err != nil {
return fmt.Errorf("parsing Cargo workspace configuration: %w", err)
}
if err := d.addWorkspaceManifests(
config.Workspace.Members,
config.Workspace.Exclude,
"Cargo.toml",
parentPath,
); err != nil {
return fmt.Errorf("discovering Cargo workspace members: %w", err)
}
return nil
}
func (d *manifestDiscovery) discoverGoWorkspace() error {
const parentPath = "go.work"
content, exists, err := d.readOptional(parentPath)
if err != nil {
return fmt.Errorf("reading Go workspace configuration: %w", err)
}
if !exists {
return nil
}
members := goWorkspaceMembers(content)
if err := d.addWorkspaceManifests(members, nil, "go.mod", parentPath); err != nil {
return fmt.Errorf("discovering Go workspace members: %w", err)
}
return nil
}
func goWorkspaceMembers(content []byte) []string {
var members []string
inUseBlock := false
for line := range strings.SplitSeq(string(content), "\n") {
line = stripGoWorkComment(line)
fields := strings.Fields(line)
if len(fields) == 0 {
continue
}
if inUseBlock {
var closed bool
members, closed = appendGoWorkUseFields(members, fields)
inUseBlock = !closed
continue
}
if fields[0] != "use" {
continue
}
if len(fields) >= 2 && fields[1] == "(" {
var closed bool
members, closed = appendGoWorkUseFields(members, fields[2:])
inUseBlock = !closed
continue
}
members, _ = appendGoWorkUseFields(members, fields[1:])
}
return members
}
func appendGoWorkUseFields(members, fields []string) ([]string, bool) {
for _, field := range fields {
switch field {
case "(":
continue
case ")":
return members, true
default:
members = append(members, strings.Trim(field, `"'`))
}
}
return members, false
}
func stripGoWorkComment(line string) string {
if index := strings.Index(line, "//"); index >= 0 {
line = line[:index]
}
return strings.TrimSpace(line)
}
func (d *manifestDiscovery) discoverNPMWorkspace() error {
const parentPath = "package.json"
content, exists, err := d.readOptional(parentPath)
if err != nil {
return fmt.Errorf("reading npm workspace configuration: %w", err)
}
if !exists {
return nil
}
patterns, err := npmWorkspacePatterns(content)
if err != nil {
return fmt.Errorf("parsing npm workspace configuration: %w", err)
}
if err := d.addWorkspaceManifests(patterns, nil, "package.json", parentPath); err != nil {
return fmt.Errorf("discovering npm workspace members: %w", err)
}
return nil
}
func npmWorkspacePatterns(content []byte) ([]string, error) {
var config struct {
Workspaces json.RawMessage `json:"workspaces"`
}
if err := json.Unmarshal(content, &config); err != nil {
return nil, err
}
if len(config.Workspaces) == 0 || bytes.Equal(bytes.TrimSpace(config.Workspaces), []byte("null")) {
return nil, nil
}
var patterns []string
if err := json.Unmarshal(config.Workspaces, &patterns); err == nil {
return patterns, nil
}
var grouped struct {
Packages []string `json:"packages"`
}
if err := json.Unmarshal(config.Workspaces, &grouped); err != nil {
return nil, err
}
return grouped.Packages, nil
}
func (d *manifestDiscovery) discoverPnpmWorkspace() error {
const parentPath = "pnpm-workspace.yaml"
content, exists, err := d.readOptional(parentPath)
if err != nil {
return fmt.Errorf("reading pnpm workspace configuration: %w", err)
}
if !exists {
return nil
}
var config struct {
Packages []string `yaml:"packages"`
}
if err := yaml.Unmarshal(content, &config); err != nil {
return fmt.Errorf("parsing pnpm workspace configuration: %w", err)
}
includes := make([]string, 0, len(config.Packages))
excludes := make([]string, 0, len(config.Packages))
for _, pattern := range config.Packages {
if len(pattern) > 0 && pattern[0] == '!' {
excludes = append(excludes, pattern[1:])
continue
}
includes = append(includes, pattern)
}
if err := d.addWorkspaceManifests(includes, excludes, "package.json", parentPath); err != nil {
return fmt.Errorf("discovering pnpm workspace members: %w", err)
}
return nil
}