-
Notifications
You must be signed in to change notification settings - Fork 198
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 support for .azdignore #4146
Changes from all commits
de39424
353b325
60abb7c
b66ce60
155f258
3fafee5
ff4312e
0becf16
d2d7169
f8b6d27
0aa4880
9c81afa
532664f
234db5a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,4 +53,4 @@ | |
"default": "auth login --use-device-code" | ||
} | ||
] | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,120 @@ | ||||||
package azdignore | ||||||
|
||||||
import ( | ||||||
"fmt" | ||||||
"os" | ||||||
"path/filepath" | ||||||
"strings" | ||||||
|
||||||
"github.com/denormal/go-gitignore" | ||||||
) | ||||||
|
||||||
// ReadIgnoreFiles reads all .azdignore files in the directory hierarchy, from the projectDir upwards, | ||||||
// and returns a slice of gitignore.GitIgnore structures. | ||||||
func ReadIgnoreFiles(projectDir string) ([]gitignore.GitIgnore, error) { | ||||||
var ignoreMatchers []gitignore.GitIgnore | ||||||
|
||||||
// Traverse upwards from the projectDir to the root directory | ||||||
currentDir := projectDir | ||||||
for { | ||||||
ignoreFilePath := filepath.Join(currentDir, ".azdignore") | ||||||
if _, err := os.Stat(ignoreFilePath); !os.IsNotExist(err) { | ||||||
ignoreMatcher, err := gitignore.NewFromFile(ignoreFilePath) | ||||||
if err != nil { | ||||||
return nil, fmt.Errorf("error reading .azdignore file at %s: %w", ignoreFilePath, err) | ||||||
} | ||||||
ignoreMatchers = append([]gitignore.GitIgnore{ignoreMatcher}, ignoreMatchers...) | ||||||
} | ||||||
|
||||||
// Stop if we've reached the root directory | ||||||
parentDir := filepath.Dir(currentDir) | ||||||
if parentDir == currentDir { | ||||||
break | ||||||
} | ||||||
currentDir = parentDir | ||||||
} | ||||||
|
||||||
return ignoreMatchers, nil | ||||||
} | ||||||
|
||||||
// ShouldIgnore checks if a file or directory should be ignored based on a slice of gitignore.GitIgnore structures. | ||||||
func ShouldIgnore(path string, isDir bool, ignoreMatchers []gitignore.GitIgnore) bool { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Consider not exporting this function (make lowercase) if its only used internally within the package. |
||||||
for _, matcher := range ignoreMatchers { | ||||||
match := matcher.Relative(path, isDir) | ||||||
if match != nil && match.Ignore() { | ||||||
return true | ||||||
} | ||||||
} | ||||||
return false | ||||||
} | ||||||
|
||||||
// RemoveIgnoredFiles removes files and directories based on .azdignore rules using a pre-collected list of paths. | ||||||
func RemoveIgnoredFiles(staging string, ignoreMatchers []gitignore.GitIgnore) error { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: consider renaming this to
Suggested change
|
||||||
if len(ignoreMatchers) == 0 { | ||||||
return nil // No .azdignore files, no files to ignore | ||||||
} | ||||||
|
||||||
// Collect all file and directory paths | ||||||
paths, err := CollectFilePaths(staging) | ||||||
if err != nil { | ||||||
return fmt.Errorf("collecting file paths: %w", err) | ||||||
} | ||||||
|
||||||
// Map to store directories that should be ignored, preventing their children from being processed | ||||||
ignoredDirs := make(map[string]struct{}) | ||||||
|
||||||
// Iterate through collected paths and determine which to remove | ||||||
for _, path := range paths { | ||||||
relativePath, err := filepath.Rel(staging, path) | ||||||
if err != nil { | ||||||
return err | ||||||
} | ||||||
|
||||||
// Skip processing if the path is within an ignored directory | ||||||
skip := false | ||||||
for ignoredDir := range ignoredDirs { | ||||||
if strings.HasPrefix(relativePath, ignoredDir) { | ||||||
skip = true | ||||||
break | ||||||
} | ||||||
} | ||||||
if skip { | ||||||
continue | ||||||
} | ||||||
|
||||||
isDir := false | ||||||
info, err := os.Lstat(path) | ||||||
if err == nil { | ||||||
isDir = info.IsDir() | ||||||
} | ||||||
|
||||||
// Check if the file should be ignored | ||||||
if ShouldIgnore(relativePath, isDir, ignoreMatchers) { | ||||||
if isDir { | ||||||
ignoredDirs[relativePath] = struct{}{} | ||||||
if err := os.RemoveAll(path); err != nil { | ||||||
return fmt.Errorf("removing directory %s: %w", path, err) | ||||||
} | ||||||
} else { | ||||||
if err := os.Remove(path); err != nil { | ||||||
return fmt.Errorf("removing file %s: %w", path, err) | ||||||
} | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
return nil | ||||||
} | ||||||
|
||||||
// CollectFilePaths collects all file and directory paths under the given root directory. | ||||||
func CollectFilePaths(root string) ([]string, error) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Same as previous comment about not exporting function. |
||||||
var paths []string | ||||||
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error { | ||||||
if err != nil { | ||||||
return err | ||||||
} | ||||||
paths = append(paths, path) | ||||||
return nil | ||||||
}) | ||||||
return paths, err | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ import ( | |
"path/filepath" | ||
"time" | ||
|
||
"github.com/azure/azure-dev/cli/azd/pkg/azdignore" | ||
"github.com/azure/azure-dev/cli/azd/pkg/rzip" | ||
"github.com/otiai10/copy" | ||
) | ||
|
@@ -23,15 +24,23 @@ func createDeployableZip(projectName string, appName string, path string) (strin | |
return "", fmt.Errorf("failed when creating zip package to deploy %s: %w", appName, err) | ||
} | ||
|
||
if err := rzip.CreateFromDirectory(path, zipFile); err != nil { | ||
// if we fail here just do our best to close things out and cleanup | ||
// Read and honor the .azdignore files | ||
ignoreMatchers, err := azdignore.ReadIgnoreFiles(path) | ||
if err != nil && !os.IsNotExist(err) { | ||
return "", fmt.Errorf("reading .azdignore files: %w", err) | ||
} | ||
|
||
// Create the zip file, excluding files that match the .azdignore rules | ||
err = rzip.CreateFromDirectoryWithIgnore(path, zipFile, ignoreMatchers) | ||
if err != nil { | ||
// If we fail here, just do our best to close things out and cleanup | ||
zipFile.Close() | ||
os.Remove(zipFile.Name()) | ||
return "", err | ||
} | ||
|
||
if err := zipFile.Close(); err != nil { | ||
// may fail but, again, we'll do our best to cleanup here. | ||
// May fail, but again, we'll do our best to cleanup here. | ||
os.Remove(zipFile.Name()) | ||
return "", err | ||
} | ||
|
@@ -48,12 +57,40 @@ type buildForZipOptions struct { | |
excludeConditions []excludeDirEntryCondition | ||
} | ||
|
||
// buildForZip is use by projects which build strategy is to only copy the source code into a folder which is later | ||
// zipped for packaging. For example Python and Node framework languages. buildForZipOptions provides the specific | ||
// details for each language which should not be ever copied. | ||
// buildForZip is used by projects whose build strategy is to only copy the source code into a folder, which is later | ||
// zipped for packaging. buildForZipOptions provides the specific details for each language regarding which files should | ||
// not be copied. | ||
func buildForZip(src, dst string, options buildForZipOptions) error { | ||
// Add a global exclude condition for the .azdignore file | ||
ignoreMatchers, err := azdignore.ReadIgnoreFiles(src) | ||
if err != nil && !os.IsNotExist(err) { | ||
return fmt.Errorf("reading .azdignore files: %w", err) | ||
} | ||
|
||
options.excludeConditions = append(options.excludeConditions, func(path string, file os.FileInfo) bool { | ||
if len(ignoreMatchers) > 0 { | ||
// Get the relative path from the source directory | ||
relativePath, err := filepath.Rel(src, path) | ||
if err != nil { | ||
return false | ||
} | ||
|
||
// Check if the relative path should be ignored | ||
isDir := file.IsDir() | ||
if azdignore.ShouldIgnore(relativePath, isDir, ignoreMatchers) { | ||
return true | ||
} | ||
} | ||
|
||
// Always exclude .azdignore files | ||
if filepath.Base(path) == ".azdignore" { | ||
return true | ||
} | ||
Comment on lines
+86
to
+88
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did we ever land on whether we were going to call this |
||
|
||
return false | ||
}) | ||
|
||
// these exclude conditions applies to all projects | ||
// These exclude conditions apply to all projects | ||
options.excludeConditions = append(options.excludeConditions, globalExcludeAzdFolder) | ||
|
||
return copy.Copy(src, dst, copy.Options{ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Feels like the
RemoveIgnoredFiles
should internally determine the files to ignore from a file path instead of having to read them and pass them in.