Skip to content

Commit

Permalink
Yet another code-factorization of findAllFilesInFolder
Browse files Browse the repository at this point in the history
  • Loading branch information
cmaglie committed Aug 28, 2023
1 parent 8b7738f commit e5b6e46
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 18 deletions.
13 changes: 2 additions & 11 deletions legacy/builder/builder_utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,6 @@ import (

var tr = i18n.Tr

func findAllFilesInFolder(sourcePath *paths.Path, recurse bool) (paths.PathList, error) {
if !recurse {
return sourcePath.ReadDir(paths.FilterOutDirectories())
}
return sourcePath.ReadDirRecursiveFiltered(
utils.FilterOutSCCS,
paths.FilterOutDirectories())
}

// FilesAreOlderThan returns true if the given files are older than target.
func FilesAreOlderThan(files paths.PathList, target *paths.Path) (bool, error) {
targetStat, err := target.Stat()
Expand Down Expand Up @@ -329,7 +320,7 @@ func removeEndingBackSlash(s string) string {
}

func CoreOrReferencedCoreHasChanged(corePath, targetCorePath, targetFile *paths.Path) bool {
files, err := findAllFilesInFolder(corePath, true)
files, err := utils.FindFilesInFolder(corePath, true)
if err != nil {
return true
}
Expand All @@ -345,7 +336,7 @@ func CoreOrReferencedCoreHasChanged(corePath, targetCorePath, targetFile *paths.
}

func TXTBuildRulesHaveChanged(corePath, targetCorePath, targetFile *paths.Path) bool {
files, err := utils.FindFilesInFolder(corePath, true, []string{".txt"})
files, err := utils.FindFilesInFolder(corePath, true, ".txt")
if err != nil {
return true
}
Expand Down
2 changes: 1 addition & 1 deletion legacy/builder/container_find_includes.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ func queueSourceFilesFromFolder(ctx *types.Context, sourceFileQueue *types.Uniqu
for k := range globals.SourceFilesValidExtensions {
sourceFileExtensions = append(sourceFileExtensions, k)
}
filePaths, err := utils.FindFilesInFolder(folder, recurse, sourceFileExtensions)
filePaths, err := utils.FindFilesInFolder(folder, recurse, sourceFileExtensions...)
if err != nil {
return errors.WithStack(err)
}
Expand Down
8 changes: 4 additions & 4 deletions legacy/builder/create_cmake_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ func (s *ExportProjectCMake) Run(ctx *types.Context) error {
}

// Remove stray folders contining incompatible or not needed libraries archives
files, _ := utils.FindFilesInFolder(libDir.Join("src"), true, validStaticLibExtensions)
files, _ := utils.FindFilesInFolder(libDir.Join("src"), true, validStaticLibExtensions...)
for _, file := range files {
staticLibDir := file.Parent()
if !isStaticLib || !strings.Contains(staticLibDir.String(), mcu) {
Expand Down Expand Up @@ -251,7 +251,7 @@ func (s *ExportProjectCMake) Run(ctx *types.Context) error {
}

// remove "#line 1 ..." from exported c_make folder sketch
sketchFiles, _ := utils.FindFilesInFolder(cmakeFolder.Join("sketch"), false, validExportExtensions)
sketchFiles, _ := utils.FindFilesInFolder(cmakeFolder.Join("sketch"), false, validExportExtensions...)

for _, file := range sketchFiles {
input, err := file.ReadFile()
Expand Down Expand Up @@ -285,11 +285,11 @@ func (s *ExportProjectCMake) Run(ctx *types.Context) error {
extractCompileFlags(ctx, "recipe.cpp.o.pattern", &defines, &dynamicLibsFromGccMinusL, &linkerflags, &linkDirectories)

// Extract folders with .h in them for adding in include list
headerFiles, _ := utils.FindFilesInFolder(cmakeFolder, true, validHeaderExtensions)
headerFiles, _ := utils.FindFilesInFolder(cmakeFolder, true, validHeaderExtensions...)
foldersContainingHeaders := findUniqueFoldersRelative(headerFiles.AsStrings(), cmakeFolder.String())

// Extract folders with .a in them for adding in static libs paths list
staticLibs, _ := utils.FindFilesInFolder(cmakeFolder, true, validStaticLibExtensions)
staticLibs, _ := utils.FindFilesInFolder(cmakeFolder, true, validStaticLibExtensions...)

// Generate the CMakeLists global file

Expand Down
9 changes: 7 additions & 2 deletions legacy/builder/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,19 @@ func ExecCommand(ctx *types.Context, command *exec.Cmd, stdout int, stderr int)
return outbytes, errbytes, errors.WithStack(err)
}

func FindFilesInFolder(dir *paths.Path, recurse bool, extensions []string) (paths.PathList, error) {
func FindFilesInFolder(dir *paths.Path, recurse bool, extensions ...string) (paths.PathList, error) {
fileFilter := paths.AndFilter(
paths.FilterSuffixes(extensions...),
FilterOutHiddenFiles,
FilterOutSCCS,
paths.FilterOutDirectories(),
FilterReadableFiles,
)
if len(extensions) > 0 {
fileFilter = paths.AndFilter(
paths.FilterSuffixes(extensions...),
fileFilter,
)
}
if recurse {
dirFilter := paths.AndFilter(
FilterOutHiddenFiles,
Expand Down

0 comments on commit e5b6e46

Please sign in to comment.