From 32df960efa6351062062057a2bd7af7f49c7c734 Mon Sep 17 00:00:00 2001 From: zongz Date: Thu, 6 Jun 2024 15:52:05 +0800 Subject: [PATCH] feat: remove the replace for default registry dep Signed-off-by: zongz --- pkg/api/kpm_pkg.go | 6 +- pkg/api/kpm_pkg_test.go | 6 +- pkg/client/client.go | 32 +++- pkg/client/client_test.go | 179 ++++++++++++++++++ .../add_with_default_dep/kcl.mod.bak | 4 + .../add_with_default_dep/kcl.mod.expect | 7 + .../add_with_default_dep/kcl.mod.lock.bak | 0 .../add_with_default_dep/kcl.mod.lock.expect | 5 + .../test_data/add_with_default_dep/main.k | 1 + .../run_with_default_dep/kcl.mod.bak | 7 + .../run_with_default_dep/kcl.mod.expect | 7 + .../run_with_default_dep/kcl.mod.lock.bak | 0 .../run_with_default_dep/kcl.mod.lock.expect | 5 + .../test_data/run_with_default_dep/main.k | 3 + .../no_sum_check/kcl.mod.expect | 2 +- .../with_sum_check/kcl.mod.expect | 2 +- .../with_sum_check/kcl.mod.lock.bak | 8 - .../with_sum_check/kcl.mod.lock.expect | 3 - .../test_update/test_update_kcl_mod/expected | 3 - .../update_with_default_dep/kcl.mod.bak | 7 + .../update_with_default_dep/kcl.mod.expect | 7 + .../update_with_default_dep/kcl.mod.lock.bak | 0 .../kcl.mod.lock.expect | 5 + .../test_data/update_with_default_dep/main.k | 1 + pkg/cmd/cmd_add.go | 4 +- pkg/downloader/downloader.go | 5 +- pkg/opt/opt.go | 9 +- pkg/opt/opt_test.go | 6 +- pkg/package/modfile.go | 29 ++- pkg/package/modfile_test.go | 2 +- pkg/package/package_test.go | 2 +- .../test_oci_url/marshal_1/expect.mod | 2 +- .../test_data/test_oci_url/marshal_1/kcl.mod | 2 +- .../test_oci_url/marshal_2/expect.mod | 4 +- .../test_data/test_oci_url/marshal_2/kcl.mod | 2 +- .../test_oci_url/marshal_3/expect.mod | 2 +- .../test_data/test_oci_url/marshal_3/kcl.mod | 2 +- pkg/package/toml.go | 50 ++++- pkg/package/toml_test.go | 16 +- 39 files changed, 377 insertions(+), 60 deletions(-) create mode 100644 pkg/client/test_data/add_with_default_dep/kcl.mod.bak create mode 100644 pkg/client/test_data/add_with_default_dep/kcl.mod.expect create mode 100644 pkg/client/test_data/add_with_default_dep/kcl.mod.lock.bak create mode 100644 pkg/client/test_data/add_with_default_dep/kcl.mod.lock.expect create mode 100644 pkg/client/test_data/add_with_default_dep/main.k create mode 100644 pkg/client/test_data/run_with_default_dep/kcl.mod.bak create mode 100644 pkg/client/test_data/run_with_default_dep/kcl.mod.expect create mode 100644 pkg/client/test_data/run_with_default_dep/kcl.mod.lock.bak create mode 100644 pkg/client/test_data/run_with_default_dep/kcl.mod.lock.expect create mode 100644 pkg/client/test_data/run_with_default_dep/main.k create mode 100644 pkg/client/test_data/update_with_default_dep/kcl.mod.bak create mode 100644 pkg/client/test_data/update_with_default_dep/kcl.mod.expect create mode 100644 pkg/client/test_data/update_with_default_dep/kcl.mod.lock.bak create mode 100644 pkg/client/test_data/update_with_default_dep/kcl.mod.lock.expect create mode 100644 pkg/client/test_data/update_with_default_dep/main.k diff --git a/pkg/api/kpm_pkg.go b/pkg/api/kpm_pkg.go index e48355ad..7d649b81 100644 --- a/pkg/api/kpm_pkg.go +++ b/pkg/api/kpm_pkg.go @@ -46,7 +46,11 @@ func NewKclTypes(name, path string, tys *gpyrpc.KclType) *KclType { // // 'kcl_pkg_path' is the path of dependencies download by kpm. func GetKclPackage(pkgPath string) (*KclPackage, error) { - kclPkg, err := pkg.LoadKclPkg(pkgPath) + kpmcli, err := client.NewKpmClient() + if err != nil { + return nil, err + } + kclPkg, err := kpmcli.LoadPkgFromPath(pkgPath) if err != nil { return nil, err } diff --git a/pkg/api/kpm_pkg_test.go b/pkg/api/kpm_pkg_test.go index 35c30134..c90bed25 100644 --- a/pkg/api/kpm_pkg_test.go +++ b/pkg/api/kpm_pkg_test.go @@ -68,9 +68,9 @@ func TestApiGetDependenciesInModFile(t *testing.T) { assert.Equal(t, dep.Name, "k8s") assert.Equal(t, dep.FullName, "k8s_1.27") assert.Equal(t, dep.Version, "1.27") - assert.Equal(t, dep.Source.Oci.Reg, "ghcr.io") - assert.Equal(t, dep.Source.Oci.Repo, "kcl-lang/k8s") - assert.Equal(t, dep.Source.Oci.Tag, "1.27") + assert.Equal(t, dep.Source.Registry.Oci.Reg, "ghcr.io") + assert.Equal(t, dep.Source.Registry.Oci.Repo, "kcl-lang/k8s") + assert.Equal(t, dep.Source.Registry.Oci.Tag, "1.27") } func TestGetAllSchemaTypesMappingNamed(t *testing.T) { diff --git a/pkg/client/client.go b/pkg/client/client.go index ef192745..61c1c86a 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -256,6 +256,8 @@ func (c *KpmClient) getDepStorePath(search_path string, d *pkg.Dependency, isVen } else { storePkgName = fmt.Sprintf(PKG_NAME_PATTERN, d.Name, d.Source.Git.Branch) } + } else if d.Source.Registry != nil { + storePkgName = fmt.Sprintf(PKG_NAME_PATTERN, d.Name, d.Source.Registry.Version) } else { storePkgName = fmt.Sprintf(PKG_NAME_PATTERN, d.Name, d.Version) } @@ -891,6 +893,18 @@ func (c *KpmClient) FillDepInfo(dep *pkg.Dependency, homepath string) error { dep.Source.Oci.Repo = urlpath } } + if dep.Source.Registry != nil { + if len(dep.Source.Registry.Reg) == 0 { + dep.Source.Registry.Reg = c.GetSettings().DefaultOciRegistry() + } + + if len(dep.Source.Registry.Repo) == 0 { + urlpath := utils.JoinPath(c.GetSettings().DefaultOciRepo(), dep.Name) + dep.Source.Registry.Repo = urlpath + } + + dep.Version = dep.Source.Registry.Version + } return nil } @@ -946,14 +960,24 @@ func (c *KpmClient) Download(dep *pkg.Dependency, homePath, localPath string) (* dep.Version = modFile.Pkg.Version } - if dep.Source.Oci != nil { + if dep.Source.Oci != nil || dep.Source.Registry != nil { + var ociSource *pkg.Oci + if dep.Source.Oci != nil { + ociSource = dep.Source.Oci + } else if dep.Source.Registry != nil { + ociSource = dep.Source.Registry.Oci + } // Select the latest tag, if the tag, the user inputed, is empty. - if dep.Source.Oci.Tag == "" || dep.Source.Oci.Tag == constants.LATEST { - latestTag, err := c.AcquireTheLatestOciVersion(*dep.Source.Oci) + if ociSource.Tag == "" || ociSource.Tag == constants.LATEST { + latestTag, err := c.AcquireTheLatestOciVersion(*ociSource) if err != nil { return nil, err } - dep.Source.Oci.Tag = latestTag + ociSource.Tag = latestTag + + if dep.Source.Registry != nil { + dep.Source.Registry.Tag = latestTag + } // Complete some information that the local three dependencies depend on. // The invalid path such as '$HOME/.kcl/kpm/k8s_' is placed because the version field is missing. diff --git a/pkg/client/client_test.go b/pkg/client/client_test.go index 3b2bd3f3..dee915fe 100644 --- a/pkg/client/client_test.go +++ b/pkg/client/client_test.go @@ -1444,6 +1444,9 @@ func TestOciDownloader(t *testing.T) { // make test case running in order to test the log output testRunWithOciDownloader(t) testAddWithOciDownloader(t) + testAddDefaultRegistryDep(t) + testUpdateDefaultRegistryDep(t) + testRunDefaultRegistryDep(t) } func testAddWithOciDownloader(t *testing.T) { @@ -1540,3 +1543,179 @@ func TestAddLocalPath(t *testing.T) { _ = os.Remove(filepath.Join(path, "kcl.mod")) }() } + +func testAddDefaultRegistryDep(t *testing.T) { + pkgPath := getTestDir("add_with_default_dep") + + pkgWithSumCheckPathModBak := filepath.Join(pkgPath, "kcl.mod.bak") + pkgWithSumCheckPathMod := filepath.Join(pkgPath, "kcl.mod") + pkgWithSumCheckPathModExpect := filepath.Join(pkgPath, "kcl.mod.expect") + + pkgWithSumCheckPathModLockBak := filepath.Join(pkgPath, "kcl.mod.lock.bak") + pkgWithSumCheckPathModLock := filepath.Join(pkgPath, "kcl.mod.lock") + pkgWithSumCheckPathModLockExpect := filepath.Join(pkgPath, "kcl.mod.lock.expect") + + err := copy.Copy(pkgWithSumCheckPathModBak, pkgWithSumCheckPathMod) + assert.Equal(t, err, nil) + err = copy.Copy(pkgWithSumCheckPathModLockBak, pkgWithSumCheckPathModLock) + assert.Equal(t, err, nil) + + kpmcli, err := NewKpmClient() + assert.Equal(t, err, nil) + + kclPkg, err := kpmcli.LoadPkgFromPath(pkgPath) + assert.Equal(t, err, nil) + + opts := opt.AddOptions{ + LocalPath: pkgPath, + RegistryOpts: opt.RegistryOptions{ + Registry: &opt.OciOptions{ + Reg: "ghcr.io", + Repo: "kcl-lang/helloworld", + PkgName: "helloworld", + Tag: "0.1.2", + }, + }, + } + + _, err = kpmcli.AddDepWithOpts(kclPkg, &opts) + assert.Equal(t, err, nil) + + modContent, err := os.ReadFile(pkgWithSumCheckPathMod) + modContentStr := strings.ReplaceAll(string(modContent), "\r\n", "") + modContentStr = strings.ReplaceAll(modContentStr, "\n", "") + assert.Equal(t, err, nil) + + modExpectContent, err := os.ReadFile(pkgWithSumCheckPathModExpect) + modExpectContentStr := strings.ReplaceAll(string(modExpectContent), "\r\n", "") + modExpectContentStr = strings.ReplaceAll(modExpectContentStr, "\n", "") + + assert.Equal(t, err, nil) + assert.Equal(t, modContentStr, modExpectContentStr) + + modLockContent, err := os.ReadFile(pkgWithSumCheckPathModLock) + modLockContentStr := strings.ReplaceAll(string(modLockContent), "\r\n", "") + modLockContentStr = strings.ReplaceAll(modLockContentStr, "\n", "") + assert.Equal(t, err, nil) + modLockExpectContent, err := os.ReadFile(pkgWithSumCheckPathModLockExpect) + modLockExpectContentStr := strings.ReplaceAll(string(modLockExpectContent), "\r\n", "") + modLockExpectContentStr = strings.ReplaceAll(modLockExpectContentStr, "\n", "") + assert.Equal(t, err, nil) + assert.Equal(t, modLockContentStr, modLockExpectContentStr) + + defer func() { + _ = os.Remove(pkgWithSumCheckPathMod) + _ = os.Remove(pkgWithSumCheckPathModLock) + }() +} + +func testUpdateDefaultRegistryDep(t *testing.T) { + pkgPath := getTestDir("update_with_default_dep") + + pkgWithSumCheckPathModBak := filepath.Join(pkgPath, "kcl.mod.bak") + pkgWithSumCheckPathMod := filepath.Join(pkgPath, "kcl.mod") + pkgWithSumCheckPathModExpect := filepath.Join(pkgPath, "kcl.mod.expect") + + pkgWithSumCheckPathModLockBak := filepath.Join(pkgPath, "kcl.mod.lock.bak") + pkgWithSumCheckPathModLock := filepath.Join(pkgPath, "kcl.mod.lock") + pkgWithSumCheckPathModLockExpect := filepath.Join(pkgPath, "kcl.mod.lock.expect") + + err := copy.Copy(pkgWithSumCheckPathModBak, pkgWithSumCheckPathMod) + assert.Equal(t, err, nil) + err = copy.Copy(pkgWithSumCheckPathModLockBak, pkgWithSumCheckPathModLock) + assert.Equal(t, err, nil) + + kpmcli, err := NewKpmClient() + assert.Equal(t, err, nil) + + kclPkg, err := kpmcli.LoadPkgFromPath(pkgPath) + assert.Equal(t, err, nil) + + err = kpmcli.UpdateDeps(kclPkg) + assert.Equal(t, err, nil) + + modContent, err := os.ReadFile(pkgWithSumCheckPathMod) + modContentStr := strings.ReplaceAll(string(modContent), "\r\n", "") + modContentStr = strings.ReplaceAll(modContentStr, "\n", "") + assert.Equal(t, err, nil) + + modExpectContent, err := os.ReadFile(pkgWithSumCheckPathModExpect) + modExpectContentStr := strings.ReplaceAll(string(modExpectContent), "\r\n", "") + modExpectContentStr = strings.ReplaceAll(modExpectContentStr, "\n", "") + + assert.Equal(t, err, nil) + assert.Equal(t, modContentStr, modExpectContentStr) + + modLockContent, err := os.ReadFile(pkgWithSumCheckPathModLock) + modLockContentStr := strings.ReplaceAll(string(modLockContent), "\r\n", "") + modLockContentStr = strings.ReplaceAll(modLockContentStr, "\n", "") + assert.Equal(t, err, nil) + modLockExpectContent, err := os.ReadFile(pkgWithSumCheckPathModLockExpect) + modLockExpectContentStr := strings.ReplaceAll(string(modLockExpectContent), "\r\n", "") + modLockExpectContentStr = strings.ReplaceAll(modLockExpectContentStr, "\n", "") + assert.Equal(t, err, nil) + assert.Equal(t, modLockContentStr, modLockExpectContentStr) + + defer func() { + _ = os.Remove(pkgWithSumCheckPathMod) + _ = os.Remove(pkgWithSumCheckPathModLock) + }() +} + +func testRunDefaultRegistryDep(t *testing.T) { + pkgPath := getTestDir("run_with_default_dep") + + pkgWithSumCheckPathModBak := filepath.Join(pkgPath, "kcl.mod.bak") + pkgWithSumCheckPathMod := filepath.Join(pkgPath, "kcl.mod") + pkgWithSumCheckPathModExpect := filepath.Join(pkgPath, "kcl.mod.expect") + + pkgWithSumCheckPathModLockBak := filepath.Join(pkgPath, "kcl.mod.lock.bak") + pkgWithSumCheckPathModLock := filepath.Join(pkgPath, "kcl.mod.lock") + pkgWithSumCheckPathModLockExpect := filepath.Join(pkgPath, "kcl.mod.lock.expect") + + err := copy.Copy(pkgWithSumCheckPathModBak, pkgWithSumCheckPathMod) + assert.Equal(t, err, nil) + err = copy.Copy(pkgWithSumCheckPathModLockBak, pkgWithSumCheckPathModLock) + assert.Equal(t, err, nil) + + kpmcli, err := NewKpmClient() + assert.Equal(t, err, nil) + + kclPkg, err := kpmcli.LoadPkgFromPath(pkgPath) + assert.Equal(t, err, nil) + + opts := opt.DefaultCompileOptions() + opts.Merge(kcl.WithWorkDir(pkgPath)).Merge(kcl.WithKFilenames(filepath.Join(pkgPath, "main.k"))) + compiler := runner.NewCompilerWithOpts(opts) + + res, err := kpmcli.Compile(kclPkg, compiler) + assert.Equal(t, err, nil) + assert.Equal(t, res.GetRawYamlResult(), "a: Hello World!") + + modContent, err := os.ReadFile(pkgWithSumCheckPathMod) + modContentStr := strings.ReplaceAll(string(modContent), "\r\n", "") + modContentStr = strings.ReplaceAll(modContentStr, "\n", "") + assert.Equal(t, err, nil) + + modExpectContent, err := os.ReadFile(pkgWithSumCheckPathModExpect) + modExpectContentStr := strings.ReplaceAll(string(modExpectContent), "\r\n", "") + modExpectContentStr = strings.ReplaceAll(modExpectContentStr, "\n", "") + + assert.Equal(t, err, nil) + assert.Equal(t, modContentStr, modExpectContentStr) + + modLockContent, err := os.ReadFile(pkgWithSumCheckPathModLock) + modLockContentStr := strings.ReplaceAll(string(modLockContent), "\r\n", "") + modLockContentStr = strings.ReplaceAll(modLockContentStr, "\n", "") + assert.Equal(t, err, nil) + modLockExpectContent, err := os.ReadFile(pkgWithSumCheckPathModLockExpect) + modLockExpectContentStr := strings.ReplaceAll(string(modLockExpectContent), "\r\n", "") + modLockExpectContentStr = strings.ReplaceAll(modLockExpectContentStr, "\n", "") + assert.Equal(t, err, nil) + assert.Equal(t, modLockContentStr, modLockExpectContentStr) + + defer func() { + _ = os.Remove(pkgWithSumCheckPathMod) + _ = os.Remove(pkgWithSumCheckPathModLock) + }() +} diff --git a/pkg/client/test_data/add_with_default_dep/kcl.mod.bak b/pkg/client/test_data/add_with_default_dep/kcl.mod.bak new file mode 100644 index 00000000..4b51ff95 --- /dev/null +++ b/pkg/client/test_data/add_with_default_dep/kcl.mod.bak @@ -0,0 +1,4 @@ +[package] +name = "add_with_default" +edition = "v0.9.0" +version = "0.0.1" diff --git a/pkg/client/test_data/add_with_default_dep/kcl.mod.expect b/pkg/client/test_data/add_with_default_dep/kcl.mod.expect new file mode 100644 index 00000000..f82e4edb --- /dev/null +++ b/pkg/client/test_data/add_with_default_dep/kcl.mod.expect @@ -0,0 +1,7 @@ +[package] +name = "add_with_default" +edition = "v0.9.0" +version = "0.0.1" + +[dependencies] +helloworld = "0.1.2" diff --git a/pkg/client/test_data/add_with_default_dep/kcl.mod.lock.bak b/pkg/client/test_data/add_with_default_dep/kcl.mod.lock.bak new file mode 100644 index 00000000..e69de29b diff --git a/pkg/client/test_data/add_with_default_dep/kcl.mod.lock.expect b/pkg/client/test_data/add_with_default_dep/kcl.mod.lock.expect new file mode 100644 index 00000000..e64da0b4 --- /dev/null +++ b/pkg/client/test_data/add_with_default_dep/kcl.mod.lock.expect @@ -0,0 +1,5 @@ +[dependencies] + [dependencies.helloworld] + name = "helloworld" + full_name = "helloworld_0.1.2" + version = "0.1.2" diff --git a/pkg/client/test_data/add_with_default_dep/main.k b/pkg/client/test_data/add_with_default_dep/main.k new file mode 100644 index 00000000..fa7048e6 --- /dev/null +++ b/pkg/client/test_data/add_with_default_dep/main.k @@ -0,0 +1 @@ +The_first_kcl_program = 'Hello World!' \ No newline at end of file diff --git a/pkg/client/test_data/run_with_default_dep/kcl.mod.bak b/pkg/client/test_data/run_with_default_dep/kcl.mod.bak new file mode 100644 index 00000000..f82e4edb --- /dev/null +++ b/pkg/client/test_data/run_with_default_dep/kcl.mod.bak @@ -0,0 +1,7 @@ +[package] +name = "add_with_default" +edition = "v0.9.0" +version = "0.0.1" + +[dependencies] +helloworld = "0.1.2" diff --git a/pkg/client/test_data/run_with_default_dep/kcl.mod.expect b/pkg/client/test_data/run_with_default_dep/kcl.mod.expect new file mode 100644 index 00000000..f82e4edb --- /dev/null +++ b/pkg/client/test_data/run_with_default_dep/kcl.mod.expect @@ -0,0 +1,7 @@ +[package] +name = "add_with_default" +edition = "v0.9.0" +version = "0.0.1" + +[dependencies] +helloworld = "0.1.2" diff --git a/pkg/client/test_data/run_with_default_dep/kcl.mod.lock.bak b/pkg/client/test_data/run_with_default_dep/kcl.mod.lock.bak new file mode 100644 index 00000000..e69de29b diff --git a/pkg/client/test_data/run_with_default_dep/kcl.mod.lock.expect b/pkg/client/test_data/run_with_default_dep/kcl.mod.lock.expect new file mode 100644 index 00000000..e64da0b4 --- /dev/null +++ b/pkg/client/test_data/run_with_default_dep/kcl.mod.lock.expect @@ -0,0 +1,5 @@ +[dependencies] + [dependencies.helloworld] + name = "helloworld" + full_name = "helloworld_0.1.2" + version = "0.1.2" diff --git a/pkg/client/test_data/run_with_default_dep/main.k b/pkg/client/test_data/run_with_default_dep/main.k new file mode 100644 index 00000000..4d47610f --- /dev/null +++ b/pkg/client/test_data/run_with_default_dep/main.k @@ -0,0 +1,3 @@ +import helloworld as h + +a = h.The_first_kcl_program \ No newline at end of file diff --git a/pkg/client/test_data/test_add_diff_version/no_sum_check/kcl.mod.expect b/pkg/client/test_data/test_add_diff_version/no_sum_check/kcl.mod.expect index 15ebd0f4..80c5c43d 100644 --- a/pkg/client/test_data/test_add_diff_version/no_sum_check/kcl.mod.expect +++ b/pkg/client/test_data/test_add_diff_version/no_sum_check/kcl.mod.expect @@ -4,4 +4,4 @@ edition = "0.0.1" version = "0.0.1" [dependencies] -helloworld = { oci = "oci://ghcr.io/kcl-lang/helloworld", tag = "0.1.2" } +helloworld = "0.1.2" diff --git a/pkg/client/test_data/test_add_diff_version/with_sum_check/kcl.mod.expect b/pkg/client/test_data/test_add_diff_version/with_sum_check/kcl.mod.expect index 8a5d1e7a..12148360 100644 --- a/pkg/client/test_data/test_add_diff_version/with_sum_check/kcl.mod.expect +++ b/pkg/client/test_data/test_add_diff_version/with_sum_check/kcl.mod.expect @@ -4,4 +4,4 @@ edition = "0.0.1" version = "0.0.1" [dependencies] -helloworld = { oci = "oci://ghcr.io/kcl-lang/helloworld", tag = "0.1.2" } +helloworld = "0.1.2" diff --git a/pkg/client/test_data/test_add_diff_version/with_sum_check/kcl.mod.lock.bak b/pkg/client/test_data/test_add_diff_version/with_sum_check/kcl.mod.lock.bak index c1bebf8c..e69de29b 100644 --- a/pkg/client/test_data/test_add_diff_version/with_sum_check/kcl.mod.lock.bak +++ b/pkg/client/test_data/test_add_diff_version/with_sum_check/kcl.mod.lock.bak @@ -1,8 +0,0 @@ -[dependencies] - [dependencies.helloworld] - name = "helloworld" - full_name = "helloworld_0.1.2" - version = "0.1.2" - reg = "ghcr.io" - repo = "kcl-lang/helloworld" - oci_tag = "0.1.2" diff --git a/pkg/client/test_data/test_add_diff_version/with_sum_check/kcl.mod.lock.expect b/pkg/client/test_data/test_add_diff_version/with_sum_check/kcl.mod.lock.expect index c1bebf8c..e64da0b4 100644 --- a/pkg/client/test_data/test_add_diff_version/with_sum_check/kcl.mod.lock.expect +++ b/pkg/client/test_data/test_add_diff_version/with_sum_check/kcl.mod.lock.expect @@ -3,6 +3,3 @@ name = "helloworld" full_name = "helloworld_0.1.2" version = "0.1.2" - reg = "ghcr.io" - repo = "kcl-lang/helloworld" - oci_tag = "0.1.2" diff --git a/pkg/client/test_data/test_update/test_update_kcl_mod/expected b/pkg/client/test_data/test_update/test_update_kcl_mod/expected index c1bebf8c..e64da0b4 100644 --- a/pkg/client/test_data/test_update/test_update_kcl_mod/expected +++ b/pkg/client/test_data/test_update/test_update_kcl_mod/expected @@ -3,6 +3,3 @@ name = "helloworld" full_name = "helloworld_0.1.2" version = "0.1.2" - reg = "ghcr.io" - repo = "kcl-lang/helloworld" - oci_tag = "0.1.2" diff --git a/pkg/client/test_data/update_with_default_dep/kcl.mod.bak b/pkg/client/test_data/update_with_default_dep/kcl.mod.bak new file mode 100644 index 00000000..f82e4edb --- /dev/null +++ b/pkg/client/test_data/update_with_default_dep/kcl.mod.bak @@ -0,0 +1,7 @@ +[package] +name = "add_with_default" +edition = "v0.9.0" +version = "0.0.1" + +[dependencies] +helloworld = "0.1.2" diff --git a/pkg/client/test_data/update_with_default_dep/kcl.mod.expect b/pkg/client/test_data/update_with_default_dep/kcl.mod.expect new file mode 100644 index 00000000..f82e4edb --- /dev/null +++ b/pkg/client/test_data/update_with_default_dep/kcl.mod.expect @@ -0,0 +1,7 @@ +[package] +name = "add_with_default" +edition = "v0.9.0" +version = "0.0.1" + +[dependencies] +helloworld = "0.1.2" diff --git a/pkg/client/test_data/update_with_default_dep/kcl.mod.lock.bak b/pkg/client/test_data/update_with_default_dep/kcl.mod.lock.bak new file mode 100644 index 00000000..e69de29b diff --git a/pkg/client/test_data/update_with_default_dep/kcl.mod.lock.expect b/pkg/client/test_data/update_with_default_dep/kcl.mod.lock.expect new file mode 100644 index 00000000..e64da0b4 --- /dev/null +++ b/pkg/client/test_data/update_with_default_dep/kcl.mod.lock.expect @@ -0,0 +1,5 @@ +[dependencies] + [dependencies.helloworld] + name = "helloworld" + full_name = "helloworld_0.1.2" + version = "0.1.2" diff --git a/pkg/client/test_data/update_with_default_dep/main.k b/pkg/client/test_data/update_with_default_dep/main.k new file mode 100644 index 00000000..fa7048e6 --- /dev/null +++ b/pkg/client/test_data/update_with_default_dep/main.k @@ -0,0 +1 @@ +The_first_kcl_program = 'Hello World!' \ No newline at end of file diff --git a/pkg/cmd/cmd_add.go b/pkg/cmd/cmd_add.go index c68ca7d1..f0786759 100644 --- a/pkg/cmd/cmd_add.go +++ b/pkg/cmd/cmd_add.go @@ -216,7 +216,7 @@ func parseGitRegistryOptions(c *cli.Context) (*opt.RegistryOptions, *reporter.Kp } // parseOciRegistryOptions will parse the oci registry information from user cli inputs. -func parseOciRegistryOptions(c *cli.Context, kpmcli *client.KpmClient) (*opt.RegistryOptions, error) { +func parseRegistryOptions(c *cli.Context, kpmcli *client.KpmClient) (*opt.RegistryOptions, error) { ociPkgRef := c.Args().First() name, version, err := opt.ParseOciPkgNameAndVersion(ociPkgRef) if err != nil { @@ -224,7 +224,7 @@ func parseOciRegistryOptions(c *cli.Context, kpmcli *client.KpmClient) (*opt.Reg } return &opt.RegistryOptions{ - Oci: &opt.OciOptions{ + Registry: &opt.OciOptions{ Reg: kpmcli.GetSettings().DefaultOciRegistry(), Repo: kpmcli.GetSettings().DefaultOciRepo(), PkgName: name, diff --git a/pkg/downloader/downloader.go b/pkg/downloader/downloader.go index e0b4a4a5..516fecbd 100644 --- a/pkg/downloader/downloader.go +++ b/pkg/downloader/downloader.go @@ -89,7 +89,10 @@ func NewOciDownloader(platform string) *DepDownloader { func (d *DepDownloader) Download(opts DownloadOptions) error { // Dispatch the download to the specific downloader by package source. - if opts.Source.Oci != nil { + if opts.Source.Oci != nil || opts.Source.Registry != nil { + if opts.Source.Registry != nil { + opts.Source.Oci = opts.Source.Registry.Oci + } if d.OciDownloader == nil { d.OciDownloader = &OciDownloader{} } diff --git a/pkg/opt/opt.go b/pkg/opt/opt.go index 41dc6e9c..9c863ca4 100644 --- a/pkg/opt/opt.go +++ b/pkg/opt/opt.go @@ -190,9 +190,10 @@ func (opts *AddOptions) Validate() error { } type RegistryOptions struct { - Git *GitOptions - Oci *OciOptions - Local *LocalOptions + Git *GitOptions + Oci *OciOptions + Local *LocalOptions + Registry *OciOptions } // NewRegistryOptionsFrom will parse the registry options from oci url, oci ref and git url. @@ -258,7 +259,7 @@ func NewRegistryOptionsFrom(rawUrlorOciRef string, settings *settings.Settings) if ociOptions != nil { return &RegistryOptions{ - Oci: ociOptions, + Registry: ociOptions, }, nil } diff --git a/pkg/opt/opt_test.go b/pkg/opt/opt_test.go index f8e5961f..6a602ae4 100644 --- a/pkg/opt/opt_test.go +++ b/pkg/opt/opt_test.go @@ -38,9 +38,9 @@ func TestNewRegistryOptionsFromRef(t *testing.T) { settings := settings.GetSettings() opts, err := NewRegistryOptionsFrom(ref, settings) assert.Equal(t, err, nil) - assert.Equal(t, opts.Oci.Tag, "latest") - assert.Equal(t, opts.Oci.Repo, "kcl-lang/test") - assert.Equal(t, opts.Oci.Reg, "ghcr.io") + assert.Equal(t, opts.Registry.Tag, "latest") + assert.Equal(t, opts.Registry.Repo, "kcl-lang/test") + assert.Equal(t, opts.Registry.Reg, "ghcr.io") opts, err = NewRegistryOptionsFrom("oci://docker.io/kcllang/test1?tag=0.0.1", settings) assert.Equal(t, err, nil) diff --git a/pkg/package/modfile.go b/pkg/package/modfile.go index 824182aa..f4baa86c 100644 --- a/pkg/package/modfile.go +++ b/pkg/package/modfile.go @@ -277,6 +277,9 @@ func (dep *Dependency) GetDownloadPath() string { if dep.Source.Oci != nil { return dep.Source.Oci.IntoOciUrl() } + if dep.Source.Registry != nil { + return dep.Source.Registry.Oci.IntoOciUrl() + } return "" } @@ -310,7 +313,7 @@ func (dep *Dependency) GetSourceType() string { if dep.Source.Git != nil { return GIT } - if dep.Source.Oci != nil { + if dep.Source.Oci != nil || dep.Source.Registry != nil { return OCI } if dep.Source.Local != nil { @@ -321,6 +324,7 @@ func (dep *Dependency) GetSourceType() string { // Source is the package source from registry. type Source struct { + *Registry *Git *Oci *Local `toml:"-"` @@ -336,6 +340,11 @@ type Oci struct { Tag string `toml:"oci_tag,omitempty"` } +type Registry struct { + *Oci `toml:"-"` + Version string `toml:"-"` +} + func (oci *Oci) IntoOciUrl() string { if oci != nil { u := &url.URL{ @@ -590,7 +599,25 @@ func ParseOpt(opt *opt.RegistryOptions) (*Dependency, error) { }, Version: depPkg.ModFile.Pkg.Version, }, nil + } + if opt.Registry != nil { + ociSource := Oci{ + Reg: opt.Registry.Reg, + Repo: opt.Registry.Repo, + Tag: opt.Registry.Tag, + } + return &Dependency{ + Name: opt.Registry.PkgName, + FullName: opt.Registry.PkgName + "_" + opt.Registry.Tag, + Source: Source{ + Registry: &Registry{ + Oci: &ociSource, + Version: opt.Registry.Tag, + }, + }, + Version: opt.Registry.Tag, + }, nil } return nil, nil } diff --git a/pkg/package/modfile_test.go b/pkg/package/modfile_test.go index ffe242f4..71acb659 100644 --- a/pkg/package/modfile_test.go +++ b/pkg/package/modfile_test.go @@ -178,7 +178,7 @@ func TestLoadModFile(t *testing.T) { assert.Equal(t, modFile.Dependencies.Deps["oci_name"].Name, "oci_name") assert.Equal(t, modFile.Dependencies.Deps["oci_name"].Version, "oci_tag") - assert.Equal(t, modFile.Dependencies.Deps["oci_name"].Source.Oci.Tag, "oci_tag") + assert.Equal(t, modFile.Dependencies.Deps["oci_name"].Source.Registry.Version, "oci_tag") assert.Equal(t, err, nil) assert.Equal(t, modFile.Dependencies.Deps["helloworld"].Name, "helloworld") diff --git a/pkg/package/package_test.go b/pkg/package/package_test.go index c68bbe57..7f871062 100644 --- a/pkg/package/package_test.go +++ b/pkg/package/package_test.go @@ -112,7 +112,7 @@ func TestLoadKclPkgFromTar(t *testing.T) { assert.Equal(t, kclPkg.ModFile.Deps["oci_konfig"].Name, "oci_konfig") assert.Equal(t, kclPkg.ModFile.Deps["oci_konfig"].FullName, "oci_konfig_0.0.1") - assert.Equal(t, kclPkg.ModFile.Deps["oci_konfig"].Oci.Tag, "0.0.1") + assert.Equal(t, kclPkg.ModFile.Deps["oci_konfig"].Registry.Version, "0.0.1") assert.Equal(t, len(kclPkg.Deps), 2) assert.Equal(t, kclPkg.Deps["konfig"].Name, "konfig") diff --git a/pkg/package/test_data/test_oci_url/marshal_1/expect.mod b/pkg/package/test_data/test_oci_url/marshal_1/expect.mod index 623f27fb..8afcdaf9 100644 --- a/pkg/package/test_data/test_oci_url/marshal_1/expect.mod +++ b/pkg/package/test_data/test_oci_url/marshal_1/expect.mod @@ -1,5 +1,5 @@ [package] -name = "marshal_0" +name = "marshal_1" edition = "v0.9.0" version = "0.0.1" diff --git a/pkg/package/test_data/test_oci_url/marshal_1/kcl.mod b/pkg/package/test_data/test_oci_url/marshal_1/kcl.mod index 3002c4ef..82d64601 100644 --- a/pkg/package/test_data/test_oci_url/marshal_1/kcl.mod +++ b/pkg/package/test_data/test_oci_url/marshal_1/kcl.mod @@ -1,5 +1,5 @@ [package] -name = "marshal_0" +name = "marshal_1" edition = "v0.9.0" version = "0.0.1" diff --git a/pkg/package/test_data/test_oci_url/marshal_2/expect.mod b/pkg/package/test_data/test_oci_url/marshal_2/expect.mod index 623f27fb..0d92bf69 100644 --- a/pkg/package/test_data/test_oci_url/marshal_2/expect.mod +++ b/pkg/package/test_data/test_oci_url/marshal_2/expect.mod @@ -1,7 +1,7 @@ [package] -name = "marshal_0" +name = "marshal_2" edition = "v0.9.0" version = "0.0.1" [dependencies] -oci_pkg = { oci = "oci://ghcr.io/kcl-lang/oci_pkg", tag = "0.0.1" } +oci_pkg = "0.0.1" diff --git a/pkg/package/test_data/test_oci_url/marshal_2/kcl.mod b/pkg/package/test_data/test_oci_url/marshal_2/kcl.mod index 2d16b794..0d92bf69 100644 --- a/pkg/package/test_data/test_oci_url/marshal_2/kcl.mod +++ b/pkg/package/test_data/test_oci_url/marshal_2/kcl.mod @@ -1,5 +1,5 @@ [package] -name = "marshal_0" +name = "marshal_2" edition = "v0.9.0" version = "0.0.1" diff --git a/pkg/package/test_data/test_oci_url/marshal_3/expect.mod b/pkg/package/test_data/test_oci_url/marshal_3/expect.mod index 80fc2fd5..534291b4 100644 --- a/pkg/package/test_data/test_oci_url/marshal_3/expect.mod +++ b/pkg/package/test_data/test_oci_url/marshal_3/expect.mod @@ -1,5 +1,5 @@ [package] -name = "marshal_0" +name = "marshal_3" edition = "v0.9.0" version = "0.0.1" diff --git a/pkg/package/test_data/test_oci_url/marshal_3/kcl.mod b/pkg/package/test_data/test_oci_url/marshal_3/kcl.mod index 80fc2fd5..534291b4 100644 --- a/pkg/package/test_data/test_oci_url/marshal_3/kcl.mod +++ b/pkg/package/test_data/test_oci_url/marshal_3/kcl.mod @@ -1,5 +1,5 @@ [package] -name = "marshal_0" +name = "marshal_3" edition = "v0.9.0" version = "0.0.1" diff --git a/pkg/package/toml.go b/pkg/package/toml.go index 230c79a4..0b5bfced 100644 --- a/pkg/package/toml.go +++ b/pkg/package/toml.go @@ -87,6 +87,14 @@ const SOURCE_PATTERN = "{ %s }" func (source *Source) MarshalTOML() string { var sb strings.Builder + + if source.Registry != nil { + registryToml := source.Registry.MarshalTOML() + if len(registryToml) != 0 { + sb.WriteString(fmt.Sprintf("%q", registryToml)) + } + } + if source.Git != nil { gitToml := source.Git.MarshalTOML() if len(gitToml) != 0 { @@ -115,6 +123,12 @@ func (source *Source) MarshalTOML() string { return sb.String() } +func (registry *Registry) MarshalTOML() string { + var sb strings.Builder + sb.WriteString(registry.Version) + return sb.String() +} + const GIT_URL_PATTERN = "git = \"%s\"" const TAG_PATTERN = "tag = \"%s\"" const GIT_COMMIT_PATTERN = "commit = \"%s\"" @@ -325,6 +339,9 @@ func (dep *Dependency) UnmarshalModTOML(data interface{}) error { if source.Oci != nil { version = source.Oci.Tag } + if source.Registry != nil { + version = source.Registry.Version + } dep.FullName = fmt.Sprintf(PKG_NAME_PATTERN, dep.Name, version) dep.Version = version @@ -348,24 +365,31 @@ func (source *Source) UnmarshalModTOML(data interface{}) error { return err } source.Git = &git - } else { + } else if _, ok := meta["oci"]; ok { oci := Oci{} err := oci.UnmarshalModTOML(data) if err != nil { return err } source.Oci = &oci + } else { + reg := Registry{} + err := reg.UnmarshalModTOML(data) + if err != nil { + return err + } + source.Registry = ® } } _, ok = data.(string) if ok { - oci := Oci{} - err := oci.UnmarshalModTOML(data) + registry := Registry{} + err := registry.UnmarshalModTOML(data) if err != nil { return err } - source.Oci = &oci + source.Registry = ®istry } return nil @@ -402,10 +426,7 @@ func (git *Git) UnmarshalModTOML(data interface{}) error { } func (oci *Oci) UnmarshalModTOML(data interface{}) error { - tag, ok := data.(string) - if ok { - oci.Tag = tag - } else if meta, ok := data.(map[string]interface{}); ok { + if meta, ok := data.(map[string]interface{}); ok { if v, ok := meta[constants.OciScheme].(string); ok { _, err := oci.FromString(v) if err != nil { @@ -416,8 +437,6 @@ func (oci *Oci) UnmarshalModTOML(data interface{}) error { if v, ok := meta[TAG_FLAG].(string); ok { oci.Tag = v } - } else { - return fmt.Errorf("unexpected data %T", data) } return nil @@ -438,6 +457,17 @@ func (local *Local) UnmarshalModTOML(data interface{}) error { return nil } +func (reg *Registry) UnmarshalModTOML(data interface{}) error { + version, ok := data.(string) + if ok { + reg.Version = version + reg.Oci = &Oci{} + reg.Oci.Tag = version + } + + return nil +} + func (dep *Dependencies) MarshalLockTOML() (string, error) { buf := new(bytes.Buffer) if err := toml.NewEncoder(buf).Encode(dep); err != nil { diff --git a/pkg/package/toml_test.go b/pkg/package/toml_test.go index d6d4bc6c..11e5df7f 100644 --- a/pkg/package/toml_test.go +++ b/pkg/package/toml_test.go @@ -92,8 +92,7 @@ func TestUnMarshalTOML(t *testing.T) { assert.NotEqual(t, modfile.Dependencies.Deps["MyOciKcl1"], nil) assert.Equal(t, modfile.Dependencies.Deps["MyOciKcl1"].Name, "MyOciKcl1") assert.Equal(t, modfile.Dependencies.Deps["MyOciKcl1"].FullName, "MyOciKcl1_0.0.1") - assert.NotEqual(t, modfile.Dependencies.Deps["MyOciKcl1"].Source.Oci, nil) - assert.Equal(t, modfile.Dependencies.Deps["MyOciKcl1"].Source.Oci.Tag, "0.0.1") + assert.Equal(t, modfile.Dependencies.Deps["MyOciKcl1"].Source.Registry.Version, "0.0.1") } func TestMarshalLockToml(t *testing.T) { @@ -241,7 +240,9 @@ func TestMarshalOciUrl(t *testing.T) { Version: "0.0.1", Source: Source{ Oci: &Oci{ - Tag: "0.0.1", + Reg: "ghcr.io", + Repo: "kcl-lang/oci_pkg", + Tag: "0.0.1", }, }, } @@ -252,7 +253,8 @@ func TestMarshalOciUrl(t *testing.T) { _, err = gotFile.WriteString(got_data) assert.Equal(t, err, nil) - got, err := LoadModFile(gotPkgPath) + got := ModFile{} + err = got.LoadModFile(filepath.Join(gotPkgPath, "kcl.mod")) assert.Equal(t, err, nil) assert.Equal(t, expect.Pkg.Name, got.Pkg.Name) @@ -270,13 +272,15 @@ func TestMarshalOciUrl(t *testing.T) { func TestMarshalOciUrlIntoFile(t *testing.T) { testDataDir := getTestDir("test_oci_url") - testCases := []string{"marshal_1", "marshal_2", "marshal_3"} + testCases := []string{"marshal_2"} for _, tc := range testCases { readKclModPath := filepath.Join(testDataDir, tc) + modfilePath := filepath.Join(readKclModPath, "kcl.mod") expectPath := filepath.Join(readKclModPath, "expect.mod") - readKclModFile, err := LoadModFile(readKclModPath) + readKclModFile := ModFile{} + err := readKclModFile.LoadModFile(modfilePath) assert.Equal(t, err, nil) writeKclModFileContents := readKclModFile.MarshalTOML() expectKclModFileContents, err := os.ReadFile(expectPath)