Skip to content

Commit

Permalink
Merge pull request #477 from zong-zhe/support-update-mvs
Browse files Browse the repository at this point in the history
feat: `kpm update` support mvs algorithm
  • Loading branch information
Peefy authored Sep 6, 2024
2 parents 4ebb01c + add6bc2 commit 69c541d
Show file tree
Hide file tree
Showing 54 changed files with 549 additions and 115 deletions.
25 changes: 11 additions & 14 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,20 +473,11 @@ func (c *KpmClient) UpdateDeps(kclPkg *pkg.KclPkg) error {
return err
}

// update kcl.mod
err = kclPkg.ModFile.StoreModFile()
if err != nil {
return err
}
_, err = c.Update(
WithUpdatedKclPkg(kclPkg),
)

// Generate file kcl.mod.lock.
if !kclPkg.NoSumCheck {
err := kclPkg.LockDepsVersion()
if err != nil {
return err
}
}
return nil
return err
}

// ResolveDepsMetadataInJsonStr will calculate the local storage path of the external package,
Expand Down Expand Up @@ -813,7 +804,12 @@ func (c *KpmClient) AddDepWithOpts(kclPkg *pkg.KclPkg, opt *opt.AddOptions) (*pk
kclPkg.Dependencies.Deps.Delete(d.Name)
}

err = kclPkg.UpdateModAndLockFile()
// After adding the new dependency,
// Iterate through all the dependencies and select the version by mvs
_, err = c.Update(
WithUpdatedKclPkg(kclPkg),
)

if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1191,6 +1187,7 @@ func (c *KpmClient) Download(dep *pkg.Dependency, homePath, localPath string) (*

if dep.Source.Registry != nil {
dep.Source.Registry.Tag = latestTag
dep.Source.Registry.Version = latestTag
}

// Complete some information that the local three dependencies depend on.
Expand Down
14 changes: 3 additions & 11 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,9 @@ func TestModandLockFilesWithGitPackageDownload(t *testing.T) {
LocalPath: testPkgPath,
RegistryOpts: opt.RegistryOptions{
Git: &opt.GitOptions{
Url: "https://github.com/kcl-lang/modules.git",
Commit: "ee03122b5f45b09eb48694422fc99a0772f6bba8",
Package: "agent",
Url: "https://github.com/kcl-lang/flask-demo-kcl-manifests.git",
Commit: "8308200",
Package: "cc",
},
},
}
Expand Down Expand Up @@ -256,14 +256,6 @@ func TestModandLockFilesWithGitPackageDownload(t *testing.T) {
fmt.Println(modLockContentStr)

assert.Equal(t, modLockExpectContentStr, modLockContentStr)

defer func() {
err = os.Truncate(testPkgPathMod, 0)
assert.Equal(t, err, nil)

err = os.Truncate(testPkgPathModLock, 0)
assert.Equal(t, err, nil)
}()
}

func TestDependencyGraph(t *testing.T) {
Expand Down
16 changes: 8 additions & 8 deletions pkg/client/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type ResolveOption func(*ResolveOptions) error

// resolveFunc is the function for resolving each dependency when traversing the dependency graph.
// currentPkg is the current package to be resolved and parentPkg is the parent package of the current package.
type resolveFunc func(currentPkg, parentPkg *pkg.KclPkg) error
type resolveFunc func(dep *pkg.Dependency, parentPkg *pkg.KclPkg) error

type ResolveOptions struct {
// Source is the source of the package to be pulled.
Expand Down Expand Up @@ -40,16 +40,16 @@ func WithCachePath(cachePath string) ResolveOption {
}
}

// WithPkgSource sets the source of the package to be resolved.
func WithPkgSource(source *downloader.Source) ResolveOption {
// WithResolveSource sets the source of the package to be resolved.
func WithResolveSource(source *downloader.Source) ResolveOption {
return func(opts *ResolveOptions) error {
opts.Source = source
return nil
}
}

// WithPkgSourceUrl sets the source of the package to be resolved by the source url.
func WithPkgSourceUrl(sourceUrl string) ResolveOption {
// WithResolveSourceUrl sets the source of the package to be resolved by the source url.
func WithResolveSourceUrl(sourceUrl string) ResolveOption {
return func(opts *ResolveOptions) error {
source, err := downloader.NewSourceFromStr(sourceUrl)
if err != nil {
Expand Down Expand Up @@ -106,7 +106,7 @@ func (dr *DepsResolver) Resolve(options ...ResolveOption) error {
}
return PkgVisitor, nil
} else {
return NewVisitor(*opts.Source, dr.kpmClient), nil
return NewVisitor(*source, dr.kpmClient), nil
}
}

Expand Down Expand Up @@ -143,7 +143,7 @@ func (dr *DepsResolver) Resolve(options ...ResolveOption) error {
err = visitor.Visit(&depSource,
func(childPkg *pkg.KclPkg) error {
for _, resolveFunc := range dr.resolveFuncs {
err := resolveFunc(childPkg, kclPkg)
err := resolveFunc(&dep, kclPkg)
if err != nil {
return err
}
Expand All @@ -158,7 +158,7 @@ func (dr *DepsResolver) Resolve(options ...ResolveOption) error {

// Recursively resolve the dependencies of the dependency.
err = dr.Resolve(
WithPkgSource(&depSource),
WithResolveSource(&depSource),
WithEnableCache(opts.EnableCache),
WithCachePath(opts.CachePath),
)
Expand Down
6 changes: 3 additions & 3 deletions pkg/client/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ func TestResolver(t *testing.T) {

kpmcli.SetLogWriter(&buf)
resolver := NewDepsResolver(kpmcli)
resolver.AddResolveFunc(func(currentPkg, parentPkg *pkg.KclPkg) error {
res = append(res, fmt.Sprintf("%s -> %s", parentPkg.GetPkgName(), currentPkg.GetPkgName()))
resolver.AddResolveFunc(func(dep *pkg.Dependency, parentPkg *pkg.KclPkg) error {
res = append(res, fmt.Sprintf("%s -> %s", parentPkg.GetPkgName(), dep.Name))
return nil
})

err = resolver.Resolve(
WithEnableCache(true),
WithPkgSource(pkgSource),
WithResolveSource(pkgSource),
)

if err != nil {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
[package]
name = "test_pkg"
edition = "v0.10.0"
version = "0.0.1"

[dependencies]
agent = { git = "https://github.com/kcl-lang/modules.git", commit = "ee03122b5f45b09eb48694422fc99a0772f6bba8", package = "agent" }
cc = { git = "https://github.com/kcl-lang/flask-demo-kcl-manifests.git", commit = "8308200", package = "cc" }
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
[dependencies]
[dependencies.agent]
name = "agent"
full_name = "modules_ee03122b5f45b09eb48694422fc99a0772f6bba8"
version = "0.1.0"
url = "https://github.com/kcl-lang/modules.git"
commit = "ee03122b5f45b09eb48694422fc99a0772f6bba8"
package = "agent"
[dependencies.k8s]
name = "k8s"
full_name = "k8s_1.28"
version = "1.28"
[dependencies.cc]
name = "cc"
full_name = "flask-demo-kcl-manifests_8308200"
version = "0.0.1"
url = "https://github.com/kcl-lang/flask-demo-kcl-manifests.git"
commit = "8308200"
package = "cc"
7 changes: 7 additions & 0 deletions pkg/client/test_data/test_mod_file_package/test_pkg/kcl.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "test_pkg"
edition = "v0.10.0"
version = "0.0.1"

[dependencies]
cc = { git = "https://github.com/kcl-lang/flask-demo-kcl-manifests.git", commit = "8308200", package = "cc" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[dependencies]
[dependencies.cc]
name = "cc"
full_name = "flask-demo-kcl-manifests_8308200"
version = "0.0.1"
url = "https://github.com/kcl-lang/flask-demo-kcl-manifests.git"
commit = "8308200"
package = "cc"
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
[package]
name = "test_pkg"
edition = "v0.10.0"
version = "0.0.1"

[dependencies]
agent = { git = "https://github.com/kcl-lang/modules.git", commit = "ee03122b5f45b09eb48694422fc99a0772f6bba8", package = "agent" }
cc = { git = "https://github.com/kcl-lang/flask-demo-kcl-manifests.git", commit = "8308200", package = "cc" }
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
[dependencies]
[dependencies.agent]
name = "agent"
full_name = "modules_ee03122b5f45b09eb48694422fc99a0772f6bba8"
version = "0.1.0"
url = "https://github.com/kcl-lang/modules.git"
commit = "ee03122b5f45b09eb48694422fc99a0772f6bba8"
package = "agent"
[dependencies.k8s]
name = "k8s"
full_name = "k8s_1.28"
version = "1.28"
[dependencies.cc]
name = "cc"
full_name = "flask-demo-kcl-manifests_8308200"
version = "0.0.1"
url = "https://github.com/kcl-lang/flask-demo-kcl-manifests.git"
commit = "8308200"
package = "cc"
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[package]
name = "test_pkg"
edition = "v0.10.0"
version = "0.0.1"
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "dep_0"
edition = "v0.10.0"
version = "0.0.1"

[dependencies]
helloworld = "0.1.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[dependencies]
[dependencies.helloworld]
name = "helloworld"
full_name = "helloworld_0.1.0"
version = "0.1.0"
sum = "aqrvSsd8zGHzRERbOzxYxARmK6QjvpQMYC1OqemdZvc="
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The_first_kcl_program = 'Hello World!'
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "dep_1"
edition = "v0.10.0"
version = "0.0.1"

[dependencies]
helloworld = "0.1.1"
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[dependencies]
[dependencies.helloworld]
name = "helloworld"
full_name = "helloworld_0.1.1"
version = "0.1.1"
sum = "7OO4YK2QuRWPq9C7KTzcWcti5yUnueCjptT3OXiPVeQ="
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The_first_kcl_program = 'Hello World!'
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "dep_2"
edition = "v0.10.0"
version = "0.0.1"

[dependencies]
helloworld = "0.1.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[dependencies]
[dependencies.helloworld]
name = "helloworld"
full_name = "helloworld_0.1.0"
version = "0.1.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The_first_kcl_program = 'Hello World!'
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "pkg"
edition = "v0.10.0"
version = "0.0.1"

[dependencies]
dep_0 = { path = "../dep_0" }
dep_1 = { path = "../dep_1" }
dep_2 = { path = "../dep_2" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "pkg"
edition = "v0.10.0"
version = "0.0.1"

[dependencies]
dep_0 = { path = "../dep_0" }
dep_1 = { path = "../dep_1" }
dep_2 = { path = "../dep_2" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[dependencies]
[dependencies.dep_0]
name = "dep_0"
full_name = "dep_0_0.0.1"
version = "0.0.1"
[dependencies.dep_1]
name = "dep_1"
full_name = "dep_1_0.0.1"
version = "0.0.1"
[dependencies.dep_2]
name = "dep_2"
full_name = "dep_2_0.0.1"
version = "0.0.1"
[dependencies.helloworld]
name = "helloworld"
full_name = "helloworld_0.1.1"
version = "0.1.1"
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[dependencies]
[dependencies.dep_0]
name = "dep_0"
full_name = "dep_0_0.0.1"
version = "0.0.1"
[dependencies.dep_1]
name = "dep_1"
full_name = "dep_1_0.0.1"
version = "0.0.1"
[dependencies.dep_2]
name = "dep_2"
full_name = "dep_2_0.0.1"
version = "0.0.1"
[dependencies.helloworld]
name = "helloworld"
full_name = "helloworld_0.1.1"
version = "0.1.1"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The_first_kcl_program = 'Hello World!'
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "dep_0"
edition = "v0.10.0"
version = "0.0.1"

[dependencies]
helloworld = "0.1.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[dependencies]
[dependencies.helloworld]
name = "helloworld"
full_name = "helloworld_0.1.0"
version = "0.1.0"
sum = "aqrvSsd8zGHzRERbOzxYxARmK6QjvpQMYC1OqemdZvc="
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The_first_kcl_program = 'Hello World!'
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "dep_1"
edition = "v0.10.0"
version = "0.0.1"

[dependencies]
helloworld = "0.1.1"
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[dependencies]
[dependencies.helloworld]
name = "helloworld"
full_name = "helloworld_0.1.1"
version = "0.1.1"
sum = "7OO4YK2QuRWPq9C7KTzcWcti5yUnueCjptT3OXiPVeQ="
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The_first_kcl_program = 'Hello World!'
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "dep_2"
edition = "v0.10.0"
version = "0.0.1"

[dependencies]
helloworld = "0.1.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[dependencies]
[dependencies.helloworld]
name = "helloworld"
full_name = "helloworld_0.1.0"
version = "0.1.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The_first_kcl_program = 'Hello World!'
Loading

0 comments on commit 69c541d

Please sign in to comment.