Skip to content

Commit cdc8654

Browse files
committed
Fs
1 parent ee06931 commit cdc8654

File tree

14 files changed

+351
-109
lines changed

14 files changed

+351
-109
lines changed

‎common/maps/ordered.go‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@ func NewOrdered[K comparable, T any]() *Ordered[K, T] {
3333
return &Ordered[K, T]{values: make(map[K]T)}
3434
}
3535

36+
// Contains returns whether the map contains the given key.
37+
func (m *Ordered[K, T]) Contains(key K) bool {
38+
if m == nil {
39+
return false
40+
}
41+
_, found := m.values[key]
42+
return found
43+
}
44+
3645
// Set sets the value for the given key.
3746
// Note that insertion order is not affected if a key is re-inserted into the map.
3847
func (m *Ordered[K, T]) Set(key K, value T) {

‎hugofs/dirsmerger.go‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,41 @@ var AppendDirsMerger overlayfs.DirsMerger = func(lofi, bofi []fs.DirEntry) []fs.
6363

6464
return lofi
6565
}
66+
67+
// DirsMergerPreserveDuplicateFunc returns a DirsMerger that will preserve any duplicate
68+
// as defined by the given func.
69+
func DirsMergerPreserveDuplicateFunc(preserveDuplicate func(fs.DirEntry) bool) overlayfs.DirsMerger {
70+
return func(lofi, bofi []fs.DirEntry) []fs.DirEntry {
71+
for _, fi1 := range bofi {
72+
var found bool
73+
if !preserveDuplicate(fi1) {
74+
for _, fi2 := range lofi {
75+
if fi1.Name() == fi2.Name() {
76+
found = true
77+
break
78+
}
79+
}
80+
}
81+
if !found {
82+
lofi = append(lofi, fi1)
83+
}
84+
}
85+
return lofi
86+
}
87+
}
88+
89+
var FuncDirsMerger2 overlayfs.DirsMerger = func(lofi, bofi []fs.DirEntry) []fs.DirEntry {
90+
for _, bofi := range bofi {
91+
var found bool
92+
for _, lofi := range lofi {
93+
if bofi.Name() == lofi.Name() {
94+
found = true
95+
break
96+
}
97+
}
98+
if !found {
99+
lofi = append(lofi, bofi)
100+
}
101+
}
102+
return lofi
103+
}

‎hugofs/files/classifier.go‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ const (
4747
ComponentFolderAssets = "assets"
4848
ComponentFolderI18n = "i18n"
4949

50-
FolderVendor = "_vendor"
51-
50+
FolderVendor = "_vendor"
5251
FolderResources = "resources" // TODO1 remove.
5352
FolderJSConfig = "_jsconfig" // Mounted below /assets with postcss.config.js etc.
5453

‎hugofs/rootmapping_fs.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ func NewRootMappingFs(fs afero.Fs, rms ...RootMapping) (*RootMappingFs, error) {
6060
for _, rm := range rms {
6161
(&rm).clean()
6262

63+
if rm.From == files.FolderVendor {
64+
continue
65+
}
66+
6367
rm.FromBase = files.ResolveComponentFolder(rm.From)
6468

6569
if len(rm.To) < 2 {

‎hugolib/filesystems/basefs.go‎

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package filesystems
1818
import (
1919
"fmt"
2020
"io"
21+
"io/fs"
2122
"os"
2223
"path/filepath"
2324
"strings"
@@ -235,9 +236,6 @@ type SourceFilesystems struct {
235236
Archetypes *SourceFilesystem
236237
Assets *SourceFilesystem
237238

238-
// Note that this can not be mounted. It's currently fixed at the project root.
239-
Vendor *SourceFilesystem
240-
241239
AssetsWithDuplicatesPreserved *SourceFilesystem
242240

243241
RootFss []*hugofs.RootMappingFs
@@ -246,6 +244,10 @@ type SourceFilesystems struct {
246244
// with any sub module's resource fs layered below.
247245
ResourcesCache afero.Fs // TODO1 remove this.
248246

247+
// A writable filesystem on top of the project's vendor directory
248+
// with any sub module's vendor fs layered.
249+
VendorFs afero.Fs
250+
249251
// The work folder (may be a composite of project and theme components).
250252
Work afero.Fs
251253

@@ -572,13 +574,13 @@ func (b *sourceFilesystemsBuilder) Build() (*SourceFilesystems, error) {
572574
b.result.Layouts = createView(files.ComponentFolderLayouts, b.theBigFs.overlayMounts)
573575
b.result.Assets = createView(files.ComponentFolderAssets, b.theBigFs.overlayMounts)
574576
b.result.ResourcesCache = b.theBigFs.overlayResources
577+
b.result.VendorFs = b.theBigFs.overlayVendor
575578
b.result.RootFss = b.theBigFs.rootFss
576579

577580
// data and i18n needs a different merge strategy.
578581
overlayMountsPreserveDupes := b.theBigFs.overlayMounts.WithDirsMerger(hugofs.AppendDirsMerger)
579582
b.result.Data = createView(files.ComponentFolderData, overlayMountsPreserveDupes)
580583
b.result.I18n = createView(files.ComponentFolderI18n, overlayMountsPreserveDupes)
581-
b.result.Vendor = createView(files.FolderVendor, overlayMountsPreserveDupes)
582584
b.result.AssetsWithDuplicatesPreserved = createView(files.ComponentFolderAssets, overlayMountsPreserveDupes)
583585

584586
contentFs := hugofs.NewComponentFs(
@@ -632,6 +634,9 @@ func (b *sourceFilesystemsBuilder) createMainOverlayFs(p *paths.Paths) (*filesys
632634
overlayMountsStatic: overlayfs.New(overlayfs.Options{DirsMerger: hugofs.LanguageDirsMerger}),
633635
overlayFull: overlayfs.New(overlayfs.Options{}),
634636
overlayResources: overlayfs.New(overlayfs.Options{FirstWritable: true}),
637+
overlayVendor: overlayfs.New(overlayfs.Options{FirstWritable: true, DirsMerger: hugofs.DirsMergerPreserveDuplicateFunc(func(fi fs.DirEntry) bool {
638+
return fi.Name() == "resources.json"
639+
})}),
635640
}
636641

637642
mods := p.AllModules()
@@ -682,6 +687,7 @@ func (b *sourceFilesystemsBuilder) createOverlayFs(
682687
collector.overlayMountsFull = appendNopIfEmpty(collector.overlayMountsFull)
683688
collector.overlayFull = appendNopIfEmpty(collector.overlayFull)
684689
collector.overlayResources = appendNopIfEmpty(collector.overlayResources)
690+
collector.overlayVendor = appendNopIfEmpty(collector.overlayVendor)
685691

686692
return nil
687693
}
@@ -700,7 +706,16 @@ func (b *sourceFilesystemsBuilder) createOverlayFs(
700706
return md.dir, hpaths.AbsPathify(md.dir, path)
701707
}
702708

709+
modBase := collector.sourceProject
710+
if !md.isMainProject {
711+
modBase = collector.sourceModules
712+
}
713+
703714
for i, mount := range md.Mounts() {
715+
if mount.Target == files.FolderVendor {
716+
collector.overlayVendor = collector.overlayVendor.Append(hugofs.NewBasePathFs(modBase, mount.Source))
717+
continue
718+
}
704719
// Add more weight to early mounts.
705720
// When two mounts contain the same filename,
706721
// the first entry wins.
@@ -748,11 +763,6 @@ func (b *sourceFilesystemsBuilder) createOverlayFs(
748763
}
749764
}
750765

751-
modBase := collector.sourceProject
752-
if !md.isMainProject {
753-
modBase = collector.sourceModules
754-
}
755-
756766
sourceStatic := modBase
757767

758768
rmfs, err := hugofs.NewRootMappingFs(modBase, fromTo...)
@@ -835,7 +845,8 @@ type filesystemsCollector struct {
835845
overlayMountsStatic *overlayfs.OverlayFs
836846
overlayMountsFull *overlayfs.OverlayFs
837847
overlayFull *overlayfs.OverlayFs
838-
overlayResources *overlayfs.OverlayFs
848+
overlayResources *overlayfs.OverlayFs // TODO1 remove
849+
overlayVendor *overlayfs.OverlayFs
839850

840851
rootFss []*hugofs.RootMappingFs
841852

‎hugolib/hugo_sites_build.go‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -709,10 +709,6 @@ func (h *HugoSites) writeVendor(l logg.LevelLogger) error {
709709
return err
710710
}
711711

712-
for _, vf := range v.VendoredResources {
713-
fmt.Println("=== ==", vf.Path, vf.Hash)
714-
}
715-
716712
return nil
717713
}
718714

‎hugolib/integrationtest_builder.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,11 @@ func (s *IntegrationTestBuilder) AddFiles(filenameContent ...string) *Integratio
572572
return s
573573
}
574574

575+
func (s *IntegrationTestBuilder) RemovePublic() *IntegrationTestBuilder {
576+
s.Assert(s.fs.WorkingDirWritable.RemoveAll("public"), qt.IsNil)
577+
return s
578+
}
579+
575580
func (s *IntegrationTestBuilder) RemoveFiles(filenames ...string) *IntegrationTestBuilder {
576581
for _, filename := range filenames {
577582
absFilename := s.absFilename(filename)

‎modules/client.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ func (c *Client) Vendor() error {
270270
}
271271

272272
// Include the resource cache if present.
273+
// TODO1 remove
273274
resourcesDir := filepath.Join(dir, files.FolderResources)
274275
_, err := c.fs.Stat(resourcesDir)
275276
if err == nil {

‎modules/collect.go‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,11 @@ func (c *collector) applyMounts(moduleImport Import, mod *moduleAdapter) error {
400400
return err
401401
}
402402

403+
mounts, err = c.mountVendorDir(mod, mounts)
404+
if err != nil {
405+
return err
406+
}
407+
403408
mod.mounts = mounts
404409
return nil
405410
}
@@ -597,6 +602,26 @@ func (c *collector) loadModules() error {
597602
// Matches postcss.config.js etc.
598603
var commonJSConfigs = regexp.MustCompile(`(babel|postcss|tailwind)\.config\.js`)
599604

605+
func (c *collector) mountVendorDir(owner *moduleAdapter, mounts []Mount) ([]Mount, error) {
606+
dir := filepath.Join(owner.Dir(), files.FolderVendor)
607+
608+
add := owner.projectMod
609+
if !add {
610+
if _, err := c.fs.Stat(files.FolderVendor); err == nil {
611+
add = true
612+
}
613+
}
614+
615+
if add {
616+
mounts = append(mounts, Mount{
617+
Source: dir,
618+
Target: files.FolderVendor,
619+
})
620+
}
621+
622+
return mounts, nil
623+
}
624+
600625
func (c *collector) mountCommonJSConfig(owner *moduleAdapter, mounts []Mount) ([]Mount, error) {
601626
for _, m := range mounts {
602627
if strings.HasPrefix(m.Target, files.JsConfigFolderMountPrefix) {

0 commit comments

Comments
 (0)