-
Notifications
You must be signed in to change notification settings - Fork 0
/
updates.go
210 lines (173 loc) · 5.17 KB
/
updates.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// Copyright 2022 The Firefly Authors.
//
// Use of this source code is governed by a BSD 3-clause
// license that can be found in the LICENSE file.
package vendeps
import (
"context"
"fmt"
"os"
"github.com/bazelbuild/buildtools/build"
"golang.org/x/mod/semver"
)
// UpdateDependencies parses the given set of
// dependencies and checks each for an update,
// updating the document if possible.
//
// Note that UpdateDependencies does not modify
// the set of vendored dependencies, only the
// dependency specification.
func UpdateDependencies(name string) error {
data, err := os.ReadFile(name)
if err != nil {
return err
}
f, err := build.ParseBzl(name, data)
if err != nil {
return err
}
deps, err := ParseUpdateDeps(name, f)
if err != nil {
return err
}
anyUpdated := false
ctx := context.Background()
for _, dep := range deps.Rust {
updated, err := UpdateRustCrate(ctx, dep)
if err != nil {
return err
}
anyUpdated = anyUpdated || updated
}
for _, dep := range deps.Go {
updated, err := UpdateGoModule(ctx, dep)
if err != nil {
return err
}
anyUpdated = anyUpdated || updated
}
if !anyUpdated {
fmt.Println("No dependencies had updates available.")
return nil
}
// We've updated the Starlark file's
// AST, so we format it and write it
// back out.
data = build.Format(f)
err = os.WriteFile(name, data, 0644)
if err != nil {
return fmt.Errorf("failed to write updates back to %s: %v", name, err)
}
return nil
}
// MajorUpdate returns true if the newer
// version has a different major number,
// or if both have major version 0 and
// the newer version has a different
// minor version.
func MajorUpdate(current, next string) bool {
if semver.Major(current) == "v0" && semver.Major(next) == "v0" {
return semver.Compare(current, next) == -1 && semver.MajorMinor(current) != semver.MajorMinor(next)
}
return semver.Compare(current, next) == -1 && semver.Major(current) != semver.Major(next)
}
// ParseUpdateDeps parses a deps.bzl file for
// the set of dependencies so they can be
// checked for updates.
func ParseUpdateDeps(filename string, f *build.File) (*UpdateDeps, error) {
// pos is a helper for printing file:line prefixes
// for error messages.
pos := func(x build.Expr) string {
start, _ := x.Span()
return fmt.Sprintf("%s:%d", filename, start.Line)
}
var deps UpdateDeps
for _, stmt := range f.Stmt {
if _, ok := stmt.(*build.CommentBlock); ok {
continue
}
// At the top level, we only allow assignments,
// where the identifier being assigned to indicates
// which field in the structure we populate.
assign, ok := stmt.(*build.AssignExpr)
if !ok {
return nil, fmt.Errorf("%s: unexpected statement type: %T", pos(stmt), stmt)
}
lhs, ok := assign.LHS.(*build.Ident)
if !ok {
return nil, fmt.Errorf("%s: found assignment to %T, expected identifier", pos(assign.LHS), assign.LHS)
}
switch lhs.Name {
case "rust", "go":
default:
return nil, fmt.Errorf("%s: found assignment to unrecognised identifier %q", pos(assign.LHS), lhs.Name)
}
list, ok := assign.RHS.(*build.ListExpr)
if !ok {
return nil, fmt.Errorf("%s: found assignment of %T to %s, expected list", pos(assign.RHS), assign.RHS, lhs.Name)
}
dep := make([]*UpdateDep, len(list.List))
for i, elt := range list.List {
call, ok := elt.(*build.CallExpr)
if !ok {
return nil, fmt.Errorf("%s: found dependency type %T, expected structure", pos(elt), elt)
}
var next UpdateDep
for _, elt := range call.List {
assign, ok := elt.(*build.AssignExpr)
if !ok {
return nil, fmt.Errorf("%s: found structure entry type %T, expected assignment", pos(elt), elt)
}
lhs, ok := assign.LHS.(*build.Ident)
if !ok || lhs.Name == "True" || lhs.Name == "False" {
typeName := fmt.Sprintf("%T", assign.LHS)
if lhs != nil && lhs.Name != "" {
typeName = "bool"
}
return nil, fmt.Errorf("%s: found structure field assignment to %s, expected identifier", pos(assign.LHS), typeName)
}
switch lhs.Name {
case "name", "version":
default:
continue
}
str, ok := assign.RHS.(*build.StringExpr)
if !ok {
return nil, fmt.Errorf("%s: found assignment of %T to %s, expected string", pos(assign.RHS), assign.RHS, lhs.Name)
}
switch lhs.Name {
case "name":
if next.Name != "" {
return nil, fmt.Errorf("%s: %s assigned to for the second time", pos(lhs), lhs.Name)
}
next.Name = str.Value
case "version":
if next.Version != nil {
return nil, fmt.Errorf("%s: %s assigned to for the second time", pos(lhs), lhs.Name)
}
next.Version = &str.Value
}
}
if next.Name == "" {
return nil, fmt.Errorf("%s: dependency has no name", pos(call))
}
if next.Version == nil {
return nil, fmt.Errorf("%s: dependency has no version", pos(call))
}
dep[i] = &next
}
switch lhs.Name {
case "rust":
if len(deps.Rust) != 0 {
return nil, fmt.Errorf("%s: found %s for the second time", pos(assign.LHS), lhs.Name)
}
deps.Rust = dep
case "go":
if len(deps.Go) != 0 {
return nil, fmt.Errorf("%s: found %s for the second time", pos(assign.LHS), lhs.Name)
}
deps.Go = dep
}
}
return &deps, nil
}