Skip to content

Commit 15d995b

Browse files
authored
feat: add a switch to control the auto-updating for mod file (#566)
* feat: add a switch to control the auto-updating for mod file Signed-off-by: zongz <zongzhe1024@163.com> * fix: fix test case Signed-off-by: zongz <zongzhe1024@163.com> * fix: fix test case Signed-off-by: zongz <zongzhe1024@163.com> * fix: fix typo Signed-off-by: zongz <zongzhe1024@163.com> * fix: fix test case Signed-off-by: zongz <zongzhe1024@163.com> * fix: test case Signed-off-by: zongz <zongzhe1024@163.com> --------- Signed-off-by: zongz <zongzhe1024@163.com>
1 parent ba02f26 commit 15d995b

File tree

6 files changed

+43
-14
lines changed

6 files changed

+43
-14
lines changed

pkg/api/kpm_run.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"kcl-lang.io/kpm/pkg/errors"
1313
"kcl-lang.io/kpm/pkg/oci"
1414
"kcl-lang.io/kpm/pkg/opt"
15+
pkg "kcl-lang.io/kpm/pkg/package"
1516
"kcl-lang.io/kpm/pkg/reporter"
1617
"kcl-lang.io/kpm/pkg/runner"
1718
"kcl-lang.io/kpm/pkg/utils"
@@ -207,7 +208,10 @@ func run(kpmcli *client.KpmClient, opts *opt.CompileOptions) (*kcl.KCLResultList
207208
return nil, reporter.NewErrorEvent(reporter.Bug, err, "internal bugs, please contact us to fix it.")
208209
}
209210

210-
kclPkg, err := kpmcli.LoadPkgFromPath(pkgPath)
211+
kclPkg, err := pkg.LoadKclPkgWithOpts(
212+
pkg.WithPath(pkgPath),
213+
pkg.WithSettings(kpmcli.GetSettings()),
214+
)
211215
if err != nil {
212216
return nil, err
213217
}

pkg/client/client_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,7 +1146,6 @@ func testMetadataOffline(t *testing.T) {
11461146
testDir := getTestDir("test_metadata_offline")
11471147
kclMod := filepath.Join(testDir, "kcl.mod")
11481148
uglyKclMod := filepath.Join(testDir, "ugly.kcl.mod")
1149-
BeautifulKclMod := filepath.Join(testDir, "beautiful.kcl.mod")
11501149

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

1160-
beautifulContent, err := os.ReadFile(BeautifulKclMod)
1161-
assert.Equal(t, err, nil)
11621159
kclPkg, err := pkg.LoadKclPkg(testDir)
11631160
assert.Equal(t, err, nil)
11641161

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

11841181
func testAddWithNoSumCheck(t *testing.T) {

pkg/client/metadata.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ func (c *KpmClient) ResolvePkgDepsMetadata(kclPkg *pkg.KclPkg, update bool) erro
2020
_, err = c.Update(
2121
WithUpdatedKclPkg(kclPkg),
2222
WithOffline(!update),
23+
WithUpdateModFile(false),
2324
)
2425
}
2526
return err

pkg/client/test_data/test_run_with_modspec_version/kcl.mod.expect

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ edition = "v0.11.0-alpha.1"
44
version = "0.0.1"
55

66
[dependencies]
7-
helloworld = "0.1.2"
7+
helloworld = { version = "0.1.2" }

pkg/client/update.go

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package client
33
import (
44
"encoding/json"
55
"fmt"
6+
"path/filepath"
67

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

2729
type UpdateOption func(*UpdateOptions) error
2830

31+
// WithUpdateModFile sets the flag to update the mod file.
32+
func WithUpdateModFile(updateModFile bool) UpdateOption {
33+
return func(opts *UpdateOptions) error {
34+
opts.updateModFile = updateModFile
35+
return nil
36+
}
37+
}
38+
2939
// WithOffline sets the offline option to update the package.
3040
func WithOffline(offline bool) UpdateOption {
3141
return func(opts *UpdateOptions) error {
@@ -43,7 +53,7 @@ func WithUpdatedKclPkg(kpkg *pkg.KclPkg) UpdateOption {
4353
}
4454

4555
func (c *KpmClient) Update(options ...UpdateOption) (*pkg.KclPkg, error) {
46-
opts := &UpdateOptions{}
56+
opts := &UpdateOptions{updateModFile: true}
4757
for _, option := range options {
4858
if err := option(opts); err != nil {
4959
return nil, err
@@ -143,8 +153,16 @@ func (c *KpmClient) Update(options ...UpdateOption) (*pkg.KclPkg, error) {
143153
return nil, err
144154
}
145155

146-
if !opts.offline {
147-
err = kMod.UpdateModAndLockFile()
156+
if opts.updateModFile && utils.DirExists(filepath.Join(kMod.HomePath, constants.KCL_MOD)) {
157+
err = kMod.UpdateModFile()
158+
if err != nil {
159+
return nil, err
160+
}
161+
}
162+
163+
// Generate file kcl.mod.lock.
164+
if !kMod.NoSumCheck && utils.DirExists(filepath.Join(kMod.HomePath, constants.KCL_MOD)) {
165+
err := kMod.LockDepsVersion()
148166
if err != nil {
149167
return nil, err
150168
}

pkg/package/package.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -385,9 +385,8 @@ func (kclPkg *KclPkg) LocalVendorPath() string {
385385
return filepath.Join(kclPkg.HomePath, "vendor")
386386
}
387387

388-
// updateModAndLockFile will update kcl.mod and kcl.mod.lock
389-
func (kclPkg *KclPkg) UpdateModAndLockFile() error {
390-
388+
// UpdateModFile will update the kcl.mod file.
389+
func (kclPkg *KclPkg) UpdateModFile() error {
391390
// Load kcl.mod SnapShot.
392391
depSnapShot := kclPkg.depUI
393392

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

414+
return nil
415+
}
416+
417+
// updateModAndLockFile will update kcl.mod and kcl.mod.lock
418+
func (kclPkg *KclPkg) UpdateModAndLockFile() error {
419+
err := kclPkg.UpdateModFile()
420+
if err != nil {
421+
return err
422+
}
423+
415424
// Generate file kcl.mod.lock.
416425
if !kclPkg.NoSumCheck {
417426
err := kclPkg.LockDepsVersion()

0 commit comments

Comments
 (0)