diff options
Diffstat (limited to 'create')
-rw-r--r-- | create/content.go | 53 | ||||
-rw-r--r-- | create/content_template_handler.go | 7 | ||||
-rw-r--r-- | create/content_test.go | 13 |
3 files changed, 33 insertions, 40 deletions
diff --git a/create/content.go b/create/content.go index 29fe47394..6d022282e 100644 --- a/create/content.go +++ b/create/content.go @@ -16,6 +16,7 @@ package create import ( "bytes" + "fmt" "os" "os/exec" "path/filepath" @@ -31,6 +32,7 @@ func NewContent( ps *helpers.PathSpec, siteFactory func(filename string, siteUsed bool) (*hugolib.Site, error), kind, targetPath string) error { ext := helpers.Ext(targetPath) + fs := ps.BaseFs.SourceFilesystems.Archetypes.Fs jww.INFO.Printf("attempting to create %q of %q of ext %q", targetPath, kind, ext) @@ -40,9 +42,9 @@ func NewContent( siteUsed := false if archetypeFilename != "" { - f, err := ps.Fs.Source.Open(archetypeFilename) + f, err := fs.Open(archetypeFilename) if err != nil { - return err + return fmt.Errorf("failed to open archetype file: %s", err) } defer f.Close() @@ -71,7 +73,7 @@ func NewContent( targetDir := filepath.Dir(targetPath) if targetDir != "" && targetDir != "." { - exists, _ = helpers.Exists(targetDir, ps.Fs.Source) + exists, _ = helpers.Exists(targetDir, fs) } if exists { @@ -101,42 +103,27 @@ func NewContent( return nil } -// FindArchetype takes a given kind/archetype of content and returns an output -// path for that archetype. If no archetype is found, an empty string is -// returned. +// FindArchetype takes a given kind/archetype of content and returns the path +// to the archetype in the archetype filesystem, blank if none found. func findArchetype(ps *helpers.PathSpec, kind, ext string) (outpath string) { - search := []string{ps.AbsPathify(ps.Cfg.GetString("archetypeDir"))} + fs := ps.BaseFs.Archetypes.Fs - if ps.Cfg.GetString("theme") != "" { - themeDir := filepath.Join(ps.AbsPathify(ps.Cfg.GetString("themesDir")+"/"+ps.Cfg.GetString("theme")), "/archetypes/") - if _, err := ps.Fs.Source.Stat(themeDir); os.IsNotExist(err) { - jww.ERROR.Printf("Unable to find archetypes directory for theme %q at %q", ps.Cfg.GetString("theme"), themeDir) + // If the new content isn't in a subdirectory, kind == "". + // Therefore it should be excluded otherwise `is a directory` + // error will occur. github.com/gohugoio/hugo/issues/411 + var pathsToCheck = []string{"default"} + + if ext != "" { + if kind != "" { + pathsToCheck = append([]string{kind + ext, "default" + ext}, pathsToCheck...) } else { - search = append(search, themeDir) + pathsToCheck = append([]string{"default" + ext}, pathsToCheck...) } } - for _, x := range search { - // If the new content isn't in a subdirectory, kind == "". - // Therefore it should be excluded otherwise `is a directory` - // error will occur. github.com/gohugoio/hugo/issues/411 - var pathsToCheck = []string{"default"} - - if ext != "" { - if kind != "" { - pathsToCheck = append([]string{kind + ext, "default" + ext}, pathsToCheck...) - } else { - pathsToCheck = append([]string{"default" + ext}, pathsToCheck...) - } - } - - for _, p := range pathsToCheck { - curpath := filepath.Join(x, p) - jww.DEBUG.Println("checking", curpath, "for archetypes") - if exists, _ := helpers.Exists(curpath, ps.Fs.Source); exists { - jww.INFO.Println("curpath: " + curpath) - return curpath - } + for _, p := range pathsToCheck { + if exists, _ := helpers.Exists(p, fs); exists { + return p } } diff --git a/create/content_template_handler.go b/create/content_template_handler.go index 17e52cae0..37eed52cf 100644 --- a/create/content_template_handler.go +++ b/create/content_template_handler.go @@ -89,10 +89,11 @@ func executeArcheTypeAsTemplate(s *hugolib.Site, kind, targetPath, archetypeFile ) ps, err := helpers.NewPathSpec(s.Deps.Fs, s.Deps.Cfg) - sp := source.NewSourceSpec(ps, ps.Fs.Source) if err != nil { return nil, err } + sp := source.NewSourceSpec(ps, ps.Fs.Source) + f := sp.NewFileInfo("", targetPath, false, nil) name := f.TranslationBaseName() @@ -115,9 +116,9 @@ func executeArcheTypeAsTemplate(s *hugolib.Site, kind, targetPath, archetypeFile // TODO(bep) archetype revive the issue about wrong tpl funcs arg order archetypeTemplate = []byte(ArchetypeTemplateTemplate) } else { - archetypeTemplate, err = afero.ReadFile(s.Fs.Source, archetypeFilename) + archetypeTemplate, err = afero.ReadFile(s.BaseFs.Archetypes.Fs, archetypeFilename) if err != nil { - return nil, fmt.Errorf("Failed to read archetype file %q: %s", archetypeFilename, err) + return nil, fmt.Errorf("failed to read archetype file %s", err) } } diff --git a/create/content_test.go b/create/content_test.go index 62d5ed1da..e9d46becf 100644 --- a/create/content_test.go +++ b/create/content_test.go @@ -58,17 +58,15 @@ func TestNewContent(t *testing.T) { for _, c := range cases { cfg, fs := newTestCfg() - ps, err := helpers.NewPathSpec(fs, cfg) - require.NoError(t, err) + require.NoError(t, initFs(fs)) h, err := hugolib.NewHugoSites(deps.DepsCfg{Cfg: cfg, Fs: fs}) require.NoError(t, err) - require.NoError(t, initFs(fs)) siteFactory := func(filename string, siteUsed bool) (*hugolib.Site, error) { return h.Sites[0], nil } - require.NoError(t, create.NewContent(ps, siteFactory, c.kind, c.path)) + require.NoError(t, create.NewContent(h.PathSpec, siteFactory, c.kind, c.path)) fname := filepath.Join("content", filepath.FromSlash(c.path)) content := readFileFromFs(t, fs.Source, fname) @@ -89,6 +87,7 @@ func initViper(v *viper.Viper) { v.Set("layoutDir", "layouts") v.Set("i18nDir", "i18n") v.Set("theme", "sample") + v.Set("archetypeDir", "archetypes") } func initFs(fs *hugofs.Fs) error { @@ -187,6 +186,12 @@ func readFileFromFs(t *testing.T, fs afero.Fs, filename string) string { func newTestCfg() (*viper.Viper, *hugofs.Fs) { v := viper.New() + v.Set("contentDir", "content") + v.Set("dataDir", "data") + v.Set("i18nDir", "i18n") + v.Set("layoutDir", "layouts") + v.Set("archetypeDir", "archetypes") + fs := hugofs.NewMem(v) v.SetFs(fs.Source) |