diff --git a/pkg/client/client.go b/pkg/client/client.go index 0732784e..f3ab6022 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -320,6 +320,12 @@ func (c *KpmClient) UpdateDeps(kclPkg *pkg.KclPkg) error { return err } + // update kcl.mod + err = kclPkg.ModFile.StoreModFile() + if err != nil { + return err + } + // Generate file kcl.mod.lock. if !kclPkg.NoSumCheck { err := kclPkg.LockDepsVersion() diff --git a/pkg/cmd/cmd_update.go b/pkg/cmd/cmd_update.go index 330d3678..5a99c8b0 100644 --- a/pkg/cmd/cmd_update.go +++ b/pkg/cmd/cmd_update.go @@ -5,12 +5,21 @@ package cmd import ( + "fmt" "os" + "slices" + "strings" + "github.com/dominikbraun/graph" "github.com/urfave/cli/v2" + "golang.org/x/mod/module" + mvsExt "kcl-lang.io/kpm/pkg/3rdparty/mvs" "kcl-lang.io/kpm/pkg/client" "kcl-lang.io/kpm/pkg/env" + "kcl-lang.io/kpm/pkg/mvs" + pkg "kcl-lang.io/kpm/pkg/package" "kcl-lang.io/kpm/pkg/reporter" + "kcl-lang.io/kpm/pkg/semver" ) // NewUpdateCmd new a Command for `kpm update`. @@ -48,39 +57,163 @@ func KpmUpdate(c *cli.Context, kpmcli *client.KpmClient) error { } }() - input_paths := c.Args().Slice() + pkgInfos := c.Args().Slice() - pkg_paths := []string{} - if len(input_paths) == 0 { - pwd, err := os.Getwd() - if err != nil { - return reporter.NewErrorEvent(reporter.Bug, err, "internal bugs, please contact us to fix it") - } - pkg_paths = append(pkg_paths, pwd) - } else { - pkg_paths = input_paths + pwd, err := os.Getwd() + if err != nil { + return reporter.NewErrorEvent(reporter.Bug, err, "internal bugs, please contact us to fix it.") } - for _, pkg_path := range pkg_paths { - kclPkg, err := kpmcli.LoadPkgFromPath(pkg_path) - if err != nil { - return err + fetchNameAndVersion := func(pkgInfo string) (string, string) { + // the version is specified after : + if strings.Contains(pkgInfo, ":") { + return strings.Split(pkgInfo, ":")[0], strings.Split(pkgInfo, ":")[1] } + return pkgInfo, "" + } - globalPkgPath, err := env.GetAbsPkgPath() - if err != nil { - return err + kclPkg, err := kpmcli.LoadPkgFromPath(pwd) + if err != nil { + return err + } + + globalPkgPath, err := env.GetAbsPkgPath() + if err != nil { + return err + } + + err = kclPkg.ValidateKpmHome(globalPkgPath) + if err != (*reporter.KpmEvent)(nil) { + return err + } + + var ( + modulesToUpgrade []module.Version + modulesToDowngrade []module.Version + ) + + // validate if the packages is present in kcl.mod file and + // find the latest version if version is not specified + for _, pkgInfo := range pkgInfos { + pkgInfo = strings.TrimSpace(pkgInfo) + pkgName, pkgVersion := fetchNameAndVersion(pkgInfo) + + var dep pkg.Dependency + var ok bool + if dep, ok = kclPkg.Deps[pkgName]; !ok { + reporter.Report(fmt.Sprintf("package %s not found in kcl.mod file", pkgName)) + continue } - err = kclPkg.ValidateKpmHome(globalPkgPath) - if err != (*reporter.KpmEvent)(nil) { - return err + if pkgVersion == "" { + var releases []string + if dep.Git != nil { + releases, err = mvs.GetReleasesFromSource(dep.GetSourceType(), dep.Git.Url) + } else if dep.Oci != nil { + releases, err = mvs.GetReleasesFromSource(dep.GetSourceType(), "fjdkasd") + } + if err != nil { + reporter.ReportEventToStdout(reporter.NewEvent( + reporter.FailedGetReleases, + fmt.Sprintf("failed to get releases for %s", pkgName), + )) + continue + } + pkgVersion, err = semver.LatestCompatibleVersion(releases, dep.Version) + if err != nil { + reporter.NewErrorEvent( + reporter.FailedSelectLatestCompatibleVersion, + err, + fmt.Sprintf("failed to find the latest version for %s", pkgName), + ) + continue + } + } + if pkgVersion < dep.Version { + modulesToDowngrade = append(modulesToDowngrade, module.Version{Path: pkgName, Version: pkgVersion}) + } else if pkgVersion > dep.Version { + modulesToUpgrade = append(modulesToUpgrade, module.Version{Path: pkgName, Version: pkgVersion}) } + } + + _, depGraph, err := kpmcli.InitGraphAndDownloadDeps(kclPkg) + if err != nil { + return err + } + + reqs := mvs.ReqsGraph{ + Graph: depGraph, + KpmClient: kpmcli, + KpmPkg: kclPkg, + } + + target := module.Version{Path: kclPkg.GetPkgName(), Version: kclPkg.GetPkgVersion()} + buildList, err := UpdateBuildList(target, modulesToUpgrade, modulesToDowngrade, &reqs) + if err != nil { + return reporter.NewErrorEvent(reporter.FailedUpdatingBuildList, err, "failed to update build list") + } + + modules, err := graph.TopologicalSort(depGraph) + if err != nil { + return reporter.NewErrorEvent(reporter.FailedTopologicalSort, err, "failed to sort the dependencies") + } - err = kpmcli.UpdateDeps(kclPkg) + kclPkg.ModFile.Dependencies.Deps = make(map[string]pkg.Dependency) + + for _, module := range modules { + if module.Path == target.Path || !slices.Contains(buildList, module) { + continue + } + d := pkg.Dependency{ + Name: module.Path, + Version: module.Version, + } + d.FullName = d.GenDepFullName() + _, properties, err := depGraph.VertexWithProperties(module) if err != nil { - return err + return reporter.NewErrorEvent(reporter.FailedGetVertexProperties, err, "failed to get vertex with properties") + } + // there must be one property depending on the download source type + for sourceType, uri := range properties.Attributes { + d.Source, err = pkg.GenSource(sourceType, uri, module.Version) + if err != nil { + return reporter.NewErrorEvent(reporter.FailedGenerateSource, err, "failed to generate source") + } + // if d.Oci != nil && d.Oci.Reg == "ghcr.io" { + // d.Oci = nil + // } } + kclPkg.ModFile.Dependencies.Deps[module.Path] = d + } + + err = kpmcli.UpdateDeps(kclPkg) + if err != nil { + return err } return nil } + +func UpdateBuildList(target module.Version, modulesToUpgrade []module.Version, modulesToDowngrade []module.Version, reqs *mvs.ReqsGraph) ([]module.Version, error) { + var ( + UpdBuildLists []module.Version + err error + ) + + if len(modulesToUpgrade) == 0 { + UpdBuildLists, err = mvsExt.UpgradeAll(target, reqs) + } else { + UpdBuildLists, err = mvsExt.Upgrade(target, reqs, modulesToUpgrade...) + } + if err != nil { + return []module.Version{}, err + } + + if len(modulesToDowngrade) != 0 { + UpdBuildLists, err = mvsExt.Downgrade(target, reqs, modulesToDowngrade...) + } + if err != nil { + return []module.Version{}, err + } + + return UpdBuildLists, nil +} diff --git a/pkg/mvs/mvs.go b/pkg/mvs/mvs.go index fbd817c7..521e8e57 100644 --- a/pkg/mvs/mvs.go +++ b/pkg/mvs/mvs.go @@ -17,8 +17,8 @@ import ( type ReqsGraph struct { graph.Graph[module.Version, module.Version] - kpmClient *client.KpmClient - kpmPkg *pkg.KclPkg + KpmClient *client.KpmClient + KpmPkg *pkg.KclPkg } func (r ReqsGraph) Max(path, v1, v2 string) string { @@ -50,9 +50,17 @@ func (r ReqsGraph) Upgrade(m module.Version) (module.Version, error) { return module.Version{}, err } - releases, err := getReleasesFromSource(properties) - if err != nil { - return module.Version{}, err + // there must be only one property depending on the download source type + if len(properties.Attributes) != 1 { + return module.Version{}, errInt.MultipleSources + } + + var releases []string + for sourceType, uri := range properties.Attributes { + releases, err = GetReleasesFromSource(sourceType, uri) + if err != nil { + return module.Version{}, err + } } if releases == nil { @@ -84,7 +92,7 @@ func (r ReqsGraph) Upgrade(m module.Version) (module.Version, error) { lockDeps := pkg.Dependencies{ Deps: make(map[string]pkg.Dependency), } - _, err = r.kpmClient.DownloadDeps(&deps, &lockDeps, r.Graph, r.kpmPkg.HomePath, module.Version{}) + _, err = r.KpmClient.DownloadDeps(&deps, &lockDeps, r.Graph, r.KpmPkg.HomePath, module.Version{}) if err != nil { return module.Version{}, err } @@ -98,9 +106,17 @@ func (r ReqsGraph) Previous(m module.Version) (module.Version, error) { return module.Version{}, err } - releases, err := getReleasesFromSource(properties) - if err != nil { - return module.Version{}, err + // there must be only one property depending on the download source type + if len(properties.Attributes) != 1 { + return module.Version{}, errInt.MultipleSources + } + + var releases []string + for sourceType, uri := range properties.Attributes { + releases, err = GetReleasesFromSource(sourceType, uri) + if err != nil { + return module.Version{}, err + } } if releases == nil { @@ -140,7 +156,7 @@ func (r ReqsGraph) Previous(m module.Version) (module.Version, error) { lockDeps := pkg.Dependencies{ Deps: make(map[string]pkg.Dependency), } - _, err = r.kpmClient.DownloadDeps(&deps, &lockDeps, r.Graph, r.kpmPkg.HomePath, module.Version{}) + _, err = r.KpmClient.DownloadDeps(&deps, &lockDeps, r.Graph, r.KpmPkg.HomePath, module.Version{}) if err != nil { return module.Version{}, err } @@ -160,26 +176,19 @@ func (r ReqsGraph) Required(m module.Version) ([]module.Version, error) { return reqs, nil } -func getReleasesFromSource(properties graph.VertexProperties) ([]string, error) { +func GetReleasesFromSource(sourceType, uri string) ([]string, error) { var releases []string var err error - // there must be only one property depending on the download source type - if len(properties.Attributes) != 1 { - return nil, errInt.MultipleSources + switch sourceType { + case pkg.GIT: + releases, err = git.GetAllGithubReleases(uri) + case pkg.OCI: + releases, err = oci.GetAllImageTags(uri) } - - for k, v := range properties.Attributes { - switch k { - case pkg.GIT: - releases, err = git.GetAllGithubReleases(v) - case pkg.OCI: - releases, err = oci.GetAllImageTags(v) - } - if err != nil { - return nil, err - } + if err != nil { + return nil, err } return releases, nil -} +} \ No newline at end of file diff --git a/pkg/package/modfile.go b/pkg/package/modfile.go index 38946a04..fff7dbac 100644 --- a/pkg/package/modfile.go +++ b/pkg/package/modfile.go @@ -283,6 +283,11 @@ func GenSource(sourceType string, uri string, tagName string) (Source, error) { } source.Oci = &oci } + if sourceType == LOCAL { + source.Local = &Local{ + Path: uri, + } + } return source, nil } diff --git a/pkg/reporter/reporter.go b/pkg/reporter/reporter.go index 29e0ccf9..27af7912 100644 --- a/pkg/reporter/reporter.go +++ b/pkg/reporter/reporter.go @@ -55,6 +55,11 @@ const ( FailedLoadCredential FailedCreateOciClient FailedSelectLatestVersion + FailedSelectLatestCompatibleVersion + FailedGetReleases + FailedTopologicalSort + FailedGetVertexProperties + FailedGenerateSource FailedGetPackageVersions FailedCreateStorePath FailedPush @@ -81,6 +86,7 @@ const ( WithoutGitTag FailedCloneFromGit FailedHashPkg + FailedUpdatingBuildList Bug // normal event type means the event is a normal event. diff --git a/test/e2e/test_suites/kpm/kpm_update/test_update_with_diff_version/test_suite.stdout b/test/e2e/test_suites/kpm/kpm_update/test_update_with_diff_version/test_suite.stdout index 0c666431..f37f0853 100644 --- a/test/e2e/test_suites/kpm/kpm_update/test_update_with_diff_version/test_suite.stdout +++ b/test/e2e/test_suites/kpm/kpm_update/test_update_with_diff_version/test_suite.stdout @@ -1 +1 @@ -adding 'k8s' with version '1.14' \ No newline at end of file +downloading 'test/k8s:1.14' from 'localhost:5001/test/k8s:1.14' \ No newline at end of file