diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2019-05-03 10:16:58 +0300 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2019-07-24 10:35:53 +0300 |
commit | 9f5a92078a3f388b52d597b5a59af5c933a112d2 (patch) | |
tree | 0b2b07e5b3a3f21877bc5585a4bdd76306a09dde /source/filesystem.go | |
parent | 47953148b6121441d0147c960a99829c53b5a5ba (diff) |
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
Diffstat (limited to 'source/filesystem.go')
-rw-r--r-- | source/filesystem.go | 96 |
1 files changed, 45 insertions, 51 deletions
diff --git a/source/filesystem.go b/source/filesystem.go index 0c1a6ac7b..ce62c15a4 100644 --- a/source/filesystem.go +++ b/source/filesystem.go @@ -14,29 +14,25 @@ package source import ( - "os" "path/filepath" - "runtime" "sync" - "github.com/gohugoio/hugo/helpers" - jww "github.com/spf13/jwalterweatherman" - "golang.org/x/text/unicode/norm" + "github.com/pkg/errors" + + "github.com/gohugoio/hugo/hugofs" ) // Filesystem represents a source filesystem. type Filesystem struct { - files []ReadableFile - filesInit sync.Once + files []File + filesInit sync.Once + filesInitErr error Base string - SourceSpec -} + fi hugofs.FileMetaInfo -// Input describes a source input. -type Input interface { - Files() []ReadableFile + SourceSpec } // NewFilesystem returns a new filesytem for a given source spec. @@ -44,76 +40,74 @@ func (sp SourceSpec) NewFilesystem(base string) *Filesystem { return &Filesystem{SourceSpec: sp, Base: base} } +func (sp SourceSpec) NewFilesystemFromFileMetaInfo(fi hugofs.FileMetaInfo) *Filesystem { + return &Filesystem{SourceSpec: sp, fi: fi} +} + // Files returns a slice of readable files. -func (f *Filesystem) Files() []ReadableFile { +func (f *Filesystem) Files() ([]File, error) { f.filesInit.Do(func() { - f.captureFiles() + err := f.captureFiles() + if err != nil { + f.filesInitErr = errors.Wrap(err, "capture files") + } }) - return f.files + return f.files, f.filesInitErr } // add populates a file in the Filesystem.files -func (f *Filesystem) add(name string, fi os.FileInfo) (err error) { - var file ReadableFile +func (f *Filesystem) add(name string, fi hugofs.FileMetaInfo) (err error) { + var file File - if runtime.GOOS == "darwin" { - // When a file system is HFS+, its filepath is in NFD form. - name = norm.NFC.String(name) + file, err = f.SourceSpec.NewFileInfo(fi) + if err != nil { + return err } - file = f.SourceSpec.NewFileInfo(f.Base, name, false, fi) f.files = append(f.files, file) return err } -func (f *Filesystem) captureFiles() { - walker := func(filePath string, fi os.FileInfo, err error) error { +func (f *Filesystem) captureFiles() error { + walker := func(path string, fi hugofs.FileMetaInfo, err error) error { if err != nil { + return err + } + + if fi.IsDir() { return nil } - b, err := f.shouldRead(filePath, fi) + meta := fi.Meta() + filename := meta.Filename() + + b, err := f.shouldRead(filename, fi) if err != nil { return err } + if b { - f.add(filePath, fi) + err = f.add(filename, fi) } + return err } - if f.SourceFs == nil { - panic("Must have a fs") - } - err := helpers.SymbolicWalk(f.SourceFs, f.Base, walker) + w := hugofs.NewWalkway(hugofs.WalkwayConfig{ + Fs: f.SourceFs, + Info: f.fi, + Root: f.Base, + WalkFn: walker, + }) - if err != nil { - jww.ERROR.Println(err) - } + return w.Walk() } -func (f *Filesystem) shouldRead(filename string, fi os.FileInfo) (bool, error) { - if fi.Mode()&os.ModeSymlink == os.ModeSymlink { - link, err := filepath.EvalSymlinks(filename) - if err != nil { - jww.ERROR.Printf("Cannot read symbolic link '%s', error was: %s", filename, err) - return false, nil - } - linkfi, err := f.SourceFs.Stat(link) - if err != nil { - jww.ERROR.Printf("Cannot stat '%s', error was: %s", link, err) - return false, nil - } - - if !linkfi.Mode().IsRegular() { - jww.ERROR.Printf("Symbolic links for directories not supported, skipping '%s'", filename) - } - return false, nil - } +func (f *Filesystem) shouldRead(filename string, fi hugofs.FileMetaInfo) (bool, error) { - ignore := f.SourceSpec.IgnoreFile(filename) + ignore := f.SourceSpec.IgnoreFile(fi.Meta().Filename()) if fi.IsDir() { if ignore { |