2017-07-19 22:06:43 +02:00
|
|
|
package commands
|
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
|
|
|
|
2022-01-29 20:17:23 +01:00
|
|
|
"github.com/danog/gojekyll/config"
|
|
|
|
"github.com/danog/gojekyll/site"
|
|
|
|
"github.com/danog/gojekyll/version"
|
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-17 14:00:13 +02:00
|
|
|
if options.Destination != nil {
|
|
|
|
dest, err := filepath.Abs(*options.Destination)
|
2017-06-21 19:14:30 +02:00
|
|
|
app.FatalIfError(err, "")
|
2017-07-17 14:00:13 +02:00
|
|
|
options.Destination = &dest
|
2017-06-18 22:54:58 +02:00
|
|
|
}
|
2017-07-17 14:00:13 +02:00
|
|
|
if options.DryRun {
|
2017-07-23 17:25:17 +02:00
|
|
|
verbose := true
|
|
|
|
options.Verbose = &verbose
|
2017-07-02 15:38:00 +02:00
|
|
|
}
|
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-08-11 20:24:09 +02:00
|
|
|
case pluginsApp.FullCommand():
|
|
|
|
return pluginsCommand()
|
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-17 14:00:13 +02:00
|
|
|
site, err := loadSite(*source, options)
|
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 {
|
2017-08-14 21:23:00 +02:00
|
|
|
logger.label("Version:", version.Version)
|
2017-07-13 02:40:05 +02:00
|
|
|
}
|
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-07-25 15:48:49 +02:00
|
|
|
if cf := site.Config().ConfigFile; cf != "" {
|
|
|
|
logger.path(configurationFileLabel, cf)
|
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-23 16:28:04 +02:00
|
|
|
logger.path("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
|
|
|
}
|