2019-01-02 06:33:26 -05:00
|
|
|
// Copyright 2019 The Hugo Authors. All rights reserved.
|
2016-11-10 14:55:52 -05:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package hugolib
|
|
|
|
|
|
|
|
import (
|
2017-09-27 15:17:43 -04:00
|
|
|
"bytes"
|
2019-01-02 06:33:26 -05:00
|
|
|
"context"
|
2020-03-03 06:25:03 -05:00
|
|
|
"encoding/json"
|
2018-03-15 04:37:30 -04:00
|
|
|
"fmt"
|
2020-03-03 06:25:03 -05:00
|
|
|
"path/filepath"
|
2020-02-25 15:40:02 -05:00
|
|
|
"strings"
|
2023-01-04 12:24:36 -05:00
|
|
|
"time"
|
2020-02-25 15:40:02 -05:00
|
|
|
|
2023-06-16 02:17:42 -04:00
|
|
|
"github.com/bep/logg"
|
2023-07-19 04:28:56 -04:00
|
|
|
"github.com/gohugoio/hugo/hugofs/files"
|
2023-05-17 03:59:57 -04:00
|
|
|
"github.com/gohugoio/hugo/langs"
|
2020-03-03 06:25:03 -05:00
|
|
|
"github.com/gohugoio/hugo/publisher"
|
2023-06-30 02:47:11 -04:00
|
|
|
"github.com/gohugoio/hugo/tpl"
|
2020-03-03 06:25:03 -05:00
|
|
|
|
|
|
|
"github.com/gohugoio/hugo/hugofs"
|
|
|
|
|
2020-02-25 15:40:02 -05:00
|
|
|
"github.com/gohugoio/hugo/common/para"
|
|
|
|
"github.com/gohugoio/hugo/config"
|
|
|
|
"github.com/gohugoio/hugo/resources/postpub"
|
|
|
|
|
|
|
|
"github.com/spf13/afero"
|
|
|
|
|
2019-01-02 06:33:26 -05:00
|
|
|
"github.com/gohugoio/hugo/output"
|
2016-11-10 14:55:52 -05:00
|
|
|
|
2022-05-02 10:07:52 -04:00
|
|
|
"errors"
|
2016-11-10 14:55:52 -05:00
|
|
|
|
|
|
|
"github.com/fsnotify/fsnotify"
|
2017-06-13 12:42:45 -04:00
|
|
|
"github.com/gohugoio/hugo/helpers"
|
2016-11-10 14:55:52 -05:00
|
|
|
)
|
|
|
|
|
2023-05-17 03:59:57 -04:00
|
|
|
func init() {
|
|
|
|
// To avoid circular dependencies, we set this here.
|
|
|
|
langs.DeprecationFunc = helpers.Deprecated
|
|
|
|
}
|
|
|
|
|
2016-11-10 14:55:52 -05:00
|
|
|
// Build builds all sites. If filesystem events are provided,
|
|
|
|
// this is considered to be a potential partial rebuild.
|
|
|
|
func (h *HugoSites) Build(config BuildCfg, events ...fsnotify.Event) error {
|
2023-01-04 12:24:36 -05:00
|
|
|
if h == nil {
|
|
|
|
return errors.New("cannot build nil *HugoSites")
|
|
|
|
}
|
|
|
|
|
|
|
|
if h.Deps == nil {
|
|
|
|
return errors.New("cannot build nil *Deps")
|
|
|
|
}
|
2019-01-02 06:33:26 -05:00
|
|
|
|
2021-10-17 05:54:55 -04:00
|
|
|
if !config.NoBuildLock {
|
|
|
|
unlock, err := h.BaseFs.LockBuild()
|
|
|
|
if err != nil {
|
2022-05-02 10:07:52 -04:00
|
|
|
return fmt.Errorf("failed to acquire a build lock: %w", err)
|
2021-10-17 05:54:55 -04:00
|
|
|
}
|
|
|
|
defer unlock()
|
|
|
|
}
|
|
|
|
|
2023-06-16 02:17:42 -04:00
|
|
|
infol := h.Log.InfoCommand("build")
|
|
|
|
|
2018-10-21 06:20:21 -04:00
|
|
|
errCollector := h.StartErrorCollector()
|
|
|
|
errs := make(chan error)
|
|
|
|
|
|
|
|
go func(from, to chan error) {
|
|
|
|
var errors []error
|
|
|
|
i := 0
|
|
|
|
for e := range from {
|
|
|
|
i++
|
|
|
|
if i > 50 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
errors = append(errors, e)
|
|
|
|
}
|
|
|
|
to <- h.pickOneAndLogTheRest(errors)
|
|
|
|
|
|
|
|
close(to)
|
|
|
|
}(errCollector, errs)
|
2018-07-11 13:23:22 -04:00
|
|
|
|
2017-09-26 14:03:04 -04:00
|
|
|
if h.Metrics != nil {
|
|
|
|
h.Metrics.Reset()
|
|
|
|
}
|
|
|
|
|
2020-01-22 05:57:23 -05:00
|
|
|
h.testCounters = config.testCounters
|
|
|
|
|
2016-11-10 14:55:52 -05:00
|
|
|
// Need a pointer as this may be modified.
|
|
|
|
conf := &config
|
|
|
|
|
|
|
|
if conf.whatChanged == nil {
|
|
|
|
// Assume everything has changed
|
2019-11-27 07:42:36 -05:00
|
|
|
conf.whatChanged = &whatChanged{source: true}
|
2016-11-10 14:55:52 -05:00
|
|
|
}
|
|
|
|
|
2018-10-21 06:20:21 -04:00
|
|
|
var prepareErr error
|
|
|
|
|
2018-10-17 03:28:04 -04:00
|
|
|
if !config.PartialReRender {
|
2018-10-21 06:20:21 -04:00
|
|
|
prepare := func() error {
|
2019-08-03 11:27:40 -04:00
|
|
|
init := func(conf *BuildCfg) error {
|
|
|
|
for _, s := range h.Sites {
|
|
|
|
s.Deps.BuildStartListeners.Notify()
|
2018-10-21 06:20:21 -04:00
|
|
|
}
|
2019-08-03 11:27:40 -04:00
|
|
|
|
|
|
|
if len(events) > 0 {
|
|
|
|
// Rebuild
|
|
|
|
if err := h.initRebuild(conf); err != nil {
|
2022-05-02 10:07:52 -04:00
|
|
|
return fmt.Errorf("initRebuild: %w", err)
|
2019-08-03 11:27:40 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := h.initSites(conf); err != nil {
|
2022-05-02 10:07:52 -04:00
|
|
|
return fmt.Errorf("initSites: %w", err)
|
2019-08-03 11:27:40 -04:00
|
|
|
}
|
2018-10-21 06:20:21 -04:00
|
|
|
}
|
2019-08-03 11:27:40 -04:00
|
|
|
|
|
|
|
return nil
|
2018-10-21 06:20:21 -04:00
|
|
|
}
|
2018-07-11 13:23:22 -04:00
|
|
|
|
2023-06-16 02:17:42 -04:00
|
|
|
if err := h.process(infol, conf, init, events...); err != nil {
|
2022-05-02 10:07:52 -04:00
|
|
|
return fmt.Errorf("process: %w", err)
|
2018-10-17 03:28:04 -04:00
|
|
|
}
|
2018-10-21 06:20:21 -04:00
|
|
|
|
2023-06-16 02:17:42 -04:00
|
|
|
if err := h.assemble(infol, conf); err != nil {
|
2023-01-04 12:24:36 -05:00
|
|
|
return fmt.Errorf("assemble: %w", err)
|
2018-10-17 03:28:04 -04:00
|
|
|
}
|
2019-01-02 06:33:26 -05:00
|
|
|
|
2018-10-21 06:20:21 -04:00
|
|
|
return nil
|
2016-11-10 14:55:52 -05:00
|
|
|
}
|
2018-10-17 03:28:04 -04:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
if prepareErr = prepare(); prepareErr != nil {
|
2018-10-21 06:20:21 -04:00
|
|
|
h.SendError(prepareErr)
|
2016-11-10 14:55:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-21 06:20:21 -04:00
|
|
|
if prepareErr == nil {
|
2023-06-16 02:17:42 -04:00
|
|
|
if err := h.render(infol, conf); err != nil {
|
2023-01-04 12:24:36 -05:00
|
|
|
h.SendError(fmt.Errorf("render: %w", err))
|
2018-10-21 06:20:21 -04:00
|
|
|
}
|
2023-06-27 03:16:42 -04:00
|
|
|
|
2023-06-30 02:47:11 -04:00
|
|
|
if err := h.postRenderOnce(); err != nil {
|
|
|
|
h.SendError(fmt.Errorf("postRenderOnce: %w", err))
|
2023-06-27 03:16:42 -04:00
|
|
|
}
|
|
|
|
|
2023-06-16 02:17:42 -04:00
|
|
|
if err := h.postProcess(infol); err != nil {
|
2023-01-04 12:24:36 -05:00
|
|
|
h.SendError(fmt.Errorf("postProcess: %w", err))
|
2020-03-03 06:25:03 -05:00
|
|
|
}
|
2020-02-25 15:40:02 -05:00
|
|
|
}
|
|
|
|
|
2017-09-27 15:17:43 -04:00
|
|
|
if h.Metrics != nil {
|
|
|
|
var b bytes.Buffer
|
|
|
|
h.Metrics.WriteMetrics(&b)
|
|
|
|
|
2020-10-21 05:17:48 -04:00
|
|
|
h.Log.Printf("\nTemplate Metrics:\n\n")
|
|
|
|
h.Log.Println(b.String())
|
2017-09-27 15:17:43 -04:00
|
|
|
}
|
|
|
|
|
2023-04-13 05:44:22 -04:00
|
|
|
h.StopErrorCollector()
|
2018-10-21 06:20:21 -04:00
|
|
|
|
|
|
|
err := <-errs
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-01-02 06:33:26 -05:00
|
|
|
if err := h.fatalErrorHandler.getErr(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-06-16 02:17:42 -04:00
|
|
|
errorCount := h.Log.LoggCount(logg.LevelError)
|
2018-03-15 04:37:30 -04:00
|
|
|
if errorCount > 0 {
|
|
|
|
return fmt.Errorf("logged %d error(s)", errorCount)
|
|
|
|
}
|
|
|
|
|
2016-11-10 14:55:52 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build lifecycle methods below.
|
|
|
|
// The order listed matches the order of execution.
|
|
|
|
|
2019-01-02 06:33:26 -05:00
|
|
|
func (h *HugoSites) initSites(config *BuildCfg) error {
|
|
|
|
h.reset(config)
|
2016-11-10 14:55:52 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *HugoSites) initRebuild(config *BuildCfg) error {
|
|
|
|
if config.ResetState {
|
2019-01-02 06:33:26 -05:00
|
|
|
return errors.New("rebuild does not support 'ResetState'")
|
2016-11-10 14:55:52 -05:00
|
|
|
}
|
|
|
|
|
2023-05-24 03:26:30 -04:00
|
|
|
if !h.Configs.Base.Internal.Watch {
|
2019-01-02 06:33:26 -05:00
|
|
|
return errors.New("rebuild called when not in watch mode")
|
2017-02-21 07:55:08 -05:00
|
|
|
}
|
|
|
|
|
2016-11-10 14:55:52 -05:00
|
|
|
for _, s := range h.Sites {
|
2019-08-03 11:27:40 -04:00
|
|
|
s.resetBuildState(config.whatChanged.source)
|
2016-11-10 14:55:52 -05:00
|
|
|
}
|
|
|
|
|
2019-01-02 06:33:26 -05:00
|
|
|
h.reset(config)
|
2018-06-05 03:05:20 -04:00
|
|
|
h.resetLogs()
|
2016-11-10 14:55:52 -05:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-06-16 02:17:42 -04:00
|
|
|
func (h *HugoSites) process(l logg.LevelLogger, config *BuildCfg, init func(config *BuildCfg) error, events ...fsnotify.Event) error {
|
|
|
|
defer h.timeTrack(l, time.Now(), "process")
|
2023-01-04 12:24:36 -05:00
|
|
|
|
2016-11-10 14:55:52 -05:00
|
|
|
// We should probably refactor the Site and pull up most of the logic from there to here,
|
|
|
|
// but that seems like a daunting task.
|
|
|
|
// So for now, if there are more than one site (language),
|
|
|
|
// we pre-process the first one, then configure all the sites based on that.
|
2017-02-04 22:20:06 -05:00
|
|
|
|
2016-11-10 14:55:52 -05:00
|
|
|
firstSite := h.Sites[0]
|
|
|
|
|
|
|
|
if len(events) > 0 {
|
|
|
|
// This is a rebuild
|
2019-08-03 11:27:40 -04:00
|
|
|
return firstSite.processPartial(config, init, events)
|
2016-11-10 14:55:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return firstSite.process(*config)
|
|
|
|
}
|
|
|
|
|
2023-06-16 02:17:42 -04:00
|
|
|
func (h *HugoSites) assemble(l logg.LevelLogger, bcfg *BuildCfg) error {
|
|
|
|
defer h.timeTrack(l, time.Now(), "assemble")
|
2016-11-10 14:55:52 -05:00
|
|
|
|
2019-08-09 04:05:22 -04:00
|
|
|
if !bcfg.whatChanged.source {
|
2019-08-03 11:27:40 -04:00
|
|
|
return nil
|
2019-01-02 06:33:26 -05:00
|
|
|
}
|
|
|
|
|
2020-06-16 04:42:41 -04:00
|
|
|
if err := h.getContentMaps().AssemblePages(); err != nil {
|
2019-08-09 04:05:22 -04:00
|
|
|
return err
|
2016-11-10 14:55:52 -05:00
|
|
|
}
|
|
|
|
|
2019-08-03 11:27:40 -04:00
|
|
|
if err := h.createPageCollections(); err != nil {
|
|
|
|
return err
|
2016-11-10 14:55:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-06-16 02:17:42 -04:00
|
|
|
func (h *HugoSites) timeTrack(l logg.LevelLogger, start time.Time, name string) {
|
2023-01-04 12:24:36 -05:00
|
|
|
elapsed := time.Since(start)
|
2023-06-16 02:17:42 -04:00
|
|
|
l.WithField("step", name).WithField("duration", elapsed).Logf("running")
|
2023-01-04 12:24:36 -05:00
|
|
|
}
|
|
|
|
|
2023-06-16 02:17:42 -04:00
|
|
|
func (h *HugoSites) render(l logg.LevelLogger, config *BuildCfg) error {
|
|
|
|
defer h.timeTrack(l, time.Now(), "render")
|
2023-02-11 10:20:24 -05:00
|
|
|
if _, err := h.init.layouts.Do(context.Background()); err != nil {
|
2020-01-15 09:59:56 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
siteRenderContext := &siteRenderContext{cfg: config, multihost: h.Configs.IsMultihost}
|
2019-01-02 06:33:26 -05:00
|
|
|
|
2018-10-17 03:28:04 -04:00
|
|
|
if !config.PartialReRender {
|
2019-01-02 06:33:26 -05:00
|
|
|
h.renderFormats = output.Formats{}
|
2019-09-10 05:26:34 -04:00
|
|
|
h.withSite(func(s *Site) error {
|
2018-10-17 03:28:04 -04:00
|
|
|
s.initRenderFormats()
|
2019-09-10 05:26:34 -04:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
for _, s := range h.Sites {
|
2019-01-02 06:33:26 -05:00
|
|
|
h.renderFormats = append(h.renderFormats, s.renderFormats...)
|
2018-10-17 03:28:04 -04:00
|
|
|
}
|
2018-04-23 23:57:33 -04:00
|
|
|
}
|
|
|
|
|
2019-01-02 06:33:26 -05:00
|
|
|
i := 0
|
2023-01-04 12:24:36 -05:00
|
|
|
|
2018-04-23 23:57:33 -04:00
|
|
|
for _, s := range h.Sites {
|
2022-04-05 03:57:58 -04:00
|
|
|
h.currentSite = s
|
2019-01-02 06:33:26 -05:00
|
|
|
for siteOutIdx, renderFormat := range s.renderFormats {
|
|
|
|
siteRenderContext.outIdx = siteOutIdx
|
|
|
|
siteRenderContext.sitesOutIdx = i
|
|
|
|
i++
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-h.Done():
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
for _, s2 := range h.Sites {
|
|
|
|
// We render site by site, but since the content is lazily rendered
|
|
|
|
// and a site can "borrow" content from other sites, every site
|
|
|
|
// needs this set.
|
|
|
|
s2.rc = &siteRenderingContext{Format: renderFormat}
|
|
|
|
|
2019-04-10 04:11:51 -04:00
|
|
|
if err := s2.preparePagesForRender(s == s2, siteRenderContext.sitesOutIdx); err != nil {
|
2019-04-04 06:37:55 -04:00
|
|
|
return err
|
2018-10-17 03:28:04 -04:00
|
|
|
}
|
2018-05-08 04:10:13 -04:00
|
|
|
}
|
2019-01-02 06:33:26 -05:00
|
|
|
if !config.SkipRender {
|
|
|
|
if config.PartialReRender {
|
|
|
|
if err := s.renderPages(siteRenderContext); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := s.render(siteRenderContext); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-10-17 03:28:04 -04:00
|
|
|
}
|
2017-05-05 03:24:37 -04:00
|
|
|
}
|
2016-11-10 14:55:52 -05:00
|
|
|
}
|
2019-01-02 06:33:26 -05:00
|
|
|
|
2016-11-10 14:55:52 -05:00
|
|
|
}
|
2017-05-05 03:24:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if !config.SkipRender {
|
2020-06-20 09:00:25 -04:00
|
|
|
if err := h.renderCrossSitesSitemap(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := h.renderCrossSitesRobotsTXT(); err != nil {
|
2016-11-10 14:55:52 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-02-25 15:40:02 -05:00
|
|
|
|
2023-06-30 02:47:11 -04:00
|
|
|
func (h *HugoSites) postRenderOnce() error {
|
|
|
|
h.postRenderInit.Do(func() {
|
|
|
|
conf := h.Configs.Base
|
|
|
|
if conf.PrintPathWarnings {
|
|
|
|
// We need to do this before any post processing, as that may write to the same files twice
|
|
|
|
// and create false positives.
|
|
|
|
hugofs.WalkFilesystems(h.Fs.PublishDir, func(fs afero.Fs) bool {
|
|
|
|
if dfs, ok := fs.(hugofs.DuplicatesReporter); ok {
|
|
|
|
dupes := dfs.ReportDuplicates()
|
|
|
|
if dupes != "" {
|
|
|
|
h.Log.Warnln("Duplicate target paths:", dupes)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if conf.PrintUnusedTemplates {
|
|
|
|
unusedTemplates := h.Tmpl().(tpl.UnusedTemplatesProvider).UnusedTemplates()
|
|
|
|
for _, unusedTemplate := range unusedTemplates {
|
|
|
|
h.Log.Warnf("Template %s is unused, source file %s", unusedTemplate.Name(), unusedTemplate.Filename())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-06-16 02:17:42 -04:00
|
|
|
func (h *HugoSites) postProcess(l logg.LevelLogger) error {
|
|
|
|
defer h.timeTrack(l, time.Now(), "postProcess")
|
2023-01-04 12:24:36 -05:00
|
|
|
|
2020-03-03 06:25:03 -05:00
|
|
|
// Make sure to write any build stats to disk first so it's available
|
|
|
|
// to the post processors.
|
|
|
|
if err := h.writeBuildStats(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-05 07:34:14 -04:00
|
|
|
// This will only be set when js.Build have been triggered with
|
|
|
|
// imports that resolves to the project or a module.
|
|
|
|
// Write a jsconfig.json file to the project's /asset directory
|
2023-05-22 13:11:12 -04:00
|
|
|
// to help JS IntelliSense in VS Code etc.
|
2023-01-04 12:24:36 -05:00
|
|
|
if !h.ResourceSpec.BuildConfig().NoJSConfigInAssets && h.BaseFs.Assets.Dirs != nil {
|
2020-11-04 10:13:37 -05:00
|
|
|
fi, err := h.BaseFs.Assets.Fs.Stat("")
|
|
|
|
if err != nil {
|
|
|
|
h.Log.Warnf("Failed to resolve jsconfig.json dir: %s", err)
|
|
|
|
} else {
|
|
|
|
m := fi.(hugofs.FileMetaInfo).Meta()
|
2021-07-13 05:41:02 -04:00
|
|
|
assetsDir := m.SourceRoot
|
2023-01-04 12:24:36 -05:00
|
|
|
if strings.HasPrefix(assetsDir, h.Configs.LoadingInfo.BaseConfig.WorkingDir) {
|
2020-11-04 10:13:37 -05:00
|
|
|
if jsConfig := h.ResourceSpec.JSConfigBuilder.Build(assetsDir); jsConfig != nil {
|
2020-10-05 07:34:14 -04:00
|
|
|
|
2020-11-04 10:13:37 -05:00
|
|
|
b, err := json.MarshalIndent(jsConfig, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
h.Log.Warnf("Failed to create jsconfig.json: %s", err)
|
|
|
|
} else {
|
|
|
|
filename := filepath.Join(assetsDir, "jsconfig.json")
|
2023-01-04 12:24:36 -05:00
|
|
|
if h.Configs.Base.Internal.Running {
|
2020-11-04 10:13:37 -05:00
|
|
|
h.skipRebuildForFilenamesMu.Lock()
|
|
|
|
h.skipRebuildForFilenames[filename] = true
|
|
|
|
h.skipRebuildForFilenamesMu.Unlock()
|
|
|
|
}
|
|
|
|
// Make sure it's written to the OS fs as this is used by
|
|
|
|
// editors.
|
|
|
|
if err := afero.WriteFile(hugofs.Os, filename, b, 0666); err != nil {
|
|
|
|
h.Log.Warnf("Failed to write jsconfig.json: %s", err)
|
|
|
|
}
|
2020-10-05 07:34:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-25 15:40:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-05 07:34:14 -04:00
|
|
|
var toPostProcess []postpub.PostPublishedResource
|
|
|
|
for _, r := range h.ResourceSpec.PostProcessResources {
|
|
|
|
toPostProcess = append(toPostProcess, r)
|
|
|
|
}
|
|
|
|
|
2020-02-25 15:40:02 -05:00
|
|
|
if len(toPostProcess) == 0 {
|
2020-10-05 07:34:14 -04:00
|
|
|
// Nothing more to do.
|
2020-02-25 15:40:02 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
workers := para.New(config.GetNumWorkerMultiplier())
|
|
|
|
g, _ := workers.Start(context.Background())
|
|
|
|
|
|
|
|
handleFile := func(filename string) error {
|
|
|
|
content, err := afero.ReadFile(h.BaseFs.PublishFs, filename)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
k := 0
|
|
|
|
changed := false
|
|
|
|
|
|
|
|
for {
|
|
|
|
l := bytes.Index(content[k:], []byte(postpub.PostProcessPrefix))
|
|
|
|
if l == -1 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
m := bytes.Index(content[k+l:], []byte(postpub.PostProcessSuffix)) + len(postpub.PostProcessSuffix)
|
|
|
|
|
|
|
|
low, high := k+l, k+l+m
|
|
|
|
|
|
|
|
field := content[low:high]
|
|
|
|
|
|
|
|
forward := l + m
|
|
|
|
|
|
|
|
for i, r := range toPostProcess {
|
|
|
|
if r == nil {
|
|
|
|
panic(fmt.Sprintf("resource %d to post process is nil", i+1))
|
|
|
|
}
|
|
|
|
v, ok := r.GetFieldString(string(field))
|
|
|
|
if ok {
|
|
|
|
content = append(content[:low], append([]byte(v), content[high:]...)...)
|
|
|
|
changed = true
|
|
|
|
forward = len(v)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
k += forward
|
|
|
|
}
|
|
|
|
|
|
|
|
if changed {
|
|
|
|
return afero.WriteFile(h.BaseFs.PublishFs, filename, content, 0666)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
filenames := h.Deps.BuildState.GetFilenamesWithPostPrefix()
|
2022-09-14 13:42:55 -04:00
|
|
|
for _, filename := range filenames {
|
2022-09-14 05:58:45 -04:00
|
|
|
filename := filename
|
2020-02-25 15:40:02 -05:00
|
|
|
g.Run(func() error {
|
2022-09-14 05:58:45 -04:00
|
|
|
return handleFile(filename)
|
2020-02-25 15:40:02 -05:00
|
|
|
})
|
2022-09-14 05:58:45 -04:00
|
|
|
}
|
2020-02-25 15:40:02 -05:00
|
|
|
|
|
|
|
// Prepare for a new build.
|
|
|
|
for _, s := range h.Sites {
|
|
|
|
s.ResourceSpec.PostProcessResources = make(map[string]postpub.PostPublishedResource)
|
|
|
|
}
|
|
|
|
|
|
|
|
return g.Wait()
|
|
|
|
}
|
2020-03-03 06:25:03 -05:00
|
|
|
|
|
|
|
type publishStats struct {
|
|
|
|
CSSClasses string `json:"cssClasses"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *HugoSites) writeBuildStats() error {
|
2023-01-04 12:24:36 -05:00
|
|
|
if h.ResourceSpec == nil {
|
|
|
|
panic("h.ResourceSpec is nil")
|
|
|
|
}
|
2023-07-02 05:02:47 -04:00
|
|
|
if !h.ResourceSpec.BuildConfig().BuildStats.Enabled() {
|
2020-03-03 06:25:03 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
htmlElements := &publisher.HTMLElements{}
|
|
|
|
for _, s := range h.Sites {
|
|
|
|
stats := s.publisher.PublishStats()
|
|
|
|
htmlElements.Merge(stats.HTMLElements)
|
|
|
|
}
|
|
|
|
|
|
|
|
htmlElements.Sort()
|
|
|
|
|
|
|
|
stats := publisher.PublishStats{
|
|
|
|
HTMLElements: *htmlElements,
|
|
|
|
}
|
|
|
|
|
|
|
|
js, err := json.MarshalIndent(stats, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-07-19 04:28:56 -04:00
|
|
|
filename := filepath.Join(h.Configs.LoadingInfo.BaseConfig.WorkingDir, files.FilenameHugoStatsJSON)
|
2023-05-22 09:29:27 -04:00
|
|
|
|
|
|
|
if existingContent, err := afero.ReadFile(hugofs.Os, filename); err == nil {
|
|
|
|
// Check if the content has changed.
|
|
|
|
if bytes.Equal(existingContent, js) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2020-03-03 06:25:03 -05:00
|
|
|
|
|
|
|
// Make sure it's always written to the OS fs.
|
|
|
|
if err := afero.WriteFile(hugofs.Os, filename, js, 0666); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-03-21 04:35:15 -04:00
|
|
|
// Write to the destination as well if it's a in-memory fs.
|
|
|
|
if !hugofs.IsOsFs(h.Fs.Source) {
|
|
|
|
if err := afero.WriteFile(h.Fs.WorkingDirWritable, filename, js, 0666); err != nil {
|
2020-03-03 06:25:03 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|