2017-07-04 09:09:36 -04:00
|
|
|
package site
|
2017-06-10 15:38:09 -04:00
|
|
|
|
|
|
|
import (
|
2017-06-13 11:00:24 -04:00
|
|
|
"fmt"
|
2017-06-10 15:38:09 -04:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2017-07-17 08:23:54 -04:00
|
|
|
"time"
|
2017-06-16 19:17:22 -04:00
|
|
|
|
2017-07-09 16:17:20 -04:00
|
|
|
"github.com/osteele/gojekyll/utils"
|
2017-06-10 15:38:09 -04:00
|
|
|
)
|
|
|
|
|
2017-07-17 08:23:54 -04:00
|
|
|
// Clean the destination. Remove files that aren't in keep_files, and resulting empty directories.
|
2017-07-17 08:00:13 -04:00
|
|
|
func (s *Site) Clean() error {
|
2017-06-19 14:03:04 -04:00
|
|
|
// TODO PERF when called as part of build, keep files that will be re-generated
|
2017-07-01 19:42:48 -04:00
|
|
|
removeFiles := func(filename string, info os.FileInfo, err error) error {
|
2017-07-17 08:00:13 -04:00
|
|
|
if s.config.Verbose {
|
2017-07-01 19:42:48 -04:00
|
|
|
fmt.Println("rm", filename)
|
|
|
|
}
|
2017-06-13 11:00:24 -04:00
|
|
|
switch {
|
|
|
|
case err != nil && os.IsNotExist(err):
|
|
|
|
return nil
|
|
|
|
case err != nil:
|
2017-06-10 15:38:09 -04:00
|
|
|
return err
|
2017-06-13 11:00:24 -04:00
|
|
|
case info.IsDir():
|
2017-06-10 15:38:09 -04:00
|
|
|
return nil
|
2017-07-09 16:17:20 -04:00
|
|
|
case s.KeepFile(utils.MustRel(s.DestDir(), filename)):
|
2017-06-13 11:00:24 -04:00
|
|
|
return nil
|
2017-07-17 08:00:13 -04:00
|
|
|
case s.config.DryRun:
|
2017-06-18 16:55:52 -04:00
|
|
|
return nil
|
2017-06-13 11:00:24 -04:00
|
|
|
default:
|
2017-08-08 17:22:44 -04:00
|
|
|
// empirically, moving the os.Remove into a goroutine has no performance benefit
|
2017-07-01 19:42:48 -04:00
|
|
|
return os.Remove(filename)
|
2017-06-10 15:38:09 -04:00
|
|
|
}
|
|
|
|
}
|
2017-06-30 20:00:38 -04:00
|
|
|
if err := filepath.Walk(s.DestDir(), removeFiles); err != nil {
|
2017-06-10 17:51:46 -04:00
|
|
|
return err
|
2017-06-10 15:38:09 -04:00
|
|
|
}
|
2017-07-09 16:17:20 -04:00
|
|
|
return utils.RemoveEmptyDirectories(s.DestDir())
|
2017-06-10 15:38:09 -04:00
|
|
|
}
|
|
|
|
|
2017-07-17 08:23:54 -04: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
|
|
|
|
}
|