Skip to content

Commit

Permalink
Merge pull request #316 from officialasishkumar/kpm-pkg-include-and-e…
Browse files Browse the repository at this point in the history
…xclude

add kpm pkg include and exclude
  • Loading branch information
Peefy committed May 11, 2024
2 parents b8a87ca + 0f2e6c5 commit f60d5bd
Show file tree
Hide file tree
Showing 12 changed files with 124 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,7 @@ func (c *KpmClient) Package(kclPkg *pkg.KclPkg, tarPath string, vendorMode bool)
}

// Tar the current kcl package into a "*.tar" file.
err := utils.TarDir(kclPkg.HomePath, tarPath)
err := utils.TarDir(kclPkg.HomePath, tarPath, kclPkg.GetPkgInclude(), kclPkg.GetPkgExclude())
if err != nil {
return reporter.NewErrorEvent(reporter.FailedPackage, err, "failed to package the kcl module")
}
Expand Down
10 changes: 6 additions & 4 deletions pkg/package/modfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ const (

// 'Package' is the kcl package section of 'kcl.mod'.
type Package struct {
Name string `toml:"name,omitempty"` // kcl package name
Edition string `toml:"edition,omitempty"` // kcl compiler version
Version string `toml:"version,omitempty"` // kcl package version
Description string `toml:"description,omitempty"` // kcl package description
Name string `toml:"name,omitempty"` // kcl package name
Edition string `toml:"edition,omitempty"` // kcl compiler version
Version string `toml:"version,omitempty"` // kcl package version
Description string `toml:"description,omitempty"` // kcl package description
Include []string `toml:"include,omitempty"` // kcl package include
Exclude []string `toml:"exclude,omitempty"` // kcl package exclude
}

// 'ModFile' is kcl package file 'kcl.mod'.
Expand Down
10 changes: 10 additions & 0 deletions pkg/package/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,16 @@ func (KclPkg *KclPkg) GetPkgDescription() string {
return KclPkg.ModFile.Pkg.Description
}

// GetPkgInclude returns the include of package.
func (KclPkg *KclPkg) GetPkgInclude() []string {
return KclPkg.ModFile.Pkg.Include
}

// GetPkgExclude returns the exclude of package.
func (KclPkg *KclPkg) GetPkgExclude() []string {
return KclPkg.ModFile.Pkg.Exclude
}

// GenCheckSum generates the checksum of the current kcl package.
func (KclPkg *KclPkg) GenCheckSum() (string, error) {
return utils.HashDir(KclPkg.HomePath)
Expand Down
2 changes: 2 additions & 0 deletions pkg/package/test_data/test_data_toml/expected.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
name = "MyKcl"
edition = "v0.0.1"
version = "v0.0.1"
include = ["src/", "README.md", "LICENSE"]
exclude = ["target/", ".git/", "*.log"]

[dependencies]
MyOciKcl1 = "0.0.1"
Expand Down
19 changes: 19 additions & 0 deletions pkg/package/toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,8 @@ const NAME_FLAG = "name"
const EDITION_FLAG = "edition"
const VERSION_FLAG = "version"
const DESCRIPTION_FLAG = "description"
const INCLUDE_FLAG = "include"
const EXCLUDE_FLAG = "exclude"

func (pkg *Package) UnmarshalTOML(data interface{}) error {
meta, ok := data.(map[string]interface{})
Expand All @@ -253,6 +255,23 @@ func (pkg *Package) UnmarshalTOML(data interface{}) error {
if v, ok := meta[DESCRIPTION_FLAG].(string); ok {
pkg.Description = v
}

convertToStringArray := func(v interface{}) []string {
var arr []string
for _, item := range v.([]interface{}) {
arr = append(arr, item.(string))
}
return arr
}

if v, ok := meta[INCLUDE_FLAG].([]interface{}); ok {
pkg.Include = convertToStringArray(v)
}

if v, ok := meta[EXCLUDE_FLAG].([]interface{}); ok {
pkg.Exclude = convertToStringArray(v)
}

return nil
}

Expand Down
2 changes: 2 additions & 0 deletions pkg/package/toml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ func TestUnMarshalTOML(t *testing.T) {
assert.Equal(t, modfile.Pkg.Name, "MyKcl")
assert.Equal(t, modfile.Pkg.Edition, "v0.0.1")
assert.Equal(t, modfile.Pkg.Version, "v0.0.1")
assert.Equal(t, modfile.Pkg.Include, []string{"src/", "README.md", "LICENSE"})
assert.Equal(t, modfile.Pkg.Exclude, []string{"target/", ".git/", "*.log"})
assert.Equal(t, len(modfile.Dependencies.Deps), 2)
assert.NotEqual(t, modfile.Dependencies.Deps["MyKcl1"], nil)
assert.Equal(t, modfile.Dependencies.Deps["MyKcl1"].Name, "MyKcl1")
Expand Down
Empty file.
Empty file.
Empty file.
Empty file.
23 changes: 21 additions & 2 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,7 @@ func Exists(path string) (bool, error) {
// todo: Consider using the OCI tarball as the standard tar format.
var ignores = []string{".git", ".tar"}

func TarDir(srcDir string, tarPath string) error {

func TarDir(srcDir string, tarPath string, include []string, exclude []string) error {
fw, err := os.Create(tarPath)
if err != nil {
log.Fatal(err)
Expand All @@ -148,6 +147,26 @@ func TarDir(srcDir string, tarPath string) error {
}
}

getNewPattern := func(ex string) string {
newPath := ex
if !strings.HasPrefix(ex, srcDir + "/") {
newPath = srcDir + "/" + ex
}
return newPath
}

for _, ex := range exclude {
if matched, _ := filepath.Match(getNewPattern(ex), path); matched {
return nil
}
}

for _, inc := range include {
if matched, _ := filepath.Match(getNewPattern(inc), path); !matched {
return nil
}
}

relPath, _ := filepath.Rel(srcDir, path)
relPath = filepath.ToSlash(relPath)

Expand Down
64 changes: 63 additions & 1 deletion pkg/utils/utils_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package utils

import (
"archive/tar"
"io"
"os"
"path"
"path/filepath"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -75,9 +79,67 @@ func TestTarDir(t *testing.T) {
os.Remove(tarPath)
}

err = TarDir(filepath.Join(testDir, "test_src"), tarPath)
testSrcDir := filepath.Join(testDir, "test_src")

getTarFileNames := func(filePath string) ([]string, error) {
file, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer file.Close()

reader := tar.NewReader(file)
filePaths := []string{}

for {
header, err := reader.Next()
if err == io.EOF {
break
}
if err != nil {
return nil, err
}

if header.Typeflag != tar.TypeReg {
continue
}

fullPath := path.Join(header.Name)
fullPath = path.Join(filePath, fullPath)
fullPath = strings.Replace(fullPath, "test.tar", "test_src", 1)

filePaths = append(filePaths, fullPath)
}

return filePaths, nil
}

getNewPattern := func(ex string) string {
return testSrcDir + "/" + ex
}

err = TarDir(testSrcDir, tarPath, []string{}, []string{})
assert.Equal(t, err, nil)
_, err = os.Stat(tarPath)
assert.Equal(t, err, nil)
os.Remove(tarPath)

_ = TarDir(testSrcDir, tarPath, []string{}, []string{"*.mod"})
fileNames, _ := getTarFileNames(tarPath)
for _, fileName := range fileNames {
flag, _ := filepath.Match(getNewPattern("*.mod"), fileName)
assert.Equal(t, flag, false)
}
_, err = os.Stat(tarPath)
assert.Equal(t, err, nil)
os.Remove(tarPath)

_ = TarDir(testSrcDir, tarPath, []string{"*/*.lock", "*.mod"}, []string{})
fileNames, _ = getTarFileNames(tarPath)
for _, fileName := range fileNames {
flag, _ := filepath.Match(getNewPattern("*/*.lock"), fileName)
assert.Equal(t, flag, true)
}
_, err = os.Stat(tarPath)
assert.Equal(t, err, nil)
os.Remove(tarPath)
Expand Down

0 comments on commit f60d5bd

Please sign in to comment.