Skip to content
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

Sparse checkout - kcl mod add --package #453

Merged
merged 29 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
11632e5
download a repo success
officialasishkumar Aug 17, 2024
0b57c39
change kcl.mod and kcl.mod.lock names with pkg name
officialasishkumar Aug 18, 2024
b913fbb
fix duplication of download in kcl.mod
officialasishkumar Aug 18, 2024
5fc146e
merge package functionality to kcl mod run
officialasishkumar Aug 18, 2024
53b67a2
fix seg fault in test case
officialasishkumar Aug 18, 2024
56b3ff8
remove last changes
officialasishkumar Aug 18, 2024
a8e640f
fix seg fault in e2e
officialasishkumar Aug 18, 2024
af3a900
remove pkg flag from kpmClient
officialasishkumar Aug 19, 2024
a45ba80
add unit test for FindPackage and matchesPackageName
officialasishkumar Aug 19, 2024
a16e457
new unit test for parseopt
officialasishkumar Aug 19, 2024
e4a4dd2
add unit test for download with git and package flag
officialasishkumar Aug 19, 2024
64621be
update parseOpt unit test
officialasishkumar Aug 19, 2024
7558424
test folder for utils_test
officialasishkumar Aug 19, 2024
5f4b6d1
new unit test
officialasishkumar Aug 19, 2024
766ce39
fix seg fault
officialasishkumar Aug 19, 2024
4221eb3
add unit test for mod file when package flag is used
officialasishkumar Aug 19, 2024
ecf16e6
fix failing unit test
officialasishkumar Aug 19, 2024
3e9d372
fix failing unit test
officialasishkumar Aug 19, 2024
e06bb6c
remove the contents of the generated lock files
officialasishkumar Aug 19, 2024
8cdac96
revert
officialasishkumar Aug 19, 2024
c8ffe70
add windows test
officialasishkumar Aug 20, 2024
b36c95f
add test folder
officialasishkumar Aug 20, 2024
fee67ab
fix failing unit test
officialasishkumar Aug 20, 2024
b74e785
remove the filter
officialasishkumar Aug 20, 2024
f973335
add nosumcheck
officialasishkumar Aug 20, 2024
e0c8c5a
nosumcheck to true
officialasishkumar Aug 20, 2024
3bd9a77
remove sum from actual
officialasishkumar Aug 20, 2024
9b3de39
remove comments from client_test
officialasishkumar Aug 20, 2024
d3a0c63
remove comments from client_test
officialasishkumar Aug 20, 2024
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
38 changes: 31 additions & 7 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ type KpmClient struct {
settings settings.Settings
// The flag of whether to check the checksum of the package and update kcl.mod.lock.
noSumCheck bool
// The package to use in case of multiple packages.
pkg string
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may not be appropriate to add this field to KpmClient, which is the client of the package management tool, and it should not be associated with a specific package.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the comment @zong-zhe. I have removed it!

}

// NewKpmClient will create a new kpm client with default settings.
Expand All @@ -75,6 +77,16 @@ func NewKpmClient() (*KpmClient, error) {
}, nil
}

// SetPackage will set the 'package' to be used in case of multiple packages in a single repo.
func (c *KpmClient) SetPackage(pkgName string) {
c.pkg = pkgName
}

// GetPackage will get the 'package' to be used in case of multiple packages in a single repo.
func (c *KpmClient) GetPackage() string {
return c.pkg
}

// SetNoSumCheck will set the 'noSumCheck' flag.
func (c *KpmClient) SetNoSumCheck(noSumCheck bool) {
c.noSumCheck = noSumCheck
Expand Down Expand Up @@ -283,17 +295,20 @@ const PKG_NAME_PATTERN = "%s_%s"
// 2. in the vendor subdirectory of the current package.
// 3. the dependency is from the local path.
func (c *KpmClient) getDepStorePath(search_path string, d *pkg.Dependency, isVendor bool) string {

storePkgName := d.GenPathSuffix()

if d.IsFromLocal() {
return d.GetLocalFullPath(search_path)
} else {
path := ""
if isVendor {
return filepath.Join(search_path, "vendor", storePkgName)
path = filepath.Join(search_path, "vendor", storePkgName)
} else {
return filepath.Join(c.homePath, storePkgName)
path = filepath.Join(c.homePath, storePkgName)
}
if c.GetPackage() != "" {
path, _ = utils.FindPackage(path, c.GetPackage())
}
return path
}
}

Expand Down Expand Up @@ -747,7 +762,7 @@ func (c *KpmClient) AddDepWithOpts(kclPkg *pkg.KclPkg, opt *opt.AddOptions) (*pk
c.noSumCheck = opt.NoSumCheck
kclPkg.NoSumCheck = opt.NoSumCheck

// 1. get the name and version of the repository from the input arguments.
// 1. get the name and version of the repository/package from the input arguments.
d, err := pkg.ParseOpt(&opt.RegistryOpts)
if err != nil {
return nil, err
Expand Down Expand Up @@ -1101,7 +1116,16 @@ func (c *KpmClient) Download(dep *pkg.Dependency, homePath, localPath string) (*
return nil, err
}

dep.LocalFullPath = localPath
if c.GetPackage() != "" {
localFullPath, err := utils.FindPackage(localPath, c.GetPackage())
if err != nil {
return nil, err
}
dep.LocalFullPath = localFullPath
dep.Name = c.GetPackage()
} else {
dep.LocalFullPath = localPath
}
// Creating symbolic links in a global cache is not an optimal solution.
// This allows kclvm to locate the package by default.
// This feature is unstable and will be removed soon.
Expand All @@ -1111,7 +1135,7 @@ func (c *KpmClient) Download(dep *pkg.Dependency, homePath, localPath string) (*
// }
dep.FullName = dep.GenDepFullName()

modFile, err := c.LoadModFile(localPath)
modFile, err := c.LoadModFile(dep.LocalFullPath)
if err != nil {
return nil, err
}
Expand Down
21 changes: 18 additions & 3 deletions pkg/cmd/cmd_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ func NewAddCmd(kpmcli *client.KpmClient) *cli.Command {
Name: "rename",
Usage: "rename the package name in kcl.mod.lock",
},
&cli.StringSliceFlag{
Name: "package",
Usage: "package name to use in case of git",
},
},

Action: func(c *cli.Context) error {
Expand Down Expand Up @@ -135,6 +139,10 @@ func onlyOnceOption(c *cli.Context, name string) (string, *reporter.KpmEvent) {
func parseAddOptions(c *cli.Context, kpmcli *client.KpmClient, localPath string) (*opt.AddOptions, error) {
noSumCheck := c.Bool(FLAG_NO_SUM_CHECK)
newPkgName := c.String("rename")
pkg := c.StringSlice("package")
if len(pkg) >= 1 {
kpmcli.SetPackage(pkg[0])
}
// parse from 'kpm add -git https://xxx/xxx.git -tag v0.0.1'.
if c.NArg() == 0 {
gitOpts, err := parseGitRegistryOptions(c)
Expand Down Expand Up @@ -198,6 +206,12 @@ func parseGitRegistryOptions(c *cli.Context) (*opt.RegistryOptions, *reporter.Kp
return nil, err
}

gitPackage, err := onlyOnceOption(c, "package")

if err != (*reporter.KpmEvent)(nil) {
return nil, err
}

if gitUrl == "" {
return nil, reporter.NewErrorEvent(reporter.InvalidGitUrl, fmt.Errorf("the argument 'git' is required"))
}
Expand All @@ -208,9 +222,10 @@ func parseGitRegistryOptions(c *cli.Context) (*opt.RegistryOptions, *reporter.Kp

return &opt.RegistryOptions{
Git: &opt.GitOptions{
Url: gitUrl,
Tag: gitTag,
Commit: gitCommit,
Url: gitUrl,
Tag: gitTag,
Commit: gitCommit,
Package: gitPackage,
},
}, nil
}
Expand Down
1 change: 1 addition & 0 deletions pkg/downloader/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type Git struct {
Commit string `toml:"commit,omitempty"`
Tag string `toml:"git_tag,omitempty"`
Version string `toml:"version,omitempty"`
Package string `toml:"package,omitempty"`
}

type Registry struct {
Expand Down
12 changes: 12 additions & 0 deletions pkg/downloader/toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ const TAG_PATTERN = "tag = \"%s\""
const GIT_COMMIT_PATTERN = "commit = \"%s\""
const GIT_BRANCH_PATTERN = "branch = \"%s\""
const VERSION_PATTERN = "version = \"%s\""
const GIT_PACKAGE = "package = \"%s\""
const SEPARATOR = ", "

func (git *Git) MarshalTOML() string {
Expand All @@ -94,6 +95,12 @@ func (git *Git) MarshalTOML() string {
sb.WriteString(SEPARATOR)
sb.WriteString(fmt.Sprintf(VERSION_PATTERN, git.Version))
}

if len(git.Package) != 0 {
sb.WriteString(SEPARATOR)
sb.WriteString(fmt.Sprintf(GIT_PACKAGE, git.Package))
}

return sb.String()
}

Expand Down Expand Up @@ -175,6 +182,7 @@ const GIT_URL_FLAG = "git"
const TAG_FLAG = "tag"
const GIT_COMMIT_FLAG = "commit"
const GIT_BRANCH_FLAG = "branch"
const GIT_PACKAGE_FLAG = "package"

func (git *Git) UnmarshalModTOML(data interface{}) error {
meta, ok := data.(map[string]interface{})
Expand All @@ -198,6 +206,10 @@ func (git *Git) UnmarshalModTOML(data interface{}) error {
git.Branch = v
}

if v, ok := meta[GIT_PACKAGE_FLAG].(string); ok {
git.Package = v
}

return nil
}

Expand Down
9 changes: 5 additions & 4 deletions pkg/opt/opt.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,10 +378,11 @@ func ParseLocalPathOptions(localPath string) (*LocalOptions, *reporter.KpmEvent)
}

type GitOptions struct {
Url string
Branch string
Commit string
Tag string
Url string
Branch string
Commit string
Tag string
Package string
}

func (opts *GitOptions) Validate() error {
Expand Down
20 changes: 16 additions & 4 deletions pkg/package/modfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -482,10 +482,11 @@ func (deps *Dependencies) loadLockFile(filepath string) error {
func ParseOpt(opt *opt.RegistryOptions) (*Dependency, error) {
if opt.Git != nil {
gitSource := downloader.Git{
Url: opt.Git.Url,
Branch: opt.Git.Branch,
Commit: opt.Git.Commit,
Tag: opt.Git.Tag,
Url: opt.Git.Url,
Branch: opt.Git.Branch,
Commit: opt.Git.Commit,
Tag: opt.Git.Tag,
Package: opt.Git.Package,
}

gitRef, err := gitSource.GetValidGitReference()
Expand Down Expand Up @@ -566,18 +567,29 @@ func ParseOpt(opt *opt.RegistryOptions) (*Dependency, error) {
const PKG_NAME_PATTERN = "%s_%s"

// ParseRepoFullNameFromGitSource will extract the kcl package name from the git url.
// If the package flag is passed then it will be used as the package name.
func ParseRepoFullNameFromGitSource(gitSrc downloader.Git) (string, error) {
ref, err := gitSrc.GetValidGitReference()
if err != nil {
return "", err
}
if len(ref) != 0 {
if len(gitSrc.Package) != 0 {
return fmt.Sprintf(PKG_NAME_PATTERN, gitSrc.Package, ref), nil
}
return fmt.Sprintf(PKG_NAME_PATTERN, utils.ParseRepoNameFromGitUrl(gitSrc.Url), ref), nil
}
if len(gitSrc.Package) != 0 {
return gitSrc.Package, nil
}
return utils.ParseRepoNameFromGitUrl(gitSrc.Url), nil
}

// ParseRepoNameFromGitSource will extract the kcl package name from the git url.
// If the package flag is passed then it will be used
func ParseRepoNameFromGitSource(gitSrc downloader.Git) string {
if gitSrc.Package != "" {
return gitSrc.Package
}
return utils.ParseRepoNameFromGitUrl(gitSrc.Url)
}
48 changes: 48 additions & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"runtime"
"strings"

"github.com/BurntSushi/toml"
"github.com/distribution/reference"
"github.com/moby/term"
"github.com/otiai10/copy"
Expand Down Expand Up @@ -599,3 +600,50 @@ func AbsTarPath(tarPath string) (string, error) {

return absTarPath, nil
}

// FindPackage finds the package with the package name 'targetPackage' under the 'root' directory kcl.mod file.
func FindPackage(root, targetPackage string) (string, error) {
var result string
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
kclModPath := filepath.Join(path, constants.KCL_MOD)
if _, err := os.Stat(kclModPath); err == nil {
if matchesPackageName(kclModPath, targetPackage) {
result = path
return filepath.SkipAll
}
}
}
return nil
})

if err != nil {
return "", err
}
if result == "" {
return "", fmt.Errorf("kcl.mod with package '%s' not found", targetPackage)
}
return result, nil
}

// MatchesPackageName checks whether the package name in the kcl.mod file under 'kclModPath' is equal to 'targetPackage'.
func matchesPackageName(kclModPath, targetPackage string) bool {
type Package struct {
Name string `toml:"name"`
}
type ModFile struct {
Package Package `toml:"package"`
}

var modFile ModFile
_, err := toml.DecodeFile(kclModPath, &modFile)
if err != nil {
fmt.Printf("Error parsing kcl.mod file: %v\n", err)
return false
}

return modFile.Package.Name == targetPackage
}