Skip to content

Commit

Permalink
fix: fix CR comments
Browse files Browse the repository at this point in the history
Signed-off-by: zongz <[email protected]>
  • Loading branch information
zong-zhe committed Sep 19, 2024
1 parent 7dfce4e commit 6300e6d
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 116 deletions.
6 changes: 0 additions & 6 deletions .github/workflows/e2e-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,3 @@ jobs:
- name: run e2e
run: |
make e2e
- name: Running go tests with coverage and feature gate
env:
KPM_FEATURE_GATES: "SupportPackageLoader=true"
run: |
make e2e
6 changes: 0 additions & 6 deletions .github/workflows/test-win.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,3 @@ jobs:
GO111MODULE: on
run : |
make cover
- name: Running go tests with coverage on Windows
env:
GO111MODULE: on
KPM_FEATURE_GATES: "SupportPackageLoader=true"
run : |
make cover
6 changes: 0 additions & 6 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,6 @@ jobs:
uses: shogo82148/actions-goveralls@v1
with:
path-to-profile: coverage.out
- name: Running go tests with coverage and feature gate
env:
GO111MODULE: on
KPM_FEATURE_GATES: "SupportPackageLoader=true"
run: |
make cover
- name: Send coverage
uses: shogo82148/actions-goveralls@v1
with:
Expand Down
54 changes: 4 additions & 50 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ import (

// KpmClient is the client of kpm.
type KpmClient struct {
// The loader of the kcl package.
PkgLoader loader.Loader
// The writer of the log.
logWriter io.Writer
// The downloader of the dependencies.
Expand Down Expand Up @@ -79,7 +77,6 @@ func NewKpmClient() (*KpmClient, error) {
logWriter: os.Stdout,
settings: *settings,
homePath: homePath,
PkgLoader: loader.NewFileLoader(settings),
DepDownloader: &downloader.DepDownloader{},
}, nil
}
Expand Down Expand Up @@ -155,53 +152,10 @@ func (c *KpmClient) GetSettings() *settings.Settings {
}

func (c *KpmClient) LoadPkgFromPath(pkgPath string) (*pkg.KclPkg, error) {
if ok, err := features.Enabled(features.SupportPackageLoader); err != nil && ok {
return c.PkgLoader.Load(pkgPath)
} else {
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
}
return loader.Load(
loader.WithPkgPath(pkgPath),
loader.WithSettings(&c.settings),
)
}

func (c *KpmClient) LoadModFile(pkgPath string) (*pkg.ModFile, error) {
Expand Down
5 changes: 1 addition & 4 deletions pkg/features/features.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,10 @@ import (
const (
// SupportMVS is the feature gate for enabling the support for MVS.
SupportMVS = "SupportMVS"
// SupportPackageLoader is the feature gate for enabling the support for package loader.
SupportPackageLoader = "SupportPackageLoader"
)

var features = map[string]bool{
SupportMVS: false,
SupportPackageLoader: false,
SupportMVS: false,
}

func init() {
Expand Down
84 changes: 42 additions & 42 deletions pkg/loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,8 @@ import (
"kcl-lang.io/kpm/pkg/utils"
)

// Loader is an interface that defines the method to load a package.
type Loader interface {
Load(pkgPath string) (*pkg.KclPkg, error)
}

// PkgLoader is a struct that contains the settings.
type PkgLoader struct {
settings *settings.Settings
}

// NewPkgLoader creates a new PkgLoader.
func NewPkgLoader(settings *settings.Settings) *PkgLoader {
return &PkgLoader{
settings: settings,
}
}

// FileLoader is a struct that load a package from the file system.
type FileLoader struct {
PkgLoader
}

// NewFileLoader creates a new FileLoader.
func NewFileLoader(settings *settings.Settings) *FileLoader {
return &FileLoader{
PkgLoader: PkgLoader{
settings: settings,
},
}
}

// loadModFile loads the kcl.mod file from the package path.
func (fl *FileLoader) loadModFile(pkgPath string) (*pkg.ModFile, error) {
func loadModFile(pkgPath string) (*pkg.ModFile, error) {
modFile := new(pkg.ModFile)
err := modFile.LoadModFile(filepath.Join(pkgPath, pkg.MOD_FILE))
if err != nil {
Expand All @@ -59,7 +28,7 @@ func (fl *FileLoader) loadModFile(pkgPath string) (*pkg.ModFile, error) {
// preProcess pre-processes the package loaded from kcl.mod and kcl.mod.lock
// 1. transform the local path to the absolute path.
// 2. fill the default oci registry.
func (fl *FileLoader) preProcess(kpkg *pkg.KclPkg) error {
func preProcess(kpkg *pkg.KclPkg, settings *settings.Settings) error {
for _, name := range kpkg.ModFile.Dependencies.Deps.Keys() {
dep, ok := kpkg.ModFile.Dependencies.Deps.Get(name)
if !ok {
Expand All @@ -83,21 +52,21 @@ func (fl *FileLoader) preProcess(kpkg *pkg.KclPkg) error {
// Fill the default oci registry.
if dep.Source.Oci != nil {
if len(dep.Source.Oci.Reg) == 0 {
dep.Source.Oci.Reg = fl.settings.DefaultOciRegistry()
dep.Source.Oci.Reg = settings.DefaultOciRegistry()
}

if len(dep.Source.Oci.Repo) == 0 {
urlpath := utils.JoinPath(fl.settings.DefaultOciRepo(), dep.Name)
urlpath := utils.JoinPath(settings.DefaultOciRepo(), dep.Name)
dep.Source.Oci.Repo = urlpath
}
}
if dep.Source.Registry != nil {
if len(dep.Source.Registry.Reg) == 0 {
dep.Source.Registry.Reg = fl.settings.DefaultOciRegistry()
dep.Source.Registry.Reg = settings.DefaultOciRegistry()
}

if len(dep.Source.Registry.Repo) == 0 {
urlpath := utils.JoinPath(fl.settings.DefaultOciRepo(), dep.Name)
urlpath := utils.JoinPath(settings.DefaultOciRepo(), dep.Name)
dep.Source.Registry.Repo = urlpath
}

Expand All @@ -108,8 +77,8 @@ func (fl *FileLoader) preProcess(kpkg *pkg.KclPkg) error {
Name: dep.Name,
Version: dep.Version,
Oci: &downloader.Oci{
Reg: fl.settings.DefaultOciRegistry(),
Repo: utils.JoinPath(fl.settings.DefaultOciRepo(), dep.Name),
Reg: settings.DefaultOciRegistry(),
Repo: utils.JoinPath(settings.DefaultOciRepo(), dep.Name),
Tag: dep.Version,
},
}
Expand All @@ -124,9 +93,40 @@ func (fl *FileLoader) preProcess(kpkg *pkg.KclPkg) error {
return nil
}

type LoadOptions struct {
// The package path.
PkgPath string
// The settings with default oci registry.
Settings *settings.Settings
}

type Option func(*LoadOptions)

// WithPkgPath sets the package path.
func WithPkgPath(pkgPath string) Option {
return func(opts *LoadOptions) {
opts.PkgPath = pkgPath
}
}

// WithSettings sets the settings with default oci registry.
func WithSettings(settings *settings.Settings) Option {
return func(opts *LoadOptions) {
opts.Settings = settings
}
}

// Load loads a package from the file system.
func (fl *FileLoader) Load(pkgPath string) (*pkg.KclPkg, error) {
modFile, err := fl.loadModFile(pkgPath)
func Load(options ...Option) (*pkg.KclPkg, error) {

opts := &LoadOptions{}
for _, opt := range options {
opt(opts)
}

pkgPath := opts.PkgPath

modFile, err := loadModFile(pkgPath)
if err != nil {
return nil, fmt.Errorf("failed to load the package from the path %s: %w", pkgPath, err)
}
Expand All @@ -145,7 +145,7 @@ func (fl *FileLoader) Load(pkgPath string) (*pkg.KclPkg, error) {
}

// pre-process the package.
err = fl.preProcess(kpkg)
err = preProcess(kpkg, opts.Settings)
if err != nil {
return nil, fmt.Errorf("failed to load the package from the path %s: %w", pkgPath, err)
}
Expand Down
6 changes: 4 additions & 2 deletions pkg/loader/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ func getTestDir(subDir string) string {
}

func load(t *testing.T, pkgPath string) *pkg.KclPkg {
loader := NewFileLoader(settings.GetSettings())
pkg, err := loader.Load(pkgPath)
pkg, err := Load(
WithPkgPath(pkgPath),
WithSettings(settings.GetSettings()),
)
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit 6300e6d

Please sign in to comment.