2017-06-22 21:21:47 +02:00
|
|
|
package sites
|
2017-06-10 21:38:09 +02:00
|
|
|
|
|
|
|
import (
|
2017-06-13 17:00:24 +02:00
|
|
|
"fmt"
|
2017-06-10 21:38:09 +02:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2017-06-17 01:17:22 +02:00
|
|
|
|
2017-06-17 02:11:52 +02:00
|
|
|
"github.com/osteele/gojekyll/helpers"
|
2017-06-10 21:38:09 +02:00
|
|
|
)
|
|
|
|
|
2017-06-17 16:51:32 +02:00
|
|
|
// BuildOptions holds options for Build and Clean
|
|
|
|
type BuildOptions struct {
|
|
|
|
DryRun bool
|
|
|
|
UseHardLinks bool
|
2017-06-18 22:55:52 +02:00
|
|
|
Verbose bool
|
2017-06-17 16:51:32 +02:00
|
|
|
}
|
|
|
|
|
2017-06-13 17:00:24 +02:00
|
|
|
// Clean the destination. Remove files that aren't in keep_files, and resulting empty diretories.
|
2017-06-22 16:37:31 +02:00
|
|
|
func (s *Site) Clean(options BuildOptions) 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-06-17 02:06:55 +02:00
|
|
|
removeFiles := func(name string, info os.FileInfo, err error) error {
|
2017-06-18 22:55:52 +02:00
|
|
|
if options.Verbose {
|
|
|
|
fmt.Println("rm", name)
|
|
|
|
}
|
2017-06-13 17:00:24 +02:00
|
|
|
switch {
|
|
|
|
case err != nil && os.IsNotExist(err):
|
|
|
|
return nil
|
|
|
|
case err != nil:
|
2017-06-10 21:38:09 +02:00
|
|
|
return err
|
2017-06-13 17:00:24 +02:00
|
|
|
case info.IsDir():
|
2017-06-10 21:38:09 +02:00
|
|
|
return nil
|
2017-06-22 16:37:31 +02:00
|
|
|
case s.KeepFile(name):
|
2017-06-13 17:00:24 +02:00
|
|
|
return nil
|
2017-06-17 16:51:32 +02:00
|
|
|
case options.DryRun:
|
2017-06-18 22:55:52 +02:00
|
|
|
return nil
|
2017-06-13 17:00:24 +02:00
|
|
|
default:
|
2017-06-17 02:06:55 +02:00
|
|
|
return os.Remove(name)
|
2017-06-10 21:38:09 +02:00
|
|
|
}
|
|
|
|
}
|
2017-06-22 16:37:31 +02:00
|
|
|
if err := filepath.Walk(s.Destination, removeFiles); err != nil {
|
2017-06-10 23:51:46 +02:00
|
|
|
return err
|
2017-06-10 21:38:09 +02:00
|
|
|
}
|
2017-06-22 16:37:31 +02:00
|
|
|
return helpers.RemoveEmptyDirectories(s.Destination)
|
2017-06-10 21:38:09 +02:00
|
|
|
}
|
|
|
|
|
2017-06-13 17:00:24 +02:00
|
|
|
// Build cleans the destination and create files in it.
|
|
|
|
// It attends to the global options.dry_run.
|
2017-06-22 16:37:31 +02:00
|
|
|
func (s *Site) Build(options BuildOptions) (int, error) {
|
2017-06-20 14:59:54 +02:00
|
|
|
count := 0
|
2017-06-29 17:00:59 +02:00
|
|
|
if err := s.prepareRendering(); err != nil {
|
2017-06-24 20:00:19 +02:00
|
|
|
return 0, err
|
|
|
|
}
|
2017-06-22 16:37:31 +02:00
|
|
|
if err := s.Clean(options); err != nil {
|
2017-06-24 20:00:19 +02:00
|
|
|
return 0, err
|
2017-06-10 21:38:09 +02:00
|
|
|
}
|
2017-06-23 21:27:13 +02:00
|
|
|
n, err := s.WritePages(options)
|
2017-06-20 14:59:54 +02:00
|
|
|
return count + n, err
|
|
|
|
}
|