| 1 |
// Copyright 2024 The Hugo Authors. All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package commands |
| 15 |
|
| 16 |
import ( |
| 17 |
"context" |
| 18 |
"errors" |
| 19 |
"fmt" |
| 20 |
"os" |
| 21 |
"path/filepath" |
| 22 |
"runtime" |
| 23 |
"runtime/pprof" |
| 24 |
"runtime/trace" |
| 25 |
"strings" |
| 26 |
"sync" |
| 27 |
"sync/atomic" |
| 28 |
"time" |
| 29 |
|
| 30 |
"github.com/bep/debounce" |
| 31 |
"github.com/bep/simplecobra" |
| 32 |
"github.com/fsnotify/fsnotify" |
| 33 |
"github.com/gohugoio/hugo/common/herrors" |
| 34 |
"github.com/gohugoio/hugo/common/hmaps" |
| 35 |
"github.com/gohugoio/hugo/common/hstrings" |
| 36 |
"github.com/gohugoio/hugo/common/htime" |
| 37 |
"github.com/gohugoio/hugo/common/hugo" |
| 38 |
"github.com/gohugoio/hugo/common/loggers" |
| 39 |
"github.com/gohugoio/hugo/common/paths" |
| 40 |
"github.com/gohugoio/hugo/common/terminal" |
| 41 |
"github.com/gohugoio/hugo/common/types" |
| 42 |
"github.com/gohugoio/hugo/config" |
| 43 |
"github.com/gohugoio/hugo/helpers" |
| 44 |
"github.com/gohugoio/hugo/hugofs" |
| 45 |
"github.com/gohugoio/hugo/hugolib" |
| 46 |
"github.com/gohugoio/hugo/hugolib/filesystems" |
| 47 |
"github.com/gohugoio/hugo/identity" |
| 48 |
"github.com/gohugoio/hugo/livereload" |
| 49 |
"github.com/gohugoio/hugo/resources/page" |
| 50 |
"github.com/gohugoio/hugo/watcher" |
| 51 |
"github.com/spf13/fsync" |
| 52 |
"golang.org/x/sync/errgroup" |
| 53 |
"golang.org/x/sync/semaphore" |
| 54 |
) |
| 55 |
|
| 56 |
type hugoBuilder struct { |
| 57 |
r *rootCommand |
| 58 |
|
| 59 |
confmu sync.Mutex |
| 60 |
confOld *commonConfig |
| 61 |
conf *commonConfig |
| 62 |
|
| 63 |
// May be nil. |
| 64 |
s *serverCommand |
| 65 |
|
| 66 |
// Currently only set when in "fast render mode". |
| 67 |
changeDetector *fileChangeDetector |
| 68 |
visitedURLs *types.EvictingQueue[string] |
| 69 |
|
| 70 |
fullRebuildSem *semaphore.Weighted |
| 71 |
debounce func(f func()) |
| 72 |
|
| 73 |
onConfigLoaded func(reloaded bool) error |
| 74 |
|
| 75 |
fastRenderMode bool |
| 76 |
showErrorInBrowser bool |
| 77 |
|
| 78 |
errState hugoBuilderErrState |
| 79 |
} |
| 80 |
|
| 81 |
var errConfigNotSet = errors.New("config not set") |
| 82 |
|
| 83 |
func (c *hugoBuilder) withConfE(fn func(conf *commonConfig) error) error { |
| 84 |
c.confmu.Lock() |
| 85 |
defer c.confmu.Unlock() |
| 86 |
if c.conf == nil { |
| 87 |
return errConfigNotSet |
| 88 |
} |
| 89 |
return fn(c.conf) |
| 90 |
} |
| 91 |
|
| 92 |
func (c *hugoBuilder) withConf(fn func(conf *commonConfig)) { |
| 93 |
c.confmu.Lock() |
| 94 |
defer c.confmu.Unlock() |
| 95 |
fn(c.conf) |
| 96 |
} |
| 97 |
|
| 98 |
func (c *hugoBuilder) withConfOrOldConf(fn func(conf *commonConfig)) { |
| 99 |
c.confmu.Lock() |
| 100 |
defer c.confmu.Unlock() |
| 101 |
if c.conf != nil { |
| 102 |
fn(c.conf) |
| 103 |
} else if c.confOld != nil { |
| 104 |
fn(c.confOld) |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
func (c *hugoBuilder) withConfOrOldConfE(fn func(conf *commonConfig) error) error { |
| 109 |
c.confmu.Lock() |
| 110 |
defer c.confmu.Unlock() |
| 111 |
if c.conf != nil { |
| 112 |
return fn(c.conf) |
| 113 |
} else if c.confOld != nil { |
| 114 |
return fn(c.confOld) |
| 115 |
} |
| 116 |
return errConfigNotSet |
| 117 |
} |
| 118 |
|
| 119 |
type hugoBuilderErrState struct { |
| 120 |
mu sync.Mutex |
| 121 |
paused bool |
| 122 |
builderr error |
| 123 |
waserr bool |
| 124 |
} |
| 125 |
|
| 126 |
func (e *hugoBuilderErrState) setPaused(p bool) { |
| 127 |
e.mu.Lock() |
| 128 |
defer e.mu.Unlock() |
| 129 |
e.paused = p |
| 130 |
} |
| 131 |
|
| 132 |
func (e *hugoBuilderErrState) isPaused() bool { |
| 133 |
e.mu.Lock() |
| 134 |
defer e.mu.Unlock() |
| 135 |
return e.paused |
| 136 |
} |
| 137 |
|
| 138 |
func (e *hugoBuilderErrState) setBuildErr(err error) { |
| 139 |
e.mu.Lock() |
| 140 |
defer e.mu.Unlock() |
| 141 |
e.builderr = err |
| 142 |
} |
| 143 |
|
| 144 |
func (e *hugoBuilderErrState) buildErr() error { |
| 145 |
e.mu.Lock() |
| 146 |
defer e.mu.Unlock() |
| 147 |
return e.builderr |
| 148 |
} |
| 149 |
|
| 150 |
func (e *hugoBuilderErrState) setWasErr(w bool) { |
| 151 |
e.mu.Lock() |
| 152 |
defer e.mu.Unlock() |
| 153 |
e.waserr = w |
| 154 |
} |
| 155 |
|
| 156 |
func (e *hugoBuilderErrState) wasErr() bool { |
| 157 |
e.mu.Lock() |
| 158 |
defer e.mu.Unlock() |
| 159 |
return e.waserr |
| 160 |
} |
| 161 |
|
| 162 |
// getDirList provides NewWatcher() with a list of directories to watch for changes. |
| 163 |
func (c *hugoBuilder) getDirList() ([]string, error) { |
| 164 |
h, err := c.hugo() |
| 165 |
if err != nil { |
| 166 |
return nil, err |
| 167 |
} |
| 168 |
|
| 169 |
return hstrings.UniqueStringsSorted(h.PathSpec.BaseFs.WatchFilenames()), nil |
| 170 |
} |
| 171 |
|
| 172 |
func (c *hugoBuilder) initCPUProfile() (func(), error) { |
| 173 |
if c.r.cpuprofile == "" { |
| 174 |
return nil, nil |
| 175 |
} |
| 176 |
|
| 177 |
f, err := os.Create(c.r.cpuprofile) |
| 178 |
if err != nil { |
| 179 |
return nil, fmt.Errorf("failed to create CPU profile: %w", err) |
| 180 |
} |
| 181 |
if err := pprof.StartCPUProfile(f); err != nil { |
| 182 |
f.Close() |
| 183 |
return nil, fmt.Errorf("failed to start CPU profile: %w", err) |
| 184 |
} |
| 185 |
return func() { |
| 186 |
pprof.StopCPUProfile() |
| 187 |
f.Close() |
| 188 |
}, nil |
| 189 |
} |
| 190 |
|
| 191 |
func (c *hugoBuilder) initMemProfile() { |
| 192 |
if c.r.memprofile == "" { |
| 193 |
return |
| 194 |
} |
| 195 |
|
| 196 |
f, err := os.Create(c.r.memprofile) |
| 197 |
if err != nil { |
| 198 |
c.r.logger.Errorf("could not create memory profile: ", err) |
| 199 |
} |
| 200 |
defer f.Close() |
| 201 |
runtime.GC() // get up-to-date statistics |
| 202 |
if err := pprof.WriteHeapProfile(f); err != nil { |
| 203 |
c.r.logger.Errorf("could not write memory profile: ", err) |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
func (c *hugoBuilder) initMemTicker() func() { |
| 208 |
memticker := time.NewTicker(5 * time.Second) |
| 209 |
quit := make(chan struct{}) |
| 210 |
printMem := func() { |
| 211 |
var m runtime.MemStats |
| 212 |
runtime.ReadMemStats(&m) |
| 213 |
fmt.Printf("\n\nAlloc = %v\nTotalAlloc = %v\nSys = %v\nNumGC = %v\n\n", formatByteCount(m.Alloc), formatByteCount(m.TotalAlloc), formatByteCount(m.Sys), m.NumGC) |
| 214 |
} |
| 215 |
|
| 216 |
go func() { |
| 217 |
for { |
| 218 |
select { |
| 219 |
case <-memticker.C: |
| 220 |
printMem() |
| 221 |
case <-quit: |
| 222 |
memticker.Stop() |
| 223 |
printMem() |
| 224 |
return |
| 225 |
} |
| 226 |
} |
| 227 |
}() |
| 228 |
|
| 229 |
return func() { |
| 230 |
close(quit) |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
func (c *hugoBuilder) initMutexProfile() (func(), error) { |
| 235 |
if c.r.mutexprofile == "" { |
| 236 |
return nil, nil |
| 237 |
} |
| 238 |
|
| 239 |
f, err := os.Create(c.r.mutexprofile) |
| 240 |
if err != nil { |
| 241 |
return nil, err |
| 242 |
} |
| 243 |
|
| 244 |
runtime.SetMutexProfileFraction(1) |
| 245 |
|
| 246 |
return func() { |
| 247 |
pprof.Lookup("mutex").WriteTo(f, 0) |
| 248 |
f.Close() |
| 249 |
}, nil |
| 250 |
} |
| 251 |
|
| 252 |
func (c *hugoBuilder) initProfiling() (func(), error) { |
| 253 |
stopCPUProf, err := c.initCPUProfile() |
| 254 |
if err != nil { |
| 255 |
return nil, err |
| 256 |
} |
| 257 |
|
| 258 |
stopMutexProf, err := c.initMutexProfile() |
| 259 |
if err != nil { |
| 260 |
return nil, err |
| 261 |
} |
| 262 |
|
| 263 |
stopTraceProf, err := c.initTraceProfile() |
| 264 |
if err != nil { |
| 265 |
return nil, err |
| 266 |
} |
| 267 |
|
| 268 |
var stopMemTicker func() |
| 269 |
if c.r.printm { |
| 270 |
stopMemTicker = c.initMemTicker() |
| 271 |
} |
| 272 |
|
| 273 |
return func() { |
| 274 |
c.initMemProfile() |
| 275 |
|
| 276 |
if stopCPUProf != nil { |
| 277 |
stopCPUProf() |
| 278 |
} |
| 279 |
if stopMutexProf != nil { |
| 280 |
stopMutexProf() |
| 281 |
} |
| 282 |
|
| 283 |
if stopTraceProf != nil { |
| 284 |
stopTraceProf() |
| 285 |
} |
| 286 |
|
| 287 |
if stopMemTicker != nil { |
| 288 |
stopMemTicker() |
| 289 |
} |
| 290 |
}, nil |
| 291 |
} |
| 292 |
|
| 293 |
func (c *hugoBuilder) initTraceProfile() (func(), error) { |
| 294 |
if c.r.traceprofile == "" { |
| 295 |
return nil, nil |
| 296 |
} |
| 297 |
|
| 298 |
f, err := os.Create(c.r.traceprofile) |
| 299 |
if err != nil { |
| 300 |
return nil, fmt.Errorf("failed to create trace file: %w", err) |
| 301 |
} |
| 302 |
|
| 303 |
if err := trace.Start(f); err != nil { |
| 304 |
return nil, fmt.Errorf("failed to start trace: %w", err) |
| 305 |
} |
| 306 |
|
| 307 |
return func() { |
| 308 |
trace.Stop() |
| 309 |
f.Close() |
| 310 |
}, nil |
| 311 |
} |
| 312 |
|
| 313 |
// newWatcher creates a new watcher to watch filesystem events. |
| 314 |
func (c *hugoBuilder) newWatcher(pollIntervalStr string, dirList ...string) (*watcher.Batcher, error) { |
| 315 |
staticSyncer := &staticSyncer{c: c} |
| 316 |
|
| 317 |
var pollInterval time.Duration |
| 318 |
poll := pollIntervalStr != "" |
| 319 |
if poll { |
| 320 |
pollInterval, err := types.ToDurationE(pollIntervalStr) |
| 321 |
if err != nil { |
| 322 |
return nil, fmt.Errorf("invalid value for flag poll: %s", err) |
| 323 |
} |
| 324 |
c.r.logger.Printf("Use watcher with poll interval %v", pollInterval) |
| 325 |
} |
| 326 |
|
| 327 |
if pollInterval == 0 { |
| 328 |
pollInterval = 500 * time.Millisecond |
| 329 |
} |
| 330 |
|
| 331 |
watcher, err := watcher.New(500*time.Millisecond, pollInterval, poll) |
| 332 |
if err != nil { |
| 333 |
return nil, err |
| 334 |
} |
| 335 |
|
| 336 |
h, err := c.hugo() |
| 337 |
if err != nil { |
| 338 |
return nil, err |
| 339 |
} |
| 340 |
spec := h.Deps.SourceSpec |
| 341 |
|
| 342 |
for _, d := range dirList { |
| 343 |
if d != "" { |
| 344 |
if spec.IgnoreFile(d) { |
| 345 |
continue |
| 346 |
} |
| 347 |
_ = watcher.Add(d) |
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
// Identifies changes to config (config.toml) files. |
| 352 |
configSet := make(map[string]bool) |
| 353 |
var configFiles []string |
| 354 |
c.withConf(func(conf *commonConfig) { |
| 355 |
configFiles = conf.configs.LoadingInfo.ConfigFiles |
| 356 |
}) |
| 357 |
|
| 358 |
c.r.Println("Watching for config changes in", strings.Join(configFiles, ", ")) |
| 359 |
for _, configFile := range configFiles { |
| 360 |
watcher.Add(configFile) |
| 361 |
configSet[configFile] = true |
| 362 |
} |
| 363 |
|
| 364 |
go func() { |
| 365 |
for { |
| 366 |
select { |
| 367 |
case changes := <-c.r.changesFromBuild: |
| 368 |
unlock, err := h.LockBuild() |
| 369 |
if err != nil { |
| 370 |
c.r.logger.Errorf("Failed to acquire a build lock: %s", err) |
| 371 |
return |
| 372 |
} |
| 373 |
c.changeDetector.PrepareNew() |
| 374 |
err = c.rebuildSitesForChanges(changes) |
| 375 |
if err != nil { |
| 376 |
c.r.logger.Errorln("Error while watching:", err) |
| 377 |
} |
| 378 |
if c.s != nil && c.s.doLiveReload { |
| 379 |
doReload := c.changeDetector == nil || len(c.changeDetector.changed()) > 0 |
| 380 |
doReload = doReload || c.showErrorInBrowser && c.errState.buildErr() != nil |
| 381 |
if doReload { |
| 382 |
livereload.ForceRefresh() |
| 383 |
} |
| 384 |
} |
| 385 |
unlock() |
| 386 |
|
| 387 |
case evs := <-watcher.Events: |
| 388 |
unlock, err := h.LockBuild() |
| 389 |
if err != nil { |
| 390 |
c.r.logger.Errorf("Failed to acquire a build lock: %s", err) |
| 391 |
return |
| 392 |
} |
| 393 |
c.handleEvents(watcher, staticSyncer, evs, configSet) |
| 394 |
if c.showErrorInBrowser && c.errState.buildErr() != nil { |
| 395 |
// Need to reload browser to show the error |
| 396 |
livereload.ForceRefresh() |
| 397 |
} |
| 398 |
unlock() |
| 399 |
case err := <-watcher.Errors(): |
| 400 |
if err != nil && !herrors.IsNotExist(err) { |
| 401 |
c.r.logger.Errorln("Error while watching:", err) |
| 402 |
} |
| 403 |
} |
| 404 |
} |
| 405 |
}() |
| 406 |
|
| 407 |
return watcher, nil |
| 408 |
} |
| 409 |
|
| 410 |
func (c *hugoBuilder) build() error { |
| 411 |
stopProfiling, err := c.initProfiling() |
| 412 |
if err != nil { |
| 413 |
return err |
| 414 |
} |
| 415 |
|
| 416 |
defer func() { |
| 417 |
if stopProfiling != nil { |
| 418 |
stopProfiling() |
| 419 |
} |
| 420 |
}() |
| 421 |
|
| 422 |
if err := c.fullBuild(false); err != nil { |
| 423 |
return err |
| 424 |
} |
| 425 |
|
| 426 |
if !c.r.quiet { |
| 427 |
c.r.Println() |
| 428 |
h, err := c.hugo() |
| 429 |
if err != nil { |
| 430 |
return err |
| 431 |
} |
| 432 |
|
| 433 |
h.PrintProcessingStats(os.Stdout) |
| 434 |
c.r.Println() |
| 435 |
} |
| 436 |
|
| 437 |
return nil |
| 438 |
} |
| 439 |
|
| 440 |
func (c *hugoBuilder) buildSites(noBuildLock bool) (err error) { |
| 441 |
defer func() { |
| 442 |
c.errState.setBuildErr(err) |
| 443 |
}() |
| 444 |
|
| 445 |
var h *hugolib.HugoSites |
| 446 |
h, err = c.hugo() |
| 447 |
if err != nil { |
| 448 |
return |
| 449 |
} |
| 450 |
err = h.Build(hugolib.BuildCfg{NoBuildLock: noBuildLock}) |
| 451 |
return |
| 452 |
} |
| 453 |
|
| 454 |
func (c *hugoBuilder) copyStatic() (map[string]uint64, error) { |
| 455 |
m, err := c.doWithPublishDirs(c.copyStaticTo) |
| 456 |
if err == nil || herrors.IsNotExist(err) { |
| 457 |
return m, nil |
| 458 |
} |
| 459 |
return m, err |
| 460 |
} |
| 461 |
|
| 462 |
func (c *hugoBuilder) copyStaticTo(sourceFs *filesystems.SourceFilesystem) (uint64, error) { |
| 463 |
infol := c.r.logger.InfoCommand("static") |
| 464 |
publishDir := helpers.FilePathSeparator |
| 465 |
|
| 466 |
if sourceFs.PublishFolder != "" { |
| 467 |
publishDir = filepath.Join(publishDir, sourceFs.PublishFolder) |
| 468 |
} |
| 469 |
|
| 470 |
fs := &countingStatFs{Fs: sourceFs.Fs} |
| 471 |
|
| 472 |
syncer := fsync.NewSyncer() |
| 473 |
c.withConf(func(conf *commonConfig) { |
| 474 |
syncer.NoTimes = conf.configs.Base.NoTimes |
| 475 |
syncer.NoChmod = conf.configs.Base.NoChmod |
| 476 |
syncer.ChmodFilter = chmodFilter |
| 477 |
|
| 478 |
syncer.DestFs = conf.fs.PublishDirStatic |
| 479 |
// Now that we are using a unionFs for the static directories |
| 480 |
// We can effectively clean the publishDir on initial sync |
| 481 |
syncer.Delete = conf.configs.Base.CleanDestinationDir |
| 482 |
}) |
| 483 |
|
| 484 |
syncer.SrcFs = fs |
| 485 |
|
| 486 |
if syncer.Delete { |
| 487 |
infol.Logf("removing all files from destination that don't exist in static dirs") |
| 488 |
|
| 489 |
syncer.DeleteFilter = func(f fsync.FileInfo) bool { |
| 490 |
name := f.Name() |
| 491 |
|
| 492 |
// Keep .gitignore and .gitattributes anywhere |
| 493 |
if name == ".gitignore" || name == ".gitattributes" { |
| 494 |
return true |
| 495 |
} |
| 496 |
|
| 497 |
// Keep Hugo's original dot-directory behavior |
| 498 |
return f.IsDir() && strings.HasPrefix(name, ".") |
| 499 |
} |
| 500 |
} |
| 501 |
start := time.Now() |
| 502 |
|
| 503 |
// because we are using a baseFs (to get the union right). |
| 504 |
// set sync src to root |
| 505 |
err := syncer.Sync(publishDir, helpers.FilePathSeparator) |
| 506 |
if err != nil { |
| 507 |
return 0, err |
| 508 |
} |
| 509 |
loggers.TimeTrackf(infol, start, nil, "syncing static files to %s", publishDir) |
| 510 |
|
| 511 |
// Sync runs Stat 2 times for every source file. |
| 512 |
numFiles := fs.statCounter / 2 |
| 513 |
|
| 514 |
return numFiles, err |
| 515 |
} |
| 516 |
|
| 517 |
func (c *hugoBuilder) doWithPublishDirs(f func(sourceFs *filesystems.SourceFilesystem) (uint64, error)) (map[string]uint64, error) { |
| 518 |
langCount := make(map[string]uint64) |
| 519 |
|
| 520 |
h, err := c.hugo() |
| 521 |
if err != nil { |
| 522 |
return nil, err |
| 523 |
} |
| 524 |
staticFilesystems := h.BaseFs.SourceFilesystems.Static |
| 525 |
|
| 526 |
if len(staticFilesystems) == 0 { |
| 527 |
c.r.logger.Infoln("No static directories found to sync") |
| 528 |
return langCount, nil |
| 529 |
} |
| 530 |
|
| 531 |
for lang, fs := range staticFilesystems { |
| 532 |
cnt, err := f(fs) |
| 533 |
if err != nil { |
| 534 |
return langCount, err |
| 535 |
} |
| 536 |
if lang == "" { |
| 537 |
// Not multihost |
| 538 |
c.withConf(func(conf *commonConfig) { |
| 539 |
for _, l := range conf.configs.Languages { |
| 540 |
langCount[l.Lang] = cnt |
| 541 |
} |
| 542 |
}) |
| 543 |
} else { |
| 544 |
langCount[lang] = cnt |
| 545 |
} |
| 546 |
} |
| 547 |
|
| 548 |
return langCount, nil |
| 549 |
} |
| 550 |
|
| 551 |
func (c *hugoBuilder) progressIntermediate() { |
| 552 |
terminal.ReportProgress(c.r.StdOut, terminal.ProgressIntermediate, 0) |
| 553 |
} |
| 554 |
|
| 555 |
func (c *hugoBuilder) progressHidden() { |
| 556 |
terminal.ReportProgress(c.r.StdOut, terminal.ProgressHidden, 0) |
| 557 |
} |
| 558 |
|
| 559 |
func (c *hugoBuilder) fullBuild(noBuildLock bool) error { |
| 560 |
var ( |
| 561 |
g errgroup.Group |
| 562 |
langCount map[string]uint64 |
| 563 |
) |
| 564 |
|
| 565 |
c.r.logger.Println("Start building sites … ") |
| 566 |
c.r.logger.Println(hugo.BuildVersionString()) |
| 567 |
c.r.logger.Println() |
| 568 |
if terminal.IsTerminal(os.Stdout) { |
| 569 |
defer func() { |
| 570 |
fmt.Print(showCursor + clearLine) |
| 571 |
}() |
| 572 |
} |
| 573 |
|
| 574 |
copyStaticFunc := func() error { |
| 575 |
cnt, err := c.copyStatic() |
| 576 |
if err != nil { |
| 577 |
return fmt.Errorf("error copying static files: %w", err) |
| 578 |
} |
| 579 |
langCount = cnt |
| 580 |
return nil |
| 581 |
} |
| 582 |
buildSitesFunc := func() error { |
| 583 |
if err := c.buildSites(noBuildLock); err != nil { |
| 584 |
return fmt.Errorf("error building site: %w", err) |
| 585 |
} |
| 586 |
return nil |
| 587 |
} |
| 588 |
// Do not copy static files and build sites in parallel if cleanDestinationDir is enabled. |
| 589 |
// This flag deletes all static resources in /public folder that are missing in /static, |
| 590 |
// and it does so at the end of copyStatic() call. |
| 591 |
var cleanDestinationDir bool |
| 592 |
c.withConf(func(conf *commonConfig) { |
| 593 |
cleanDestinationDir = conf.configs.Base.CleanDestinationDir |
| 594 |
}) |
| 595 |
if cleanDestinationDir { |
| 596 |
if err := copyStaticFunc(); err != nil { |
| 597 |
return err |
| 598 |
} |
| 599 |
if err := buildSitesFunc(); err != nil { |
| 600 |
return err |
| 601 |
} |
| 602 |
} else { |
| 603 |
g.Go(copyStaticFunc) |
| 604 |
g.Go(buildSitesFunc) |
| 605 |
if err := g.Wait(); err != nil { |
| 606 |
return err |
| 607 |
} |
| 608 |
} |
| 609 |
|
| 610 |
h, err := c.hugo() |
| 611 |
if err != nil { |
| 612 |
return err |
| 613 |
} |
| 614 |
for _, s := range h.Sites { |
| 615 |
s.ProcessingStats.Static = langCount[s.Language().Lang] |
| 616 |
} |
| 617 |
|
| 618 |
if c.r.gc { |
| 619 |
count, err := h.GC() |
| 620 |
if err != nil { |
| 621 |
return err |
| 622 |
} |
| 623 |
for _, s := range h.Sites { |
| 624 |
// We have no way of knowing what site the garbage belonged to. |
| 625 |
s.ProcessingStats.Cleaned = uint64(count) |
| 626 |
} |
| 627 |
} |
| 628 |
|
| 629 |
return nil |
| 630 |
} |
| 631 |
|
| 632 |
func (c *hugoBuilder) fullRebuild(changeType string) { |
| 633 |
if changeType == configChangeGoMod { |
| 634 |
// go.mod may be changed during the build itself, and |
| 635 |
// we really want to prevent superfluous builds. |
| 636 |
if !c.fullRebuildSem.TryAcquire(1) { |
| 637 |
return |
| 638 |
} |
| 639 |
c.fullRebuildSem.Release(1) |
| 640 |
} |
| 641 |
|
| 642 |
c.fullRebuildSem.Acquire(context.Background(), 1) |
| 643 |
|
| 644 |
go func() { |
| 645 |
defer c.fullRebuildSem.Release(1) |
| 646 |
|
| 647 |
c.printChangeDetected(changeType) |
| 648 |
|
| 649 |
defer func() { |
| 650 |
// Allow any file system events to arrive basimplecobra. |
| 651 |
// This will block any rebuild on config changes for the |
| 652 |
// duration of the sleep. |
| 653 |
time.Sleep(2 * time.Second) |
| 654 |
}() |
| 655 |
|
| 656 |
defer c.postBuild("Rebuilt", time.Now()) |
| 657 |
|
| 658 |
err := c.reloadConfig() |
| 659 |
if err != nil { |
| 660 |
// Set the processing on pause until the state is recovered. |
| 661 |
c.errState.setPaused(true) |
| 662 |
c.handleBuildErr(err, "Failed to reload config") |
| 663 |
if c.s.doLiveReload { |
| 664 |
livereload.ForceRefresh() |
| 665 |
} |
| 666 |
} else { |
| 667 |
c.errState.setPaused(false) |
| 668 |
} |
| 669 |
|
| 670 |
if !c.errState.isPaused() { |
| 671 |
_, err := c.copyStatic() |
| 672 |
if err != nil { |
| 673 |
c.r.logger.Errorln(err) |
| 674 |
return |
| 675 |
} |
| 676 |
err = c.buildSites(false) |
| 677 |
if err != nil { |
| 678 |
c.r.logger.Errorln(err) |
| 679 |
} else if c.s != nil && c.s.doLiveReload { |
| 680 |
livereload.ForceRefresh() |
| 681 |
} |
| 682 |
} |
| 683 |
}() |
| 684 |
} |
| 685 |
|
| 686 |
func (c *hugoBuilder) handleBuildErr(err error, msg string) { |
| 687 |
c.errState.setBuildErr(err) |
| 688 |
c.r.logger.Errorln(msg + ": " + cleanErrorLog(err.Error())) |
| 689 |
} |
| 690 |
|
| 691 |
func (c *hugoBuilder) handleEvents(watcher *watcher.Batcher, |
| 692 |
staticSyncer *staticSyncer, |
| 693 |
evs []fsnotify.Event, |
| 694 |
configSet map[string]bool, |
| 695 |
) { |
| 696 |
defer func() { |
| 697 |
c.errState.setWasErr(false) |
| 698 |
}() |
| 699 |
|
| 700 |
var isHandled bool |
| 701 |
|
| 702 |
// Filter out ghost events (from deleted, renamed directories). |
| 703 |
// This seems to be a bug in fsnotify, or possibly MacOS. |
| 704 |
var n int |
| 705 |
for _, ev := range evs { |
| 706 |
keep := true |
| 707 |
// Write and rename operations are often followed by CHMOD. |
| 708 |
// There may be valid use cases for rebuilding the site on CHMOD, |
| 709 |
// but that will require more complex logic than this simple conditional. |
| 710 |
// On OS X this seems to be related to Spotlight, see: |
| 711 |
// https://github.com/go-fsnotify/fsnotify/issues/15 |
| 712 |
// A workaround is to put your site(s) on the Spotlight exception list, |
| 713 |
// but that may be a little mysterious for most end users. |
| 714 |
// So, for now, we skip reload on CHMOD. |
| 715 |
// We do have to check for WRITE though. On slower laptops a Chmod |
| 716 |
// could be aggregated with other important events, and we still want |
| 717 |
// to rebuild on those |
| 718 |
if ev.Op == fsnotify.Chmod { |
| 719 |
keep = false |
| 720 |
} else if ev.Has(fsnotify.Create) || ev.Has(fsnotify.Write) { |
| 721 |
if _, err := os.Stat(ev.Name); err != nil { |
| 722 |
keep = false |
| 723 |
} |
| 724 |
} |
| 725 |
if keep { |
| 726 |
evs[n] = ev |
| 727 |
n++ |
| 728 |
} |
| 729 |
} |
| 730 |
evs = evs[:n] |
| 731 |
|
| 732 |
for _, ev := range evs { |
| 733 |
isConfig := configSet[ev.Name] |
| 734 |
configChangeType := configChangeConfig |
| 735 |
if isConfig { |
| 736 |
if strings.Contains(ev.Name, "go.mod") { |
| 737 |
configChangeType = configChangeGoMod |
| 738 |
} |
| 739 |
if strings.Contains(ev.Name, ".work") { |
| 740 |
configChangeType = configChangeGoWork |
| 741 |
} |
| 742 |
} |
| 743 |
if !isConfig { |
| 744 |
// It may be one of the /config folders |
| 745 |
dirname := filepath.Dir(ev.Name) |
| 746 |
if dirname != "." && configSet[dirname] { |
| 747 |
isConfig = true |
| 748 |
} |
| 749 |
} |
| 750 |
|
| 751 |
if isConfig { |
| 752 |
isHandled = true |
| 753 |
|
| 754 |
if ev.Op&fsnotify.Chmod == fsnotify.Chmod { |
| 755 |
continue |
| 756 |
} |
| 757 |
|
| 758 |
if ev.Op&fsnotify.Remove == fsnotify.Remove || ev.Op&fsnotify.Rename == fsnotify.Rename { |
| 759 |
c.withConf(func(conf *commonConfig) { |
| 760 |
for _, configFile := range conf.configs.LoadingInfo.ConfigFiles { |
| 761 |
counter := 0 |
| 762 |
for watcher.Add(configFile) != nil { |
| 763 |
counter++ |
| 764 |
if counter >= 100 { |
| 765 |
break |
| 766 |
} |
| 767 |
time.Sleep(100 * time.Millisecond) |
| 768 |
} |
| 769 |
} |
| 770 |
}) |
| 771 |
} |
| 772 |
|
| 773 |
// Config file(s) changed. Need full rebuild. |
| 774 |
c.fullRebuild(configChangeType) |
| 775 |
|
| 776 |
return |
| 777 |
} |
| 778 |
} |
| 779 |
|
| 780 |
if isHandled { |
| 781 |
return |
| 782 |
} |
| 783 |
|
| 784 |
if c.errState.isPaused() { |
| 785 |
// Wait for the server to get into a consistent state before |
| 786 |
// we continue with processing. |
| 787 |
return |
| 788 |
} |
| 789 |
|
| 790 |
if len(evs) > 50 { |
| 791 |
// This is probably a mass edit of the content dir. |
| 792 |
// Schedule a full rebuild for when it slows down. |
| 793 |
c.debounce(func() { |
| 794 |
c.fullRebuild("") |
| 795 |
}) |
| 796 |
return |
| 797 |
} |
| 798 |
|
| 799 |
c.r.logger.Debugln("Received System Events:", evs) |
| 800 |
|
| 801 |
staticEvents := []fsnotify.Event{} |
| 802 |
dynamicEvents := []fsnotify.Event{} |
| 803 |
|
| 804 |
filterDuplicateEvents := func(evs []fsnotify.Event) []fsnotify.Event { |
| 805 |
seen := make(map[string]bool) |
| 806 |
var n int |
| 807 |
for _, ev := range evs { |
| 808 |
if seen[ev.Name] { |
| 809 |
continue |
| 810 |
} |
| 811 |
seen[ev.Name] = true |
| 812 |
evs[n] = ev |
| 813 |
n++ |
| 814 |
} |
| 815 |
return evs[:n] |
| 816 |
} |
| 817 |
|
| 818 |
h, err := c.hugo() |
| 819 |
if err != nil { |
| 820 |
c.r.logger.Errorln("Error getting the Hugo object:", err) |
| 821 |
return |
| 822 |
} |
| 823 |
n = 0 |
| 824 |
for _, ev := range evs { |
| 825 |
if h.ShouldSkipFileChangeEvent(ev) { |
| 826 |
continue |
| 827 |
} |
| 828 |
evs[n] = ev |
| 829 |
n++ |
| 830 |
} |
| 831 |
evs = evs[:n] |
| 832 |
|
| 833 |
for _, ev := range evs { |
| 834 |
ext := filepath.Ext(ev.Name) |
| 835 |
baseName := filepath.Base(ev.Name) |
| 836 |
istemp := strings.HasSuffix(ext, "~") || |
| 837 |
(ext == ".swp") || // vim |
| 838 |
(ext == ".swx") || // vim |
| 839 |
(ext == ".bck") || // helix |
| 840 |
(ext == ".tmp") || // generic temp file |
| 841 |
(ext == ".DS_Store") || // OSX Thumbnail |
| 842 |
baseName == "4913" || // vim |
| 843 |
strings.HasPrefix(ext, ".goutputstream") || // gnome |
| 844 |
strings.HasSuffix(ext, "jb_old___") || // intelliJ |
| 845 |
strings.HasSuffix(ext, "jb_tmp___") || // intelliJ |
| 846 |
strings.HasSuffix(ext, "jb_bak___") || // intelliJ |
| 847 |
strings.HasPrefix(ext, ".sb-") || // byword |
| 848 |
strings.HasPrefix(baseName, ".#") || // emacs |
| 849 |
strings.HasPrefix(baseName, "#") // emacs |
| 850 |
if istemp { |
| 851 |
continue |
| 852 |
} |
| 853 |
|
| 854 |
if h.Deps.SourceSpec.IgnoreFile(ev.Name) { |
| 855 |
continue |
| 856 |
} |
| 857 |
// Sometimes during rm -rf operations a '"": REMOVE' is triggered. Just ignore these |
| 858 |
if ev.Name == "" { |
| 859 |
continue |
| 860 |
} |
| 861 |
|
| 862 |
walkAdder := func(ctx context.Context, path string, f hugofs.FileMetaInfo) error { |
| 863 |
if f.IsDir() { |
| 864 |
c.r.logger.Println("adding created directory to watchlist", path) |
| 865 |
if err := watcher.Add(path); err != nil { |
| 866 |
return err |
| 867 |
} |
| 868 |
} else if !staticSyncer.isStatic(h, path) { |
| 869 |
// Hugo's rebuilding logic is entirely file based. When you drop a new folder into |
| 870 |
// /content on OSX, the above logic will handle future watching of those files, |
| 871 |
// but the initial CREATE is lost. |
| 872 |
dynamicEvents = append(dynamicEvents, fsnotify.Event{Name: path, Op: fsnotify.Create}) |
| 873 |
} |
| 874 |
return nil |
| 875 |
} |
| 876 |
|
| 877 |
// recursively add new directories to watch list |
| 878 |
if ev.Has(fsnotify.Create) || ev.Has(fsnotify.Rename) { |
| 879 |
c.withConf(func(conf *commonConfig) { |
| 880 |
if s, err := conf.fs.Source.Stat(ev.Name); err == nil && s.Mode().IsDir() { |
| 881 |
_ = helpers.Walk(conf.fs.Source, ev.Name, walkAdder) |
| 882 |
} |
| 883 |
}) |
| 884 |
} |
| 885 |
|
| 886 |
if staticSyncer.isStatic(h, ev.Name) { |
| 887 |
staticEvents = append(staticEvents, ev) |
| 888 |
} else { |
| 889 |
dynamicEvents = append(dynamicEvents, ev) |
| 890 |
} |
| 891 |
} |
| 892 |
|
| 893 |
lrl := c.r.logger.InfoCommand("livereload") |
| 894 |
|
| 895 |
staticEvents = filterDuplicateEvents(staticEvents) |
| 896 |
dynamicEvents = filterDuplicateEvents(dynamicEvents) |
| 897 |
|
| 898 |
if len(staticEvents) > 0 { |
| 899 |
c.printChangeDetected("Static files") |
| 900 |
|
| 901 |
if c.r.forceSyncStatic { |
| 902 |
c.r.logger.Printf("Syncing all static files\n") |
| 903 |
_, err := c.copyStatic() |
| 904 |
if err != nil { |
| 905 |
c.r.logger.Errorln("Error copying static files to publish dir:", err) |
| 906 |
return |
| 907 |
} |
| 908 |
} else { |
| 909 |
if err := staticSyncer.syncsStaticEvents(staticEvents); err != nil { |
| 910 |
c.r.logger.Errorln("Error syncing static files to publish dir:", err) |
| 911 |
return |
| 912 |
} |
| 913 |
} |
| 914 |
|
| 915 |
if c.s != nil && c.s.doLiveReload { |
| 916 |
// Will block forever trying to write to a channel that nobody is reading if livereload isn't initialized |
| 917 |
|
| 918 |
if !c.errState.wasErr() && len(staticEvents) == 1 { |
| 919 |
h, err := c.hugo() |
| 920 |
if err != nil { |
| 921 |
c.r.logger.Errorln("Error getting the Hugo object:", err) |
| 922 |
return |
| 923 |
} |
| 924 |
|
| 925 |
path := h.BaseFs.SourceFilesystems.MakeStaticPathRelative(staticEvents[0].Name) |
| 926 |
path = h.RelURL(paths.ToSlashTrimLeading(path), false) |
| 927 |
|
| 928 |
lrl.Logf("refreshing static file %q", path) |
| 929 |
livereload.RefreshPath(path) |
| 930 |
} else { |
| 931 |
lrl.Logf("got %d static file change events, force refresh", len(staticEvents)) |
| 932 |
livereload.ForceRefresh() |
| 933 |
} |
| 934 |
} |
| 935 |
} |
| 936 |
|
| 937 |
if len(dynamicEvents) > 0 { |
| 938 |
partitionedEvents := partitionDynamicEvents( |
| 939 |
h.BaseFs.SourceFilesystems, |
| 940 |
dynamicEvents) |
| 941 |
|
| 942 |
onePageName := pickOneWriteOrCreatePath(h.Conf.ContentTypes(), partitionedEvents.ContentEvents) |
| 943 |
|
| 944 |
c.printChangeDetected("") |
| 945 |
c.changeDetector.PrepareNew() |
| 946 |
|
| 947 |
func() { |
| 948 |
defer c.postBuild("Total", time.Now()) |
| 949 |
if err := c.rebuildSites(dynamicEvents); err != nil { |
| 950 |
c.handleBuildErr(err, "Rebuild failed") |
| 951 |
} |
| 952 |
}() |
| 953 |
|
| 954 |
if c.s != nil && c.s.doLiveReload { |
| 955 |
if c.errState.wasErr() { |
| 956 |
livereload.ForceRefresh() |
| 957 |
return |
| 958 |
} |
| 959 |
|
| 960 |
changed := c.changeDetector.changed() |
| 961 |
if c.changeDetector != nil { |
| 962 |
if len(changed) >= 10 { |
| 963 |
lrl.Logf("build changed %d files", len(changed)) |
| 964 |
} else { |
| 965 |
lrl.Logf("build changed %d files: %q", len(changed), changed) |
| 966 |
} |
| 967 |
if len(changed) == 0 { |
| 968 |
// Nothing has changed. |
| 969 |
return |
| 970 |
} |
| 971 |
} |
| 972 |
|
| 973 |
// If this change set also contains one or more CSS files, we need to |
| 974 |
// refresh these as well. |
| 975 |
var cssChanges []string |
| 976 |
var otherChanges []string |
| 977 |
|
| 978 |
for _, ev := range changed { |
| 979 |
if strings.HasSuffix(ev, ".css") { |
| 980 |
cssChanges = append(cssChanges, ev) |
| 981 |
} else { |
| 982 |
otherChanges = append(otherChanges, ev) |
| 983 |
} |
| 984 |
} |
| 985 |
|
| 986 |
if len(partitionedEvents.ContentEvents) > 0 { |
| 987 |
navigate := c.s != nil && c.s.navigateToChanged |
| 988 |
// We have fetched the same page above, but it may have |
| 989 |
// changed. |
| 990 |
var p page.Page |
| 991 |
|
| 992 |
if navigate { |
| 993 |
if onePageName != "" { |
| 994 |
p = h.GetContentPage(onePageName) |
| 995 |
} |
| 996 |
} |
| 997 |
|
| 998 |
if p != nil && p.RelPermalink() != "" { |
| 999 |
link, port := p.RelPermalink(), p.Site().ServerPort() |
| 1000 |
lrl.Logf("navigating to %q using port %d", link, port) |
| 1001 |
livereload.NavigateToPathForPort(link, port) |
| 1002 |
} else { |
| 1003 |
lrl.Logf("no page to navigate to, force refresh") |
| 1004 |
livereload.ForceRefresh() |
| 1005 |
} |
| 1006 |
} else if len(otherChanges) > 0 || len(cssChanges) > 0 { |
| 1007 |
if len(otherChanges) == 1 { |
| 1008 |
// Allow single changes to be refreshed without a full page reload. |
| 1009 |
pathToRefresh := h.PathSpec.RelURL(paths.ToSlashTrimLeading(otherChanges[0]), false) |
| 1010 |
lrl.Logf("refreshing %q", pathToRefresh) |
| 1011 |
livereload.RefreshPath(pathToRefresh) |
| 1012 |
} else if len(cssChanges) == 0 || len(otherChanges) > 1 { |
| 1013 |
lrl.Logf("force refresh") |
| 1014 |
livereload.ForceRefresh() |
| 1015 |
} |
| 1016 |
} else { |
| 1017 |
lrl.Logf("force refresh") |
| 1018 |
livereload.ForceRefresh() |
| 1019 |
} |
| 1020 |
|
| 1021 |
if len(cssChanges) > 0 { |
| 1022 |
// Allow some time for the live reload script to get reconnected. |
| 1023 |
if len(otherChanges) > 0 { |
| 1024 |
time.Sleep(200 * time.Millisecond) |
| 1025 |
} |
| 1026 |
for _, ev := range cssChanges { |
| 1027 |
pathToRefresh := h.PathSpec.RelURL(paths.ToSlashTrimLeading(ev), false) |
| 1028 |
lrl.Logf("refreshing CSS %q", pathToRefresh) |
| 1029 |
livereload.RefreshPath(pathToRefresh) |
| 1030 |
} |
| 1031 |
} |
| 1032 |
} |
| 1033 |
} |
| 1034 |
} |
| 1035 |
|
| 1036 |
func (c *hugoBuilder) postBuild(what string, start time.Time) { |
| 1037 |
if h, err := c.hugo(); err == nil && h.Conf.Running() { |
| 1038 |
h.LogServerAddresses() |
| 1039 |
} |
| 1040 |
c.r.timeTrack(start, what) |
| 1041 |
} |
| 1042 |
|
| 1043 |
func (c *hugoBuilder) hugo() (*hugolib.HugoSites, error) { |
| 1044 |
var h *hugolib.HugoSites |
| 1045 |
if err := c.withConfE(func(conf *commonConfig) error { |
| 1046 |
var err error |
| 1047 |
h, err = c.r.HugFromConfig(conf) |
| 1048 |
return err |
| 1049 |
}); err != nil { |
| 1050 |
return nil, err |
| 1051 |
} |
| 1052 |
|
| 1053 |
if c.s != nil { |
| 1054 |
// A running server, register the media types. |
| 1055 |
for _, s := range h.Sites { |
| 1056 |
s.RegisterMediaTypes() |
| 1057 |
} |
| 1058 |
} |
| 1059 |
return h, nil |
| 1060 |
} |
| 1061 |
|
| 1062 |
func (c *hugoBuilder) hugoTry() *hugolib.HugoSites { |
| 1063 |
var h *hugolib.HugoSites |
| 1064 |
c.withConf(func(conf *commonConfig) { |
| 1065 |
h, _ = c.r.HugFromConfig(conf) |
| 1066 |
}) |
| 1067 |
return h |
| 1068 |
} |
| 1069 |
|
| 1070 |
func (c *hugoBuilder) loadConfig(cd *simplecobra.Commandeer, running bool) error { |
| 1071 |
if terminal.PrintANSIColors(os.Stdout) { |
| 1072 |
defer c.progressHidden() |
| 1073 |
// If the configuration takes a while to load, we want to show some progress. |
| 1074 |
// This is typically loading of external modules. |
| 1075 |
d := debounce.New(500 * time.Millisecond) |
| 1076 |
d(func() { |
| 1077 |
c.progressIntermediate() |
| 1078 |
}) |
| 1079 |
defer d(func() {}) |
| 1080 |
} |
| 1081 |
|
| 1082 |
cfg := config.New() |
| 1083 |
cfg.Set("renderToMemory", c.r.renderToMemory) |
| 1084 |
watch := c.r.buildWatch || (c.s != nil && c.s.serverWatch) |
| 1085 |
// We need to set the environment as early as possible because we need it to load the correct config. |
| 1086 |
c.r.resolveEnvironment(c.s != nil) |
| 1087 |
cfg.Set("environment", c.r.environment) |
| 1088 |
|
| 1089 |
cfg.Set("internal", hmaps.Params{ |
| 1090 |
"running": running, |
| 1091 |
"watch": watch, |
| 1092 |
"verbose": c.r.isVerbose(), |
| 1093 |
"fastRenderMode": c.fastRenderMode, |
| 1094 |
}) |
| 1095 |
|
| 1096 |
conf, err := c.r.ConfigFromProvider(configKey{counter: c.r.configVersionID.Load()}, flagsToCfg(cd, cfg)) |
| 1097 |
if err != nil { |
| 1098 |
return err |
| 1099 |
} |
| 1100 |
|
| 1101 |
if len(conf.configs.LoadingInfo.ConfigFiles) == 0 { |
| 1102 |
//lint:ignore ST1005 end user message. |
| 1103 |
return errors.New("Unable to locate config file or config directory. Perhaps you need to create a new project.\nRun `hugo help new` for details.") |
| 1104 |
} |
| 1105 |
|
| 1106 |
c.conf = conf |
| 1107 |
c.confOld = conf |
| 1108 |
if c.onConfigLoaded != nil { |
| 1109 |
if err := c.onConfigLoaded(false); err != nil { |
| 1110 |
return err |
| 1111 |
} |
| 1112 |
} |
| 1113 |
|
| 1114 |
return nil |
| 1115 |
} |
| 1116 |
|
| 1117 |
var rebuildCounter atomic.Uint64 |
| 1118 |
|
| 1119 |
func (c *hugoBuilder) printChangeDetected(typ string) { |
| 1120 |
msg := "\nChange" |
| 1121 |
if typ != "" { |
| 1122 |
msg += " of " + typ |
| 1123 |
} |
| 1124 |
msg += fmt.Sprintf(" detected, rebuilding site (#%d).", rebuildCounter.Add(1)) |
| 1125 |
|
| 1126 |
c.r.logger.Println(msg) |
| 1127 |
const layout = "2006-01-02 15:04:05.000 -0700" |
| 1128 |
c.r.logger.Println(htime.Now().Format(layout)) |
| 1129 |
} |
| 1130 |
|
| 1131 |
func (c *hugoBuilder) rebuildSites(events []fsnotify.Event) (err error) { |
| 1132 |
defer func() { |
| 1133 |
c.errState.setBuildErr(err) |
| 1134 |
}() |
| 1135 |
if err := c.errState.buildErr(); err != nil { |
| 1136 |
ferrs := herrors.UnwrapFileErrorsWithErrorContext(err) |
| 1137 |
for _, err := range ferrs { |
| 1138 |
events = append(events, fsnotify.Event{Name: err.Position().Filename, Op: fsnotify.Write}) |
| 1139 |
} |
| 1140 |
} |
| 1141 |
var h *hugolib.HugoSites |
| 1142 |
h, err = c.hugo() |
| 1143 |
if err != nil { |
| 1144 |
return |
| 1145 |
} |
| 1146 |
err = h.Build(hugolib.BuildCfg{NoBuildLock: true, RecentlyTouched: c.visitedURLs, ErrRecovery: c.errState.wasErr()}, events...) |
| 1147 |
return |
| 1148 |
} |
| 1149 |
|
| 1150 |
func (c *hugoBuilder) rebuildSitesForChanges(ids []identity.Identity) (err error) { |
| 1151 |
defer func() { |
| 1152 |
c.errState.setBuildErr(err) |
| 1153 |
}() |
| 1154 |
|
| 1155 |
var h *hugolib.HugoSites |
| 1156 |
h, err = c.hugo() |
| 1157 |
if err != nil { |
| 1158 |
return |
| 1159 |
} |
| 1160 |
whatChanged := &hugolib.WhatChanged{} |
| 1161 |
whatChanged.Add(ids...) |
| 1162 |
err = h.Build(hugolib.BuildCfg{NoBuildLock: true, WhatChanged: whatChanged, RecentlyTouched: c.visitedURLs, ErrRecovery: c.errState.wasErr()}) |
| 1163 |
|
| 1164 |
return |
| 1165 |
} |
| 1166 |
|
| 1167 |
func (c *hugoBuilder) reloadConfig() error { |
| 1168 |
c.r.resetLogs() |
| 1169 |
c.r.configVersionID.Add(1) |
| 1170 |
|
| 1171 |
if err := c.withConfOrOldConfE(func(conf *commonConfig) error { |
| 1172 |
oldConf := conf |
| 1173 |
c.conf = nil |
| 1174 |
newConf, err := c.r.ConfigFromConfig(configKey{counter: c.r.configVersionID.Load()}, conf) |
| 1175 |
if err != nil { |
| 1176 |
return err |
| 1177 |
} |
| 1178 |
sameLen := len(oldConf.configs.Languages) == len(newConf.configs.Languages) |
| 1179 |
if !sameLen { |
| 1180 |
if oldConf.configs.IsMultihost || newConf.configs.IsMultihost { |
| 1181 |
return errors.New("multihost change detected, please restart server") |
| 1182 |
} |
| 1183 |
} |
| 1184 |
c.conf = newConf |
| 1185 |
return nil |
| 1186 |
}); err != nil { |
| 1187 |
return err |
| 1188 |
} |
| 1189 |
|
| 1190 |
if c.onConfigLoaded != nil { |
| 1191 |
if err := c.onConfigLoaded(true); err != nil { |
| 1192 |
return err |
| 1193 |
} |
| 1194 |
} |
| 1195 |
|
| 1196 |
return nil |
| 1197 |
} |