-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
group.go
211 lines (196 loc) · 5.55 KB
/
group.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
211
package genrgst
import (
"fmt"
"reflect"
"sort"
"strings"
"github.com/aquaproj/aqua/v2/pkg/asset"
"github.com/aquaproj/aqua/v2/pkg/config/registry"
"github.com/aquaproj/aqua/v2/pkg/ptr"
"github.com/sirupsen/logrus"
)
type Group struct {
releases []*Release
allAsset string
pkg *Package
assetNames []string
}
func ConvertPkgToVO(pkgInfo *registry.PackageInfo) *registry.VersionOverride {
return ®istry.VersionOverride{
Asset: pkgInfo.Asset,
Files: pkgInfo.Files,
Format: pkgInfo.Format,
Overrides: pkgInfo.Overrides,
Replacements: pkgInfo.Replacements,
SupportedEnvs: pkgInfo.SupportedEnvs,
CompleteWindowsExt: pkgInfo.CompleteWindowsExt,
Checksum: pkgInfo.Checksum,
}
}
func mergeGroups(groups []*Group) (*registry.PackageInfo, []string) { //nolint:cyclop
pkg := ®istry.PackageInfo{}
if len(groups) == 0 {
return pkg, nil
}
versions := make([]string, 0, len(groups))
for _, group := range groups {
if len(group.releases) == 0 {
continue
}
pkgInfo := group.pkg.Info
vo := ConvertPkgToVO(pkgInfo)
if len(group.releases) == 1 {
v := group.releases[0].Tag
vo.VersionConstraints = fmt.Sprintf(`Version == "%s"`, v)
if !group.pkg.Info.NoAsset {
versions = append(versions, v)
}
} else {
v := group.releases[len(group.releases)-1].Tag
vo.VersionConstraints = fmt.Sprintf(`semver("<= %s")`, strings.TrimPrefix(v, "v"))
if !group.pkg.Info.NoAsset {
versions = append(versions, v)
}
}
if pkgInfo.NoAsset {
vo.NoAsset = ptr.Bool(true)
}
if pkgInfo.Rosetta2 {
vo.Rosetta2 = ptr.Bool(true)
}
if pkgInfo.WindowsARMEmulation {
vo.WindowsARMEmulation = ptr.Bool(true)
}
if pkgInfo.VersionFilter != "" {
vo.VersionFilter = ptr.String(pkgInfo.VersionFilter)
}
if pkgInfo.VersionPrefix != "" {
vo.VersionPrefix = ptr.String(pkgInfo.VersionPrefix)
}
pkg.VersionOverrides = append(pkg.VersionOverrides, vo)
}
pkg.VersionOverrides[len(pkg.VersionOverrides)-1].VersionConstraints = "true"
reverse(versions)
return pkg, versions
}
func reverse(versions []string) {
// sort.Reverse doesn't work well.
// https://qiita.com/shibukawa/items/0e6e01dc41c352ccedb5
size := len(versions)
for i := range size / 2 {
versions[i], versions[size-i-1] = versions[size-i-1], versions[i]
}
}
func replaceVersion(assetName, version string) string {
return strings.Replace(
strings.Replace(assetName, version, "{{.Version}}", 1),
strings.TrimPrefix(version, "v"), "{{trimV .Version}}", 1)
}
func groupByAllAsset(releases []*Release) []*Group {
groups := []*Group{}
var group *Group
for _, release := range releases {
assetNames := make([]string, len(release.assets))
for i, asset := range release.assets {
assetNames[i] = replaceVersion(asset.GetName(), release.Tag)
}
sort.Strings(assetNames)
allAsset := strings.Join(assetNames, "\n")
if group == nil {
group = &Group{
releases: []*Release{
release,
},
allAsset: allAsset,
assetNames: assetNames,
}
continue
}
if group.allAsset == allAsset {
group.releases = append(group.releases, release)
continue
}
groups = append(groups, group)
group = &Group{
releases: []*Release{
release,
},
allAsset: allAsset,
assetNames: assetNames,
}
}
if len(groups) == 0 && group != nil {
groups = append(groups, group)
}
if groups[len(groups)-1].allAsset != group.allAsset {
groups = append(groups, group)
}
return groups
}
func groupByExcludedAsset(groups []*Group, pkgName string) []*Group {
newGroups := make([]*Group, 1, len(groups))
prevGroup := groups[0]
newGroups[0] = prevGroup
for _, group := range groups[1:] {
assetNames := make([]string, 0, len(group.assetNames))
for _, assetName := range group.assetNames {
if asset.Exclude(pkgName, assetName) {
continue
}
assetNames = append(assetNames, assetName)
}
group.assetNames = assetNames
group.allAsset = strings.Join(assetNames, "\n")
if prevGroup.allAsset == group.allAsset {
prevGroup.releases = append(prevGroup.releases, group.releases...)
continue
}
newGroups = append(newGroups, prevGroup)
prevGroup = group
}
if newGroups[len(newGroups)-1].allAsset != prevGroup.allAsset {
newGroups = append(newGroups, prevGroup)
}
return newGroups
}
func (c *Controller) group(logE *logrus.Entry, pkgName string, releases []*Release) []*Group {
if len(releases) == 0 {
return nil
}
groups := groupByExcludedAsset(groupByAllAsset(releases), pkgName)
for _, group := range groups {
release := group.releases[0]
pkgInfo := ®istry.PackageInfo{}
c.patchRelease(logE, pkgInfo, pkgName, release.Tag, release.assets)
group.pkg = &Package{
Info: pkgInfo,
Version: release.Tag,
SemVer: release.Tag,
}
}
if len(groups) == 1 {
return groups
}
prevGroup := groups[0]
newGroups := make([]*Group, 1, len(groups))
newGroups[0] = prevGroup
for _, group := range groups[1:] {
if reflect.DeepEqual(group.pkg.Info, prevGroup.pkg.Info) {
prevGroup.releases = append(prevGroup.releases, group.releases...)
continue
}
newGroups = append(newGroups, group)
prevGroup = group
}
if newGroups[len(newGroups)-1].allAsset != prevGroup.allAsset {
newGroups = append(newGroups, prevGroup)
}
group := newGroups[len(newGroups)-1]
if group.pkg.Info.NoAsset || group.pkg.Info.Asset == "" {
return newGroups[:len(newGroups)-1]
}
return newGroups
}
func (c *Controller) generatePackage(logE *logrus.Entry, pkgName string, releases []*Release) (*registry.PackageInfo, []string) {
return mergeGroups(c.group(logE, pkgName, releases))
}