Skip to content

Commit

Permalink
Merge pull request #276 from liangyuanpeng/mod_opt_version
Browse files Browse the repository at this point in the history
Add version option for mod init
  • Loading branch information
Peefy committed Mar 14, 2024
2 parents ab7426f + 36a43ff commit 116d6e4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
8 changes: 8 additions & 0 deletions pkg/opt/opt.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"path/filepath"

"github.com/hashicorp/go-version"
"kcl-lang.io/kcl-go/pkg/kcl"
"kcl-lang.io/kpm/pkg/errors"
"kcl-lang.io/kpm/pkg/reporter"
Expand Down Expand Up @@ -144,6 +145,7 @@ func (opts *CompileOptions) LogWriter() io.Writer {
type InitOptions struct {
Name string
InitPath string
Version string
}

func (opts *InitOptions) Validate() error {
Expand All @@ -152,6 +154,12 @@ func (opts *InitOptions) Validate() error {
} else if len(opts.InitPath) == 0 {
return errors.InternalBug
}
if opts.Version != "" {
if _, err := version.NewSemver(opts.Version); err != nil {
return errors.InvalidVersionFormat
}
}

return nil
}

Expand Down
10 changes: 10 additions & 0 deletions pkg/opt/opt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/stretchr/testify/assert"
"kcl-lang.io/kcl-go/pkg/kcl"
"kcl-lang.io/kpm/pkg/errors"
)

func TestWorkDirAsPkgPath(t *testing.T) {
Expand All @@ -20,3 +21,12 @@ func TestWorkDirAsPkgPath(t *testing.T) {
opts.SetEntries([]string{"override.k"})
assert.Equal(t, opts.Entries(), []string{"override.k"})
}

func TestInitOptions(t *testing.T) {
o1 := InitOptions{Name: "foo", InitPath: "bar", Version: "v0.0.1"}
o2 := InitOptions{Name: "foo", InitPath: "bar", Version: "v0.0.2"}
o3 := InitOptions{Name: "foo", InitPath: "bar", Version: "abc.0.3"}
assert.Equal(t, o1.Validate(), nil)
assert.Equal(t, o2.Validate(), nil)
assert.Equal(t, o3.Validate(), errors.InvalidVersionFormat)
}
6 changes: 5 additions & 1 deletion pkg/package/modfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,14 +316,18 @@ func (mfile *ModFile) GetModLockFilePath() string {
}

const defaultVerion = "0.0.1"

var defaultEdition = runner.GetKclVersion()

func NewModFile(opts *opt.InitOptions) *ModFile {
if opts.Version == "" {
opts.Version = defaultVerion
}
return &ModFile{
HomePath: opts.InitPath,
Pkg: Package{
Name: opts.Name,
Version: defaultVerion,
Version: opts.Version,
Edition: defaultEdition,
},
Dependencies: Dependencies{
Expand Down

0 comments on commit 116d6e4

Please sign in to comment.