Skip to content

Commit

Permalink
fix: fix missing options in kcl run
Browse files Browse the repository at this point in the history
Signed-off-by: zongz <[email protected]>
  • Loading branch information
zong-zhe committed Aug 8, 2024
1 parent fbafede commit 63eaf4e
Show file tree
Hide file tree
Showing 14 changed files with 111 additions and 50 deletions.
40 changes: 25 additions & 15 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1804,48 +1804,58 @@ func TestRunLocalWithArgs(t *testing.T) {
kpmcli.SetLogWriter(logbuf)

tests := []struct {
inputs []string
workdir string
withVendor bool
diagnostic string
expected string
inputs []string
settingsFiles []string
workdir string
withVendor bool
diagnostic string
expected string
}{
{
[]string{filepath.Join(pkgPath, "with_args", "run_0", "main.k")}, filepath.Join(pkgPath, "with_args", "run_0"),
[]string{filepath.Join(pkgPath, "with_args", "run_0", "main.k")}, []string{}, filepath.Join(pkgPath, "with_args", "run_0"),
false, "", "The_first_kcl_program: Hello World!"},
{
[]string{filepath.Join(pkgPath, "with_args", "run_1", "main.k")}, filepath.Join(pkgPath, "with_args", "run_1"),
[]string{filepath.Join(pkgPath, "with_args", "run_1", "main.k")}, []string{}, filepath.Join(pkgPath, "with_args", "run_1"),
false, "", "The_first_kcl_program: Hello World!"},
{[]string{
filepath.Join(pkgPath, "with_args", "run_2", "base.k"),
filepath.Join(pkgPath, "with_args", "run_2", "main.k"),
}, filepath.Join(pkgPath, "with_args", "run_2"), false, "", "base: Base\nThe_first_kcl_program: Hello World!"},
}, []string{}, filepath.Join(pkgPath, "with_args", "run_2"), false, "", "base: Base\nThe_first_kcl_program: Hello World!"},
{[]string{
filepath.Join(pkgPath, "with_args", "run_3", "main.k"),
}, filepath.Join(pkgPath, "with_args", "run_3"), false, "", "The_first_kcl_program: Hello World!"},
}, []string{}, filepath.Join(pkgPath, "with_args", "run_3"), false, "", "The_first_kcl_program: Hello World!"},
{[]string{
filepath.Join(pkgPath, "with_args", "run_4", "main.k"),
}, filepath.Join(pkgPath, "with_args", "run_4"), false, "", "The_first_kcl_program: Hello World!"},
}, []string{}, filepath.Join(pkgPath, "with_args", "run_4"), false, "", "The_first_kcl_program: Hello World!"},
{[]string{
filepath.Join(pkgPath, "with_args", "run_5"),
}, filepath.Join(pkgPath, "with_args", "run_5"), false, "", "The_first_kcl_program: Hello World!"},
}, []string{}, filepath.Join(pkgPath, "with_args", "run_5"), false, "", "The_first_kcl_program: Hello World!"},
{[]string{
filepath.Join(pkgPath, "with_args", "run_6"),
}, filepath.Join(pkgPath, "with_args", "run_6"), false, "", "The_first_kcl_program: Hello World!"},
}, []string{}, filepath.Join(pkgPath, "with_args", "run_6"), false, "", "The_first_kcl_program: Hello World!"},
{[]string{
filepath.Join(pkgPath, "with_args", "run_7"),
}, filepath.Join(pkgPath, "with_args", "run_7"), false, "", "base: Base\nThe_first_kcl_program: Hello World!"},
}, []string{}, filepath.Join(pkgPath, "with_args", "run_7"), false, "", "base: Base\nThe_first_kcl_program: Hello World!"},
{[]string{
filepath.Join(pkgPath, "with_args", "run_8"),
}, filepath.Join(pkgPath, "with_args", "run_8"), false, "", "The_first_kcl_program: Hello World!"},
}, []string{}, filepath.Join(pkgPath, "with_args", "run_8"), false, "", "sub: SUB"},
{[]string{
filepath.Join(pkgPath, "with_args", "run_9"),
}, filepath.Join(pkgPath, "with_args", "run_9"), false, "", "The_first_kcl_program: Hello World!"},
}, []string{}, filepath.Join(pkgPath, "with_args", "run_9"), false, "", "The_sub_kcl_program: Hello Sub World!"},
{[]string{}, []string{
filepath.Join(pkgPath, "with_args", "run_10", "sub", "kcl.yaml"),
}, filepath.Join(pkgPath, "with_args", "run_10"), false, "", "The_sub_kcl_program_1: Hello Sub World 1!"},
{[]string{
filepath.Join(pkgPath, "with_args", "run_11", "sub", "sub.k"),
}, []string{
filepath.Join(pkgPath, "with_args", "run_11", "sub", "kcl.yaml"),
}, filepath.Join(pkgPath, "with_args", "run_11"), false, "", "The_sub_kcl_program: Hello Sub World!"},
}

for _, test := range tests {
res, err := kpmcli.Run(
WithRunSourceUrls(test.inputs),
WithSettingFiles(test.settingsFiles),
WithWorkDir(test.workdir),
)

Expand Down
96 changes: 61 additions & 35 deletions pkg/client/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,52 +326,52 @@ func WithVendor(vendor bool) RunOption {
}

// applyCompileOptionsFromYaml applies the compile options from the kcl.yaml file.
func (o *RunOptions) applyCompileOptionsFromYaml(workdir string) bool {
succeed := false
func (o *RunOptions) getCompileOptionsFromYaml(workdir string) *kcl.Option {
resOpts := kcl.NewOption()
var settingsYamlDir string
// load the kcl.yaml from cli
if len(o.settingYamlFiles) != 0 {
for _, settingYamlFile := range o.settingYamlFiles {
o.Merge(kcl.WithSettings(settingYamlFile))
succeed = true
settingsYamlDir = filepath.Dir(settingYamlFile)
resOpts.Merge(kcl.WithSettings(settingYamlFile))
}
} else {
// load the kcl.yaml from the workdir
// If the workdir is not empty, try to find the settings.yaml file in the workdir.
settingsYamlPath := filepath.Join(workdir, constants.KCL_YAML)
settingsYamlDir = workdir
settingsYamlPath := filepath.Join(settingsYamlDir, constants.KCL_YAML)
if utils.DirExists(settingsYamlPath) {
o.Merge(kcl.WithSettings(settingsYamlPath))
succeed = true
resOpts.Merge(kcl.WithSettings(settingsYamlPath))
}
}

// transform the relative path to the absolute path in kcl.yaml by workdir
var updatedKFilenameList []string
for _, kfile := range o.KFilenameList {
for _, kfile := range resOpts.KFilenameList {
if !filepath.IsAbs(kfile) && !utils.IsModRelativePath(kfile) {
kfile = filepath.Join(workdir, kfile)
kfile = filepath.Join(settingsYamlDir, kfile)
}
updatedKFilenameList = append(updatedKFilenameList, kfile)
}
o.KFilenameList = updatedKFilenameList
resOpts.KFilenameList = updatedKFilenameList

return succeed
return resOpts
}

// applyCompileOptionsFromKclMod applies the compile options from the kcl.mod file.
func (o *RunOptions) applyCompileOptionsFromKclMod(kclPkg *pkg.KclPkg) bool {
o.Merge(*kclPkg.GetKclOpts())

func getCompileOptionsFromKclMod(kclPkg *pkg.KclPkg) *kcl.Option {
resOpts := kcl.NewOption()
resOpts.Merge(*kclPkg.GetKclOpts())
var updatedKFilenameList []string
// transform the relative path to the absolute path in kcl.yaml by kcl.mod path
for _, kfile := range o.KFilenameList {
for _, kfile := range resOpts.KFilenameList {
if !filepath.IsAbs(kfile) && !utils.IsModRelativePath(kfile) {
kfile = filepath.Join(kclPkg.HomePath, kfile)
}
updatedKFilenameList = append(updatedKFilenameList, kfile)
}
o.KFilenameList = updatedKFilenameList

return len(o.KFilenameList) != 0
resOpts.KFilenameList = updatedKFilenameList
return resOpts
}

// applyCompileOptions applies the compile options from cli, kcl.yaml and kcl.mod.
Expand All @@ -383,7 +383,7 @@ func (o *RunOptions) applyCompileOptions(source downloader.Source, kclPkg *pkg.K
var compiledFiles []string
// All the cli relative path should be transformed to the absolute path by workdir
for _, source := range o.Sources {
if source.IsLocalPath() {
if source.IsLocalPath() && source.Path != kclPkg.HomePath {
sPath := source.Path
if !filepath.IsAbs(sPath) && !utils.IsModRelativePath(sPath) {
sPath = filepath.Join(workDir, sPath)
Expand All @@ -392,23 +392,49 @@ func (o *RunOptions) applyCompileOptions(source downloader.Source, kclPkg *pkg.K
}
}
o.KFilenameList = compiledFiles
if len(o.KFilenameList) == 0 {
if !o.applyCompileOptionsFromKclMod(kclPkg) {
o.KFilenameList = []string{kclPkg.HomePath}
}
}
}

cliOpts := o.Option
// Get the compile options from kcl.mod
modOpts := getCompileOptionsFromKclMod(kclPkg)

// Get the compile options from kcl.yaml
var yamlOpts *kcl.Option
// For the packaged kcl package, git, oci and tar, settings yaml file should be find from the package path if not set by cli.
// For the local kcl package, settings yaml file should be find from the workdir if not set by cli.
if source.IsPackaged() {
yamlOpts = o.getCompileOptionsFromYaml(kclPkg.HomePath)
} else {
// If the sources from cli is empty, try to apply the compile options from kcl.yaml
if !o.applyCompileOptionsFromYaml(workDir) {
// If the sources from kcl.yaml is empty, try to apply the compile options from kcl.mod
if !o.applyCompileOptionsFromKclMod(kclPkg) {
// If the sources from kcl.mod is empty, compile the current sources.
if source.IsLocalPath() {
o.KFilenameList = []string{workDir}
} else {
o.KFilenameList = []string{kclPkg.HomePath}
}
}
yamlOpts = o.getCompileOptionsFromYaml(workDir)
}

// Merge the compile options from cli, kcl.mod and kcl.yaml
// The options from cli will override the options from kcl.mod and kcl.yaml
// The options from kcl.yaml will override the options from kcl.mod
o.Option = kcl.NewOption()
o.Merge(*modOpts).Merge(*yamlOpts).Merge(*cliOpts)
if len(modOpts.KFilenameList) != 0 {
o.KFilenameList = modOpts.KFilenameList
}

if len(yamlOpts.KFilenameList) != 0 {
o.KFilenameList = yamlOpts.KFilenameList
}

if len(cliOpts.KFilenameList) != 0 {
o.KFilenameList = cliOpts.KFilenameList
}

// There is no compiled files from cli, kcl.mod and kcl.yaml,
// use the workdir as input.
// the workdir is the dictionary which executes `kcl run`
if len(o.KFilenameList) == 0 {
// For the packaged kcl package, git, oci and tar, no *.k files are set by cli, kcl.yaml or kcl.mod, compile the package path.
// For the local kcl package, no *.k files are set by cli, kcl.yaml or kcl.mod, compile the workdir.
if source.IsPackaged() {
o.KFilenameList = []string{kclPkg.HomePath}
} else {
o.KFilenameList = []string{workDir}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "run_10"
edition = "v0.10.0"
version = "0.0.1"


Empty file.
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,3 @@
kcl_cli_configs:
files:
- ../sub1/sub.k
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The_sub_kcl_program = "Hello Sub World!"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The_sub_kcl_program_1 = "Hello Sub World 1!"
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "run_11"
edition = "v0.10.0"
version = "0.0.1"

[profile]
entries = ["sub/sub.k"]
Empty file.
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,3 @@
kcl_cli_configs:
files:
- ../sub1/sub.k
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The_sub_kcl_program = "Hello Sub World!"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The_sub_kcl_program_1 = "Hello Sub World 1!"

0 comments on commit 63eaf4e

Please sign in to comment.