mirror of
https://github.com/danog/gojekyll.git
synced 2024-11-30 08:39:01 +01:00
Move dependencies to own file
This commit is contained in:
parent
924fe4e06a
commit
c6423be20f
113
site/dependencies.go
Normal file
113
site/dependencies.go
Normal file
@ -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]
|
||||
}
|
41
site/dependencies_test.go
Normal file
41
site/dependencies_test.go
Normal file
@ -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) {
|
@ -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]
|
||||
}
|
||||
|
25
site/site.go
25
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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user