| 1 |
// Copyright 2021 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 hglob |
| 15 |
|
| 16 |
import ( |
| 17 |
"os" |
| 18 |
"path" |
| 19 |
"path/filepath" |
| 20 |
"runtime" |
| 21 |
"strings" |
| 22 |
|
| 23 |
"github.com/gobwas/glob" |
| 24 |
"github.com/gobwas/glob/syntax" |
| 25 |
"github.com/gohugoio/hugo/common/hmaps" |
| 26 |
"github.com/gohugoio/hugo/common/hstrings" |
| 27 |
"github.com/gohugoio/hugo/identity" |
| 28 |
) |
| 29 |
|
| 30 |
const filepathSeparator = string(os.PathSeparator) |
| 31 |
|
| 32 |
var ( |
| 33 |
isWindows = runtime.GOOS == "windows" |
| 34 |
defaultGlobCache = &pathGlobCache{ |
| 35 |
isWindows: isWindows, |
| 36 |
cache: hmaps.NewCache[string, globErr](), |
| 37 |
} |
| 38 |
dotGlobCache = hmaps.NewCache[string, globErr]() |
| 39 |
) |
| 40 |
|
| 41 |
type globErr struct { |
| 42 |
glob hstrings.Matcher |
| 43 |
err error |
| 44 |
} |
| 45 |
|
| 46 |
type pathGlobCache struct { |
| 47 |
// Config |
| 48 |
isWindows bool |
| 49 |
|
| 50 |
// Cache |
| 51 |
cache *hmaps.Cache[string, globErr] |
| 52 |
} |
| 53 |
|
| 54 |
// GetGlobDot returns a hstrings.Matcher that matches the given pattern, using '.' as the path separator. |
| 55 |
func GetGlobDot(pattern string) (hstrings.Matcher, error) { |
| 56 |
v, err := dotGlobCache.GetOrCreate(pattern, func() (globErr, error) { |
| 57 |
g, err := glob.Compile(pattern, '.') |
| 58 |
return globErr{ |
| 59 |
glob: g, |
| 60 |
err: err, |
| 61 |
}, nil |
| 62 |
}) |
| 63 |
if err != nil { |
| 64 |
return nil, err |
| 65 |
} |
| 66 |
|
| 67 |
return v.glob, v.err |
| 68 |
} |
| 69 |
|
| 70 |
func (gc *pathGlobCache) GetGlob(pattern string) (hstrings.Matcher, error) { |
| 71 |
v, err := gc.cache.GetOrCreate(pattern, func() (globErr, error) { |
| 72 |
pattern = filepath.ToSlash(pattern) |
| 73 |
g, err := glob.Compile(strings.ToLower(pattern), '/') |
| 74 |
return globErr{ |
| 75 |
glob: globDecorator{ |
| 76 |
isWindows: gc.isWindows, |
| 77 |
g: g, |
| 78 |
}, |
| 79 |
err: err, |
| 80 |
}, nil |
| 81 |
}) |
| 82 |
if err != nil { |
| 83 |
return nil, err |
| 84 |
} |
| 85 |
|
| 86 |
return v.glob, v.err |
| 87 |
} |
| 88 |
|
| 89 |
// Or creates a new Glob from the given globs. |
| 90 |
func Or(globs ...hstrings.Matcher) hstrings.Matcher { |
| 91 |
return globSlice{globs: globs} |
| 92 |
} |
| 93 |
|
| 94 |
// MatchesFunc is a convenience type to create a hstrings.Matcher from a function. |
| 95 |
type MatchesFunc func(s string) bool |
| 96 |
|
| 97 |
func (m MatchesFunc) Match(s string) bool { |
| 98 |
return m(s) |
| 99 |
} |
| 100 |
|
| 101 |
type globSlice struct { |
| 102 |
globs []hstrings.Matcher |
| 103 |
} |
| 104 |
|
| 105 |
func (g globSlice) Match(s string) bool { |
| 106 |
for _, g := range g.globs { |
| 107 |
if g.Match(s) { |
| 108 |
return true |
| 109 |
} |
| 110 |
} |
| 111 |
return false |
| 112 |
} |
| 113 |
|
| 114 |
type globDecorator struct { |
| 115 |
// On Windows we may get filenames with Windows slashes to match, |
| 116 |
// which we need to normalize. |
| 117 |
isWindows bool |
| 118 |
|
| 119 |
g hstrings.Matcher |
| 120 |
} |
| 121 |
|
| 122 |
func (g globDecorator) Match(s string) bool { |
| 123 |
if g.isWindows { |
| 124 |
s = filepath.ToSlash(s) |
| 125 |
} |
| 126 |
s = strings.ToLower(s) |
| 127 |
return g.g.Match(s) |
| 128 |
} |
| 129 |
|
| 130 |
func GetGlob(pattern string) (hstrings.Matcher, error) { |
| 131 |
return defaultGlobCache.GetGlob(pattern) |
| 132 |
} |
| 133 |
|
| 134 |
func NormalizePath(p string) string { |
| 135 |
return strings.ToLower(NormalizePathNoLower(p)) |
| 136 |
} |
| 137 |
|
| 138 |
func NormalizePathNoLower(p string) string { |
| 139 |
return strings.Trim(path.Clean(filepath.ToSlash(p)), "/.") |
| 140 |
} |
| 141 |
|
| 142 |
// ResolveRootDir takes a normalized path on the form "assets/**.json" and |
| 143 |
// determines any root dir, i.e. any start path without any wildcards. |
| 144 |
func ResolveRootDir(p string) string { |
| 145 |
parts := strings.Split(path.Dir(p), "/") |
| 146 |
var roots []string |
| 147 |
for _, part := range parts { |
| 148 |
if HasGlobChar(part) { |
| 149 |
break |
| 150 |
} |
| 151 |
roots = append(roots, part) |
| 152 |
} |
| 153 |
|
| 154 |
if len(roots) == 0 { |
| 155 |
return "" |
| 156 |
} |
| 157 |
|
| 158 |
return strings.Join(roots, "/") |
| 159 |
} |
| 160 |
|
| 161 |
// FilterGlobParts removes any string with glob wildcard. |
| 162 |
func FilterGlobParts(a []string) []string { |
| 163 |
b := a[:0] |
| 164 |
for _, x := range a { |
| 165 |
if !HasGlobChar(x) { |
| 166 |
b = append(b, x) |
| 167 |
} |
| 168 |
} |
| 169 |
return b |
| 170 |
} |
| 171 |
|
| 172 |
// HasGlobChar returns whether s contains any glob wildcards. |
| 173 |
func HasGlobChar(s string) bool { |
| 174 |
for i := range len(s) { |
| 175 |
if syntax.IsSpecial(s[i]) { |
| 176 |
return true |
| 177 |
} |
| 178 |
} |
| 179 |
return false |
| 180 |
} |
| 181 |
|
| 182 |
// NewGlobIdentity creates a new Identity that |
| 183 |
// is probably dependent on any other Identity |
| 184 |
// that matches the given pattern. |
| 185 |
func NewGlobIdentity(pattern string) identity.Identity { |
| 186 |
glob, err := GetGlob(pattern) |
| 187 |
if err != nil { |
| 188 |
panic(err) |
| 189 |
} |
| 190 |
|
| 191 |
predicate := func(other identity.Identity) bool { |
| 192 |
return glob.Match(other.IdentifierBase()) |
| 193 |
} |
| 194 |
|
| 195 |
return identity.NewPredicateIdentity(predicate, nil) |
| 196 |
} |