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-02-25 15:40:02 -05:00
|
|
|
"os"
|
2020-03-03 06:25:03 -05:00
|
|
|
"path/filepath"
|
2019-01-02 06:33:26 -05:00
|
|
|
"runtime/trace"
|
2020-02-25 15:40:02 -05:00
|
|
|
"strings"
|
|
|
|
|
2020-03-03 06:25:03 -05:00
|
|
|
"github.com/gohugoio/hugo/publisher"
|
|
|
|
|
|
|
|
"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"
|
|
|
|
|
|
|
|
"github.com/gohugoio/hugo/resources/resource"
|
2019-01-02 06:33:26 -05:00
|
|
|
|
|
|
|
"github.com/gohugoio/hugo/output"
|
2016-11-10 14:55:52 -05:00
|
|
|
|
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
|
|
|
"github.com/pkg/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
|
|
|
)
|
|
|
|
|
|
|
|
// 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 {
|
2019-08-03 11:27:40 -04:00
|
|
|
|
2019-05-18 02:38:58 -04:00
|
|
|
if h.running {
|
|
|
|
// Make sure we don't trigger rebuilds in parallel.
|
|
|
|
h.runningMu.Lock()
|
|
|
|
defer h.runningMu.Unlock()
|
|
|
|
}
|
|
|
|
|
2019-01-02 06:33:26 -05:00
|
|
|
ctx, task := trace.NewTask(context.Background(), "Build")
|
|
|
|
defer task.End()
|
|
|
|
|
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 {
|
|
|
|
return errors.Wrap(err, "initRebuild")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := h.initSites(conf); err != nil {
|
|
|
|
return errors.Wrap(err, "initSites")
|
|
|
|
}
|
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
|
|
|
|
2019-01-02 06:33:26 -05:00
|
|
|
var err error
|
|
|
|
|
|
|
|
f := func() {
|
2019-08-03 11:27:40 -04:00
|
|
|
err = h.process(conf, init, events...)
|
2019-01-02 06:33:26 -05:00
|
|
|
}
|
|
|
|
trace.WithRegion(ctx, "process", f)
|
|
|
|
if err != nil {
|
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
|
|
|
return errors.Wrap(err, "process")
|
2018-10-17 03:28:04 -04:00
|
|
|
}
|
2018-10-21 06:20:21 -04:00
|
|
|
|
2019-01-02 06:33:26 -05:00
|
|
|
f = func() {
|
|
|
|
err = h.assemble(conf)
|
|
|
|
}
|
|
|
|
trace.WithRegion(ctx, "assemble", f)
|
|
|
|
if err != nil {
|
2018-10-17 03:28:04 -04:00
|
|
|
return err
|
|
|
|
}
|
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
|
|
|
|
2019-01-02 06:33:26 -05:00
|
|
|
f := func() {
|
|
|
|
prepareErr = prepare()
|
|
|
|
}
|
|
|
|
trace.WithRegion(ctx, "prepare", f)
|
2018-10-21 06:20:21 -04:00
|
|
|
if prepareErr != nil {
|
|
|
|
h.SendError(prepareErr)
|
2016-11-10 14:55:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-10-21 06:20:21 -04:00
|
|
|
if prepareErr == nil {
|
2019-01-02 06:33:26 -05:00
|
|
|
var err error
|
|
|
|
f := func() {
|
|
|
|
err = h.render(conf)
|
|
|
|
}
|
|
|
|
trace.WithRegion(ctx, "render", f)
|
|
|
|
if err != nil {
|
2018-10-21 06:20:21 -04:00
|
|
|
h.SendError(err)
|
|
|
|
}
|
2016-11-10 14:55:52 -05:00
|
|
|
|
2020-03-03 06:25:03 -05:00
|
|
|
if err = h.postProcess(); err != nil {
|
|
|
|
h.SendError(err)
|
|
|
|
}
|
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)
|
|
|
|
|
|
|
|
h.Log.FEEDBACK.Printf("\nTemplate Metrics:\n\n")
|
|
|
|
h.Log.FEEDBACK.Print(b.String())
|
|
|
|
h.Log.FEEDBACK.Println()
|
|
|
|
}
|
|
|
|
|
2018-10-21 06:20:21 -04:00
|
|
|
select {
|
|
|
|
// Make sure the channel always gets something.
|
|
|
|
case errCollector <- nil:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
close(errCollector)
|
|
|
|
|
|
|
|
err := <-errs
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-01-02 06:33:26 -05:00
|
|
|
if err := h.fatalErrorHandler.getErr(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-10-03 08:58:09 -04:00
|
|
|
errorCount := h.Log.ErrorCounter.Count()
|
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
|
|
|
|
2018-11-15 03:28:02 -05:00
|
|
|
if config.NewConfig != nil {
|
|
|
|
if err := h.createSitesFromConfig(config.NewConfig); err != nil {
|
2016-11-10 14:55:52 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *HugoSites) initRebuild(config *BuildCfg) error {
|
2018-11-15 03:28:02 -05:00
|
|
|
if config.NewConfig != nil {
|
2019-01-02 06:33:26 -05:00
|
|
|
return errors.New("rebuild does not support 'NewConfig'")
|
2016-11-10 14:55:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
:sparkles: Implement Page bundling and image handling
This commit is not the smallest in Hugo's history.
Some hightlights include:
* Page bundles (for complete articles, keeping images and content together etc.).
* Bundled images can be processed in as many versions/sizes as you need with the three methods `Resize`, `Fill` and `Fit`.
* Processed images are cached inside `resources/_gen/images` (default) in your project.
* Symbolic links (both files and dirs) are now allowed anywhere inside /content
* A new table based build summary
* The "Total in nn ms" now reports the total including the handling of the files inside /static. So if it now reports more than you're used to, it is just **more real** and probably faster than before (see below).
A site building benchmark run compared to `v0.31.1` shows that this should be slightly faster and use less memory:
```bash
▶ ./benchSite.sh "TOML,num_langs=.*,num_root_sections=5,num_pages=(500|1000),tags_per_page=5,shortcodes,render"
benchmark old ns/op new ns/op delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 101785785 78067944 -23.30%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 185481057 149159919 -19.58%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 103149918 85679409 -16.94%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 203515478 169208775 -16.86%
benchmark old allocs new allocs delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 532464 391539 -26.47%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 1056549 772702 -26.87%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 555974 406630 -26.86%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 1086545 789922 -27.30%
benchmark old bytes new bytes delta
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 53243246 43598155 -18.12%
BenchmarkSiteBuilding/TOML,num_langs=1,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 105811617 86087116 -18.64%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=500,tags_per_page=5,shortcodes,render-4 54558852 44545097 -18.35%
BenchmarkSiteBuilding/TOML,num_langs=3,num_root_sections=5,num_pages=1000,tags_per_page=5,shortcodes,render-4 106903858 86978413 -18.64%
```
Fixes #3651
Closes #3158
Fixes #1014
Closes #2021
Fixes #1240
Updates #3757
2017-07-24 03:00:23 -04:00
|
|
|
if !h.running {
|
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
|
|
|
helpers.InitLoggers()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-08-03 11:27:40 -04:00
|
|
|
func (h *HugoSites) process(config *BuildCfg, init func(config *BuildCfg) error, events ...fsnotify.Event) error {
|
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)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-08-09 04:05:22 -04:00
|
|
|
func (h *HugoSites) assemble(bcfg *BuildCfg) error {
|
2016-11-10 14:55:52 -05:00
|
|
|
|
|
|
|
if len(h.Sites) > 1 {
|
|
|
|
// The first is initialized during process; initialize the rest
|
|
|
|
for _, site := range h.Sites[1:] {
|
2018-05-04 04:18:45 -04:00
|
|
|
if err := site.initializeSiteInfo(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
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
|
2016-11-13 08:27:10 -05:00
|
|
|
|
2016-11-10 14:55:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *HugoSites) render(config *BuildCfg) error {
|
2020-01-15 09:59:56 -05:00
|
|
|
if _, err := h.init.layouts.Do(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-01-02 06:33:26 -05:00
|
|
|
siteRenderContext := &siteRenderContext{cfg: config, multihost: h.multihost}
|
|
|
|
|
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
|
2018-04-23 23:57:33 -04:00
|
|
|
for _, s := range h.Sites {
|
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
|
|
|
}
|
2018-04-23 23:57:33 -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
|
|
|
}
|
2019-01-02 06:33:26 -05:00
|
|
|
|
2017-05-05 03:24:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if !config.SkipRender {
|
2016-11-10 14:55:52 -05:00
|
|
|
if err := h.renderCrossSitesArtifacts(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-02-25 15:40:02 -05:00
|
|
|
|
|
|
|
func (h *HugoSites) postProcess() error {
|
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-02-25 15:40:02 -05:00
|
|
|
var toPostProcess []resource.OriginProvider
|
|
|
|
for _, s := range h.Sites {
|
|
|
|
for _, v := range s.ResourceSpec.PostProcessResources {
|
|
|
|
toPostProcess = append(toPostProcess, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(toPostProcess) == 0 {
|
|
|
|
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
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
_ = afero.Walk(h.BaseFs.PublishFs, "", func(path string, info os.FileInfo, err error) error {
|
|
|
|
if info == nil || info.IsDir() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.HasSuffix(path, "html") {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
g.Run(func() error {
|
|
|
|
return handleFile(path)
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
if !h.ResourceSpec.BuildConfig.WriteStats {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
filename := filepath.Join(h.WorkingDir, "hugo_stats.json")
|
|
|
|
|
|
|
|
// Make sure it's always written to the OS fs.
|
|
|
|
if err := afero.WriteFile(hugofs.Os, filename, js, 0666); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write to the destination, too, if a mem fs is in play.
|
|
|
|
if h.Fs.Source != hugofs.Os {
|
|
|
|
if err := afero.WriteFile(h.Fs.Destination, filename, js, 0666); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|