Skip to content

Commit

Permalink
Add excludeCallback
Browse files Browse the repository at this point in the history
  • Loading branch information
jongio committed Sep 16, 2024
1 parent a3aa556 commit e64531c
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 45 deletions.
6 changes: 5 additions & 1 deletion cli/azd/pkg/project/framework_service_npm.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,11 @@ func (np *npmProject) Package(
if err := buildForZip(
packageSource,
packageDest,
buildForZipOptions{}); err != nil {
buildForZipOptions{
excludeCallback: func(src string) ([]excludeDirEntryCondition, error) {
return []excludeDirEntryCondition{excludeNodeModules}, nil
},
}); err != nil {
return nil, fmt.Errorf("packaging for %s: %w", serviceConfig.Name, err)
}

Expand Down
6 changes: 5 additions & 1 deletion cli/azd/pkg/project/framework_service_python.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,11 @@ func (pp *pythonProject) Package(
if err := buildForZip(
packageSource,
packageDest,
buildForZipOptions{}); err != nil {
buildForZipOptions{
excludeCallback: func(src string) ([]excludeDirEntryCondition, error) {
return []excludeDirEntryCondition{excludeVirtualEnv, excludePyCache}, nil
},
}); err != nil {

return nil, fmt.Errorf("packaging for %s: %w", serviceConfig.Name, err)
}
Expand Down
9 changes: 8 additions & 1 deletion cli/azd/pkg/project/project_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type excludeDirEntryCondition func(path string, file os.FileInfo) bool
// buildForZipOptions provides a set of options for doing build for zip
type buildForZipOptions struct {
excludeConditions []excludeDirEntryCondition
excludeCallback func(src string) ([]excludeDirEntryCondition, error)
}

// buildForZip is used by projects whose build strategy is to only copy the source code into a folder, which is later
Expand All @@ -72,7 +73,13 @@ func buildForZip(src, dst string, options buildForZipOptions) error {

// Conditionally exclude virtual environments, __pycache__, and node_modules only if .zipignore doesn't exist
if !zipIgnoreExists {
options.excludeConditions = append(options.excludeConditions, excludeVirtualEnv, excludePyCache, excludeNodeModules)
if options.excludeCallback != nil {
callbackExcludes, err := options.excludeCallback(src)
if err != nil {
return fmt.Errorf("applying exclude callback: %w", err)
}
options.excludeConditions = append(options.excludeConditions, callbackExcludes...)
}
}

options.excludeConditions = append(options.excludeConditions, func(path string, file os.FileInfo) bool {
Expand Down
44 changes: 16 additions & 28 deletions cli/azd/test/functional/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,16 +216,13 @@ func Test_CLI_Package_ZipIgnore(t *testing.T) {
enabled: true,
expectedFiles: map[string]map[string]bool{
"service1": {
"testfile.py": true,
"__pycache__/testcache.txt": false,
".venv/pyvenv.cfg": false,
"node_modules/some_package/package.json": false,
"logs/log.txt": true,
"testfile.py": true,
"__pycache__/testcache.txt": false,
".venv/pyvenv.cfg": false,
"logs/log.txt": true,
},
"service2": {
"testfile.js": true,
"__pycache__/testcache.txt": false,
".venv/pyvenv.cfg": false,
"node_modules/some_package/package.json": false,
"logs/log.txt": true,
},
Expand All @@ -239,16 +236,13 @@ func Test_CLI_Package_ZipIgnore(t *testing.T) {
rootZipIgnore: "__pycache__\n",
expectedFiles: map[string]map[string]bool{
"service1": {
"testfile.py": true,
"__pycache__/testcache.txt": false,
".venv/pyvenv.cfg": true,
"node_modules/some_package/package.json": true,
"logs/log.txt": true,
"testfile.py": true,
"__pycache__/testcache.txt": false,
".venv/pyvenv.cfg": true,
"logs/log.txt": true,
},
"service2": {
"testfile.js": true,
"__pycache__/testcache.txt": false,
".venv/pyvenv.cfg": true,
"node_modules/some_package/package.json": true,
"logs/log.txt": true,
},
Expand All @@ -264,16 +258,13 @@ func Test_CLI_Package_ZipIgnore(t *testing.T) {
service1ZipIgnore: "__pycache__\n",
expectedFiles: map[string]map[string]bool{
"service1": {
"testfile.py": true,
"__pycache__/testcache.txt": false,
".venv/pyvenv.cfg": true,
"node_modules/some_package/package.json": true,
"logs/log.txt": false,
"testfile.py": true,
"__pycache__/testcache.txt": false,
".venv/pyvenv.cfg": true,
"logs/log.txt": false,
},
"service2": {
"testfile.js": true,
"__pycache__/testcache.txt": true,
".venv/pyvenv.cfg": true,
"node_modules/some_package/package.json": true,
"logs/log.txt": false,
},
Expand All @@ -287,16 +278,13 @@ func Test_CLI_Package_ZipIgnore(t *testing.T) {
service1ZipIgnore: "__pycache__\n",
expectedFiles: map[string]map[string]bool{
"service1": {
"testfile.py": true,
"__pycache__/testcache.txt": false,
".venv/pyvenv.cfg": true,
"node_modules/some_package/package.json": true,
"logs/log.txt": true,
"testfile.py": true,
"__pycache__/testcache.txt": false,
".venv/pyvenv.cfg": true,
"logs/log.txt": true,
},
"service2": {
"testfile.js": true,
"__pycache__/testcache.txt": false,
".venv/pyvenv.cfg": false,
"node_modules/some_package/package.json": false,
"logs/log.txt": true,
},
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Empty file.

0 comments on commit e64531c

Please sign in to comment.