Skip to content

Commit 3684f9e

Browse files
committed
feat: align the compile arguments in kcl.mod with the kcl.yaml
Signed-off-by: zongz <zongzhe1024@163.com>
1 parent 0c34bfc commit 3684f9e

File tree

42 files changed

+157
-34
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+157
-34
lines changed

pkg/api/kpm_run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ func run(kpmcli *client.KpmClient, opts *opt.CompileOptions) (*kcl.KCLResultList
243243
}
244244
}
245245
// add entry from 'kcl.mod'
246-
} else if len(kclPkg.GetEntryKclFilesFromModFile()) > 0 {
246+
} else if kclPkg.HasProfile() {
247247
opts.Merge(*kclPkg.GetKclOpts())
248248
} else if !opts.HasSettingsYaml() {
249249
// no entry

pkg/client/client.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,8 @@ func (c *KpmClient) CompileWithOpts(opts *opt.CompileOptions) (*kcl.KCLResultLis
313313
if err != (*reporter.KpmEvent)(nil) {
314314
return nil, err
315315
}
316-
316+
// add all the options from 'kcl.mod'
317+
opts.Merge(*kclPkg.GetKclOpts())
317318
if len(opts.Entries()) > 0 {
318319
// add entry from '--input'
319320
for _, entry := range opts.Entries() {
@@ -323,10 +324,7 @@ func (c *KpmClient) CompileWithOpts(opts *opt.CompileOptions) (*kcl.KCLResultLis
323324
opts.Merge(kcl.WithKFilenames(filepath.Join(opts.PkgPath(), entry)))
324325
}
325326
}
326-
// add entry from 'kcl.mod'
327-
} else if len(kclPkg.GetEntryKclFilesFromModFile()) > 0 {
328-
opts.Merge(*kclPkg.GetKclOpts())
329-
} else if !opts.HasSettingsYaml() {
327+
} else if len(kclPkg.GetEntryKclFilesFromModFile()) == 0 && !opts.HasSettingsYaml() {
330328
// no entry
331329
opts.Merge(kcl.WithKFilenames(opts.PkgPath()))
332330
}

pkg/cmd/cmd_flags.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ const FLAG_DISABLE_NONE = "disable_none"
1313
const FLAG_ARGUMENT = "argument"
1414
const FLAG_OVERRIDES = "overrides"
1515
const FLAG_SORT_KEYS = "sort_keys"
16+
1617
const FLAG_QUIET = "quiet"

pkg/cmd/cmd_run.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -166,20 +166,10 @@ func CompileOptionFromCli(c *cli.Context) *opt.CompileOptions {
166166
}
167167

168168
// --argument, -D
169-
argumentsOpt := c.StringSlice(FLAG_ARGUMENT)
170-
if len(argumentsOpt) != 0 {
171-
for _, arg := range argumentsOpt {
172-
opts.Merge(kcl.WithOptions(arg))
173-
}
174-
}
169+
opts.Merge(kcl.WithOptions(c.StringSlice(FLAG_ARGUMENT)...))
175170

176171
// --overrides, -O
177-
overridesOpt := c.StringSlice(FLAG_OVERRIDES)
178-
if len(overridesOpt) != 0 {
179-
for _, override := range overridesOpt {
180-
opts.Merge(kcl.WithOverrides(override))
181-
}
182-
}
172+
opts.Merge(kcl.WithOverrides(c.StringSlice(FLAG_OVERRIDES)...))
183173

184174
// --disable_none, -n
185175
opts.Merge(kcl.WithDisableNone(c.Bool(FLAG_DISABLE_NONE)))

pkg/package/modfile.go

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,41 +34,74 @@ type ModFile struct {
3434
// Whether the current package uses the vendor mode
3535
// In the vendor mode, kpm will look for the package in the vendor subdirectory
3636
// in the current package directory.
37-
VendorMode bool `toml:"-"`
38-
Profiles Profile `toml:"profile"`
37+
VendorMode bool `toml:"-"`
38+
Profiles *Profile `toml:"profile"`
3939
Dependencies
4040
}
4141

4242
// Profile is the profile section of 'kcl.mod'.
4343
// It is used to specify the compilation options of the current package.
4444
type Profile struct {
45-
Entries []string `toml:"entries"`
45+
Entries *[]string `toml:"entries"`
46+
DisableNone *bool `toml:"disable_none"`
47+
SortKeys *bool `toml:"sort_keys"`
48+
Selectors *[]string `toml:"selectors"`
49+
Overrides *[]string `toml:"overrides"`
50+
Options *[]string `toml:"arguments"`
4651
}
4752

4853
// NewProfile will create a new profile.
4954
func NewProfile() Profile {
50-
return Profile{
51-
Entries: []string{},
52-
}
55+
return Profile{}
5356
}
5457

5558
// IntoKclOptions will transform the profile into kcl options.
5659
func (profile *Profile) IntoKclOptions() *kcl.Option {
5760

5861
opts := kcl.NewOption()
5962

60-
for _, entry := range profile.Entries {
61-
ext := filepath.Ext(entry)
62-
if ext == ".yaml" {
63-
opts.Merge(kcl.WithSettings(entry))
64-
} else {
65-
opts.Merge(kcl.WithKFilenames(entry))
63+
if profile.Entries != nil {
64+
for _, entry := range *profile.Entries {
65+
ext := filepath.Ext(entry)
66+
if ext == ".yaml" {
67+
opts.Merge(kcl.WithSettings(entry))
68+
} else {
69+
opts.Merge(kcl.WithKFilenames(entry))
70+
}
6671
}
6772
}
6873

74+
if profile.DisableNone != nil {
75+
opts.Merge(kcl.WithDisableNone(*profile.DisableNone))
76+
}
77+
78+
if profile.SortKeys != nil {
79+
opts.Merge(kcl.WithSortKeys(*profile.SortKeys))
80+
}
81+
82+
if profile.Selectors != nil {
83+
opts.Merge(kcl.WithSelectors(*profile.Selectors...))
84+
}
85+
86+
if profile.Overrides != nil {
87+
opts.Merge(kcl.WithOverrides(*profile.Overrides...))
88+
}
89+
90+
if profile.Options != nil {
91+
opts.Merge(kcl.WithOptions(*profile.Options...))
92+
}
93+
6994
return opts
7095
}
7196

97+
// GetEntries will get the entry kcl files from profile.
98+
func (profile *Profile) GetEntries() []string {
99+
if profile.Entries == nil {
100+
return []string{}
101+
}
102+
return *profile.Entries
103+
}
104+
72105
// FillDependenciesInfo will fill registry information for all dependencies in a kcl.mod.
73106
func (modFile *ModFile) FillDependenciesInfo() error {
74107
for k, v := range modFile.Deps {
@@ -81,6 +114,14 @@ func (modFile *ModFile) FillDependenciesInfo() error {
81114
return nil
82115
}
83116

117+
// GetEntries will get the entry kcl files from kcl.mod.
118+
func (modFile *ModFile) GetEntries() []string {
119+
if modFile.Profiles == nil {
120+
return []string{}
121+
}
122+
return modFile.Profiles.GetEntries()
123+
}
124+
84125
// 'Dependencies' is dependencies section of 'kcl.mod'.
85126
type Dependencies struct {
86127
Deps map[string]Dependency `json:"packages" toml:"dependencies,omitempty"`

pkg/package/package.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,20 @@ func LoadKclPkgFromTar(pkgTarPath string) (*KclPkg, error) {
6060

6161
// GetKclOpts will return the kcl options from kcl.mod.
6262
func (kclPkg *KclPkg) GetKclOpts() *kcl.Option {
63+
if kclPkg.ModFile.Profiles == nil {
64+
return kcl.NewOption()
65+
}
6366
return kclPkg.ModFile.Profiles.IntoKclOptions()
6467
}
6568

6669
// GetEntryKclFilesFromModFile will return the entry kcl files from kcl.mod.
6770
func (kclPkg *KclPkg) GetEntryKclFilesFromModFile() []string {
68-
return kclPkg.ModFile.Profiles.Entries
71+
return kclPkg.ModFile.GetEntries()
72+
}
73+
74+
// HasProfile will return true if the current kcl package has the profile.
75+
func (kclPkg *KclPkg) HasProfile() bool {
76+
return kclPkg.ModFile.Profiles != nil
6977
}
7078

7179
func (kclPkg *KclPkg) IsVendorMode() bool {
@@ -170,7 +178,7 @@ func (kclPkg *KclPkg) GetPkgEdition() string {
170178

171179
// GetPkgProfile returns the profile of package.
172180
func (kclPkg *KclPkg) GetPkgProfile() Profile {
173-
return kclPkg.ModFile.Profiles
181+
return *kclPkg.ModFile.Profiles
174182
}
175183

176184
// GetPkgTarName returns the kcl package tar name "<package_name>-v<package_version>.tar"

pkg/package/toml.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ const PROFILE_PATTERN = "[profile]"
147147

148148
func (p *Profile) MarshalTOML() string {
149149
var sb strings.Builder
150-
if len(p.Entries) != 0 {
150+
if p != nil {
151151
sb.WriteString(PROFILE_PATTERN)
152152
sb.WriteString(NEWLINE)
153153
var buf bytes.Buffer
@@ -202,7 +202,7 @@ func (mod *ModFile) UnmarshalTOML(data interface{}) error {
202202
if err != nil {
203203
return err
204204
}
205-
mod.Profiles = p
205+
mod.Profiles = &p
206206
}
207207
return nil
208208
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
KPM_HOME=""
2+
KCLVM_VENDOR_HOME=""
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
kpm run

test/e2e/test_suites/kpm/kpm_run/test_kpm_run_with_disable_none_profile/test_suite.stderr

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
KPM_HOME=""
2+
KCLVM_VENDOR_HOME=""
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
kpm run

test/e2e/test_suites/kpm/kpm_run/test_kpm_run_with_option_profile/test_suite.stderr

Whitespace-only changes.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
The_first_kcl_program1: Hello1
2+
The_first_kcl_program2: Hello2
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
KPM_HOME=""
2+
KCLVM_VENDOR_HOME=""
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
kpm run

test/e2e/test_suites/kpm/kpm_run/test_kpm_run_with_override_profile/test_suite.stderr

Whitespace-only changes.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
a1:
2+
image: default
3+
name: a1-app
4+
a2:
5+
image: new-a2-image:v123
6+
name: app
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
KPM_HOME=""
2+
KCLVM_VENDOR_HOME=""
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
kpm run

test/e2e/test_suites/kpm/kpm_run/test_kpm_run_with_path_selector_profile/test_suite.stderr

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
yellow
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
KPM_HOME=""
2+
KCLVM_VENDOR_HOME=""
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
kpm run

test/e2e/test_suites/kpm/kpm_run/test_kpm_run_with_sort_key_profile/test_suite.stderr

Whitespace-only changes.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
a: 2
2+
b: 1
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "test_kpm_run_with_disable_none"
3+
edition = "0.0.1"
4+
version = "0.0.1"
5+
6+
[profile]
7+
path_selector = true

test/e2e/test_suites/test_data/test_kpm_run_with_disable_none_profile/kcl.mod.lock

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The_first_kcl_program = None
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "test_kpm_run_with_option"
3+
edition = "0.0.1"
4+
version = "0.0.1"
5+
6+
[profile]
7+
arguments = ["name1=Hello1", "name2=Hello2"]
8+

test/e2e/test_suites/test_data/test_kpm_run_with_option_profile/kcl.mod.lock

Whitespace-only changes.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
The_first_kcl_program1 = option("name1")
2+
The_first_kcl_program2 = option("name2")
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "test_kpm_run_with_override"
3+
edition = "0.0.1"
4+
version = "0.0.1"
5+
6+
[profile]
7+
overrides = [ "__main__:a2.image=\"new-a2-image:v123\"" ]

test/e2e/test_suites/test_data/test_kpm_run_with_override_profile/kcl.mod.lock

Whitespace-only changes.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
schema App:
2+
image: str = "default"
3+
name: str = "app"
4+
5+
a1 = App {
6+
name = "a1-app"
7+
}
8+
9+
a2 = App {
10+
image = "a2"
11+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "test_kpm_run_with_disable_none"
3+
edition = "0.0.1"
4+
version = "0.0.1"
5+
6+
[profile]
7+
path_selector = "alice.labels.skin"

test/e2e/test_suites/test_data/test_kpm_run_with_path_selector_profile/kcl.mod.lock

Whitespace-only changes.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
schema Person:
2+
labels: {str:str}
3+
4+
alice = Person {
5+
"labels": {"skin": "yellow"}
6+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "test_kpm_run_with_sort_key"
3+
edition = "0.0.1"
4+
version = "0.0.1"
5+
6+
[profile]
7+
sort_keys = true

test/e2e/test_suites/test_data/test_kpm_run_with_sort_key_profile/kcl.mod.lock

Whitespace-only changes.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
b = 1
2+
a = 2

0 commit comments

Comments
 (0)