Skip to content

Commit

Permalink
feat: supports rename when add dependency (#524)
Browse files Browse the repository at this point in the history
* feat: supports rename when add dependency

Signed-off-by: zongz <[email protected]>

* refactor: add 'alias' to struct 'Source'

Signed-off-by: zongz <[email protected]>

* fix: rm useless code changes

Signed-off-by: zongz <[email protected]>

* fix: fix test case

Signed-off-by: zongz <[email protected]>

* fix: rename WithAddPkgNameAlias to WithAlias

Signed-off-by: zongz <[email protected]>

* fix: fix typo

Signed-off-by: zongz <[email protected]>

* fix: rm ModSpec in source url

Signed-off-by: zongz <[email protected]>

---------

Signed-off-by: zongz <[email protected]>
  • Loading branch information
zong-zhe authored Nov 7, 2024
1 parent 735bff3 commit 1e2a6a9
Show file tree
Hide file tree
Showing 12 changed files with 185 additions and 17 deletions.
22 changes: 21 additions & 1 deletion pkg/client/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@ type AddOptions struct {

type AddOption func(*AddOptions) error

func WithAlias(alias string) AddOption {
return func(opts *AddOptions) error {
if opts.Source == nil {
return fmt.Errorf("source cannot be nil")
}
if opts.Source.ModSpec.IsNil() {
return fmt.Errorf("modSpec cannot be nil")
}
opts.Source.ModSpec.Alias = alias
return nil
}
}

func WithAddModSpec(modSpec *downloader.ModSpec) AddOption {
return func(opts *AddOptions) error {
if modSpec == nil {
Expand Down Expand Up @@ -146,8 +159,15 @@ func (c *KpmClient) Add(options ...AddOption) error {
depSource.ModSpec = modSpec
}

var depName string
if opts.Source.ModSpec.Alias != "" {
depName = opts.Source.ModSpec.Alias
} else {
depName = depPkg.ModFile.Pkg.Name
}

dep := pkg.Dependency{
Name: depPkg.ModFile.Pkg.Name,
Name: depName,
FullName: depPkg.GetPkgFullName(),
Version: depPkg.ModFile.Pkg.Version,
LocalFullPath: depPkg.HomePath,
Expand Down
85 changes: 85 additions & 0 deletions pkg/client/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/otiai10/copy"
"github.com/stretchr/testify/assert"
"kcl-lang.io/kpm/pkg/downloader"
pkg "kcl-lang.io/kpm/pkg/package"
"kcl-lang.io/kpm/pkg/utils"
)
Expand Down Expand Up @@ -135,3 +136,87 @@ func testAddWithModSpec(t *testing.T) {
})
}
}

func TestAddRenameWithModSpec(t *testing.T) {
testDir := getTestDir("add_with_mod_spec")
pkgPath := filepath.Join(testDir, "rename")

modbkPath := filepath.Join(pkgPath, "kcl.mod.bk")
modPath := filepath.Join(pkgPath, "kcl.mod")
modExpect := filepath.Join(pkgPath, "kcl.mod.expect")
lockbkPath := filepath.Join(pkgPath, "kcl.mod.lock.bk")
lockPath := filepath.Join(pkgPath, "kcl.mod.lock")
lockExpect := filepath.Join(pkgPath, "kcl.mod.lock.expect")

err := copy.Copy(modbkPath, modPath)
if err != nil {
t.Fatal(err)
}

err = copy.Copy(lockbkPath, lockPath)
if err != nil {
t.Fatal(err)
}

defer func() {
// remove the copied files
err := os.RemoveAll(modPath)
if err != nil {
t.Fatal(err)
}
err = os.RemoveAll(lockPath)
if err != nil {
t.Fatal(err)
}
}()

kpmcli, err := NewKpmClient()
if err != nil {
t.Fatal(err)
}

kpkg, err := pkg.LoadKclPkgWithOpts(
pkg.WithPath(pkgPath),
pkg.WithSettings(kpmcli.GetSettings()),
)

if err != nil {
t.Fatal(err)
}

err = kpmcli.Add(
WithAddKclPkg(kpkg),
WithAddSourceUrl("oci://ghcr.io/kcl-lang/helloworld?tag=0.1.4"),
WithAddModSpec(&downloader.ModSpec{
Name: "subhelloworld",
Version: "0.0.1",
}),
WithAlias("newpkg"),
)

if err != nil {
t.Fatal(err)
}

expectedMod, err := os.ReadFile(modExpect)
if err != nil {
t.Fatal(err)
}
gotMod, err := os.ReadFile(modPath)
if err != nil {
t.Fatal(err)
}

expectedLock, err := os.ReadFile(lockExpect)
if err != nil {
t.Fatal(err)
}

gotLock, err := os.ReadFile(lockPath)
if err != nil {
t.Fatal(err)
}

assert.Equal(t, utils.RmNewline(string(expectedMod)), utils.RmNewline(string(gotMod)))
assert.Equal(t, utils.RmNewline(string(expectedLock)), utils.RmNewline(string(gotLock)))
}
4 changes: 4 additions & 0 deletions pkg/client/test_data/add_with_mod_spec/rename/kcl.mod.bk
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[package]
name = "rename"
edition = "v0.10.0"
version = "0.0.1"
7 changes: 7 additions & 0 deletions pkg/client/test_data/add_with_mod_spec/rename/kcl.mod.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "rename"
edition = "v0.10.0"
version = "0.0.1"

[dependencies]
newpkg = { oci = "oci://ghcr.io/kcl-lang/helloworld", tag = "0.1.4", package = "subhelloworld", version = "0.0.1" }
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[dependencies]
[dependencies.newpkg]
name = "newpkg"
full_name = "subhelloworld_0.0.1"
version = "0.0.1"
reg = "ghcr.io"
repo = "kcl-lang/helloworld"
oci_tag = "0.1.4"
1 change: 1 addition & 0 deletions pkg/client/test_data/add_with_mod_spec/rename/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The_first_kcl_program = 'Hello World!'
1 change: 1 addition & 0 deletions pkg/downloader/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
type ModSpec struct {
Name string
Version string
Alias string
}

// IsNil returns true if the ModSpec is nil.
Expand Down
34 changes: 22 additions & 12 deletions pkg/downloader/toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ const SPEC_PATTERN = "%s = %q"
func (ps *ModSpec) MarshalTOML() string {
var sb strings.Builder
if ps != nil && len(ps.Version) != 0 && len(ps.Name) != 0 {
sb.WriteString(fmt.Sprintf(SPEC_PATTERN, ps.Name, ps.Version))
if len(ps.Alias) == 0 {
sb.WriteString(fmt.Sprintf("%q", ps.Version))
} else {
sb.WriteString(fmt.Sprintf(SOURCE_PATTERN, fmt.Sprintf("package = %q, version = %q", ps.Name, ps.Version)))
}
return sb.String()
}

Expand All @@ -28,40 +32,41 @@ func (source *Source) MarshalTOML() string {
if source.SpecOnly() {
return source.ModSpec.MarshalTOML()
} else {
var pkgVersion string
var pkgSpec string
var tomlStr string

if source.ModSpec != nil && len(source.ModSpec.Version) > 0 {
pkgVersion = fmt.Sprintf(", version = %q", source.ModSpec.Version)
if source.ModSpec.Alias != "" {
pkgSpec = fmt.Sprintf(", package = %q, version = %q", source.ModSpec.Name, source.ModSpec.Version)
} else {
pkgSpec = fmt.Sprintf(", version = %q", source.ModSpec.Version)
}
}

if source.Git != nil {
tomlStr = source.Git.MarshalTOML()
if len(tomlStr) != 0 {
tomlStr = fmt.Sprintf(SOURCE_PATTERN, tomlStr+pkgVersion)
tomlStr = fmt.Sprintf(SOURCE_PATTERN, tomlStr+pkgSpec)
}
}

if source.Oci != nil {
tomlStr = source.Oci.MarshalTOML()
if len(tomlStr) != 0 {
if len(source.Oci.Reg) != 0 && len(source.Oci.Repo) != 0 {
tomlStr = fmt.Sprintf(SOURCE_PATTERN, tomlStr+pkgVersion)
tomlStr = fmt.Sprintf(SOURCE_PATTERN, tomlStr+pkgSpec)
}
}
}

if source.Local != nil {
tomlStr = source.Local.MarshalTOML()
if len(tomlStr) != 0 {
tomlStr = fmt.Sprintf(SOURCE_PATTERN, tomlStr+pkgVersion)
tomlStr = fmt.Sprintf(SOURCE_PATTERN, tomlStr+pkgSpec)
}
}

if source.ModSpec != nil && len(source.ModSpec.Name) != 0 {
sb.WriteString(fmt.Sprintf(DEP_PATTERN, source.ModSpec.Name, tomlStr))
} else {
sb.WriteString(tomlStr)
}
sb.WriteString(tomlStr)
}

return sb.String()
Expand Down Expand Up @@ -160,14 +165,19 @@ func (source *Source) UnmarshalModTOML(data interface{}) error {
source.Oci = &oci
}

pSpec := ModSpec{}
if v, ok := meta["version"].(string); ok {
pSpec := ModSpec{}
err := pSpec.UnmarshalModTOML(v)
if err != nil {
return err
}
source.ModSpec = &pSpec
}

if v, ok := meta["package"].(string); ok {
pSpec.Name = v
source.ModSpec = &pSpec
}
}

_, ok = data.(string)
Expand Down
7 changes: 7 additions & 0 deletions pkg/package/test_data/test_rename_pkg/kcl.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "rename"
edition = "v0.10.0"
version = "0.0.1"

[dependencies]
newpkg = { oci = "oci://ghcr.io/kcl-lang/helloworld", tag = "0.1.4", package = "subhelloworld", version = "0.0.1" }
15 changes: 11 additions & 4 deletions pkg/package/toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,14 @@ const DEP_PATTERN = "%s = %s"
func (dep *Dependency) MarshalTOML() string {
var sb strings.Builder

if dep.Source.ModSpec != nil && dep.Source.ModSpec.Version != "" && dep.Source.ModSpec.Name != "" {
sb.WriteString(dep.Source.MarshalTOML())
} else {
sb.WriteString(fmt.Sprintf(DEP_PATTERN, dep.Name, dep.Source.MarshalTOML()))
depName := dep.Name
if !dep.Source.ModSpec.IsNil() {
if dep.Source.ModSpec.Alias != "" {
depName = dep.Source.ModSpec.Alias
}
}

sb.WriteString(fmt.Sprintf(DEP_PATTERN, depName, dep.Source.MarshalTOML()))
return sb.String()
}

Expand Down Expand Up @@ -231,6 +233,11 @@ func (deps *Dependencies) UnmarshalModTOML(data interface{}) error {
if err != nil {
return err
}
if !dep.Source.ModSpec.IsNil() {
if dep.Source.ModSpec.Name != dep.Name {
dep.Source.ModSpec.Alias = dep.Name
}
}
deps.Deps.Set(k, dep)
}

Expand Down
18 changes: 18 additions & 0 deletions pkg/package/toml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,3 +361,21 @@ func TestInitEmptyPkg(t *testing.T) {
fmt.Printf("modfile: '%q'\n", got_data)
assert.Equal(t, expected_toml, got_data)
}

func TestUnMarshalRename(t *testing.T) {
modfile := ModFile{}
modfile.LoadModFile(filepath.Join(getTestDir("test_rename_pkg"), "kcl.mod"))
assert.Equal(t, modfile.Pkg.Name, "rename")
assert.Equal(t, modfile.Pkg.Version, "0.0.1")
assert.Equal(t, modfile.Pkg.Edition, "v0.10.0")
assert.Equal(t, modfile.Dependencies.Deps.Len(), 1)
assert.Equal(t, modfile.Dependencies.Deps.GetOrDefault("newpkg", TestPkgDependency).Name, "newpkg")
assert.Equal(t, modfile.Dependencies.Deps.GetOrDefault("newpkg", TestPkgDependency).FullName, "newpkg_0.0.1")
assert.Equal(t, modfile.Dependencies.Deps.GetOrDefault("newpkg", TestPkgDependency).Version, "0.0.1")
assert.Equal(t, modfile.Dependencies.Deps.GetOrDefault("newpkg", TestPkgDependency).Source.ModSpec.Name, "subhelloworld")
assert.Equal(t, modfile.Dependencies.Deps.GetOrDefault("newpkg", TestPkgDependency).Source.ModSpec.Version, "0.0.1")
assert.Equal(t, modfile.Dependencies.Deps.GetOrDefault("newpkg", TestPkgDependency).Source.ModSpec.Alias, "newpkg")
assert.Equal(t, modfile.Dependencies.Deps.GetOrDefault("newpkg", TestPkgDependency).Oci.Reg, "ghcr.io")
assert.Equal(t, modfile.Dependencies.Deps.GetOrDefault("newpkg", TestPkgDependency).Oci.Repo, "kcl-lang/helloworld")
assert.Equal(t, modfile.Dependencies.Deps.GetOrDefault("newpkg", TestPkgDependency).Oci.Tag, "0.1.4")
}

0 comments on commit 1e2a6a9

Please sign in to comment.