-
Notifications
You must be signed in to change notification settings - Fork 2
/
vendormodule.go
207 lines (185 loc) · 5.55 KB
/
vendormodule.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
// Copyright 2019 Torben Schinke
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"fmt"
"io/ioutil"
"path/filepath"
"sort"
"strconv"
"strings"
)
// A Version represents a semver version
type Version struct {
Major int64
Minor int64
Micro int64
}
func (v Version) String() string {
return "v" + strconv.Itoa(int(v.Major)) + "." + strconv.Itoa(int(v.Minor)) + "." + strconv.Itoa(int(v.Micro))
}
// IsNewer if this version is newer or higher than the other
func (v Version) IsNewer(other Version) bool {
if v.Major > other.Major {
return true
}
if v.Minor > other.Minor {
return true
}
if v.Micro > other.Micro {
return true
}
return false
}
// ParseSemanticVersion reads strings like v0.1.2 or 0.1.2
func ParseSemanticVersion(str string) (Version, error) {
str = strings.TrimSpace(str)
if strings.HasPrefix(str, "v") {
str = str[1:]
}
msg := "failed to parse version: %s: %v"
tokens := strings.Split(str, ".")
if len(tokens) != 3 {
return Version{}, fmt.Errorf("failed to parse version: %s", str)
}
major, err := strconv.ParseInt(tokens[0], 10, 64)
if err != nil {
return Version{}, fmt.Errorf(msg, str, err)
}
minor, err := strconv.ParseInt(tokens[1], 10, 64)
if err != nil {
return Version{}, fmt.Errorf(msg, str, err)
}
micro, err := strconv.ParseInt(tokens[2], 10, 64)
if err != nil {
return Version{}, fmt.Errorf(msg, str, err)
}
return Version{major, minor, micro}, nil
}
// A VendoredModule represents an entry from modules.txt, as generated by go mod vendor
// e.g.
// # github.com/worldiety/std v0.0.0-20190429141453-4964c97755c6
// github.com/worldiety/std
type VendoredModule struct {
// ModuleName is the actual name of the module
ModuleName string
// Version is the semantic version, usually 0.0.0 if tip of repo should be used
Version Version
// ModuleImport is probably the url from where to get the module data
ModuleImport string
// Local determines the fully qualified local path
Local Path
}
// ParseModulesTxT parsed the modules.txt, as generated by go mod-vendor.
// Example:
// # github.com/json-iterator/go v1.1.6
// github.com/json-iterator/go
// # github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd
// github.com/modern-go/concurrent
// # github.com/modern-go/reflect2 v1.0.1
// github.com/modern-go/reflect2
// # golang.org/x/text v0.3.2
// golang.org/x/text/collate
// golang.org/x/text/language
// golang.org/x/text/internal/colltab
// golang.org/x/text/unicode/norm
// golang.org/x/text/internal/language
// golang.org/x/text/internal/language/compact
// golang.org/x/text/transform
// golang.org/x/text/internal/tag
func ParseModulesTxT(fname string) ([]VendoredModule, error) {
//there is no modules.txt if there are no dependencies, that is valid
if !Path(fname).Exists() {
return nil, nil
}
text, err := ioutil.ReadFile(fname)
if err != nil {
return nil, fmt.Errorf("unable to parse module.txt: %v", err)
}
res := make([]VendoredModule, 0)
lines := strings.Split(string(text), "\n")
for i := 0; i < len(lines); {
line := strings.TrimSpace(lines[i])
// skip empty line
if len(line) < 1 {
i++
continue
}
var modImportURL string
var version Version
// is it a comment?
if strings.HasPrefix(line, "#") {
pathVersionTokens := strings.Split(line, " ")
if len(pathVersionTokens) < 2 {
fmt.Printf("%s: invalid path-version at line %d: %s", fname, i, lines[i])
i++
continue
}
var versionTokens string
for _, token := range pathVersionTokens {
if token == "#" {
continue
}
if len(modImportURL) == 0 {
modImportURL = token
}
if len(modImportURL) > 0 && strings.HasPrefix(token, "v") {
versionTokens = token
}
}
if strings.Contains(versionTokens, "-") {
versionTokens = versionTokens[0:strings.Index(versionTokens, "-")]
}
if len(versionTokens) == 0 {
versionTokens = "v0.0.0"
}
version, err = ParseSemanticVersion(versionTokens)
if err != nil {
i++
fmt.Printf("%s: invalid version string at line %d: %s: %v", fname, i, lines[i], err)
continue
}
i++
}
modName := lines[i]
if len(modImportURL) == 0 {
modImportURL = modName
}
path := Path(filepath.Dir(fname)).Add(Path(modName))
res = append(res, VendoredModule{Version: version, ModuleName: modName, ModuleImport: modImportURL, Local: path})
i++
}
return res, nil
}
// we need to sort the dependencies because the order of a vendored module.txt is not deterministic (at least
// after putting them into a map ;-) but
// because we move them later to the correct location, we need to ensure that shortest paths come first
func asSortedSlice(m map[string]VendoredModule) []VendoredModule {
tmp := make([]VendoredModule, 0)
for _, v := range m {
tmp = append(tmp, v)
}
sort.Sort(byVendoredModuleName(tmp))
return tmp
}
type byVendoredModuleName []VendoredModule
func (s byVendoredModuleName) Len() int {
return len(s)
}
func (s byVendoredModuleName) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s byVendoredModuleName) Less(i, j int) bool {
return len(s[i].ModuleImport) < len(s[j].ModuleImport)
}