Skip to content

feat: align the compile arguments in kcl.mod with the kcl.yaml #222

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 4, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/api/kpm_run.go
Original file line number Diff line number Diff line change
@@ -243,7 +243,7 @@ func run(kpmcli *client.KpmClient, opts *opt.CompileOptions) (*kcl.KCLResultList
}
}
// add entry from 'kcl.mod'
} else if len(kclPkg.GetEntryKclFilesFromModFile()) > 0 {
} else if kclPkg.HasProfile() {
opts.Merge(*kclPkg.GetKclOpts())
} else if !opts.HasSettingsYaml() {
// no entry
8 changes: 3 additions & 5 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
@@ -313,7 +313,8 @@ func (c *KpmClient) CompileWithOpts(opts *opt.CompileOptions) (*kcl.KCLResultLis
if err != (*reporter.KpmEvent)(nil) {
return nil, err
}

// add all the options from 'kcl.mod'
opts.Merge(*kclPkg.GetKclOpts())
if len(opts.Entries()) > 0 {
// add entry from '--input'
for _, entry := range opts.Entries() {
@@ -323,10 +324,7 @@ func (c *KpmClient) CompileWithOpts(opts *opt.CompileOptions) (*kcl.KCLResultLis
opts.Merge(kcl.WithKFilenames(filepath.Join(opts.PkgPath(), entry)))
}
}
// add entry from 'kcl.mod'
} else if len(kclPkg.GetEntryKclFilesFromModFile()) > 0 {
opts.Merge(*kclPkg.GetKclOpts())
} else if !opts.HasSettingsYaml() {
} else if len(kclPkg.GetEntryKclFilesFromModFile()) == 0 && !opts.HasSettingsYaml() {
// no entry
opts.Merge(kcl.WithKFilenames(opts.PkgPath()))
}
1 change: 1 addition & 0 deletions pkg/cmd/cmd_flags.go
Original file line number Diff line number Diff line change
@@ -13,4 +13,5 @@ const FLAG_DISABLE_NONE = "disable_none"
const FLAG_ARGUMENT = "argument"
const FLAG_OVERRIDES = "overrides"
const FLAG_SORT_KEYS = "sort_keys"

const FLAG_QUIET = "quiet"
14 changes: 2 additions & 12 deletions pkg/cmd/cmd_run.go
Original file line number Diff line number Diff line change
@@ -166,20 +166,10 @@ func CompileOptionFromCli(c *cli.Context) *opt.CompileOptions {
}

// --argument, -D
argumentsOpt := c.StringSlice(FLAG_ARGUMENT)
if len(argumentsOpt) != 0 {
for _, arg := range argumentsOpt {
opts.Merge(kcl.WithOptions(arg))
}
}
opts.Merge(kcl.WithOptions(c.StringSlice(FLAG_ARGUMENT)...))

// --overrides, -O
overridesOpt := c.StringSlice(FLAG_OVERRIDES)
if len(overridesOpt) != 0 {
for _, override := range overridesOpt {
opts.Merge(kcl.WithOverrides(override))
}
}
opts.Merge(kcl.WithOverrides(c.StringSlice(FLAG_OVERRIDES)...))

// --disable_none, -n
opts.Merge(kcl.WithDisableNone(c.Bool(FLAG_DISABLE_NONE)))
65 changes: 53 additions & 12 deletions pkg/package/modfile.go
Original file line number Diff line number Diff line change
@@ -34,41 +34,74 @@ type ModFile struct {
// Whether the current package uses the vendor mode
// In the vendor mode, kpm will look for the package in the vendor subdirectory
// in the current package directory.
VendorMode bool `toml:"-"`
Profiles Profile `toml:"profile"`
VendorMode bool `toml:"-"`
Profiles *Profile `toml:"profile"`
Dependencies
}

// Profile is the profile section of 'kcl.mod'.
// It is used to specify the compilation options of the current package.
type Profile struct {
Entries []string `toml:"entries"`
Entries *[]string `toml:"entries"`
DisableNone *bool `toml:"disable_none"`
SortKeys *bool `toml:"sort_keys"`
Selectors *[]string `toml:"selectors"`
Overrides *[]string `toml:"overrides"`
Options *[]string `toml:"arguments"`
}

// NewProfile will create a new profile.
func NewProfile() Profile {
return Profile{
Entries: []string{},
}
return Profile{}
}

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

opts := kcl.NewOption()

for _, entry := range profile.Entries {
ext := filepath.Ext(entry)
if ext == ".yaml" {
opts.Merge(kcl.WithSettings(entry))
} else {
opts.Merge(kcl.WithKFilenames(entry))
if profile.Entries != nil {
for _, entry := range *profile.Entries {
ext := filepath.Ext(entry)
if ext == ".yaml" {
opts.Merge(kcl.WithSettings(entry))
} else {
opts.Merge(kcl.WithKFilenames(entry))
}
}
}

if profile.DisableNone != nil {
opts.Merge(kcl.WithDisableNone(*profile.DisableNone))
}

if profile.SortKeys != nil {
opts.Merge(kcl.WithSortKeys(*profile.SortKeys))
}

if profile.Selectors != nil {
opts.Merge(kcl.WithSelectors(*profile.Selectors...))
}

if profile.Overrides != nil {
opts.Merge(kcl.WithOverrides(*profile.Overrides...))
}

if profile.Options != nil {
opts.Merge(kcl.WithOptions(*profile.Options...))
}

return opts
}

// GetEntries will get the entry kcl files from profile.
func (profile *Profile) GetEntries() []string {
if profile.Entries == nil {
return []string{}
}
return *profile.Entries
}

// FillDependenciesInfo will fill registry information for all dependencies in a kcl.mod.
func (modFile *ModFile) FillDependenciesInfo() error {
for k, v := range modFile.Deps {
@@ -81,6 +114,14 @@ func (modFile *ModFile) FillDependenciesInfo() error {
return nil
}

// GetEntries will get the entry kcl files from kcl.mod.
func (modFile *ModFile) GetEntries() []string {
if modFile.Profiles == nil {
return []string{}
}
return modFile.Profiles.GetEntries()
}

// 'Dependencies' is dependencies section of 'kcl.mod'.
type Dependencies struct {
Deps map[string]Dependency `json:"packages" toml:"dependencies,omitempty"`
12 changes: 10 additions & 2 deletions pkg/package/package.go
Original file line number Diff line number Diff line change
@@ -60,12 +60,20 @@ func LoadKclPkgFromTar(pkgTarPath string) (*KclPkg, error) {

// GetKclOpts will return the kcl options from kcl.mod.
func (kclPkg *KclPkg) GetKclOpts() *kcl.Option {
if kclPkg.ModFile.Profiles == nil {
return kcl.NewOption()
}
return kclPkg.ModFile.Profiles.IntoKclOptions()
}

// GetEntryKclFilesFromModFile will return the entry kcl files from kcl.mod.
func (kclPkg *KclPkg) GetEntryKclFilesFromModFile() []string {
return kclPkg.ModFile.Profiles.Entries
return kclPkg.ModFile.GetEntries()
}

// HasProfile will return true if the current kcl package has the profile.
func (kclPkg *KclPkg) HasProfile() bool {
return kclPkg.ModFile.Profiles != nil
}

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

// GetPkgProfile returns the profile of package.
func (kclPkg *KclPkg) GetPkgProfile() Profile {
return kclPkg.ModFile.Profiles
return *kclPkg.ModFile.Profiles
}

// GetPkgTarName returns the kcl package tar name "<package_name>-v<package_version>.tar"
4 changes: 2 additions & 2 deletions pkg/package/toml.go
Original file line number Diff line number Diff line change
@@ -147,7 +147,7 @@ const PROFILE_PATTERN = "[profile]"

func (p *Profile) MarshalTOML() string {
var sb strings.Builder
if len(p.Entries) != 0 {
if p != nil {
sb.WriteString(PROFILE_PATTERN)
sb.WriteString(NEWLINE)
var buf bytes.Buffer
@@ -202,7 +202,7 @@ func (mod *ModFile) UnmarshalTOML(data interface{}) error {
if err != nil {
return err
}
mod.Profiles = p
mod.Profiles = &p
}
return nil
}
2 changes: 1 addition & 1 deletion pkg/package/toml_test.go
Original file line number Diff line number Diff line change
@@ -167,5 +167,5 @@ func TestUnMarshalTOMLWithProfile(t *testing.T) {
assert.Equal(t, modfile.Pkg.Name, "kpm")
assert.Equal(t, modfile.Pkg.Version, "0.0.1")
assert.Equal(t, modfile.Pkg.Edition, "0.0.1")
assert.Equal(t, modfile.Profiles.Entries, []string{"main.k", "xxx/xxx/dir", "test.yaml"})
assert.Equal(t, *modfile.Profiles.Entries, []string{"main.k", "xxx/xxx/dir", "test.yaml"})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
KPM_HOME=""
KCLVM_VENDOR_HOME=""
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
kpm run
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
KPM_HOME=""
KCLVM_VENDOR_HOME=""
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
kpm run
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
The_first_kcl_program1: Hello1
The_first_kcl_program2: Hello2
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
KPM_HOME=""
KCLVM_VENDOR_HOME=""
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
kpm run
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
a1:
image: default
name: a1-app
a2:
image: new-a2-image:v123
name: app
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
KPM_HOME=""
KCLVM_VENDOR_HOME=""
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
kpm run
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
yellow
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
KPM_HOME=""
KCLVM_VENDOR_HOME=""
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
kpm run
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
a: 2
b: 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "test_kpm_run_with_disable_none"
edition = "0.0.1"
version = "0.0.1"

[profile]
path_selector = true
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The_first_kcl_program = None
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "test_kpm_run_with_option"
edition = "0.0.1"
version = "0.0.1"

[profile]
arguments = ["name1=Hello1", "name2=Hello2"]

Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
The_first_kcl_program1 = option("name1")
The_first_kcl_program2 = option("name2")
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "test_kpm_run_with_override"
edition = "0.0.1"
version = "0.0.1"

[profile]
overrides = [ "__main__:a2.image=\"new-a2-image:v123\"" ]
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
schema App:
image: str = "default"
name: str = "app"

a1 = App {
name = "a1-app"
}

a2 = App {
image = "a2"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "test_kpm_run_with_disable_none"
edition = "0.0.1"
version = "0.0.1"

[profile]
path_selector = "alice.labels.skin"
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
schema Person:
labels: {str:str}

alice = Person {
"labels": {"skin": "yellow"}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "test_kpm_run_with_sort_key"
edition = "0.0.1"
version = "0.0.1"

[profile]
sort_keys = true
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
b = 1
a = 2