From 8e63d64f8445695ede37c261454b67fe2fb55c80 Mon Sep 17 00:00:00 2001 From: Itamar Date: Sun, 11 Jun 2023 10:26:33 +0300 Subject: [PATCH] fix(internal/build): Using forward slash with embed fs --- internal/cli/build.go | 64 +++++++++++++++++++++++++++++-------------- 1 file changed, 44 insertions(+), 20 deletions(-) diff --git a/internal/cli/build.go b/internal/cli/build.go index b6c74f96..33826d24 100644 --- a/internal/cli/build.go +++ b/internal/cli/build.go @@ -4,6 +4,8 @@ import ( "bytes" "embed" "fmt" + "io" + "io/fs" "os" "os/exec" "path" @@ -286,34 +288,56 @@ func mergeI18nFiles(b *buildingMaterial) (err error) { } func copyDirEntries(sourceFs embed.FS, sourceDir string, targetDir string) (err error) { - entries, err := ui.Build.ReadDir(sourceDir) - if err != nil { - return err - } - err = dir.CreateDirIfNotExist(targetDir) if err != nil { return err } - for _, entry := range entries { - if entry.IsDir() { - err = copyDirEntries(sourceFs, filepath.Join(sourceDir, entry.Name()), filepath.Join(targetDir, entry.Name())) + err = fs.WalkDir(sourceFs, sourceDir, func(path string, d fs.DirEntry, err error) error { + if err != nil { + return err + } + + // Convert the path to use forward slashes, important because we use embedded FS which always uses forward slashes + path = filepath.ToSlash(path) + + // Construct the absolute path for the source file/directory + srcPath := filepath.Join(sourceDir, path) + + // Construct the absolute path for the destination file/directory + dstPath := filepath.Join(targetDir, path) + + if d.IsDir() { + // Create the directory in the destination + err := os.MkdirAll(dstPath, d.Type()) + if err != nil { + return err + } + } else { + // Open the source file + srcFile, err := sourceFs.Open(srcPath) + if err != nil { + return err + } + defer srcFile.Close() + + // Create the destination file + dstFile, err := os.Create(dstPath) + if err != nil { + return err + } + defer dstFile.Close() + + // Copy the file contents + _, err = io.Copy(dstFile, srcFile) if err != nil { return err } - continue } - file, err := sourceFs.ReadFile(filepath.Join(sourceDir, entry.Name())) - if err != nil { - return err - } - filename := filepath.Join(targetDir, entry.Name()) - err = os.WriteFile(filename, file, 0666) - if err != nil { - return err - } - } - return nil + + return nil + }) + + return err } func buildBinary(b *buildingMaterial) (err error) {