Repos / s4g / 00885b8e24
commit 00885b8e246aa50062e335de64c09b7a7964cfef
Author: Nhân <hi@imnhan.com>
Date: Sun Jul 9 16:58:39 2023 +0700
delete orphaned generated files
diff --git a/main.go b/main.go
index 909c968..ebbf72c 100644
--- a/main.go
+++ b/main.go
@@ -84,6 +84,8 @@ func regenerate(fsys writablefs.FS) {
return
}
+ generatedFiles := make(map[string]bool)
+
// Sort articles, newest first
sort.Slice(articles, func(i int, j int) bool {
return articles[i].PostedAt.Compare(articles[j].PostedAt) > 0
@@ -111,11 +113,13 @@ func regenerate(fsys writablefs.FS) {
for _, a := range articles {
fmt.Println(">", a.Path, "-", a.Title)
a.WriteHtmlFile(&site, articlesInNav, startYear)
+ generatedFiles[a.WebPath] = true
}
fmt.Printf("Processed %d articles\n", len(articles))
if site.GenerateHome {
WriteHomePage(fsys, site, articlesInFeed, articlesInNav, startYear)
+ generatedFiles["index.html"] = true
fmt.Println("Generated index.html")
}
@@ -123,7 +127,11 @@ func regenerate(fsys writablefs.FS) {
FEED_PATH,
generateFeed(site, articlesInFeed, site.HomePath+FEED_PATH),
)
+ generatedFiles[FEED_PATH] = true
fmt.Println("Generated", FEED_PATH)
+
+ DeleteOldGeneratedFiles(fsys, generatedFiles)
+ WriteManifest(fsys, generatedFiles)
}
type SiteMetadata struct {
diff --git a/makesite.go b/makesite.go
index ac9b60d..af438ac 100644
--- a/makesite.go
+++ b/makesite.go
@@ -16,11 +16,13 @@
var defaultTheme embed.FS
func makeSite(path string, meta SiteMetadata) error {
+ // Create web root dir
err := os.MkdirAll(path, 0755)
if err != nil {
return fmt.Errorf("make site: %w", err)
}
+ // Create site metadata file
metaFilePath := filepath.Join(path, SITE_FILENAME)
metaFile, err := os.Create(metaFilePath)
if err != nil {
@@ -34,7 +36,9 @@ func makeSite(path string, meta SiteMetadata) error {
return fmt.Errorf("write site metadata: %w", err)
}
+ // Copy default theme into new site
copyTheme(defaultTheme, path)
+
return nil
}
diff --git a/manifest.go b/manifest.go
new file mode 100644
index 0000000..8e0f249
--- /dev/null
+++ b/manifest.go
@@ -0,0 +1,57 @@
+package main
+
+import (
+ "bufio"
+ "fmt"
+
+ "go.imnhan.com/webmaker2000/writablefs"
+)
+
+const ManifestPath = "manifest.txt"
+
+// Write list of files generated by webmaker2000
+func WriteManifest(fsys writablefs.FS, files map[string]bool) {
+ content := ""
+
+ for path := range files {
+ content += path + "\n"
+ }
+
+ fsys.WriteFile(ManifestPath, []byte(content))
+}
+
+// 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
+}
diff --git a/watcher.go b/watcher.go
index 0e2baf8..d24f350 100644
--- a/watcher.go
+++ b/watcher.go
@@ -111,5 +111,7 @@ func printWatchList(w *fsnotify.Watcher) {
// temp files or supporting data like .git.
func shouldIgnore(path string) bool {
fname := filepath.Base(path)
- return fname[0] == '.' || strings.HasSuffix(fname, ".swp")
+ return fname[0] == '.' ||
+ fname == ManifestPath ||
+ strings.HasSuffix(fname, ".swp")
}
diff --git a/www/manifest.txt b/www/manifest.txt
new file mode 100644
index 0000000..c86a93d
--- /dev/null
+++ b/www/manifest.txt
@@ -0,0 +1,5 @@
+mfws.html
+hello/index.html
+about/index.html
+index.html
+feed.xml