2017-07-14 01:02:30 +02:00
|
|
|
package cmd
|
2017-06-18 22:54:58 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2017-07-06 16:43:43 +02:00
|
|
|
"reflect"
|
2017-07-02 15:48:30 +02:00
|
|
|
"runtime/pprof"
|
2017-06-18 22:54:58 +02:00
|
|
|
|
2017-07-01 20:55:50 +02:00
|
|
|
"github.com/osteele/gojekyll/config"
|
2017-07-04 15:09:36 +02:00
|
|
|
"github.com/osteele/gojekyll/site"
|
2017-07-13 02:40:05 +02:00
|
|
|
kingpin "gopkg.in/alecthomas/kingpin.v2"
|
2017-06-18 22:54:58 +02:00
|
|
|
)
|
|
|
|
|
2017-07-13 02:40:05 +02:00
|
|
|
// ParseAndRun parses and executes the command-line arguments.
|
|
|
|
func ParseAndRun(args []string) error {
|
2017-07-06 16:43:43 +02:00
|
|
|
if reflect.DeepEqual(args, []string{"--version"}) {
|
2017-07-14 01:08:23 +02:00
|
|
|
return versionCommand()
|
2017-07-06 16:43:43 +02:00
|
|
|
}
|
2017-07-04 14:29:11 +02:00
|
|
|
cmd := kingpin.MustParse(app.Parse(args))
|
2017-07-02 01:42:48 +02:00
|
|
|
if configFlags.Destination != nil {
|
|
|
|
dest, err := filepath.Abs(*configFlags.Destination)
|
2017-06-21 19:14:30 +02:00
|
|
|
app.FatalIfError(err, "")
|
2017-07-02 01:42:48 +02:00
|
|
|
configFlags.Destination = &dest
|
2017-06-18 22:54:58 +02:00
|
|
|
}
|
2017-07-02 15:38:00 +02:00
|
|
|
if buildOptions.DryRun {
|
|
|
|
buildOptions.Verbose = true
|
|
|
|
}
|
2017-07-12 01:23:42 +02:00
|
|
|
return run(cmd)
|
2017-06-21 19:14:30 +02:00
|
|
|
}
|
2017-07-02 01:42:48 +02:00
|
|
|
|
2017-07-04 14:29:11 +02:00
|
|
|
func run(cmd string) error { // nolint: gocyclo
|
2017-07-14 01:08:23 +02:00
|
|
|
// dispatcher gets to ignore cyclo threshold ^
|
2017-07-09 21:55:12 +02:00
|
|
|
if profile || cmd == benchmark.FullCommand() {
|
|
|
|
defer setupProfiling()()
|
2017-07-04 14:29:11 +02:00
|
|
|
}
|
2017-07-06 16:43:43 +02:00
|
|
|
// These commands run *without* loading the site
|
2017-07-05 17:18:46 +02:00
|
|
|
switch cmd {
|
2017-07-06 16:43:43 +02:00
|
|
|
case benchmark.FullCommand():
|
|
|
|
return benchmarkCommand()
|
2017-07-05 17:18:46 +02:00
|
|
|
case versionCmd.FullCommand():
|
2017-07-14 01:08:23 +02:00
|
|
|
return versionCommand()
|
2017-07-05 17:18:46 +02:00
|
|
|
}
|
|
|
|
|
2017-07-01 20:55:50 +02:00
|
|
|
site, err := loadSite(*source, configFlags)
|
2017-07-14 01:08:23 +02:00
|
|
|
// Print the version at an awkward place, so its
|
|
|
|
// labels will line up. And print it even if
|
|
|
|
// loading the site produced an error.
|
2017-07-13 02:40:05 +02:00
|
|
|
if *versionFlag {
|
|
|
|
logger.label("Version:", Version)
|
|
|
|
}
|
2017-06-21 19:14:30 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2017-06-18 22:54:58 +02:00
|
|
|
}
|
|
|
|
|
2017-07-14 01:08:23 +02:00
|
|
|
// These commands run *after* the site is loaded
|
2017-06-21 19:14:30 +02:00
|
|
|
switch cmd {
|
|
|
|
case build.FullCommand():
|
|
|
|
return buildCommand(site)
|
2017-07-02 01:42:48 +02:00
|
|
|
case clean.FullCommand():
|
|
|
|
return cleanCommand(site)
|
2017-06-21 19:14:30 +02:00
|
|
|
case render.FullCommand():
|
|
|
|
return renderCommand(site)
|
|
|
|
case routes.FullCommand():
|
|
|
|
return routesCommand(site)
|
|
|
|
case serve.FullCommand():
|
|
|
|
return serveCommand(site)
|
|
|
|
case variables.FullCommand():
|
2017-07-14 01:08:23 +02:00
|
|
|
return variablesCommand(site)
|
2017-07-02 01:42:48 +02:00
|
|
|
default:
|
|
|
|
// kingpin should have provided help and exited before here
|
2017-07-14 01:08:23 +02:00
|
|
|
panic("exhaustive switch")
|
2017-06-18 22:54:58 +02:00
|
|
|
}
|
2017-06-21 19:14:30 +02:00
|
|
|
}
|
2017-06-18 22:54:58 +02:00
|
|
|
|
2017-07-01 20:55:50 +02:00
|
|
|
// Load the site, and print the common banner settings.
|
2017-07-04 15:09:36 +02:00
|
|
|
func loadSite(source string, flags config.Flags) (*site.Site, error) {
|
|
|
|
site, err := site.FromDirectory(source, flags)
|
2017-06-21 19:14:30 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-07-03 19:16:25 +02:00
|
|
|
const configurationFileLabel = "Configuration file:"
|
2017-06-21 19:14:30 +02:00
|
|
|
if site.ConfigFile != nil {
|
2017-07-03 19:16:25 +02:00
|
|
|
logger.path(configurationFileLabel, *site.ConfigFile)
|
2017-06-21 19:14:30 +02:00
|
|
|
} else {
|
2017-07-03 19:16:25 +02:00
|
|
|
logger.label(configurationFileLabel, "none")
|
2017-06-21 19:14:30 +02:00
|
|
|
}
|
2017-07-03 19:16:25 +02:00
|
|
|
logger.label("Source:", site.SourceDir())
|
2017-07-09 01:57:41 +02:00
|
|
|
err = site.Read()
|
2017-06-21 19:14:30 +02:00
|
|
|
return site, err
|
2017-06-18 22:54:58 +02:00
|
|
|
}
|
2017-07-04 14:29:11 +02:00
|
|
|
|
2017-07-09 21:55:12 +02:00
|
|
|
func setupProfiling() func() {
|
2017-07-04 14:29:11 +02:00
|
|
|
profilePath := "gojekyll.prof"
|
|
|
|
logger.label("Profiling...", "")
|
|
|
|
f, err := os.Create(profilePath)
|
|
|
|
app.FatalIfError(err, "")
|
|
|
|
err = pprof.StartCPUProfile(f)
|
|
|
|
app.FatalIfError(err, "")
|
2017-07-09 21:55:12 +02:00
|
|
|
return func() {
|
2017-07-04 14:29:11 +02:00
|
|
|
pprof.StopCPUProfile()
|
|
|
|
err = f.Close()
|
|
|
|
app.FatalIfError(err, "")
|
|
|
|
logger.Info("Wrote", profilePath)
|
2017-07-09 21:55:12 +02:00
|
|
|
}
|
2017-07-04 14:29:11 +02:00
|
|
|
}
|