-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdep_pkg.go
257 lines (221 loc) · 5.52 KB
/
dep_pkg.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package pkgcraft
// #cgo pkg-config: pkgcraft
// #include <pkgcraft.h>
import "C"
import (
"fmt"
"runtime"
"unsafe"
"github.com/hashicorp/golang-lru/v2"
)
type Dep struct {
ptr *C.Dep
// cached fields
_category string
_package string
_version *Version
_hash uint64
}
type Blocker int
const (
BlockerNone Blocker = iota
BlockerStrong
BlockerWeak
)
func BlockerFromString(s string) (Blocker, error) {
c_str := C.CString(s)
i := C.pkgcraft_dep_blocker_from_str(c_str)
C.free(unsafe.Pointer(c_str))
if i > 0 {
return Blocker(i), nil
} else {
return BlockerNone, fmt.Errorf("invalid blocker: %s", s)
}
}
type SlotOperator int
const (
SlotOpNone SlotOperator = iota
SlotOpEqual
SlotOpStar
)
func SlotOperatorFromString(s string) (SlotOperator, error) {
c_str := C.CString(s)
i := C.pkgcraft_dep_slot_op_from_str(c_str)
C.free(unsafe.Pointer(c_str))
if i > 0 {
return SlotOperator(i), nil
} else {
return SlotOpNone, fmt.Errorf("invalid slot operator: %s", s)
}
}
type Pair[T, U any] struct {
First T
Second U
}
var dep_cache, _ = lru.New[Pair[string, *Eapi], *Dep](10000)
func newDep(s string, eapi *Eapi) (*Dep, error) {
var eapi_ptr *C.Eapi
if eapi == nil {
eapi_ptr = nil
} else {
eapi_ptr = eapi.ptr
}
c_str := C.CString(s)
ptr := C.pkgcraft_dep_new(c_str, eapi_ptr)
C.free(unsafe.Pointer(c_str))
if ptr != nil {
dep := &Dep{ptr: ptr}
runtime.SetFinalizer(dep, func(self *Dep) { C.pkgcraft_dep_free(self.ptr) })
return dep, nil
} else {
return nil, newPkgcraftError()
}
}
// Parse a string into a Dep using the latest EAPI.
func NewDep(s string) (*Dep, error) {
return newDep(s, nil)
}
// Parse a string into a Dep using a specific EAPI.
func NewDepWithEapi(s string, eapi *Eapi) (*Dep, error) {
return newDep(s, eapi)
}
func newCachedDep(s string, eapi *Eapi) (*Dep, error) {
key := Pair[string, *Eapi]{s, eapi}
if dep, ok := dep_cache.Get(key); ok {
return dep, nil
} else {
dep, err := newDep(s, eapi)
if err == nil {
dep_cache.Add(key, dep)
}
return dep, err
}
}
// Return a cached Dep if one exists, otherwise return a new instance.
func NewDepCached(s string) (*Dep, error) {
return newCachedDep(s, nil)
}
// Return a cached Dep if one exists, otherwise parse using a specific EAPI.
func NewDepCachedWithEapi(s string, eapi *Eapi) (*Dep, error) {
return newCachedDep(s, eapi)
}
// Get the blocker of a package dependency.
func (self *Dep) Blocker() Blocker {
i := C.pkgcraft_dep_blocker(self.ptr)
return Blocker(i)
}
// Return a package dependency's category.
func (self *Dep) Category() string {
if self._category == "" {
s := C.pkgcraft_dep_category(self.ptr)
defer C.pkgcraft_str_free(s)
self._category = C.GoString(s)
}
return self._category
}
// Return a package dependency's package.
func (self *Dep) Package() string {
if self._package == "" {
s := C.pkgcraft_dep_package(self.ptr)
defer C.pkgcraft_str_free(s)
self._package = C.GoString(s)
}
return self._package
}
// Return a package dependency's version.
func (self *Dep) Version() *Version {
if self._version == nil {
ptr := C.pkgcraft_dep_version(self.ptr)
if ptr != nil {
self._version, _ = versionFromPtr(ptr)
} else {
self._version = &Version{}
}
}
return self._version
}
// Return a package dependency's revision.
func (self *Dep) Revision() *Revision {
version := self.Version()
if *version != (Version{}) {
return version.Revision()
}
return &Revision{}
}
// Return a package dependency's slot.
func (self *Dep) Slot() string {
s := C.pkgcraft_dep_slot(self.ptr)
defer C.pkgcraft_str_free(s)
return C.GoString(s)
}
// Return a package dependency's subslot.
func (self *Dep) Subslot() string {
s := C.pkgcraft_dep_subslot(self.ptr)
defer C.pkgcraft_str_free(s)
return C.GoString(s)
}
// Return a package dependency's slot operator.
func (self *Dep) SlotOp() SlotOperator {
i := C.pkgcraft_dep_slot_op(self.ptr)
return SlotOperator(i)
}
// Return a package dependency's USE flag dependencies.
func (self *Dep) Use() []string {
var length C.size_t
array := C.pkgcraft_dep_use_deps_str(self.ptr, &length)
use_slice := unsafe.Slice(array, length)
var use []string
for _, s := range use_slice {
use = append(use, C.GoString(s))
}
C.pkgcraft_str_array_free(array, length)
return use
}
// Return a package dependency's repository.
func (self *Dep) Repo() string {
s := C.pkgcraft_dep_repo(self.ptr)
defer C.pkgcraft_str_free(s)
return C.GoString(s)
}
// Return a package dependency's Cpn.
func (self *Dep) Cpn() *Cpn {
ptr := C.pkgcraft_dep_cpn(self.ptr)
cpn, _ := cpnFromPtr(ptr)
return cpn
}
// Return the Cpv of a package dependency if one exists.
func (self *Dep) Cpv() *Cpv {
ptr := C.pkgcraft_dep_cpv(self.ptr)
if ptr != nil {
cpv, _ := cpvFromPtr(ptr)
return cpv
}
return nil
}
func (self *Dep) String() string {
s := C.pkgcraft_dep_str(self.ptr)
defer C.pkgcraft_str_free(s)
return C.GoString(s)
}
func (self *Dep) Hash() uint64 {
if self._hash == 0 {
self._hash = uint64(C.pkgcraft_dep_hash(self.ptr))
}
return self._hash
}
// Compare two package dependencies returning -1, 0, or 1 if the first is
// less than, equal to, or greater than the second, respectively.
func (self *Dep) Cmp(other *Dep) int {
return int(C.pkgcraft_dep_cmp(self.ptr, other.ptr))
}
// Determine if two Cpv or Dep objects intersect.
func (self *Dep) Intersects(other interface{}) bool {
switch other := other.(type) {
case *Cpv:
return bool(C.pkgcraft_dep_intersects_cpv(self.ptr, other.ptr))
case *Dep:
return bool(C.pkgcraft_dep_intersects(self.ptr, other.ptr))
default:
return false
}
}