1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-11-27 11:34:46 +01:00
gojekyll/build.go

80 lines
1.9 KiB
Go
Raw Normal View History

package gojekyll
import (
"fmt"
"os"
"path/filepath"
2017-06-17 01:17:22 +02:00
2017-06-17 02:11:52 +02:00
"github.com/osteele/gojekyll/helpers"
)
// BuildOptions holds options for Build and Clean
type BuildOptions struct {
DryRun bool
UseHardLinks bool
}
// Clean the destination. Remove files that aren't in keep_files, and resulting empty diretories.
func (s *Site) Clean(options BuildOptions) error {
2017-06-17 02:06:55 +02:00
removeFiles := func(name string, info os.FileInfo, err error) error {
switch {
case err != nil && os.IsNotExist(err):
return nil
case err != nil:
return err
case info.IsDir():
return nil
2017-06-17 02:06:55 +02:00
case s.KeepFile(name):
return nil
case options.DryRun:
2017-06-17 02:06:55 +02:00
fmt.Println("rm", name)
default:
2017-06-17 02:06:55 +02:00
return os.Remove(name)
}
return nil
}
2017-06-16 04:31:36 +02:00
if err := filepath.Walk(s.Destination, removeFiles); err != nil {
2017-06-10 23:51:46 +02:00
return err
}
2017-06-17 02:11:52 +02:00
return helpers.RemoveEmptyDirectories(s.Destination)
}
// Build cleans the destination and create files in it.
// It attends to the global options.dry_run.
func (s *Site) Build(options BuildOptions) (count int, err error) {
if err = s.Clean(options); err != nil {
return
}
for _, page := range s.Paths {
count++
if err = s.WritePage(page, options); err != nil {
return
}
}
return
}
// WritePage writes a page to the destination directory.
func (s *Site) WritePage(page Page, options BuildOptions) error {
2017-06-17 02:06:55 +02:00
from := filepath.Join(s.Source, page.Path())
to := filepath.Join(s.Destination, page.Permalink())
if !page.Static() && filepath.Ext(to) == "" {
to = filepath.Join(to, "/index.html")
}
2017-06-13 23:19:05 +02:00
// nolint: gas
2017-06-17 02:06:55 +02:00
if err := os.MkdirAll(filepath.Dir(to), 0755); err != nil {
return err
}
switch {
case options.DryRun:
2017-06-17 02:06:55 +02:00
fmt.Println("create", to, "from", page.Source())
return nil
case page.Static() && options.UseHardLinks:
2017-06-17 02:06:55 +02:00
return os.Link(from, to)
case page.Static():
2017-06-17 02:11:52 +02:00
return helpers.CopyFileContents(to, from, 0644)
default:
2017-06-17 02:11:52 +02:00
return helpers.VisitCreatedFile(to, page.Write)
}
}