Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add alias name for dependency #229

Merged
merged 3 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,16 @@ func (c *KpmClient) ResolveDepsIntoMap(kclPkg *pkg.KclPkg) (map[string]string, e
}

var pkgMap map[string]string = make(map[string]string)
for name, d := range kclPkg.Dependencies.Deps {
pkgMap[name] = d.GetLocalFullPath(kclPkg.HomePath)
for _, d := range kclPkg.Dependencies.Deps {
if _, ok := pkgMap[d.GetAliasName()]; ok {
return nil, reporter.NewErrorEvent(
reporter.PathIsEmpty,
fmt.Errorf("dependency name conflict, '%s' already exists", d.GetAliasName()),
"because '-' in the original dependency names is replaced with '_'\n",
"please check your dependencies with '-' or '_' in dependency name",
)
}
pkgMap[d.GetAliasName()] = d.GetLocalFullPath(kclPkg.HomePath)
}

return pkgMap, nil
Expand Down Expand Up @@ -201,11 +209,14 @@ func (c *KpmClient) ResolvePkgDepsMetadata(kclPkg *pkg.KclPkg, update bool) erro
for name, d := range kclPkg.Dependencies.Deps {
searchFullPath := filepath.Join(searchPath, d.FullName)
if !update {
if utils.DirExists(searchFullPath) {
// Find it and update the local path of the dependency.
d.LocalFullPath = searchFullPath
kclPkg.Dependencies.Deps[name] = d
if d.IsFromLocal() {
searchFullPath = d.GetLocalFullPath(kclPkg.HomePath)
}

// Find it and update the local path of the dependency.
d.LocalFullPath = searchFullPath
kclPkg.Dependencies.Deps[name] = d

} else {
if utils.DirExists(searchFullPath) && (c.GetNoSumCheck() || utils.CheckPackageSum(d.Sum, searchFullPath)) {
// Find it and update the local path of the dependency.
Expand Down
45 changes: 23 additions & 22 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,15 +234,15 @@ func TestVendorDeps(t *testing.T) {
kcl2Sum, _ := utils.HashDir(filepath.Join(kpm_home, "kcl2"))

depKcl1 := pkg.Dependency{
Name: "kcl1",
FullName: "kcl1",
Sum: kcl1Sum,
Name: "kcl1",
FullName: "kcl1",
Sum: kcl1Sum,
}

depKcl2 := pkg.Dependency{
Name: "kcl2",
FullName: "kcl2",
Sum: kcl2Sum,
Name: "kcl2",
FullName: "kcl2",
Sum: kcl2Sum,
}

kclPkg := pkg.KclPkg{
Expand Down Expand Up @@ -291,10 +291,10 @@ func TestVendorDeps(t *testing.T) {
func TestResolveDepsWithOnlyKclMod(t *testing.T) {
testDir := getTestDir("resolve_dep_with_kclmod")
assert.Equal(t, utils.DirExists(filepath.Join(testDir, "kcl.mod.lock")), false)
kclPkg, err := pkg.LoadKclPkg(testDir)
assert.Equal(t, err, nil)
kpmcli, err := NewKpmClient()
assert.Equal(t, err, nil)
kclPkg, err := kpmcli.LoadPkgFromPath(testDir)
assert.Equal(t, err, nil)
depsMap, err := kpmcli.ResolveDepsIntoMap(kclPkg)
assert.Equal(t, err, nil)
assert.Equal(t, len(depsMap), 1)
Expand All @@ -316,15 +316,15 @@ func TestResolveDepsVendorMode(t *testing.T) {
kcl2Sum, _ := utils.HashDir(filepath.Join(kpm_home, "kcl2"))

depKcl1 := pkg.Dependency{
Name: "kcl1",
FullName: "kcl1",
Sum: kcl1Sum,
Name: "kcl1",
FullName: "kcl1",
Sum: kcl1Sum,
}

depKcl2 := pkg.Dependency{
Name: "kcl2",
FullName: "kcl2",
Sum: kcl2Sum,
Name: "kcl2",
FullName: "kcl2",
Sum: kcl2Sum,
}

kclPkg := pkg.KclPkg{
Expand Down Expand Up @@ -382,15 +382,15 @@ func TestCompileWithEntryFile(t *testing.T) {

kcl1Sum, _ := utils.HashDir(filepath.Join(kpm_home, "kcl1"))
depKcl1 := pkg.Dependency{
Name: "kcl1",
FullName: "kcl1",
Sum: kcl1Sum,
Name: "kcl1",
FullName: "kcl1",
Sum: kcl1Sum,
}
kcl2Sum, _ := utils.HashDir(filepath.Join(kpm_home, "kcl2"))
depKcl2 := pkg.Dependency{
Name: "kcl2",
FullName: "kcl2",
Sum: kcl2Sum,
Name: "kcl2",
FullName: "kcl2",
Sum: kcl2Sum,
}

kclPkg := pkg.KclPkg{
Expand Down Expand Up @@ -534,10 +534,11 @@ func TestResolveMetadataInJsonStr(t *testing.T) {
assert.Equal(t, err, nil)
}

kclpkg, err = pkg.LoadKclPkg(testDir)
kclpkg, err = kpmcli.LoadPkgFromPath(testDir)
assert.Equal(t, err, nil)
kpmcli.homePath = "not_exist"
res, err = kpmcli.ResolveDepsMetadataInJsonStr(kclpkg, true)
res, err = kpmcli.ResolveDepsMetadataInJsonStr(kclpkg, false)
fmt.Printf("err: %v\n", err)
assert.Equal(t, err, nil)
assert.Equal(t, utils.DirExists(vendorDir), false)
assert.Equal(t, utils.DirExists(filepath.Join(vendorDir, "konfig_v0.0.1")), false)
Expand Down
3 changes: 1 addition & 2 deletions pkg/cmd/cmd_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/urfave/cli/v2"
"kcl-lang.io/kpm/pkg/client"
"kcl-lang.io/kpm/pkg/env"
pkg "kcl-lang.io/kpm/pkg/package"
"kcl-lang.io/kpm/pkg/reporter"
)

Expand Down Expand Up @@ -55,7 +54,7 @@ func NewMetadataCmd(kpmcli *client.KpmClient) *cli.Command {
return reporter.NewErrorEvent(reporter.Bug, err, "internal bugs, please contact us to fix it")
}

kclPkg, err := pkg.LoadKclPkg(pwd)
kclPkg, err := kpmcli.LoadPkgFromPath(pwd)
if err != nil {
return err
}
Expand Down
7 changes: 7 additions & 0 deletions pkg/package/modfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/BurntSushi/toml"
"kcl-lang.io/kcl-go/pkg/kcl"
Expand Down Expand Up @@ -139,6 +140,11 @@ type Dependency struct {
Source `json:"-"`
}

// SetName will set the name and alias name of a dependency.
func (d *Dependency) GetAliasName() string {
return strings.ReplaceAll(d.Name, "-", "_")
}

// WithTheSameVersion will check whether two dependencies have the same version.
func (d Dependency) WithTheSameVersion(other Dependency) bool {
return d.Name == other.Name &&
Expand Down Expand Up @@ -365,6 +371,7 @@ func ParseOpt(opt *opt.RegistryOptions) (*Dependency, error) {
if err != nil {
return nil, err
}

return &Dependency{
Name: depPkg.ModFile.Pkg.Name,
FullName: depPkg.ModFile.Pkg.Name + "_" + depPkg.ModFile.Pkg.Version,
Expand Down
1 change: 1 addition & 0 deletions pkg/reporter/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ const (
DownloadingFromGit
LocalPathNotExist
PathIsEmpty
ConflictPkgName
AddItselfAsDep
PkgTagExists
DependencyNotFound
Expand Down
26 changes: 24 additions & 2 deletions test/e2e/kpm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ var _ = ginkgo.Describe("Kpm CLI Testing", func() {
CopyDir(filepath.Join(testDataRoot, ts.Name), filepath.Join(workspace, ts.Name))

input := ReplaceAllKeyByValue(ts.Input, "<workspace>", filepath.Join(workspace, ts.Name))

stdout, stderr, err := ExecKpmWithWorkDir(input, filepath.Join(workspace, ts.Name))

expectedStdout := ReplaceAllKeyByValue(ts.ExpectStdout, "<workspace>", workspace)
Expand Down Expand Up @@ -172,6 +171,30 @@ var _ = ginkgo.Describe("Kpm CLI Testing", func() {
}
})

ginkgo.Context("testing 'kpm metadata '", func() {
testSuitesRoot := filepath.Join(filepath.Join(filepath.Join(GetWorkDir(), TEST_SUITES_DIR), "kpm"), "kpm_metadata")
testSuites := LoadAllTestSuites(testSuitesRoot)
testDataRoot := filepath.Join(filepath.Join(GetWorkDir(), TEST_SUITES_DIR), "test_data")
for _, ts := range testSuites {
ts := ts
ginkgo.It(ts.GetTestSuiteInfo(), func() {
workspace := GetWorkspace()

CopyDir(filepath.Join(testDataRoot, ts.Name), filepath.Join(workspace, ts.Name))

input := ReplaceAllKeyByValue(ts.Input, "<workspace>", filepath.Join(workspace, ts.Name))
stdout, stderr, err := ExecKpmWithWorkDir(input, filepath.Join(workspace, ts.Name))

expectedStdout := ReplaceAllKeyByValue(ts.ExpectStdout, "<workspace>", workspace)
expectedStderr := ReplaceAllKeyByValue(ts.ExpectStderr, "<workspace>", workspace)

gomega.Expect(err).ShouldNot(gomega.HaveOccurred())
gomega.Expect(stdout).To(gomega.ContainSubstring(expectedStdout))
gomega.Expect(stderr).To(gomega.ContainSubstring(expectedStderr))
})
}
})

ginkgo.Context("testing 'test oci '", func() {
testSuitesRoot := filepath.Join(filepath.Join(filepath.Join(GetWorkDir(), TEST_SUITES_DIR), "kpm"), "test_oci")
testSuites := LoadAllTestSuites(testSuitesRoot)
Expand All @@ -185,7 +208,6 @@ var _ = ginkgo.Describe("Kpm CLI Testing", func() {
CopyDir(filepath.Join(testDataRoot, ts.Name), filepath.Join(workspace, ts.Name))

input := ReplaceAllKeyByValue(ts.Input, "<workspace>", filepath.Join(workspace, ts.Name))

stdout, stderr, err := ExecKpmWithWorkDir(input, filepath.Join(workspace, ts.Name))

expectedStdout := ReplaceAllKeyByValue(ts.ExpectStdout, "<workspace>", workspace)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
KPM_HOME=""
KCLVM_VENDOR_HOME=""
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
kpm metadata
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"packages":{"dep-with-line":{"name":"dep-with-line","manifest_path":"<workspace>/test_kpm_metadata/dep-with-line"}}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
KPM_HOME=""
KCLVM_VENDOR_HOME=""
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
kpm metadata
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"packages":{"dep-with-line":{"name":"dep-with-line","manifest_path":"<workspace>/test_kpm_metadata_duplication/dep-with-line"},"dep_with-line":{"name":"dep_with-line","manifest_path":"<workspace>/test_kpm_metadata_duplication/dep_with-line"}}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
KPM_HOME=""
KCLVM_VENDOR_HOME=""
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
kpm run
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
failed to compile the kcl package
because '-' in the original dependency names is replaced with '_'
please check your dependencies with '-' or '_' in dependency name
dependency name conflict, 'dep_with_line' already exists
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
KPM_HOME=""
KCLVM_VENDOR_HOME=""
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
kpm run <workspace>/test_with_line
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
inst: Hello World!
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
name = "dep-with-line"
edition = "0.0.1"
version = "0.0.1"

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The_first_kcl_program = 'Hello World!'
7 changes: 7 additions & 0 deletions test/e2e/test_suites/test_data/test_kpm_metadata/kcl.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "test_web_service"
edition = "0.0.1"
version = "0.0.1"

[dependencies]
dep-with-line = { path = "./dep-with-line" }
7 changes: 7 additions & 0 deletions test/e2e/test_suites/test_data/test_kpm_metadata/kcl.mod.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[dependencies]
[dependencies.dep-with-line]
name = "dep-with-line"
full_name = "dep-with-line_0.0.1"
version = "0.0.1"
sum = "0lzH5VUC9/wM60EnksK/9Oc3/alBezWkElgGM1l0938="
path = "./dep-with-line"
3 changes: 3 additions & 0 deletions test/e2e/test_suites/test_data/test_kpm_metadata/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import dep_with_line as dwl

inst = dwl.The_first_kcl_program
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
name = "dep-with-line"
edition = "0.0.1"
version = "0.0.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,5 @@
[package]
name = "dep_with-line"
edition = "0.0.1"
version = "0.0.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,8 @@
[package]
name = "test_web_service"
edition = "0.0.1"
version = "0.0.1"

[dependencies]
dep-with-line = { path = "./dep-with-line" }
dep_with-line = { path = "./dep_with-line" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[dependencies]
[dependencies.dep-with-line]
name = "dep-with-line"
full_name = "dep-with-line_"
sum = "0lzH5VUC9/wM60EnksK/9Oc3/alBezWkElgGM1l0938="
path = "./dep-with-line"
[dependencies.dep_with-line]
name = "dep_with-line"
full_name = "dep_with-line_"
sum = "hiIX1252Xn0QvmGlLhuM5ZsuuV8EpY/fuCgcRKLk3iE="
path = "./dep_with-line"
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import dep_with_line as dwl

inst = dwl.The_first_kcl_program
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
name = "dep-with-line"
edition = "0.0.1"
version = "0.0.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,5 @@
[package]
name = "dep_with-line"
edition = "0.0.1"
version = "0.0.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,8 @@
[package]
name = "test_web_service"
edition = "0.0.1"
version = "0.0.1"

[dependencies]
dep-with-line = { path = "./dep-with-line" }
dep_with-line = { path = "./dep_with-line" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[dependencies]
[dependencies.dep-with-line]
name = "dep-with-line"
full_name = "dep-with-line_"
sum = "0lzH5VUC9/wM60EnksK/9Oc3/alBezWkElgGM1l0938="
path = "./dep-with-line"
[dependencies.dep_with-line]
name = "dep_with-line"
full_name = "dep_with-line_"
sum = "hiIX1252Xn0QvmGlLhuM5ZsuuV8EpY/fuCgcRKLk3iE="
path = "./dep_with-line"
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import dep_with_line as dwl

inst = dwl.The_first_kcl_program
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
name = "dep-with-line"
edition = "0.0.1"
version = "0.0.1"

Loading
Loading