From 8481779221c0789d4e93074ecd06962aa9142961 Mon Sep 17 00:00:00 2001 From: James Mills Date: Sat, 18 Mar 2023 11:59:16 +1000 Subject: [PATCH] Add support for rebuilding if .zsignore or .zs/* files change --- main.go | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index 86df721..a6cd21f 100644 --- a/main.go +++ b/main.go @@ -37,6 +37,16 @@ const ( // PUBDIR is the default directory for publishing final built content PUBDIR = ".pub" + + // DefaultIgnore is the default set of ignore patterns if no .zsignore + DefaultIgnore = `*~ +*.bak + +COPYING +Dockerfile +LICENSE +Makefile +README.md` ) // Ignore holds a set of patterns to ignore from parsing a .zsignore file @@ -483,6 +493,14 @@ func buildAll(ctx context.Context, watch bool) error { case <-ticker.C: os.Mkdir(PUBDIR, 0755) filepath.Walk(".", func(path string, info os.FileInfo, err error) error { + // rebuild if changes to .zs/ or .zsignore + if (filepath.Base(path) == ZSIGNORE || filepath.Dir(path) == ZSDIR) && info.ModTime().After(lastModified) { + Ignore = ParseIgnoreFile(path) + // reset lastModified to 0 so everything rebuidls + lastModified = time.Unix(0, 0) + return nil + } + // ignore hidden files and directories and ignored patterns if filepath.Base(path)[0] == '.' || strings.HasPrefix(path, ".") || Ignore.MatchesPath(path) { return nil @@ -578,17 +596,23 @@ func init() { ensureFirstPath(filepath.Join(w, ZSDIR)) } +func ParseIgnoreFile(fn string) *ignore.GitIgnore { + obj, err := ignore.CompileIgnoreFile(ZSIGNORE) + if err != nil { + log.WithError(err).Warnf("error parsing .zsignore: % (using defaults)s", fn) + return ignore.CompileIgnoreLines(DefaultIgnore) + } + + return obj +} + func main() { // prepend .zs to $PATH, so plugins will be found before OS commands w, _ := os.Getwd() ensureFirstPath(filepath.Join(w, ZSDIR)) // initialise Ignore (.zsignore) patterns - if ignoreObject, err := ignore.CompileIgnoreFile(ZSIGNORE); err == nil { - Ignore = ignoreObject - } else { - Ignore = ignore.CompileIgnoreLines("") - } + Ignore = ParseIgnoreFile(ZSIGNORE) if err := RootCmd.Execute(); err != nil { log.WithError(err).Error("error executing command") -- 2.43.0