1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-12-02 23:27:47 +01:00
gojekyll/site/build.go

66 lines
1.5 KiB
Go
Raw Normal View History

2017-07-04 15:09:36 +02:00
package site
import (
"fmt"
"os"
"path/filepath"
2017-07-17 14:23:54 +02:00
"time"
2017-06-17 01:17:22 +02:00
2017-07-09 22:17:20 +02:00
"github.com/osteele/gojekyll/utils"
)
2017-07-17 14:23:54 +02:00
// Clean the destination. Remove files that aren't in keep_files, and resulting empty directories.
func (s *Site) Clean() error {
2017-06-19 20:03:04 +02:00
// TODO PERF when called as part of build, keep files that will be re-generated
2017-07-02 01:42:48 +02:00
removeFiles := func(filename string, info os.FileInfo, err error) error {
if s.config.Verbose {
2017-07-02 01:42:48 +02:00
fmt.Println("rm", filename)
}
switch {
case err != nil && os.IsNotExist(err):
return nil
case err != nil:
return err
case info.IsDir():
return nil
2017-07-09 22:17:20 +02:00
case s.KeepFile(utils.MustRel(s.DestDir(), filename)):
return nil
case s.config.DryRun:
2017-06-18 22:55:52 +02:00
return nil
default:
2017-08-08 23:22:44 +02:00
// empirically, moving the os.Remove into a goroutine has no performance benefit
2017-07-02 01:42:48 +02:00
return os.Remove(filename)
}
}
if err := filepath.Walk(s.DestDir(), removeFiles); err != nil {
2017-06-10 23:51:46 +02:00
return err
}
2017-07-09 22:17:20 +02:00
return utils.RemoveEmptyDirectories(s.DestDir())
}
// Build cleans the destination and create files in it.
2017-07-17 14:23:54 +02:00
// This sets TZ from the site config.
func (s *Site) Build() (int, error) {
2017-07-17 14:23:54 +02:00
if err := s.setTimeZone(); err != nil {
return 0, err
}
if err := s.ensureRendered(); err != nil {
2017-06-24 20:00:19 +02:00
return 0, err
}
if err := s.Clean(); err != nil {
2017-06-24 20:00:19 +02:00
return 0, err
}
return s.WriteFiles()
2017-06-20 14:59:54 +02:00
}
2017-07-17 14:23:54 +02:00
func (s *Site) setTimeZone() error {
if tz := s.config.Timezone; tz != "" {
if _, err := time.LoadLocation(tz); err != nil {
fmt.Fprintf(os.Stderr, "%s; using local time zone\n", err)
} else if err := os.Setenv("TZ", tz); err != nil {
return err
}
}
return nil
}