-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #543 from kcl-lang/find-submod-by-spec
feat: supports add submod by ModSpec
- Loading branch information
Showing
18 changed files
with
172 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[package] | ||
name = "git" | ||
edition = "v0.10.0" | ||
version = "0.0.1" |
7 changes: 7 additions & 0 deletions
7
pkg/client/test_data/add_with_mod_spec/git_mod_2/kcl.mod.expect
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[package] | ||
name = "git" | ||
edition = "v0.10.0" | ||
version = "0.0.1" | ||
|
||
[dependencies] | ||
cc = { git = "https://github.com/kcl-lang/flask-demo-kcl-manifests.git", commit = "3adfc81", version = "0.0.2" } |
Empty file.
7 changes: 7 additions & 0 deletions
7
pkg/client/test_data/add_with_mod_spec/git_mod_2/kcl.mod.lock.expect
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[dependencies] | ||
[dependencies.cc] | ||
name = "cc" | ||
full_name = "cc_0.0.2" | ||
version = "0.0.2" | ||
url = "https://github.com/kcl-lang/flask-demo-kcl-manifests.git" | ||
commit = "3adfc81" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
The_first_kcl_program = 'Hello World!' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[package] | ||
name = "git" | ||
edition = "v0.10.0" | ||
version = "0.0.1" |
7 changes: 7 additions & 0 deletions
7
pkg/client/test_data/add_with_mod_spec/git_mod_3/kcl.mod.expect
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[package] | ||
name = "git" | ||
edition = "v0.10.0" | ||
version = "0.0.1" | ||
|
||
[dependencies] | ||
cc = { git = "https://github.com/kcl-lang/flask-demo-kcl-manifests.git", commit = "3adfc81", version = "0.0.1" } |
Empty file.
7 changes: 7 additions & 0 deletions
7
pkg/client/test_data/add_with_mod_spec/git_mod_3/kcl.mod.lock.expect
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[dependencies] | ||
[dependencies.cc] | ||
name = "cc" | ||
full_name = "cc_0.0.1" | ||
version = "0.0.1" | ||
url = "https://github.com/kcl-lang/flask-demo-kcl-manifests.git" | ||
commit = "3adfc81" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
The_first_kcl_program = 'Hello World!' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package downloader | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/BurntSushi/toml" | ||
"github.com/hashicorp/go-version" | ||
"kcl-lang.io/kpm/pkg/constants" | ||
"kcl-lang.io/kpm/pkg/utils" | ||
) | ||
|
||
func loadModSpecFromKclMod(kclModPath string) (*ModSpec, error) { | ||
type Package struct { | ||
Name string `toml:"name"` | ||
Version string `toml:"version"` | ||
} | ||
type ModFile struct { | ||
Package Package `toml:"package"` | ||
} | ||
|
||
var modFile ModFile | ||
_, err := toml.DecodeFile(kclModPath, &modFile) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &ModSpec{ | ||
Name: modFile.Package.Name, | ||
Version: modFile.Package.Version, | ||
}, nil | ||
} | ||
|
||
// MatchesPackageName checks whether the package name in the kcl.mod file under 'kclModPath' is equal to 'targetPackage'. | ||
func matchesPackageSpec(kclModPath string, modSpec *ModSpec) bool { | ||
type Package struct { | ||
Name string `toml:"name"` | ||
Version string `toml:"version"` | ||
} | ||
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 == modSpec.Name && modFile.Package.Version == modSpec.Version | ||
} | ||
|
||
func FindPackageByModSpec(root string, modSpec *ModSpec) (string, error) { | ||
var result string | ||
var modVersion 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 the package name and version are specified, | ||
// we can directly check if the kcl.mod file matches the package. | ||
if matchesPackageSpec(kclModPath, modSpec) { | ||
result = path | ||
return filepath.SkipAll | ||
} else if modSpec.Version == "" { | ||
// If the package name specified, but version are not specified, | ||
if utils.MatchesPackageName(kclModPath, modSpec.Name) { | ||
// load the version from the kcl.mod file | ||
tmpSpec, err := loadModSpecFromKclMod(kclModPath) | ||
if err != nil { | ||
return err | ||
} | ||
// Remember the local path with the highest version | ||
tmpVer, err := version.NewSemver(tmpSpec.Version) | ||
if err != nil { | ||
return err | ||
} | ||
if modVersion != "" { | ||
modVer, err := version.NewSemver(modVersion) | ||
if err != nil { | ||
return err | ||
} | ||
if tmpVer.GreaterThan(modVer) { | ||
modVersion = tmpSpec.Version | ||
result = path | ||
} | ||
} else { | ||
modVersion = tmpSpec.Version | ||
result = path | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return nil | ||
}) | ||
|
||
if err != nil { | ||
return "", err | ||
} | ||
if result == "" { | ||
return "", fmt.Errorf("kcl.mod with package '%s:%s' not found", modSpec.Name, modSpec.Version) | ||
} | ||
return result, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters