Migrating Hugo to Afero for filesystem calls.

This commit is contained in:
spf13 2014-11-01 11:57:29 -04:00
parent dfb8482569
commit 141f3e19e0
10 changed files with 74 additions and 36 deletions

View file

@ -23,9 +23,10 @@ import (
"sync" "sync"
"time" "time"
"github.com/mostafah/fsync"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/fsync"
"github.com/spf13/hugo/helpers" "github.com/spf13/hugo/helpers"
"github.com/spf13/hugo/hugofs"
"github.com/spf13/hugo/hugolib" "github.com/spf13/hugo/hugolib"
"github.com/spf13/hugo/livereload" "github.com/spf13/hugo/livereload"
"github.com/spf13/hugo/utils" "github.com/spf13/hugo/utils"
@ -223,6 +224,10 @@ func copyStatic() error {
publishDir := helpers.AbsPathify(viper.GetString("PublishDir")) + "/" publishDir := helpers.AbsPathify(viper.GetString("PublishDir")) + "/"
syncer := fsync.NewSyncer()
syncer.SrcFs = hugofs.SourceFs
syncer.DestFs = hugofs.DestinationFS
if themeSet() { if themeSet() {
themeDir := helpers.AbsPathify("themes/"+viper.GetString("theme")) + "/static/" themeDir := helpers.AbsPathify("themes/"+viper.GetString("theme")) + "/static/"
if _, err := os.Stat(themeDir); os.IsNotExist(err) { if _, err := os.Stat(themeDir); os.IsNotExist(err) {
@ -232,12 +237,12 @@ func copyStatic() error {
// Copy Static to Destination // Copy Static to Destination
jww.INFO.Println("syncing from", themeDir, "to", publishDir) jww.INFO.Println("syncing from", themeDir, "to", publishDir)
utils.CheckErr(fsync.Sync(publishDir, themeDir), fmt.Sprintf("Error copying static files of theme to %s", publishDir)) utils.CheckErr(syncer.Sync(publishDir, themeDir), fmt.Sprintf("Error copying static files of theme to %s", publishDir))
} }
// Copy Static to Destination // Copy Static to Destination
jww.INFO.Println("syncing from", staticDir, "to", publishDir) jww.INFO.Println("syncing from", staticDir, "to", publishDir)
return fsync.Sync(publishDir, staticDir) return syncer.Sync(publishDir, staticDir)
} }
func getDirList() []string { func getDirList() []string {

View file

@ -21,6 +21,7 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/hugo/create" "github.com/spf13/hugo/create"
"github.com/spf13/hugo/helpers" "github.com/spf13/hugo/helpers"
"github.com/spf13/hugo/hugofs"
"github.com/spf13/hugo/parser" "github.com/spf13/hugo/parser"
jww "github.com/spf13/jwalterweatherman" jww "github.com/spf13/jwalterweatherman"
"github.com/spf13/viper" "github.com/spf13/viper"
@ -115,9 +116,9 @@ func NewSite(cmd *cobra.Command, args []string) {
jww.FATAL.Fatalln(err) jww.FATAL.Fatalln(err)
} }
if x, _ := helpers.Exists(createpath); x { if x, _ := helpers.Exists(createpath, hugofs.SourceFs); x {
y, _ := helpers.IsDir(createpath) y, _ := helpers.IsDir(createpath, hugofs.SourceFs)
if z, _ := helpers.IsEmpty(createpath); y && z { if z, _ := helpers.IsEmpty(createpath, hugofs.SourceFs); y && z {
jww.INFO.Println(createpath, "already exists and is empty") jww.INFO.Println(createpath, "already exists and is empty")
} else { } else {
jww.FATAL.Fatalln(createpath, "already exists and is not empty") jww.FATAL.Fatalln(createpath, "already exists and is not empty")
@ -143,7 +144,7 @@ func NewTheme(cmd *cobra.Command, args []string) {
createpath := helpers.AbsPathify(path.Join("themes", args[0])) createpath := helpers.AbsPathify(path.Join("themes", args[0]))
jww.INFO.Println("creating theme at", createpath) jww.INFO.Println("creating theme at", createpath)
if x, _ := helpers.Exists(createpath); x { if x, _ := helpers.Exists(createpath, hugofs.SourceFs); x {
jww.FATAL.Fatalln(createpath, "already exists") jww.FATAL.Fatalln(createpath, "already exists")
} }
@ -185,7 +186,7 @@ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
`) `)
err := helpers.WriteToDisk(path.Join(createpath, "LICENSE.md"), bytes.NewReader(by)) err := helpers.WriteToDisk(path.Join(createpath, "LICENSE.md"), bytes.NewReader(by), hugofs.SourceFs)
if err != nil { if err != nil {
jww.FATAL.Fatalln(err) jww.FATAL.Fatalln(err)
} }
@ -205,7 +206,7 @@ func mkdir(x ...string) {
func touchFile(x ...string) { func touchFile(x ...string) {
inpath := path.Join(x...) inpath := path.Join(x...)
mkdir(filepath.Dir(inpath)) mkdir(filepath.Dir(inpath))
err := helpers.WriteToDisk(inpath, bytes.NewReader([]byte{})) err := helpers.WriteToDisk(inpath, bytes.NewReader([]byte{}), hugofs.SourceFs)
if err != nil { if err != nil {
jww.FATAL.Fatalln(err) jww.FATAL.Fatalln(err)
} }
@ -227,7 +228,7 @@ func createThemeMD(inpath string) (err error) {
return err return err
} }
err = helpers.WriteToDisk(path.Join(inpath, "theme.toml"), bytes.NewReader(by)) err = helpers.WriteToDisk(path.Join(inpath, "theme.toml"), bytes.NewReader(by), hugofs.SourceFs)
if err != nil { if err != nil {
return return
} }
@ -244,7 +245,7 @@ func createConfig(inpath string, kind string) (err error) {
return err return err
} }
err = helpers.WriteToDisk(path.Join(inpath, "config."+kind), bytes.NewReader(by)) err = helpers.WriteToDisk(path.Join(inpath, "config."+kind), bytes.NewReader(by), hugofs.SourceFs)
if err != nil { if err != nil {
return return
} }

View file

@ -24,8 +24,10 @@ import (
"strings" "strings"
"time" "time"
"github.com/spf13/afero"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/hugo/helpers" "github.com/spf13/hugo/helpers"
"github.com/spf13/hugo/hugofs"
jww "github.com/spf13/jwalterweatherman" jww "github.com/spf13/jwalterweatherman"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
@ -111,7 +113,8 @@ func serve(port int) {
jww.FEEDBACK.Printf("Web Server is available at %s\n", viper.GetString("BaseUrl")) jww.FEEDBACK.Printf("Web Server is available at %s\n", viper.GetString("BaseUrl"))
fmt.Println("Press ctrl+c to stop") fmt.Println("Press ctrl+c to stop")
fileserver := http.FileServer(http.Dir(helpers.AbsPathify(viper.GetString("PublishDir")))) httpFs := &afero.HttpFs{SourceFs: hugofs.DestinationFS}
fileserver := http.FileServer(httpFs.Dir(helpers.AbsPathify(viper.GetString("PublishDir"))))
u, err := url.Parse(viper.GetString("BaseUrl")) u, err := url.Parse(viper.GetString("BaseUrl"))
if err != nil { if err != nil {

View file

@ -23,6 +23,7 @@ import (
"github.com/spf13/cast" "github.com/spf13/cast"
"github.com/spf13/hugo/helpers" "github.com/spf13/hugo/helpers"
"github.com/spf13/hugo/hugofs"
"github.com/spf13/hugo/hugolib" "github.com/spf13/hugo/hugolib"
"github.com/spf13/hugo/parser" "github.com/spf13/hugo/parser"
jww "github.com/spf13/jwalterweatherman" jww "github.com/spf13/jwalterweatherman"
@ -131,7 +132,7 @@ func FindArchetype(kind string) (outpath string) {
for _, p := range pathsToCheck { for _, p := range pathsToCheck {
curpath := path.Join(x, p) curpath := path.Join(x, p)
jww.DEBUG.Println("checking", curpath, "for archetypes") jww.DEBUG.Println("checking", curpath, "for archetypes")
if exists, _ := helpers.Exists(curpath); exists { if exists, _ := helpers.Exists(curpath, hugofs.SourceFs); exists {
jww.INFO.Println("curpath: " + curpath) jww.INFO.Println("curpath: " + curpath)
return curpath return curpath
} }

View file

@ -24,6 +24,7 @@ import (
"strings" "strings"
"unicode" "unicode"
"github.com/spf13/afero"
"github.com/spf13/viper" "github.com/spf13/viper"
) )
@ -71,8 +72,8 @@ func ReplaceExtension(path string, newExt string) string {
} }
// Check if Exists && is Directory // Check if Exists && is Directory
func DirExists(path string) (bool, error) { func DirExists(path string, fs afero.Fs) (bool, error) {
fi, err := os.Stat(path) fi, err := fs.Stat(path)
if err == nil && fi.IsDir() { if err == nil && fi.IsDir() {
return true, nil return true, nil
} }
@ -82,24 +83,24 @@ func DirExists(path string) (bool, error) {
return false, err return false, err
} }
func IsDir(path string) (bool, error) { func IsDir(path string, fs afero.Fs) (bool, error) {
fi, err := os.Stat(path) fi, err := fs.Stat(path)
if err != nil { if err != nil {
return false, err return false, err
} }
return fi.IsDir(), nil return fi.IsDir(), nil
} }
func IsEmpty(path string) (bool, error) { func IsEmpty(path string, fs afero.Fs) (bool, error) {
if b, _ := Exists(path); !b { if b, _ := Exists(path, fs); !b {
return false, fmt.Errorf("%q path does not exist", path) return false, fmt.Errorf("%q path does not exist", path)
} }
fi, err := os.Stat(path) fi, err := fs.Stat(path)
if err != nil { if err != nil {
return false, err return false, err
} }
if fi.IsDir() { if fi.IsDir() {
f, err := os.Open(path) f, err := fs.Open(path)
if err != nil { if err != nil {
return false, err return false, err
} }
@ -112,8 +113,8 @@ func IsEmpty(path string) (bool, error) {
} }
// Check if File / Directory Exists // Check if File / Directory Exists
func Exists(path string) (bool, error) { func Exists(path string, fs afero.Fs) (bool, error) {
_, err := os.Stat(path) _, err := fs.Stat(path)
if err == nil { if err == nil {
return true, nil return true, nil
} }
@ -267,18 +268,18 @@ func FindCWD() (string, error) {
return path, nil return path, nil
} }
func SafeWriteToDisk(inpath string, r io.Reader) (err error) { func SafeWriteToDisk(inpath string, r io.Reader, fs afero.Fs) (err error) {
dir, _ := filepath.Split(inpath) dir, _ := filepath.Split(inpath)
ospath := filepath.FromSlash(dir) ospath := filepath.FromSlash(dir)
if ospath != "" { if ospath != "" {
err = os.MkdirAll(ospath, 0777) // rwx, rw, r err = fs.MkdirAll(ospath, 0777) // rwx, rw, r
if err != nil { if err != nil {
return return
} }
} }
exists, err := Exists(inpath) exists, err := Exists(inpath, fs)
if err != nil { if err != nil {
return return
} }
@ -286,7 +287,7 @@ func SafeWriteToDisk(inpath string, r io.Reader) (err error) {
return fmt.Errorf("%v already exists", inpath) return fmt.Errorf("%v already exists", inpath)
} }
file, err := os.Create(inpath) file, err := fs.Create(inpath)
if err != nil { if err != nil {
return return
} }
@ -296,18 +297,20 @@ func SafeWriteToDisk(inpath string, r io.Reader) (err error) {
return return
} }
func WriteToDisk(inpath string, r io.Reader) (err error) { func WriteToDisk(inpath string, r io.Reader, fs afero.Fs) (err error) {
dir, _ := filepath.Split(inpath) dir, _ := filepath.Split(inpath)
ospath := filepath.FromSlash(dir) ospath := filepath.FromSlash(dir)
if ospath != "" { if ospath != "" {
err = os.MkdirAll(ospath, 0777) // rwx, rw, r err = fs.MkdirAll(ospath, 0777) // rwx, rw, r
if err != nil { if err != nil {
panic(err) if err != os.ErrExist {
panic(err)
}
} }
} }
file, err := os.Create(inpath) file, err := fs.Create(inpath)
if err != nil { if err != nil {
return return
} }

21
hugofs/fs.go Normal file
View file

@ -0,0 +1,21 @@
// Copyright © 2013-14 Steve Francia <spf@spf13.com>.
//
// Licensed under the Simple Public License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://opensource.org/licenses/Simple-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package hugofs
import "github.com/spf13/afero"
var SourceFs = new(afero.OsFs)
var DestinationFS = new(afero.OsFs)
//var DestinationFS = new(afero.MemMapFs)

View file

@ -26,6 +26,7 @@ import (
"github.com/spf13/cast" "github.com/spf13/cast"
"github.com/spf13/hugo/helpers" "github.com/spf13/hugo/helpers"
"github.com/spf13/hugo/hugofs"
"github.com/spf13/hugo/parser" "github.com/spf13/hugo/parser"
"github.com/spf13/hugo/source" "github.com/spf13/hugo/source"
jww "github.com/spf13/jwalterweatherman" jww "github.com/spf13/jwalterweatherman"
@ -597,9 +598,9 @@ func (page *Page) saveSource(by []byte, inpath string, safe bool) (err error) {
jww.INFO.Println("creating", inpath) jww.INFO.Println("creating", inpath)
if safe { if safe {
err = helpers.SafeWriteToDisk(inpath, bytes.NewReader(by)) err = helpers.SafeWriteToDisk(inpath, bytes.NewReader(by), hugofs.SourceFs)
} else { } else {
err = helpers.WriteToDisk(inpath, bytes.NewReader(by)) err = helpers.WriteToDisk(inpath, bytes.NewReader(by), hugofs.SourceFs)
} }
if err != nil { if err != nil {
return return

View file

@ -28,6 +28,7 @@ import (
"bitbucket.org/pkg/inflect" "bitbucket.org/pkg/inflect"
"github.com/spf13/cast" "github.com/spf13/cast"
"github.com/spf13/hugo/helpers" "github.com/spf13/hugo/helpers"
"github.com/spf13/hugo/hugofs"
"github.com/spf13/hugo/source" "github.com/spf13/hugo/source"
"github.com/spf13/hugo/target" "github.com/spf13/hugo/target"
"github.com/spf13/hugo/transform" "github.com/spf13/hugo/transform"
@ -312,7 +313,7 @@ func (s *Site) absPublishDir() string {
} }
func (s *Site) checkDirectories() (err error) { func (s *Site) checkDirectories() (err error) {
if b, _ := helpers.DirExists(s.absContentDir()); !b { if b, _ := helpers.DirExists(s.absContentDir(), hugofs.SourceFs); !b {
return fmt.Errorf("No source directory found, expecting to find it at " + s.absContentDir()) return fmt.Errorf("No source directory found, expecting to find it at " + s.absContentDir())
} }
return return

View file

@ -6,6 +6,7 @@ import (
"path" "path"
"github.com/spf13/hugo/helpers" "github.com/spf13/hugo/helpers"
"github.com/spf13/hugo/hugofs"
) )
type Publisher interface { type Publisher interface {
@ -34,7 +35,7 @@ func (fs *Filesystem) Publish(path string, r io.Reader) (err error) {
return return
} }
return helpers.WriteToDisk(translated, r) return helpers.WriteToDisk(translated, r, hugofs.DestinationFS)
} }
func (fs *Filesystem) Translate(src string) (dest string, err error) { func (fs *Filesystem) Translate(src string) (dest string, err error) {

View file

@ -7,6 +7,7 @@ import (
"strings" "strings"
"github.com/spf13/hugo/helpers" "github.com/spf13/hugo/helpers"
"github.com/spf13/hugo/hugofs"
) )
const ALIAS = "<!DOCTYPE html><html><head><link rel=\"canonical\" href=\"{{ .Permalink }}\"/><meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" /><meta http-equiv=\"refresh\" content=\"0;url={{ .Permalink }}\" /></head></html>" const ALIAS = "<!DOCTYPE html><html><head><link rel=\"canonical\" href=\"{{ .Permalink }}\"/><meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" /><meta http-equiv=\"refresh\" content=\"0;url={{ .Permalink }}\" /></head></html>"
@ -68,5 +69,5 @@ func (h *HTMLRedirectAlias) Publish(path string, permalink template.HTML) (err e
return return
} }
return helpers.WriteToDisk(path, buffer) return helpers.WriteToDisk(path, buffer, hugofs.DestinationFS)
} }