Skip to content

Commit

Permalink
feat: sort kcl.mod
Browse files Browse the repository at this point in the history
Signed-off-by: Asish Kumar <officialasishkumar@gmail.com>
  • Loading branch information
officialasishkumar committed May 24, 2024
1 parent aa19024 commit a4ce1a8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
16 changes: 16 additions & 0 deletions pkg/package/modfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/url"
"os"
"path/filepath"
"sort"
"strings"

"github.com/BurntSushi/toml"
Expand Down Expand Up @@ -146,6 +147,21 @@ type Dependencies struct {
Deps map[string]Dependency `json:"packages" toml:"dependencies,omitempty"`
}

// SortMapByKey will sort the dependencies by key.
func (deps *Dependencies) SortMapByKey() {
keys := make([]string, 0, len(deps.Deps))
for k := range deps.Deps {
keys = append(keys, k)
}
sort.Strings(keys)

sortedDeps := make(map[string]Dependency)
for _, k := range keys {
sortedDeps[k] = deps.Deps[k]
}
deps.Deps = sortedDeps
}

// ToDepMetadata will transform the dependencies into metadata.
// And check whether the dependency name conflicts.
func (deps *Dependencies) ToDepMetadata() (*Dependencies, error) {
Expand Down
1 change: 1 addition & 0 deletions pkg/package/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ func (kclPkg *KclPkg) LocalVendorPath() string {
// updateModAndLockFile will update kcl.mod and kcl.mod.lock
func (kclPkg *KclPkg) UpdateModAndLockFile() error {
// Generate file kcl.mod.
kclPkg.ModFile.Dependencies.SortMapByKey()
err := kclPkg.ModFile.StoreModFile()
if err != nil {
return err
Expand Down

0 comments on commit a4ce1a8

Please sign in to comment.