1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-11-30 10:39:02 +01:00
gojekyll/site/build.go
2017-07-12 21:17:11 -04:00

57 lines
1.3 KiB
Go

package site
import (
"fmt"
"os"
"path/filepath"
"github.com/osteele/gojekyll/utils"
)
// BuildOptions holds options for Build and Clean
type BuildOptions struct {
DryRun bool
UseHardLinks bool
Verbose bool
}
// Clean the destination. Remove files that aren't in keep_files, and resulting empty diretories.
func (s *Site) Clean(options BuildOptions) error {
// TODO PERF when called as part of build, keep files that will be re-generated
removeFiles := func(filename string, info os.FileInfo, err error) error {
if options.Verbose {
fmt.Println("rm", filename)
}
switch {
case err != nil && os.IsNotExist(err):
return nil
case err != nil:
return err
case info.IsDir():
return nil
case s.KeepFile(utils.MustRel(s.DestDir(), filename)):
return nil
case options.DryRun:
return nil
default:
return os.Remove(filename)
}
}
if err := filepath.Walk(s.DestDir(), removeFiles); err != nil {
return err
}
return utils.RemoveEmptyDirectories(s.DestDir())
}
// Build cleans the destination and create files in it.
// It attends to the global options.dry_run.
func (s *Site) Build(options BuildOptions) (int, error) {
if err := s.prepareRendering(); err != nil {
return 0, err
}
if err := s.Clean(options); err != nil {
return 0, err
}
return s.WriteFiles(options)
}