Skip to content

Commit

Permalink
feat: add a switch to control the auto-updating for mod file (#566)
Browse files Browse the repository at this point in the history
* feat: add a switch to control the auto-updating for mod file

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

* fix: fix test case

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

* fix: fix test case

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

* fix: fix typo

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

* fix: fix test case

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

* fix: test case

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

---------

Signed-off-by: zongz <[email protected]>
  • Loading branch information
zong-zhe authored Dec 12, 2024
1 parent ba02f26 commit 15d995b
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 14 deletions.
6 changes: 5 additions & 1 deletion pkg/api/kpm_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"kcl-lang.io/kpm/pkg/errors"
"kcl-lang.io/kpm/pkg/oci"
"kcl-lang.io/kpm/pkg/opt"
pkg "kcl-lang.io/kpm/pkg/package"
"kcl-lang.io/kpm/pkg/reporter"
"kcl-lang.io/kpm/pkg/runner"
"kcl-lang.io/kpm/pkg/utils"
Expand Down Expand Up @@ -207,7 +208,10 @@ func run(kpmcli *client.KpmClient, opts *opt.CompileOptions) (*kcl.KCLResultList
return nil, reporter.NewErrorEvent(reporter.Bug, err, "internal bugs, please contact us to fix it.")
}

kclPkg, err := kpmcli.LoadPkgFromPath(pkgPath)
kclPkg, err := pkg.LoadKclPkgWithOpts(
pkg.WithPath(pkgPath),
pkg.WithSettings(kpmcli.GetSettings()),
)
if err != nil {
return nil, err
}
Expand Down
5 changes: 1 addition & 4 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1146,7 +1146,6 @@ func testMetadataOffline(t *testing.T) {
testDir := getTestDir("test_metadata_offline")
kclMod := filepath.Join(testDir, "kcl.mod")
uglyKclMod := filepath.Join(testDir, "ugly.kcl.mod")
BeautifulKclMod := filepath.Join(testDir, "beautiful.kcl.mod")

uglyContent, err := os.ReadFile(uglyKclMod)
assert.Equal(t, err, nil)
Expand All @@ -1157,8 +1156,6 @@ func testMetadataOffline(t *testing.T) {
assert.Equal(t, err, nil)
}()

beautifulContent, err := os.ReadFile(BeautifulKclMod)
assert.Equal(t, err, nil)
kclPkg, err := pkg.LoadKclPkg(testDir)
assert.Equal(t, err, nil)

Expand All @@ -1178,7 +1175,7 @@ func testMetadataOffline(t *testing.T) {
assert.Equal(t, res, "{\"packages\":{}}")
content_after_metadata, err = os.ReadFile(kclMod)
assert.Equal(t, err, nil)
assert.Equal(t, utils.RmNewline(string(content_after_metadata)), utils.RmNewline(string(beautifulContent)))
assert.Equal(t, utils.RmNewline(string(content_after_metadata)), utils.RmNewline(string(uglyContent)))
}

func testAddWithNoSumCheck(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions pkg/client/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func (c *KpmClient) ResolvePkgDepsMetadata(kclPkg *pkg.KclPkg, update bool) erro
_, err = c.Update(
WithUpdatedKclPkg(kclPkg),
WithOffline(!update),
WithUpdateModFile(false),
)
}
return err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ edition = "v0.11.0-alpha.1"
version = "0.0.1"

[dependencies]
helloworld = "0.1.2"
helloworld = { version = "0.1.2" }
28 changes: 23 additions & 5 deletions pkg/client/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package client
import (
"encoding/json"
"fmt"
"path/filepath"

ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"kcl-lang.io/kpm/pkg/checker"
Expand All @@ -20,12 +21,21 @@ import (
// Updating a package means iterating all the dependencies of the package
// and updating the dependencies and selecting the version of the dependencies by MVS.
type UpdateOptions struct {
kpkg *pkg.KclPkg
offline bool
kpkg *pkg.KclPkg
offline bool
updateModFile bool
}

type UpdateOption func(*UpdateOptions) error

// WithUpdateModFile sets the flag to update the mod file.
func WithUpdateModFile(updateModFile bool) UpdateOption {
return func(opts *UpdateOptions) error {
opts.updateModFile = updateModFile
return nil
}
}

// WithOffline sets the offline option to update the package.
func WithOffline(offline bool) UpdateOption {
return func(opts *UpdateOptions) error {
Expand All @@ -43,7 +53,7 @@ func WithUpdatedKclPkg(kpkg *pkg.KclPkg) UpdateOption {
}

func (c *KpmClient) Update(options ...UpdateOption) (*pkg.KclPkg, error) {
opts := &UpdateOptions{}
opts := &UpdateOptions{updateModFile: true}
for _, option := range options {
if err := option(opts); err != nil {
return nil, err
Expand Down Expand Up @@ -143,8 +153,16 @@ func (c *KpmClient) Update(options ...UpdateOption) (*pkg.KclPkg, error) {
return nil, err
}

if !opts.offline {
err = kMod.UpdateModAndLockFile()
if opts.updateModFile && utils.DirExists(filepath.Join(kMod.HomePath, constants.KCL_MOD)) {
err = kMod.UpdateModFile()
if err != nil {
return nil, err
}
}

// Generate file kcl.mod.lock.
if !kMod.NoSumCheck && utils.DirExists(filepath.Join(kMod.HomePath, constants.KCL_MOD)) {
err := kMod.LockDepsVersion()
if err != nil {
return nil, err
}
Expand Down
15 changes: 12 additions & 3 deletions pkg/package/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,9 +385,8 @@ func (kclPkg *KclPkg) LocalVendorPath() string {
return filepath.Join(kclPkg.HomePath, "vendor")
}

// updateModAndLockFile will update kcl.mod and kcl.mod.lock
func (kclPkg *KclPkg) UpdateModAndLockFile() error {

// UpdateModFile will update the kcl.mod file.
func (kclPkg *KclPkg) UpdateModFile() error {
// Load kcl.mod SnapShot.
depSnapShot := kclPkg.depUI

Expand All @@ -412,6 +411,16 @@ func (kclPkg *KclPkg) UpdateModAndLockFile() error {
return err
}

return nil
}

// updateModAndLockFile will update kcl.mod and kcl.mod.lock
func (kclPkg *KclPkg) UpdateModAndLockFile() error {
err := kclPkg.UpdateModFile()
if err != nil {
return err
}

// Generate file kcl.mod.lock.
if !kclPkg.NoSumCheck {
err := kclPkg.LockDepsVersion()
Expand Down

0 comments on commit 15d995b

Please sign in to comment.