-
Notifications
You must be signed in to change notification settings - Fork 0
/
manifest.go
56 lines (47 loc) · 1.11 KB
/
manifest.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
package main
import (
"bufio"
"fmt"
"sort"
"strings"
"go.imnhan.com/s4g/writablefs"
)
// Write list of files generated by s4g
func WriteManifest(fsys writablefs.FS, files map[string]bool) {
lines := make([]string, 0, len(files))
for path := range files {
lines = append(lines, path)
}
sort.Strings(lines)
fsys.WriteFile(ManifestPath, []byte(strings.Join(lines, "\n")))
}
// Read list of old generated files from the manifest file,
// then delete those that are no longer relevant.
func DeleteOldGeneratedFiles(fsys writablefs.FS, currentFiles map[string]bool) {
oldFiles := readManifest(fsys)
numRemovals := 0
for path := range oldFiles {
_, ok := currentFiles[path]
if !ok {
fsys.RemoveAll(path)
numRemovals += 1
fmt.Println("Removed", path)
}
}
if numRemovals > 0 {
fmt.Printf("Removed %d outdated files\n", numRemovals)
}
}
func readManifest(fsys writablefs.FS) map[string]bool {
result := make(map[string]bool)
f, err := fsys.Open(ManifestPath)
if err != nil {
return result
}
defer f.Close()
s := bufio.NewScanner(f)
for s.Scan() {
result[s.Text()] = true
}
return result
}