Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add kpm pkg include and exclude #316

Merged
merged 8 commits into from
May 11, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, kclPkg.GetPkgInclude(), kclPkg.GetPkgExclude(), tarPath)
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"})
Peefy marked this conversation as resolved.
Show resolved Hide resolved
assert.Equal(t, modfile.Pkg.Exclude, []string{"target/", ".git/", "*.log"})
Peefy marked this conversation as resolved.
Show resolved Hide resolved
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
16 changes: 15 additions & 1 deletion pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +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, include []string, exclude []string, tarPath string) error {
Peefy marked this conversation as resolved.
Show resolved Hide resolved

fw, err := os.Create(tarPath)
if err != nil {
Expand All @@ -137,6 +137,8 @@ func TarDir(srcDir string, tarPath string) error {
tw := tar.NewWriter(fw)
defer tw.Close()

fmt.Println(exclude)
Peefy marked this conversation as resolved.
Show resolved Hide resolved

err = filepath.Walk(srcDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
Expand All @@ -148,6 +150,18 @@ func TarDir(srcDir string, tarPath string) error {
}
}

for _, ex := range exclude {
if strings.Contains(path, ex) {
Peefy marked this conversation as resolved.
Show resolved Hide resolved
return nil
}
}

for _, inc := range include {
if !strings.Contains(path, inc) {
return nil
}
}

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

Expand Down
4 changes: 2 additions & 2 deletions pkg/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ func TestTarDir(t *testing.T) {
if !os.IsNotExist(err) {
os.Remove(tarPath)
}

err = TarDir(filepath.Join(testDir, "test_src"), tarPath)
emptyArrayOfStrings := []string{}
err = TarDir(filepath.Join(testDir, "test_src"), emptyArrayOfStrings, emptyArrayOfStrings, tarPath)
assert.Equal(t, err, nil)

_, err = os.Stat(tarPath)
Expand Down
Loading