-
-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy paththemes_merge.go
78 lines (63 loc) · 1.89 KB
/
themes_merge.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package build
import (
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"strconv"
"time"
)
// ThemesMerge combines any nested themes with the current project.
func ThemesMerge(buildDir string) error {
defer Benchmark(time.Now(), "Merging themes with your project")
copiedProjectFileCounter := 0
// Make list of files not to copy to build.
excludedFiles := []string{
".git",
".gitignore",
"themes",
buildDir,
}
themeFilesErr := filepath.WalkDir(".", func(projectFilePath string, projectFileInfo fs.DirEntry, err error) error {
if err != nil {
return fmt.Errorf("can't stat %s: %w", projectFilePath, err)
}
// Check if the current directory is in the excluded list.
for _, excludedFile := range excludedFiles {
if projectFileInfo.IsDir() && projectFileInfo.Name() == excludedFile {
return filepath.SkipDir
}
if !projectFileInfo.IsDir() && projectFileInfo.Name() == excludedFile {
return nil
}
}
// Read the source project file.
from, err := os.Open(projectFilePath)
if err != nil {
return fmt.Errorf("Could not open project file for copying: %w\n", err)
}
defer from.Close()
// Create the folders needed to write files to tempDir.
if projectFileInfo.IsDir() {
// Make directory if it doesn't exist and move on to next path.
return ThemeFs.MkdirAll(projectFilePath, os.ModePerm)
}
to, err := ThemeFs.Create(projectFilePath)
if err != nil {
return fmt.Errorf("Could not create destination project file for copying: %w\n", err)
}
defer to.Close()
_, fileCopyErr := io.Copy(to, from)
if err != nil {
return fmt.Errorf("Could not copy project file from source to destination: %w\n", fileCopyErr)
}
copiedProjectFileCounter++
return nil
})
if themeFilesErr != nil {
return fmt.Errorf("Could not get project file: %w\n", themeFilesErr)
}
Log("Number of project files copied: " + strconv.Itoa(copiedProjectFileCounter))
return nil
}