Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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 cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func setupBuildCommand() *cobraext.Command {
Long: buildLongDescription,
RunE: buildCommandAction,
}
cmd.Flags().Bool(cobraext.BuildZipFlagName, false, cobraext.BuildZipFlagDescription)
cmd.Flags().Bool(cobraext.BuildZipFlagName, true, cobraext.BuildZipFlagDescription)
cmd.Flags().Bool(cobraext.SignPackageFlagName, false, cobraext.SignPackageFlagDescription)
return cobraext.NewCommand(cmd, cobraext.ContextPackage)
}
Expand Down
25 changes: 16 additions & 9 deletions internal/files/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,18 @@ import (
"github.com/magefile/mage/sh"
)

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

// CopyWithoutDev method copies files from the source to the destination, but skips _dev directories.
// CopyWithoutDev method copies files from the source to the destination, but skips _dev directories and empty folders.
func CopyWithoutDev(sourcePath, destinationPath string) error {
return copy(sourcePath, destinationPath, []string{"_dev"})
return CopyWithSkipped(sourcePath, destinationPath, []string{"_dev"})
}

func copy(sourcePath, destinationPath string, skippedDirs []string) error {
// 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 {
return filepath.Walk(sourcePath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
Expand All @@ -36,12 +37,17 @@ func copy(sourcePath, destinationPath string, skippedDirs []string) error {
return nil
}

if info.IsDir() && shouldDirectoryBeSkipped(info.Name(), skippedDirs) {
if info.IsDir() && shouldDirectoryBeSkipped(relativePath, skippedDirs) {
return filepath.SkipDir
}

if info.IsDir() {
return os.MkdirAll(filepath.Join(destinationPath, relativePath), 0755)
return nil // don't create empty directories inside packages, if the directory is empty, skip it.
}

err = os.MkdirAll(filepath.Join(destinationPath, filepath.Dir(relativePath)), 0755)
if err != nil {
return err
}

return sh.Copy(
Expand All @@ -50,9 +56,10 @@ func copy(sourcePath, destinationPath string, skippedDirs []string) error {
})
}

func shouldDirectoryBeSkipped(name string, skippedDirs []string) bool {
// shouldDirectoryBeSkipped function checks if absolute path or last element should be skipped.
func shouldDirectoryBeSkipped(path string, skippedDirs []string) bool {
for _, d := range skippedDirs {
if name == d {
if path == d || filepath.Base(path) == d {
return true
}
}
Comment on lines +59 to 65
Copy link
Contributor

Choose a reason for hiding this comment

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

As a suggestion, skippedDirs parameter could be changed to be of type map[string]bool and just look whether or not the key is present. That would avoid to compare all the elements for every element in the directory when executing CopyWithSkipped.

It's also true that this would just have some benefits in the boot process (BootUp function) according to the current usages of this function, so not sure if it can be kept just an array for now. WDYT ?

Some data, with the tests I've being doing with metrics I saw that there are at least 1584 packages being indexed currently.

func shouldDirectoryBeSkipped(path string, skippedDirs map[string]bool) bool {
    if _, found := skippedDirs[path]; found {
        return true
    }
    if _, found := skippedDirs[filepath.Base(path)]; found {
        return true
    }
    return false
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Keep in mind that the real use case for this is building one package at a time and including it in the stack. Also, the skippedDirs list will contain max. 1-2 entries on average, so I would keep the array here.

Let me know what you think :)

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah! Ok, I thought that the BootUp function was related to walk through all the packages and versions. If that is intended for just one package with just a few entries, then let's just keep the array of directories 👍

Expand Down
44 changes: 43 additions & 1 deletion internal/stack/boot.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ package stack

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

"github.com/pkg/errors"

Expand Down Expand Up @@ -37,7 +40,7 @@ func BootUp(options Options) error {

if found {
fmt.Printf("Custom build packages directory found: %s\n", buildPackagesPath)
err = files.CopyAll(buildPackagesPath, stackPackagesDir.PackagesDir())
err = copyUniquePackages(buildPackagesPath, stackPackagesDir.PackagesDir())
if err != nil {
return errors.Wrap(err, "copying package contents failed")
}
Expand Down Expand Up @@ -71,3 +74,42 @@ func TearDown(options Options) error {
}
return nil
}

func copyUniquePackages(sourcePath, destinationPath string) error {
var skippedDirs []string

dirEntries, err := os.ReadDir(sourcePath)
if err != nil {
return errors.Wrapf(err, "can't read source dir (sourcePath: %s)", sourcePath)
}
for _, entry := range dirEntries {
if entry.IsDir() {
continue
}

if !strings.HasSuffix(entry.Name(), ".zip") {
continue
}

name, versionZip, found := stringsCut(entry.Name(), "-")
if !found {
continue
}
version, _, found := stringsCut(versionZip, ".zip")
if !found {
continue
}
skippedDirs = append(skippedDirs, filepath.Join(name, version))
}
return files.CopyWithSkipped(sourcePath, destinationPath, skippedDirs)
}

// stringsCut has been imported from Go source code.
// Link: https://github.com/golang/go/blob/master/src/strings/strings.go#L1187
// Once we bump up Go dependency, this will be replaced with runtime function.
func stringsCut(s, sep string) (before, after string, found bool) {
if i := strings.Index(s, sep); i >= 0 {
return s[:i], s[i+len(sep):], true
}
return s, "", false
}