Exemple #1
0
// build site function
func buildSite(opt *builder.BuildOption, mustWatch bool) func(ctx *cli.Context) {
	return func(ctx *cli.Context) {
		// ctrl+C capture
		signalChan := make(chan os.Signal)
		signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)

		opt.Theme = ctx.String("theme")

		b := builder.New(opt)
		if b.Error != nil {
			log15.Crit("Builder.Fail", "error", b.Error.Error())
		}

		targetDir := ctx.String("dest")
		if targetDir == "template" || targetDir == "source" {
			log15.Crit("Builder.Fail", "error", "destination directory should not be 'template' or 'source'")
		}

		// detect deploy callback
		way, err := deploy.Detect(ctx)
		if err != nil {
			log15.Crit("Deploy.Fail", "error", err.Error())
		}
		if way != nil {
			targetDir = way.Dir()
			opt.After(func(b *builder.Builder, ctx *builder.Context) error {
				t := time.Now()
				if err := way.Do(b, ctx); err != nil {
					return err
				}
				log15.Info("Deploy.Finish", "duration", time.Since(t))
				return nil
			})
		}

		// make directory
		log15.Info("Dest." + targetDir)
		if com.IsDir(targetDir) {
			log15.Warn("Dest." + targetDir + ".Existed")
		}

		// auto watching
		b.Build(targetDir)
		if err := b.Context().Error; err != nil {
			log15.Crit("Build.Fail", "error", err.Error())
		}

		if ctx.Bool("watch") || mustWatch {
			b.Watch(targetDir)
			<-signalChan
		}

		log15.Info("Build.Close")
	}
}
Exemple #2
0
func serveSite(opt *builder.BuildOption) func(ctx *cli.Context) {
	return func(ctx *cli.Context) {
		s := server.New(ctx.String("dest"))

		opt.After(func(b *builder.Builder, ctx *builder.Context) error {
			s.SetPrefix(ctx.Meta.Base)
			log15.Debug("Server.Prefix." + ctx.Meta.Base)
			return nil
		})

		// run server in goroutine
		go func() {
			addr := ctx.String("addr")
			s.Run(addr)
		}()
		// run buildSite to build
		buildSite(opt, true)(ctx)
	}
}