-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.go
120 lines (96 loc) · 3.04 KB
/
error.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
package resolver
import (
"context"
"fmt"
"strings"
"github.com/mircearoata/pubgrub-go/pubgrub"
"github.com/mircearoata/pubgrub-go/pubgrub/semver"
)
type DependencyResolverError struct {
pubgrub.SolvingError
provider Provider
gameVersion int
}
func (e DependencyResolverError) Error() string {
rootPkg := e.Cause().Terms()[0].Dependency()
writer := pubgrub.NewStandardErrorWriter(rootPkg).WithIncompatibilityStringer(
MakeDependencyResolverErrorStringer(e.provider, e.gameVersion),
)
e.WriteTo(writer)
return writer.String()
}
type DependencyResolverErrorStringer struct {
pubgrub.StandardIncompatibilityStringer
provider Provider
packageNames map[string]string
gameVersion int
}
func MakeDependencyResolverErrorStringer(provider Provider, gameVersion int) *DependencyResolverErrorStringer {
s := &DependencyResolverErrorStringer{
provider: provider,
gameVersion: gameVersion,
packageNames: map[string]string{},
}
s.StandardIncompatibilityStringer = pubgrub.NewStandardIncompatibilityStringer().WithTermStringer(s)
return s
}
func (w *DependencyResolverErrorStringer) getPackageName(pkg string) string {
if pkg == factoryGamePkg {
return "Satisfactory"
}
if name, ok := w.packageNames[pkg]; ok {
return name
}
result, err := w.provider.GetModName(context.TODO(), pkg)
if err != nil {
return pkg
}
w.packageNames[pkg] = result.Name
return result.Name
}
func (w *DependencyResolverErrorStringer) Term(t pubgrub.Term, includeVersion bool) string {
name := w.getPackageName(t.Dependency())
fullName := fmt.Sprintf("%s (%s)", name, t.Dependency())
if name == t.Dependency() {
fullName = t.Dependency()
}
if includeVersion {
if t.Constraint().IsAny() {
return fmt.Sprintf("every version of %s", fullName)
}
switch t.Dependency() {
case factoryGamePkg:
// Remove ".0.0" from the versions mentioned, since only the major is ever used
return fmt.Sprintf("%s \"%s\"", fullName, strings.ReplaceAll(t.Constraint().String(), ".0.0", ""))
default:
res, err := w.provider.ModVersionsWithDependencies(context.TODO(), t.Dependency())
if err != nil {
return fmt.Sprintf("%s \"%s\"", fullName, t.Constraint())
}
var matched []semver.Version
for _, v := range res {
ver, err := semver.NewVersion(v.Version)
if err != nil {
// Assume it is contained in the constraint
matched = append(matched, semver.Version{})
continue
}
if t.Constraint().Contains(ver) {
matched = append(matched, ver)
}
}
if len(matched) == 1 {
return fmt.Sprintf("%s \"%s\"", fullName, matched[0])
}
return fmt.Sprintf("%s \"%s\"", fullName, t.Constraint())
}
}
return fullName
}
func (w *DependencyResolverErrorStringer) IncompatibilityString(incompatibility *pubgrub.Incompatibility, rootPkg string) string {
terms := incompatibility.Terms()
if len(terms) == 1 && terms[0].Dependency() == factoryGamePkg {
return fmt.Sprintf("Satisfactory CL%d is installed", w.gameVersion)
}
return w.StandardIncompatibilityStringer.IncompatibilityString(incompatibility, rootPkg)
}