forked from gohugoio/hugo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collect.go
734 lines (596 loc) · 16.8 KB
/
collect.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
// Copyright 2019 The Hugo Authors. All rights reserved.
//
// 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 modules
import (
"bufio"
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"time"
"github.com/bep/debounce"
"github.com/gohugoio/hugo/common/herrors"
"github.com/gohugoio/hugo/common/loggers"
"github.com/spf13/cast"
"github.com/gohugoio/hugo/common/maps"
"github.com/gohugoio/hugo/common/hugo"
"github.com/gohugoio/hugo/parser/metadecoders"
"github.com/gohugoio/hugo/hugofs/files"
"github.com/rogpeppe/go-internal/module"
"errors"
"github.com/gohugoio/hugo/config"
"github.com/spf13/afero"
)
var ErrNotExist = errors.New("module does not exist")
const vendorModulesFilename = "modules.txt"
// IsNotExist returns whether an error means that a module could not be found.
func IsNotExist(err error) bool {
return errors.Is(err, os.ErrNotExist)
}
// CreateProjectModule creates modules from the given config.
// This is used in tests only.
func CreateProjectModule(cfg config.Provider) (Module, error) {
workingDir := cfg.GetString("workingDir")
var modConfig Config
mod := createProjectModule(nil, workingDir, modConfig)
if err := ApplyProjectConfigDefaults(cfg, mod); err != nil {
return nil, err
}
return mod, nil
}
func (h *Client) Collect() (ModulesConfig, error) {
mc, coll := h.collect(true)
if coll.err != nil {
return mc, coll.err
}
if err := (&mc).setActiveMods(h.logger); err != nil {
return mc, err
}
if h.ccfg.HookBeforeFinalize != nil {
if err := h.ccfg.HookBeforeFinalize(&mc); err != nil {
return mc, err
}
}
if err := (&mc).finalize(h.logger); err != nil {
return mc, err
}
return mc, nil
}
func (h *Client) collect(tidy bool) (ModulesConfig, *collector) {
c := &collector{
Client: h,
}
c.collect()
if c.err != nil {
return ModulesConfig{}, c
}
// https://github.com/gohugoio/hugo/issues/6115
/*if !c.skipTidy && tidy {
if err := h.tidy(c.modules, true); err != nil {
c.err = err
return ModulesConfig{}, c
}
}*/
var workspaceFilename string
if h.ccfg.ModuleConfig.Workspace != WorkspaceDisabled {
workspaceFilename = h.ccfg.ModuleConfig.Workspace
}
return ModulesConfig{
AllModules: c.modules,
GoModulesFilename: c.GoModulesFilename,
GoWorkspaceFilename: workspaceFilename,
}, c
}
type ModulesConfig struct {
// All modules, including any disabled.
AllModules Modules
// All active modules.
ActiveModules Modules
// Set if this is a Go modules enabled project.
GoModulesFilename string
// Set if a Go workspace file is configured.
GoWorkspaceFilename string
}
func (m *ModulesConfig) setActiveMods(logger loggers.Logger) error {
var activeMods Modules
for _, mod := range m.AllModules {
if !mod.Config().HugoVersion.IsValid() {
logger.Warnf(`Module %q is not compatible with this Hugo version; run "hugo mod graph" for more information.`, mod.Path())
}
if !mod.Disabled() {
activeMods = append(activeMods, mod)
}
}
m.ActiveModules = activeMods
return nil
}
func (m *ModulesConfig) finalize(logger loggers.Logger) error {
for _, mod := range m.AllModules {
m := mod.(*moduleAdapter)
m.mounts = filterUnwantedMounts(m.mounts)
}
return nil
}
func filterUnwantedMounts(mounts []Mount) []Mount {
// Remove duplicates
seen := make(map[string]bool)
tmp := mounts[:0]
for _, m := range mounts {
if !seen[m.key()] {
tmp = append(tmp, m)
}
seen[m.key()] = true
}
return tmp
}
type collected struct {
// Pick the first and prevent circular loops.
seen map[string]bool
// Maps module path to a _vendor dir. These values are fetched from
// _vendor/modules.txt, and the first (top-most) will win.
vendored map[string]vendoredModule
// Set if a Go modules enabled project.
gomods goModules
// Ordered list of collected modules, including Go Modules and theme
// components stored below /themes.
modules Modules
}
// Collects and creates a module tree.
type collector struct {
*Client
// Store away any non-fatal error and return at the end.
err error
// Set to disable any Tidy operation in the end.
skipTidy bool
*collected
}
func (c *collector) initModules() error {
c.collected = &collected{
seen: make(map[string]bool),
vendored: make(map[string]vendoredModule),
gomods: goModules{},
}
// If both these are true, we don't even need Go installed to build.
if c.ccfg.IgnoreVendor == nil && c.isVendored(c.ccfg.WorkingDir) {
return nil
}
// We may fail later if we don't find the mods.
return c.loadModules()
}
func (c *collector) isSeen(path string) bool {
key := pathKey(path)
if c.seen[key] {
return true
}
c.seen[key] = true
return false
}
func (c *collector) getVendoredDir(path string) (vendoredModule, bool) {
v, found := c.vendored[path]
return v, found
}
func (c *collector) add(owner *moduleAdapter, moduleImport Import, disabled bool) (*moduleAdapter, error) {
var (
mod *goModule
moduleDir string
version string
vendored bool
)
modulePath := moduleImport.Path
var realOwner Module = owner
if !c.ccfg.shouldIgnoreVendor(modulePath) {
if err := c.collectModulesTXT(owner); err != nil {
return nil, err
}
// Try _vendor first.
var vm vendoredModule
vm, vendored = c.getVendoredDir(modulePath)
if vendored {
moduleDir = vm.Dir
realOwner = vm.Owner
version = vm.Version
if owner.projectMod {
// We want to keep the go.mod intact with the versions and all.
c.skipTidy = true
}
}
}
if moduleDir == "" {
var versionQuery string
mod = c.gomods.GetByPath(modulePath)
if mod != nil {
moduleDir = mod.Dir
versionQuery = mod.Version
}
if moduleDir == "" {
if c.GoModulesFilename != "" && isProbablyModule(modulePath) {
// Try to "go get" it and reload the module configuration.
if versionQuery == "" {
// See https://golang.org/ref/mod#version-queries
// This will select the latest release-version (not beta etc.).
versionQuery = "upgrade"
}
if err := c.Get(fmt.Sprintf("%s@%s", modulePath, versionQuery)); err != nil {
return nil, err
}
if err := c.loadModules(); err != nil {
return nil, err
}
mod = c.gomods.GetByPath(modulePath)
if mod != nil {
moduleDir = mod.Dir
}
}
// Fall back to project/themes/<mymodule>
if moduleDir == "" {
var err error
moduleDir, err = c.createThemeDirname(modulePath, owner.projectMod || moduleImport.pathProjectReplaced)
if err != nil {
c.err = err
return nil, nil
}
if found, _ := afero.Exists(c.fs, moduleDir); !found {
c.err = c.wrapModuleNotFound(fmt.Errorf(`module %q not found; either add it as a Hugo Module or store it in %q.`, modulePath, c.ccfg.ThemesDir))
return nil, nil
}
}
}
}
if found, _ := afero.Exists(c.fs, moduleDir); !found {
c.err = c.wrapModuleNotFound(fmt.Errorf("%q not found", moduleDir))
return nil, nil
}
if !strings.HasSuffix(moduleDir, fileSeparator) {
moduleDir += fileSeparator
}
ma := &moduleAdapter{
dir: moduleDir,
vendor: vendored,
disabled: disabled,
gomod: mod,
version: version,
// This may be the owner of the _vendor dir
owner: realOwner,
}
if mod == nil {
ma.path = modulePath
}
if !moduleImport.IgnoreConfig {
if err := c.applyThemeConfig(ma); err != nil {
return nil, err
}
}
if err := c.applyMounts(moduleImport, ma); err != nil {
return nil, err
}
c.modules = append(c.modules, ma)
return ma, nil
}
func (c *collector) addAndRecurse(owner *moduleAdapter, disabled bool) error {
moduleConfig := owner.Config()
if owner.projectMod {
if err := c.applyMounts(Import{}, owner); err != nil {
return err
}
}
for _, moduleImport := range moduleConfig.Imports {
disabled := disabled || moduleImport.Disable
if !c.isSeen(moduleImport.Path) {
tc, err := c.add(owner, moduleImport, disabled)
if err != nil {
return err
}
if tc == nil || moduleImport.IgnoreImports {
continue
}
if err := c.addAndRecurse(tc, disabled); err != nil {
return err
}
}
}
return nil
}
func (c *collector) applyMounts(moduleImport Import, mod *moduleAdapter) error {
if moduleImport.NoMounts {
mod.mounts = nil
return nil
}
mounts := moduleImport.Mounts
modConfig := mod.Config()
if len(mounts) == 0 {
// Mounts not defined by the import.
mounts = modConfig.Mounts
}
if !mod.projectMod && len(mounts) == 0 {
// Create default mount points for every component folder that
// exists in the module.
for _, componentFolder := range files.ComponentFolders {
sourceDir := filepath.Join(mod.Dir(), componentFolder)
_, err := c.fs.Stat(sourceDir)
if err == nil {
mounts = append(mounts, Mount{
Source: componentFolder,
Target: componentFolder,
})
}
}
}
var err error
mounts, err = c.normalizeMounts(mod, mounts)
if err != nil {
return err
}
mounts, err = c.mountCommonJSConfig(mod, mounts)
if err != nil {
return err
}
mod.mounts = mounts
return nil
}
func (c *collector) applyThemeConfig(tc *moduleAdapter) error {
var (
configFilename string
themeCfg map[string]any
hasConfigFile bool
err error
)
LOOP:
for _, configBaseName := range config.DefaultConfigNames {
for _, configFormats := range config.ValidConfigFileExtensions {
configFilename = filepath.Join(tc.Dir(), configBaseName+"."+configFormats)
hasConfigFile, _ = afero.Exists(c.fs, configFilename)
if hasConfigFile {
break LOOP
}
}
}
// The old theme information file.
themeTOML := filepath.Join(tc.Dir(), "theme.toml")
hasThemeTOML, _ := afero.Exists(c.fs, themeTOML)
if hasThemeTOML {
data, err := afero.ReadFile(c.fs, themeTOML)
if err != nil {
return err
}
themeCfg, err = metadecoders.Default.UnmarshalToMap(data, metadecoders.TOML)
if err != nil {
c.logger.Warnf("Failed to read module config for %q in %q: %s", tc.Path(), themeTOML, err)
} else {
maps.PrepareParams(themeCfg)
}
}
if hasConfigFile {
if configFilename != "" {
var err error
tc.cfg, err = config.FromFile(c.fs, configFilename)
if err != nil {
return err
}
}
tc.configFilenames = append(tc.configFilenames, configFilename)
}
// Also check for a config dir, which we overlay on top of the file configuration.
configDir := filepath.Join(tc.Dir(), "config")
dcfg, dirnames, err := config.LoadConfigFromDir(c.fs, configDir, c.ccfg.Environment)
if err != nil {
return err
}
if len(dirnames) > 0 {
tc.configFilenames = append(tc.configFilenames, dirnames...)
if hasConfigFile {
// Set will overwrite existing keys.
tc.cfg.Set("", dcfg.Get(""))
} else {
tc.cfg = dcfg
}
}
config, err := decodeConfig(tc.cfg, c.moduleConfig.replacementsMap)
if err != nil {
return err
}
const oldVersionKey = "min_version"
if hasThemeTOML {
// Merge old with new
if minVersion, found := themeCfg[oldVersionKey]; found {
if config.HugoVersion.Min == "" {
config.HugoVersion.Min = hugo.VersionString(cast.ToString(minVersion))
}
}
if config.Params == nil {
config.Params = make(map[string]any)
}
for k, v := range themeCfg {
if k == oldVersionKey {
continue
}
config.Params[k] = v
}
}
tc.config = config
return nil
}
func (c *collector) collect() {
defer c.logger.PrintTimerIfDelayed(time.Now(), "hugo: collected modules")
d := debounce.New(2 * time.Second)
d(func() {
c.logger.Println("hugo: downloading modules …")
})
defer d(func() {})
if err := c.initModules(); err != nil {
c.err = err
return
}
projectMod := createProjectModule(c.gomods.GetMain(), c.ccfg.WorkingDir, c.moduleConfig)
if err := c.addAndRecurse(projectMod, false); err != nil {
c.err = err
return
}
// Add the project mod on top.
c.modules = append(Modules{projectMod}, c.modules...)
}
func (c *collector) isVendored(dir string) bool {
_, err := c.fs.Stat(filepath.Join(dir, vendord, vendorModulesFilename))
return err == nil
}
func (c *collector) collectModulesTXT(owner Module) error {
vendorDir := filepath.Join(owner.Dir(), vendord)
filename := filepath.Join(vendorDir, vendorModulesFilename)
f, err := c.fs.Open(filename)
if err != nil {
if herrors.IsNotExist(err) {
return nil
}
return err
}
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
// # github.com/alecthomas/chroma v0.6.3
line := scanner.Text()
line = strings.Trim(line, "# ")
line = strings.TrimSpace(line)
parts := strings.Fields(line)
if len(parts) != 2 {
return fmt.Errorf("invalid modules list: %q", filename)
}
path := parts[0]
shouldAdd := c.Client.moduleConfig.VendorClosest
if !shouldAdd {
if _, found := c.vendored[path]; !found {
shouldAdd = true
}
}
if shouldAdd {
c.vendored[path] = vendoredModule{
Owner: owner,
Dir: filepath.Join(vendorDir, path),
Version: parts[1],
}
}
}
return nil
}
func (c *collector) loadModules() error {
modules, err := c.listGoMods()
if err != nil {
return err
}
c.gomods = modules
return nil
}
// Matches postcss.config.js etc.
var commonJSConfigs = regexp.MustCompile(`(babel|postcss|tailwind)\.config\.js`)
func (c *collector) mountCommonJSConfig(owner *moduleAdapter, mounts []Mount) ([]Mount, error) {
for _, m := range mounts {
if strings.HasPrefix(m.Target, files.JsConfigFolderMountPrefix) {
// This follows the convention of the other component types (assets, content, etc.),
// if one or more is specified by the user, we skip the defaults.
// These mounts were added to Hugo in 0.75.
return mounts, nil
}
}
// Mount the common JS config files.
fis, err := afero.ReadDir(c.fs, owner.Dir())
if err != nil {
return mounts, err
}
for _, fi := range fis {
n := fi.Name()
should := n == files.FilenamePackageHugoJSON || n == files.FilenamePackageJSON
should = should || commonJSConfigs.MatchString(n)
if should {
mounts = append(mounts, Mount{
Source: n,
Target: filepath.Join(files.ComponentFolderAssets, files.FolderJSConfig, n),
})
}
}
return mounts, nil
}
func (c *collector) normalizeMounts(owner *moduleAdapter, mounts []Mount) ([]Mount, error) {
var out []Mount
dir := owner.Dir()
for _, mnt := range mounts {
errMsg := fmt.Sprintf("invalid module config for %q", owner.Path())
if mnt.Source == "" || mnt.Target == "" {
return nil, errors.New(errMsg + ": both source and target must be set")
}
mnt.Source = filepath.Clean(mnt.Source)
mnt.Target = filepath.Clean(mnt.Target)
var sourceDir string
if owner.projectMod && filepath.IsAbs(mnt.Source) {
// Abs paths in the main project is allowed.
sourceDir = mnt.Source
} else {
sourceDir = filepath.Join(dir, mnt.Source)
}
// Verify that Source exists
_, err := c.fs.Stat(sourceDir)
if err != nil {
continue
}
// Verify that target points to one of the predefined component dirs
targetBase := mnt.Target
idxPathSep := strings.Index(mnt.Target, string(os.PathSeparator))
if idxPathSep != -1 {
targetBase = mnt.Target[0:idxPathSep]
}
if !files.IsComponentFolder(targetBase) {
return nil, fmt.Errorf("%s: mount target must be one of: %v", errMsg, files.ComponentFolders)
}
out = append(out, mnt)
}
return out, nil
}
func (c *collector) wrapModuleNotFound(err error) error {
err = fmt.Errorf(err.Error()+": %w", ErrNotExist)
if c.GoModulesFilename == "" {
return err
}
baseMsg := "we found a go.mod file in your project, but"
switch c.goBinaryStatus {
case goBinaryStatusNotFound:
return fmt.Errorf(baseMsg+" you need to install Go to use it. See https://golang.org/dl/ : %q", err)
case goBinaryStatusTooOld:
return fmt.Errorf(baseMsg+" you need to a newer version of Go to use it. See https://golang.org/dl/ : %w", err)
}
return err
}
type vendoredModule struct {
Owner Module
Dir string
Version string
}
func createProjectModule(gomod *goModule, workingDir string, conf Config) *moduleAdapter {
// Create a pseudo module for the main project.
var path string
if gomod == nil {
path = "project"
}
return &moduleAdapter{
path: path,
dir: workingDir,
gomod: gomod,
projectMod: true,
config: conf,
}
}
// In the first iteration of Hugo Modules, we do not support multiple
// major versions running at the same time, so we pick the first (upper most).
// We will investigate namespaces in future versions.
// TODO(bep) add a warning when the above happens.
func pathKey(p string) string {
prefix, _, _ := module.SplitPathVersion(p)
return strings.ToLower(prefix)
}