-
Notifications
You must be signed in to change notification settings - Fork 8
/
parse-pubspec-lock.go
93 lines (70 loc) · 1.9 KB
/
parse-pubspec-lock.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
package lockfile
import (
"fmt"
"gopkg.in/yaml.v2"
"os"
)
type PubspecLockDescription struct {
Name string `yaml:"name"`
URL string `yaml:"url"`
Path string `yaml:"path"`
Ref string `yaml:"resolved-ref"`
}
var _ yaml.Unmarshaler = &PubspecLockDescription{}
func (pld *PubspecLockDescription) UnmarshalYAML(unmarshal func(interface{}) error) error {
var m struct {
Name string `yaml:"name"`
URL string `yaml:"url"`
Path string `yaml:"path"`
Ref string `yaml:"resolved-ref"`
}
err := unmarshal(&m)
if err == nil {
pld.Name = m.Name
pld.Path = m.Path
pld.URL = m.URL
pld.Ref = m.Ref
return nil
}
var str *string
err = unmarshal(&str)
if err != nil {
return err
}
pld.Path = *str
return nil
}
type PubspecLockPackage struct {
Source string `yaml:"source"`
Description PubspecLockDescription `yaml:"description"`
Version string `yaml:"version"`
}
type PubspecLockfile struct {
Packages map[string]PubspecLockPackage `yaml:"packages,omitempty"`
Sdks map[string]string `yaml:"sdks"`
}
const PubEcosystem Ecosystem = "Pub"
func ParsePubspecLock(pathToLockfile string) ([]PackageDetails, error) {
var parsedLockfile *PubspecLockfile
lockfileContents, err := os.ReadFile(pathToLockfile)
if err != nil {
return []PackageDetails{}, fmt.Errorf("could not read %s: %w", pathToLockfile, err)
}
err = yaml.Unmarshal(lockfileContents, &parsedLockfile)
if err != nil {
return []PackageDetails{}, fmt.Errorf("could not parse %s: %w", pathToLockfile, err)
}
if parsedLockfile == nil {
return []PackageDetails{}, nil
}
packages := make([]PackageDetails, 0, len(parsedLockfile.Packages))
for name, pkg := range parsedLockfile.Packages {
packages = append(packages, PackageDetails{
Name: name,
Version: pkg.Version,
Commit: pkg.Description.Ref,
Ecosystem: PubEcosystem,
})
}
return packages, nil
}