1
0
mirror of https://github.com/danog/gojekyll.git synced 2025-01-23 04:51:15 +01:00

110 lines
2.7 KiB
Go
Raw Normal View History

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