Skip to content
Merged
Show file tree
Hide file tree
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
Skip swp hidden files (vim) in build process
  • Loading branch information
mrodm committed Mar 13, 2025
commit c263c0a9f78cee1d9ec64ce13f5c3d2e11ba7519
16 changes: 12 additions & 4 deletions internal/files/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,24 @@ package files
import (
"os"
"path/filepath"
"slices"
"strings"

"github.com/magefile/mage/sh"
)

// CopyAll method copies files from the source to the destination skipping empty directories.
func CopyAll(sourcePath, destinationPath string) error {
return CopyWithSkipped(sourcePath, destinationPath, []string{})
return CopyWithSkipped(sourcePath, destinationPath, []string{}, []string{})
}

// 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"})
return CopyWithSkipped(sourcePath, destinationPath, []string{"_dev", "build", ".git", ".DS_Store"}, []string{".swp"})
}

// CopyWithSkipped method copies files from the source to the destination, but skips selected directories and empty folders.
func CopyWithSkipped(sourcePath, destinationPath string, skippedDirs []string) error {
// 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 {
return filepath.Walk(sourcePath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
Expand All @@ -45,6 +47,12 @@ func CopyWithSkipped(sourcePath, destinationPath string, skippedDirs []string) e
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
}
}

err = os.MkdirAll(filepath.Join(destinationPath, filepath.Dir(relativePath)), 0755)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion internal/stack/boot.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func copyUniquePackages(sourcePath, destinationPath string) error {
}
skippedDirs = append(skippedDirs, filepath.Join(name, version))
}
return files.CopyWithSkipped(sourcePath, destinationPath, skippedDirs)
return files.CopyWithSkipped(sourcePath, destinationPath, skippedDirs, []string{})
}

// stringsCut has been imported from Go source code.
Expand Down