1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-11-27 09:34:41 +01:00
gojekyll/cmd/gojekyll/commands.go

154 lines
3.7 KiB
Go
Raw Normal View History

2017-06-18 22:54:58 +02:00
package main
import (
"fmt"
"os"
"path/filepath"
2017-06-19 15:15:52 +02:00
"runtime/pprof"
2017-06-18 22:54:58 +02:00
"sort"
"strings"
"time"
yaml "gopkg.in/yaml.v2"
"github.com/osteele/gojekyll/helpers"
2017-06-22 17:02:32 +02:00
"github.com/osteele/gojekyll/pages"
2017-06-22 23:37:46 +02:00
"github.com/osteele/gojekyll/server"
"github.com/osteele/gojekyll/sites"
2017-06-18 22:54:58 +02:00
)
// main sets this
var commandStartTime = time.Now()
func buildCommand(site *sites.Site) error {
printPathSetting("Destination:", site.DestDir())
2017-06-18 22:54:58 +02:00
printSetting("Generating...", "")
2017-06-18 22:55:52 +02:00
if buildOptions.DryRun {
buildOptions.Verbose = true
}
2017-06-18 22:54:58 +02:00
count, err := site.Build(buildOptions)
if err != nil {
return err
}
elapsed := time.Since(commandStartTime)
2017-06-19 20:03:04 +02:00
printSetting("", fmt.Sprintf("wrote %d files in %.2fs.", count, elapsed.Seconds()))
2017-06-18 22:54:58 +02:00
return nil
}
func profileCommand(_ *sites.Site) error {
2017-06-19 15:15:52 +02:00
printSetting("Profiling...", "")
var profilePath = "gojekyll.prof"
f, err := os.Create(profilePath)
if err != nil {
return err
}
if err = pprof.StartCPUProfile(f); err != nil {
return err
}
t0 := time.Now()
for i := 0; time.Since(t0) < 10*time.Second; i++ {
2017-07-01 20:55:50 +02:00
site, err := loadSite(*source, configFlags)
2017-06-19 00:54:08 +02:00
if err != nil {
return err
}
2017-06-19 15:15:52 +02:00
_, err = site.Build(buildOptions)
if err != nil {
return err
}
printSetting("", fmt.Sprintf("Run #%d; %.1fs elapsed", i+1, time.Since(t0).Seconds()))
}
pprof.StopCPUProfile()
if err := f.Close(); err != nil {
return err
2017-06-19 00:54:08 +02:00
}
2017-06-19 15:15:52 +02:00
fmt.Println("Wrote", profilePath)
2017-06-19 00:54:08 +02:00
return nil
}
func serveCommand(site *sites.Site) error {
2017-06-22 23:37:46 +02:00
server := server.Server{Site: site}
2017-06-22 01:19:08 +02:00
return server.Run(*open, printSetting)
2017-06-18 22:54:58 +02:00
}
func varsCommand(site *sites.Site) error {
2017-06-18 22:54:58 +02:00
printSetting("Variables:", "")
2017-06-29 17:00:59 +02:00
siteData := site.SiteVariables()
2017-06-18 22:54:58 +02:00
// The YAML representation including collections is impractically large for debugging.
// (Actually it's circular, which the yaml package can't handle.)
// Neuter it. This destroys it as Liquid data, but that's okay in this context.
for _, c := range site.Collections {
siteData[c.Name] = fmt.Sprintf("<elided page data for %d items>", len(siteData[c.Name].([]interface{})))
2017-06-18 22:54:58 +02:00
}
var data map[string]interface{}
switch {
2017-06-21 19:14:30 +02:00
case *siteVariable:
data = siteData
2017-06-21 19:14:30 +02:00
case *dataVariable:
data = siteData["data"].(map[string]interface{})
2017-06-21 19:14:30 +02:00
if *variablePath != "" {
data = data[*variablePath].(map[string]interface{})
}
default:
2017-06-29 17:00:59 +02:00
page, err := pageFromPathOrRoute(site, *variablePath)
if err != nil {
return err
}
2017-06-22 17:12:59 +02:00
data = page.PageVariables()
}
b, err := yaml.Marshal(data)
if err != nil {
return err
}
2017-06-18 22:54:58 +02:00
fmt.Println(string(b))
return nil
}
func routesCommand(site *sites.Site) error {
2017-06-18 22:54:58 +02:00
printSetting("Routes:", "")
urls := []string{}
2017-06-29 01:00:01 +02:00
for u, p := range site.Routes {
2017-06-21 19:14:30 +02:00
if !(*dynamicRoutes && p.Static()) {
2017-06-18 22:54:58 +02:00
urls = append(urls, u)
}
}
sort.Strings(urls)
for _, u := range urls {
2017-06-29 01:00:01 +02:00
filename := site.Routes[u].SiteRelPath()
2017-06-22 16:37:31 +02:00
fmt.Printf(" %s -> %s\n", u, filename)
2017-06-18 22:54:58 +02:00
}
return nil
}
func renderCommand(site *sites.Site) error {
2017-06-29 17:00:59 +02:00
p, err := pageFromPathOrRoute(site, *renderPath)
2017-06-18 22:54:58 +02:00
if err != nil {
return err
}
printPathSetting("Render:", filepath.Join(site.SourceDir(), p.SiteRelPath()))
2017-06-22 16:37:31 +02:00
printSetting("URL:", p.Permalink())
2017-06-18 22:54:58 +02:00
printSetting("Content:", "")
2017-06-29 17:00:59 +02:00
return site.WriteDocument(p, os.Stdout)
2017-06-18 22:54:58 +02:00
}
// If path starts with /, it's a URL path. Else it's a file path relative
// to the site source directory.
2017-06-29 17:00:59 +02:00
func pageFromPathOrRoute(s *sites.Site, path string) (pages.Document, error) {
2017-06-22 16:37:31 +02:00
if path == "" {
path = "/"
2017-06-18 22:54:58 +02:00
}
2017-06-22 16:37:31 +02:00
switch {
case strings.HasPrefix(path, "/"):
page, found := s.URLPage(path)
if !found {
return nil, helpers.NewPathError("render", path, "the site does not include a file with this URL path")
}
return page, nil
2017-06-22 16:37:31 +02:00
default:
page, found := s.RelPathPage(path)
if !found {
return nil, helpers.NewPathError("render", path, "no such file")
2017-06-18 22:54:58 +02:00
}
2017-06-22 16:37:31 +02:00
return page, nil
2017-06-18 22:54:58 +02:00
}
}