Add support for rebuilding if .zsignore or .zs/* files change

This commit is contained in:
James Mills 2023-03-18 11:59:16 +10:00
parent 02b31649bc
commit 8481779221
No known key found for this signature in database
GPG Key ID: AC4C014F1440EBD6

34
main.go
View File

@ -37,6 +37,16 @@ const (
// PUBDIR is the default directory for publishing final built content // PUBDIR is the default directory for publishing final built content
PUBDIR = ".pub" 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 // 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: case <-ticker.C:
os.Mkdir(PUBDIR, 0755) os.Mkdir(PUBDIR, 0755)
filepath.Walk(".", func(path string, info os.FileInfo, err error) error { 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 // ignore hidden files and directories and ignored patterns
if filepath.Base(path)[0] == '.' || strings.HasPrefix(path, ".") || Ignore.MatchesPath(path) { if filepath.Base(path)[0] == '.' || strings.HasPrefix(path, ".") || Ignore.MatchesPath(path) {
return nil return nil
@ -578,17 +596,23 @@ func init() {
ensureFirstPath(filepath.Join(w, ZSDIR)) 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() { func main() {
// prepend .zs to $PATH, so plugins will be found before OS commands // prepend .zs to $PATH, so plugins will be found before OS commands
w, _ := os.Getwd() w, _ := os.Getwd()
ensureFirstPath(filepath.Join(w, ZSDIR)) ensureFirstPath(filepath.Join(w, ZSDIR))
// initialise Ignore (.zsignore) patterns // initialise Ignore (.zsignore) patterns
if ignoreObject, err := ignore.CompileIgnoreFile(ZSIGNORE); err == nil { Ignore = ParseIgnoreFile(ZSIGNORE)
Ignore = ignoreObject
} else {
Ignore = ignore.CompileIgnoreLines("")
}
if err := RootCmd.Execute(); err != nil { if err := RootCmd.Execute(); err != nil {
log.WithError(err).Error("error executing command") log.WithError(err).Error("error executing command")