-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwatcher.go
More file actions
131 lines (111 loc) · 2.89 KB
/
Copy pathwatcher.go
File metadata and controls
131 lines (111 loc) · 2.89 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"sync"
"time"
)
// FileWatcher monitors for changes in theme source directories
type FileWatcher struct {
root string
sourceDir string
destDir string
tickChan <-chan time.Time
done chan bool
mu sync.Mutex
fileHashes map[string]string
}
// NewFileWatcher creates a new file watcher
func NewFileWatcher(root, sourceDir, destDir string, interval time.Duration) *FileWatcher {
ticker := time.NewTicker(interval)
return &FileWatcher{
root: root,
sourceDir: sourceDir,
destDir: destDir,
tickChan: ticker.C,
done: make(chan bool),
fileHashes: make(map[string]string),
}
}
// Start begins watching for file changes
func (w *FileWatcher) Start() {
go func() {
// Initial hash of all files
w.updateHashes()
for {
select {
case <-w.tickChan:
if w.hasChanges() {
fmt.Println("Changes detected. Running deployment...")
version := fmt.Sprintf("%d", time.Now().Unix())
fileCount, err := deployTheme(w.root, DeployJob{
Locale: "nl_NL",
Theme: "Vendor/Hyva",
Area: "frontend",
}, version)
if err != nil {
fmt.Printf("Error during deployment: %v\n", err)
} else {
fmt.Printf("✓ Deployment complete: %d files deployed\n", fileCount)
}
}
case <-w.done:
return
}
}
}()
}
// Stop stops the file watcher
func (w *FileWatcher) Stop() {
w.done <- true
}
// updateHashes computes hashes of all files in the source directory
func (w *FileWatcher) updateHashes() error {
newHashes := make(map[string]string)
err := filepath.Walk(w.sourceDir, func(path string, fileInfo os.FileInfo, err error) error {
if err != nil {
return err
}
if fileInfo.IsDir() || strings.HasPrefix(filepath.Base(path), ".") {
return nil
}
relPath, _ := filepath.Rel(w.sourceDir, path)
newHashes[relPath] = fmt.Sprintf("%d:%d", fileInfo.ModTime().Unix(), fileInfo.Size())
return nil
})
w.mu.Lock()
w.fileHashes = newHashes
w.mu.Unlock()
return err
}
// hasChanges checks if any files have changed
func (w *FileWatcher) hasChanges() bool {
currentHashes := make(map[string]string)
filepath.Walk(w.sourceDir, func(path string, info os.FileInfo, err error) error {
if err != nil || info.IsDir() || strings.HasPrefix(filepath.Base(path), ".") {
return nil
}
relPath, _ := filepath.Rel(w.sourceDir, path)
currentHashes[relPath] = fmt.Sprintf("%d:%d", info.ModTime().Unix(), info.Size())
return nil
})
w.mu.Lock()
defer w.mu.Unlock()
// Check for new or modified files
for path, currentHash := range currentHashes {
if prevHash, exists := w.fileHashes[path]; !exists || prevHash != currentHash {
w.fileHashes = currentHashes
return true
}
}
// Check for deleted files
for path := range w.fileHashes {
if _, exists := currentHashes[path]; !exists {
w.fileHashes = currentHashes
return true
}
}
return false
}