Skip to content

Commit 111960c

Browse files
committed
wit: export ComparePackages
1 parent 999af0b commit 111960c

2 files changed

Lines changed: 8 additions & 2 deletions

File tree

wit/package.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,13 @@ func (p *Package) dependsOn(dep Node) bool {
4848
return done
4949
}
5050

51-
func comparePackages(a, b *Package) int {
51+
// ComparePackages compares two WIT packages, based on dependence.
52+
// When used with [slices.SortFunc], this function will sort packages
53+
// based on their mutual dependence. Packages with fewer or no dependencies will sort last.
54+
// If package a depends on package b, this function returns 1.
55+
// If package b depends on package a, this function returns -1.
56+
// If package a == package b, this function returns 0.
57+
func ComparePackages(a, b *Package) int {
5258
// fmt.Fprintln(os.Stderr, "comparing "+b.Name.String()+" to "+a.Name.String())
5359
switch {
5460
case a == b:

wit/wit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ func (*Resolve) WITKind() string { return "resolve" }
133133
func (r *Resolve) WIT(ctx Node, _ string) string {
134134
// Sort packages topologically by dependency
135135
packages := slices.Clone(r.Packages)
136-
slices.SortFunc(packages, comparePackages)
136+
slices.SortFunc(packages, ComparePackages)
137137
slices.Reverse(packages)
138138

139139
var b strings.Builder

0 commit comments

Comments
 (0)