Skip to content
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add support to skip files based on regex
  • Loading branch information
mrodm committed Mar 13, 2025
commit bf1418d5534b60fb8c5172cbd7719898477596df
24 changes: 17 additions & 7 deletions internal/files/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
package files

import (
"fmt"
"os"
"path/filepath"
"slices"
"strings"
"regexp"

"github.com/magefile/mage/sh"
)
Expand All @@ -20,11 +20,19 @@ func CopyAll(sourcePath, destinationPath string) error {

// CopyWithoutDev method copies files from the source to the destination, but skips _dev directories and empty folders.
func CopyWithoutDev(sourcePath, destinationPath string) error {
return CopyWithSkipped(sourcePath, destinationPath, []string{"_dev", "build", ".git", ".DS_Store"}, []string{".swp"})
return CopyWithSkipped(sourcePath, destinationPath, []string{"_dev", "build", ".git"}, []string{"\\.DS_Store", "\\..*\\.swp"})
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added support to skip files based on their file names if they match some of these regexes.

}

// CopyWithSkipped method copies files from the source to the destination, but skips selected directories, empty folders and selected hidden files.
func CopyWithSkipped(sourcePath, destinationPath string, skippedDirs, skippedHiddenFiles []string) error {
func CopyWithSkipped(sourcePath, destinationPath string, skippedDirs, skippedFiles []string) error {
regexesFiles := []*regexp.Regexp{}
for _, regexFile := range skippedFiles {
r, err := regexp.Compile(regexFile)
if err != nil {
return fmt.Errorf("failed to compile regex %q: %w", r, err)
}
regexesFiles = append(regexesFiles, r)
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pre-compile all regexes before calling to the Walk() function

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this function can directly receive compiled regexps. Or would it be enough to use glob patterns instead of regexes?

Copy link
Contributor Author

@mrodm mrodm Mar 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the code to use glob patterns instead of regexes: b91048e

As everything should be file names glob, patterns should be enough.

return filepath.Walk(sourcePath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
Expand All @@ -47,9 +55,11 @@ func CopyWithSkipped(sourcePath, destinationPath string, skippedDirs, skippedHid
return nil // don't create empty directories inside packages, if the directory is empty, skip it.
}

if strings.HasPrefix(filepath.Base(relativePath), ".") {
if slices.Contains(skippedHiddenFiles, filepath.Ext(relativePath)) {
return nil
if len(regexesFiles) > 0 {
for _, r := range regexesFiles {
if r.MatchString(filepath.Base(relativePath)) {
return nil
}
}
}

Expand Down