Skip to content

Commit

Permalink
Add support for symbolic links for content, layout, static, theme
Browse files Browse the repository at this point in the history
Note: This is for the content roots only, but that should satisfy most needs.

Fixes gohugoio#1855
  • Loading branch information
bep authored and spf13 committed Mar 21, 2016
1 parent 6258d48 commit e5aa477
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 9 deletions.
14 changes: 7 additions & 7 deletions commands/hugo.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package commands

import (
"fmt"
"github.com/spf13/hugo/hugofs"
"io/ioutil"
"net/http"
"os"
Expand All @@ -35,7 +36,6 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/fsync"
"github.com/spf13/hugo/helpers"
"github.com/spf13/hugo/hugofs"
"github.com/spf13/hugo/hugolib"
"github.com/spf13/hugo/livereload"
"github.com/spf13/hugo/utils"
Expand Down Expand Up @@ -651,12 +651,12 @@ func getDirList() []string {
return nil
}

filepath.Walk(dataDir, walker)
filepath.Walk(helpers.AbsPathify(viper.GetString("ContentDir")), walker)
filepath.Walk(helpers.AbsPathify(viper.GetString("LayoutDir")), walker)
filepath.Walk(helpers.AbsPathify(viper.GetString("StaticDir")), walker)
helpers.SymbolicWalk(hugofs.SourceFs, dataDir, walker)
helpers.SymbolicWalk(hugofs.SourceFs, helpers.AbsPathify(viper.GetString("ContentDir")), walker)
helpers.SymbolicWalk(hugofs.SourceFs, helpers.AbsPathify(viper.GetString("LayoutDir")), walker)
helpers.SymbolicWalk(hugofs.SourceFs, helpers.AbsPathify(viper.GetString("StaticDir")), walker)
if helpers.ThemeSet() {
filepath.Walk(helpers.AbsPathify(viper.GetString("themesDir")+"/"+viper.GetString("theme")), walker)
helpers.SymbolicWalk(hugofs.SourceFs, helpers.AbsPathify(viper.GetString("themesDir")+"/"+viper.GetString("theme")), walker)
}

return a
Expand Down Expand Up @@ -769,7 +769,7 @@ func NewWatcher(port int) error {
// When mkdir -p is used, only the top directory triggers an event (at least on OSX)
if ev.Op&fsnotify.Create == fsnotify.Create {
if s, err := hugofs.SourceFs.Stat(ev.Name); err == nil && s.Mode().IsDir() {
afero.Walk(hugofs.SourceFs, ev.Name, walkAdder)
helpers.SymbolicWalk(hugofs.SourceFs, ev.Name, walkAdder)
}
}

Expand Down
2 changes: 1 addition & 1 deletion commands/import_jekyll.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func importFromJekyll(cmd *cobra.Command, args []string) error {
return convertJekyllPost(path, relPath, targetDir, draft)
}

err = filepath.Walk(jekyllRoot, callback)
err = helpers.SymbolicWalk(hugofs.OsFs, jekyllRoot, callback)

if err != nil {
return err
Expand Down
18 changes: 18 additions & 0 deletions helpers/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,24 @@ func FindCWD() (string, error) {
return path, nil
}

// SymbolicWalk is like filepath.Walk, but it supports the root being a
// symbolic link. It will still not follow symbolic links deeper down in
// the file structure
func SymbolicWalk(fs afero.Fs, root string, walker filepath.WalkFunc) error {
rootContent, err := afero.ReadDir(fs, root)

if err != nil {
return walker(root, nil, err)
}

for _, fi := range rootContent {
afero.Walk(fs, filepath.Join(root, fi.Name()), walker)
}

return nil

}

// Same as WriteToDisk but checks to see if file/directory already exists.
func SafeWriteToDisk(inpath string, r io.Reader, fs afero.Fs) (err error) {
return afero.SafeWriteReader(fs, inpath, r)
Expand Down
8 changes: 7 additions & 1 deletion source/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package source

import (
"github.com/spf13/hugo/hugofs"
"io"
"os"
"path/filepath"
Expand Down Expand Up @@ -92,7 +93,12 @@ func (f *Filesystem) captureFiles() {
return err
}

filepath.Walk(f.Base, walker)
err := helpers.SymbolicWalk(hugofs.SourceFs, f.Base, walker)

if err != nil {
jww.ERROR.Println(err)
}

}

func (f *Filesystem) shouldRead(filePath string, fi os.FileInfo) (bool, error) {
Expand Down

0 comments on commit e5aa477

Please sign in to comment.