Skip to content

Commit

Permalink
feat: supports add dependencies by git branch
Browse files Browse the repository at this point in the history
Signed-off-by: zongz <[email protected]>
  • Loading branch information
zong-zhe committed May 27, 2024
1 parent c670ccc commit 6bb17a2
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 6 deletions.
4 changes: 4 additions & 0 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1036,6 +1036,10 @@ func (c *KpmClient) DownloadFromGit(dep *pkg.Git, localPath string) (string, err
msg = fmt.Sprintf("with commit '%s'", dep.Commit)
}

if len(dep.Branch) != 0 {
msg = fmt.Sprintf("with branch '%s'", dep.Branch)
}

reporter.ReportMsgTo(
fmt.Sprintf("cloning '%s' %s", dep.Url, msg),
c.logWriter,
Expand Down
11 changes: 5 additions & 6 deletions pkg/cmd/cmd_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"slices"
"strings"

"github.com/dominikbraun/graph"
"github.com/urfave/cli/v2"
"golang.org/x/mod/module"
"kcl-lang.io/kpm/pkg/client"
Expand Down Expand Up @@ -131,8 +130,8 @@ func KpmUpdate(c *cli.Context, kpmcli *client.KpmClient) error {

// GetModulesToUpdate validates if the packages is present in kcl.mod file and
// find the latest version if version is not specified. Depending on the value of pkgVersion,
// modulesToUpgrade or modulesToDowngrade will be updated.
func GetModulesToUpdate(kclPkg *pkg.KclPkg, modulesToUpgrade []module.Version, modulesToDowngrade []module.Version, pkgInfo string) error {
// modulesToUpgrade or modulesToDowngrade will be updated.
func GetModulesToUpdate(kclPkg *pkg.KclPkg, modulesToUpgrade []module.Version, modulesToDowngrade []module.Version, pkgInfo string) error {
pkgInfo = strings.TrimSpace(pkgInfo)
pkgName, pkgVersion, err := ParseOciPkgNameAndVersion(pkgInfo)
if err != nil {
Expand Down Expand Up @@ -172,9 +171,9 @@ func GetModulesToUpdate(kclPkg *pkg.KclPkg, modulesToUpgrade []module.Version,
return nil
}

// InsertModuleToDeps checks whether module is present in the buildList and it is not the same as the target module,
// InsertModuleToDeps checks whether module is present in the buildList and it is not the same as the target module,
// and inserts it to the dependencies of kclPkg
func InsertModuleToDeps(kclPkg *pkg.KclPkg, module module.Version, target module.Version, buildList []module.Version, reqs mvs.ReqsGraph) (error) {
func InsertModuleToDeps(kclPkg *pkg.KclPkg, module module.Version, target module.Version, buildList []module.Version, reqs mvs.ReqsGraph) error {
if module.Path == target.Path || !slices.Contains(buildList, module) {
return nil
}
Expand All @@ -196,4 +195,4 @@ func InsertModuleToDeps(kclPkg *pkg.KclPkg, module module.Version, target module
}
kclPkg.ModFile.Dependencies.Deps[module.Path] = d
return nil
}
}
12 changes: 12 additions & 0 deletions pkg/package/toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ func (source *Source) MarshalTOML() string {
const GIT_URL_PATTERN = "git = \"%s\""
const TAG_PATTERN = "tag = \"%s\""
const GIT_COMMIT_PATTERN = "commit = \"%s\""
const GIT_BRANCH_PATTERN = "branch = \"%s\""
const VERSION_PATTERN = "version = \"%s\""
const SEPARATOR = ", "

Expand All @@ -134,6 +135,12 @@ func (git *Git) MarshalTOML() string {
sb.WriteString(SEPARATOR)
sb.WriteString(fmt.Sprintf(GIT_COMMIT_PATTERN, git.Commit))
}

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

if len(git.Version) != 0 {
sb.WriteString(SEPARATOR)
sb.WriteString(fmt.Sprintf(VERSION_PATTERN, git.Version))
Expand Down Expand Up @@ -367,6 +374,7 @@ func (source *Source) UnmarshalModTOML(data interface{}) error {
const GIT_URL_FLAG = "git"
const TAG_FLAG = "tag"
const GIT_COMMIT_FLAG = "commit"
const GIT_BRANCH_FLAG = "branch"

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

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

return nil
}

Expand Down
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 --quiet run
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
a:
name: flask-demo
replicas: 1
labels:
app: flask-demo
service:
type: NodePort
ports:
- port: 5000
protocol: TCP
targetPort: 5000
containers:
flaskdemo:
image: kcllang/flask_demo:8d31498e765ff67a2fa9933d4adffe067544b2fe
ports:
- protocol: TCP
containerPort: 5000
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "test_kpm_run_with_git_commit_dep"
edition = "0.0.1"
version = "0.0.1"

[dependencies]
flask-demo-kcl-manifests = { git = "https://github.com/kcl-lang/flask-demo-kcl-manifests.git", branch = "main" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[dependencies]
[dependencies.flask-demo-kcl-manifests]
name = "flask-demo-kcl-manifests"
full_name = "flask_manifests_0.0.1"
version = "0.0.1"
url = "https://github.com/kcl-lang/flask-demo-kcl-manifests.git"
branch = "main"
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import flask_demo_kcl_manifests as flask

a = flask.config

0 comments on commit 6bb17a2

Please sign in to comment.