From c6423be20f30e32a8b9c7ad03f9c9051fec525cd Mon Sep 17 00:00:00 2001 From: Oliver Steele Date: Thu, 24 Aug 2017 12:23:40 -0400 Subject: [PATCH] Move dependencies to own file --- site/dependencies.go | 113 ++++++++++++++++++++++++++++++++++++++ site/dependencies_test.go | 41 ++++++++++++++ site/rebuild.go | 82 --------------------------- site/site.go | 25 --------- 4 files changed, 154 insertions(+), 107 deletions(-) create mode 100644 site/dependencies.go create mode 100644 site/dependencies_test.go diff --git a/site/dependencies.go b/site/dependencies.go new file mode 100644 index 0000000..3e03157 --- /dev/null +++ b/site/dependencies.go @@ -0,0 +1,113 @@ +package site + +import ( + "path/filepath" + "regexp" + "strings" + + "github.com/osteele/gojekyll/pages" + "github.com/osteele/gojekyll/utils" +) + +var excludeFileRE = regexp.MustCompile(`^[#~]|^\..|~$`) + +// Exclude returns a boolean indicating that the site configuration excludes a file or directory. +// It does not exclude top-level _underscore files and directories. +func (s *Site) Exclude(siteRel string) bool { + for siteRel != "." { + dir, base := filepath.Dir(siteRel), filepath.Base(siteRel) + switch { + case utils.MatchList(s.config.Include, siteRel): + return false + case utils.MatchList(s.config.Exclude, siteRel): + return true + case dir != "." && base[0] == '_': + return true + default: + if excludeFileRE.MatchString(base) { + return true + } + } + siteRel = dir + } + return false +} + +// RequiresFullReload returns true if a source file requires a full reload / rebuild. +// +// This is always true outside of incremental mode, since even a +// static asset can cause pages to change if they reference its +// variables. +// +// This function works on relative paths. It does not work for theme +// sources. +func (s *Site) RequiresFullReload(paths []string) bool { + for _, path := range paths { + switch { + case s.config.IsConfigPath(path): + return true + case s.Exclude(path): + continue + case !s.config.Incremental: + return true + case strings.HasPrefix(path, s.config.DataDir): + return true + case strings.HasPrefix(path, s.config.IncludesDir): + return true + case strings.HasPrefix(path, s.config.LayoutsDir): + return true + case strings.HasPrefix(path, s.config.SassDir()): + return true + } + } + return false +} + +// De-dup relative paths, and filter to those that might affect the build. +// +// Site watch uses this to decide when to send events. +func (s *Site) affectsBuildFilter(paths []string) []string { + var ( + result = make([]string, 0, len(paths)) + seen = map[string]bool{} + ) +loop: + for _, path := range paths { + switch { + case s.config.IsConfigPath(path): + // break + case !s.fileAffectsBuild(path): + continue loop + case seen[path]: + continue loop + } + result = append(result, path) + seen[path] = true + } + return result +} + +// Returns true if the file or a parent directory is excluded. +// Cf. Site.Exclude. +func (s *Site) fileAffectsBuild(rel string) bool { + for rel != "" { + switch { + case rel == ".": + return true + case utils.MatchList(s.config.Include, rel): + return true + case utils.MatchList(s.config.Exclude, rel): + return false + case strings.HasPrefix(rel, "."): + return false + } + rel = filepath.Dir(rel) + } + return true +} + +// returns true if changes to the site-relative paths invalidate doc +func (s *Site) invalidatesDoc(paths map[string]bool, d pages.Document) bool { + rel := utils.MustRel(s.SourceDir(), d.SourcePath()) + return paths[rel] +} diff --git a/site/dependencies_test.go b/site/dependencies_test.go new file mode 100644 index 0000000..5675e32 --- /dev/null +++ b/site/dependencies_test.go @@ -0,0 +1,41 @@ +package site + +import ( + "testing" + + "github.com/osteele/gojekyll/config" + "github.com/stretchr/testify/require" +) + +//func TestSite_WatchRebuild(t *testing.T) { + +func TestSite_Reloaded(t *testing.T) { + s0 := New(config.Flags{}) + s0.config.Incremental = true + s1, _ := s0.Reloaded([]string{}) + require.Equal(t, s0, s1) + + s1, _ = s0.Reloaded([]string{"_config.yml"}) + require.NotEqual(t, s0, s1) +} + +//func TestSite_processFilesEvent(t *testing.T) { +//func TestSite_rebuild(t *testing.T) { + +func TestSite_RequiresFullReload(t *testing.T) { + s := New(config.Flags{}) + require.False(t, s.RequiresFullReload([]string{})) + require.True(t, s.RequiresFullReload([]string{"file.md"})) + require.False(t, s.RequiresFullReload([]string{".git"})) + // require.False(t, s.RequiresFullReload([]string{"_site"})) + // require.False(t, s.RequiresFullReload([]string{"_site/index.html"})) + + s.config.Incremental = true + require.False(t, s.RequiresFullReload([]string{})) + require.False(t, s.RequiresFullReload([]string{"file.md"})) + require.True(t, s.RequiresFullReload([]string{"_config.yml"})) +} + +//func TestSite_affectsBuildFilter(t *testing.T) { +//func TestSite_fileAffectsBuild(t *testing.T) { +//func TestSite_invalidatesDoc(t *testing.T) { diff --git a/site/rebuild.go b/site/rebuild.go index bb09bbd..12e5e6f 100644 --- a/site/rebuild.go +++ b/site/rebuild.go @@ -3,11 +3,8 @@ package site import ( "fmt" "os" - "path/filepath" - "strings" "time" - "github.com/osteele/gojekyll/pages" "github.com/osteele/gojekyll/utils" ) @@ -88,82 +85,3 @@ func (s *Site) rebuild(paths []string) (r *Site, n int, err error) { } return } - -// RequiresFullReload returns true if a source file requires a full reload / rebuild. -// -// This is always true outside of incremental mode, since even a -// static asset can cause pages to change if they reference its -// variables. -// -// This function works on relative paths. It does not work for theme -// sources. -func (s *Site) RequiresFullReload(paths []string) bool { - for _, path := range paths { - switch { - case s.config.IsConfigPath(path): - return true - case s.Exclude(path): - continue - case !s.config.Incremental: - return true - case strings.HasPrefix(path, s.config.DataDir): - return true - case strings.HasPrefix(path, s.config.IncludesDir): - return true - case strings.HasPrefix(path, s.config.LayoutsDir): - return true - case strings.HasPrefix(path, s.config.SassDir()): - return true - } - } - return false -} - -// De-dup relative paths, and filter to those that might affect the build. -// -// Site watch uses this to decide when to send events. -func (s *Site) affectsBuildFilter(paths []string) []string { - var ( - result = make([]string, 0, len(paths)) - seen = map[string]bool{} - ) -loop: - for _, path := range paths { - switch { - case s.config.IsConfigPath(path): - // break - case !s.fileAffectsBuild(path): - continue loop - case seen[path]: - continue loop - } - result = append(result, path) - seen[path] = true - } - return result -} - -// Returns true if the file or a parent directory is excluded. -// Cf. Site.Exclude. -func (s *Site) fileAffectsBuild(rel string) bool { - for rel != "" { - switch { - case rel == ".": - return true - case utils.MatchList(s.config.Include, rel): - return true - case utils.MatchList(s.config.Exclude, rel): - return false - case strings.HasPrefix(rel, "."): - return false - } - rel = filepath.Dir(rel) - } - return true -} - -// returns true if changes to the site-relative paths invalidate doc -func (s *Site) invalidatesDoc(paths map[string]bool, d pages.Document) bool { - rel := utils.MustRel(s.SourceDir(), d.SourcePath()) - return paths[rel] -} diff --git a/site/site.go b/site/site.go index aca3b1b..e2a0d39 100644 --- a/site/site.go +++ b/site/site.go @@ -3,7 +3,6 @@ package site import ( "fmt" "path/filepath" - "regexp" "sync" "github.com/osteele/gojekyll/collection" @@ -203,27 +202,3 @@ func (s *Site) URLPage(urlpath string) (p pages.Document, found bool) { } return } - -var excludeFileRE = regexp.MustCompile(`^[#~]|^\..|~$`) - -// Exclude returns a boolean indicating that the site configuration excludes a file or directory. -// It does not exclude top-level _underscore files and directories. -func (s *Site) Exclude(siteRel string) bool { - for siteRel != "." { - dir, base := filepath.Dir(siteRel), filepath.Base(siteRel) - switch { - case utils.MatchList(s.config.Include, siteRel): - return false - case utils.MatchList(s.config.Exclude, siteRel): - return true - case dir != "." && base[0] == '_': - return true - default: - if excludeFileRE.MatchString(base) { - return true - } - } - siteRel = dir - } - return false -}