Skip to content

Commit

Permalink
refactor: refactor load package (#488)
Browse files Browse the repository at this point in the history
* refactor: refactor load package

Signed-off-by: zongzhe <[email protected]>

* fix: fix var name

Signed-off-by: zongzhe <[email protected]>

---------

Signed-off-by: zongzhe <[email protected]>
  • Loading branch information
zong-zhe authored Sep 20, 2024
1 parent 72dd5e0 commit 94b8b96
Show file tree
Hide file tree
Showing 8 changed files with 239 additions and 154 deletions.
6 changes: 3 additions & 3 deletions pkg/api/kpm_pkg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ func TestPackageApi(t *testing.T) {
assert.Equal(t, dep.Name, "k8s")
assert.Equal(t, dep.FullName, "k8s_1.27")
assert.Equal(t, dep.Version, "1.27")
assert.Equal(t, dep.Source.Oci.Reg, "ghcr.io")
assert.Equal(t, dep.Source.Oci.Repo, "kcl-lang/k8s")
assert.Equal(t, dep.Source.Oci.Tag, "1.27")
assert.Equal(t, dep.Source.Registry.Oci.Reg, "ghcr.io")
assert.Equal(t, dep.Source.Registry.Oci.Repo, "kcl-lang/k8s")
assert.Equal(t, dep.Source.Registry .Oci.Tag, "1.27")

assert.Equal(t, dep.GetLocalFullPath(""), filepath.Join(kcl_pkg_path, "k8s_1.27"))

Expand Down
3 changes: 0 additions & 3 deletions pkg/api/test_data/test_kpm_package/kcl_pkg/kcl.mod.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,3 @@
full_name = "k8s_1.27"
version = "1.27"
sum = "xnYM1FWHAy3m+KcQMQb2rjZouTxumqYt6FGZpu2T4yM="
reg = "ghcr.io"
repo = "kcl-lang/k8s"
oci_tag = "1.27"
72 changes: 10 additions & 62 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,70 +150,18 @@ func (c *KpmClient) GetSettings() *settings.Settings {
return &c.settings
}

func (c *KpmClient) LoadPkgFromPath(pkgPath string) (*pkg.KclPkg, error) {
modFile, err := c.LoadModFile(pkgPath)
if err != nil {
return nil, reporter.NewErrorEvent(reporter.FailedLoadKclMod, err, fmt.Sprintf("could not load 'kcl.mod' in '%s'", pkgPath))
}

// Get dependencies from kcl.mod.lock.
deps, err := c.LoadLockDeps(pkgPath)

if err != nil {
return nil, reporter.NewErrorEvent(reporter.FailedLoadKclMod, err, fmt.Sprintf("could not load 'kcl.mod.lock' in '%s'", pkgPath))
}

// Align the dependencies between kcl.mod and kcl.mod.lock.
for _, name := range modFile.Dependencies.Deps.Keys() {
dep, ok := modFile.Dependencies.Deps.Get(name)
if !ok {
break
}
if dep.Local != nil {
if ldep, ok := deps.Deps.Get(name); ok {
var localFullPath string
if filepath.IsAbs(dep.Local.Path) {
localFullPath = dep.Local.Path
} else {
localFullPath, err = filepath.Abs(filepath.Join(pkgPath, dep.Local.Path))
if err != nil {
return nil, reporter.NewErrorEvent(reporter.Bug, err, "internal bugs, please contact us to fix it.")
}
}
ldep.LocalFullPath = localFullPath
dep.LocalFullPath = localFullPath
ldep.Source = dep.Source
deps.Deps.Set(name, ldep)
modFile.Dependencies.Deps.Set(name, dep)
}
}
}

return &pkg.KclPkg{
ModFile: *modFile,
HomePath: pkgPath,
Dependencies: *deps,
}, nil
func (c *KpmClient) LoadPkgFromPath(path string) (*pkg.KclPkg, error) {
return pkg.LoadKclPkgWithOpts(
pkg.WithPath(path),
pkg.WithSettings(&c.settings),
)
}

func (c *KpmClient) LoadModFile(pkgPath string) (*pkg.ModFile, error) {
modFile := new(pkg.ModFile)
err := modFile.LoadModFile(filepath.Join(pkgPath, pkg.MOD_FILE))
if err != nil {
return nil, err
}

modFile.HomePath = pkgPath

if modFile.Dependencies.Deps == nil {
modFile.Dependencies.Deps = orderedmap.NewOrderedMap[string, pkg.Dependency]()
}
err = c.FillDependenciesInfo(modFile)
if err != nil {
return nil, err
}

return modFile, nil
func (c *KpmClient) LoadModFile(path string) (*pkg.ModFile, error) {
return pkg.LoadAndFillModFileWithOpts(
pkg.WithPath(path),
pkg.WithSettings(&c.settings),
)
}

// Load the kcl.mod.lock and acquire the checksum of the dependencies from OCI registry.
Expand Down
4 changes: 4 additions & 0 deletions pkg/downloader/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ func NewSourceFromStr(sourceStr string) (*Source, error) {
return source, nil
}

func (source *Source) IsNilSource() bool {
return source == nil || (source.Git == nil && source.Oci == nil && source.Local == nil && source.Registry == nil)
}

func (source *Source) IsLocalPath() bool {
return source.Local != nil
}
Expand Down
39 changes: 15 additions & 24 deletions pkg/package/modfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,10 +457,12 @@ func NewModFile(opts *opt.InitOptions) *ModFile {
}
}

// Load the kcl.mod file.
func (mod *ModFile) LoadModFile(filepath string) error {
// Load the kcl.mod file, and make sure the `ModFile` is the same as the content in the kcl.mod file.
// For the dependency like "helloworld=0.1.0", `ModFile` will lack the source information.
// The `ModFile` will be filled with the source information loaded by `LoadAndFillModFileWithOpts`
func (mod *ModFile) LoadModFile(name string) error {

modData, err := os.ReadFile(filepath)
modData, err := os.ReadFile(name)
if err != nil {
return err
}
Expand All @@ -471,30 +473,10 @@ func (mod *ModFile) LoadModFile(filepath string) error {
return err
}

mod.HomePath = filepath.Dir(name)
return nil
}

// LoadModFile load the contents of the 'kcl.mod' file in the path.
func LoadModFile(homePath string) (*ModFile, error) {
modFile := new(ModFile)
err := modFile.LoadModFile(filepath.Join(homePath, MOD_FILE))
if err != nil {
return nil, err
}

modFile.HomePath = homePath

if modFile.Dependencies.Deps == nil {
modFile.Dependencies.Deps = orderedmap.NewOrderedMap[string, Dependency]()
}
err = modFile.FillDependenciesInfo()
if err != nil {
return nil, err
}

return modFile, nil
}

// Load the kcl.mod.lock file.
func (deps *Dependencies) loadLockFile(filepath string) error {
data, err := os.ReadFile(filepath)
Expand Down Expand Up @@ -625,3 +607,12 @@ func ParseRepoNameFromGitSource(gitSrc downloader.Git) string {
}
return utils.ParseRepoNameFromGitUrl(gitSrc.Url)
}

// LoadModFile load the contents of the 'kcl.mod' file in the path.
// Deprecated: Use 'LoadAndFillModFileWithOpts' instead.
func LoadModFile(path string) (*ModFile, error) {
return LoadAndFillModFileWithOpts(
WithPath(path),
WithSettings(settings.GetSettings()),
)
}
Loading

0 comments on commit 94b8b96

Please sign in to comment.