github.com/westcoastroms/westcoastroms-build@v0.0.0-20190928114312-2350e5a73030/build/blueprint/glob.go (about) 1 // Copyright 2015 Google Inc. 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 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package blueprint 16 17 import ( 18 "crypto/md5" 19 "fmt" 20 "sort" 21 "strings" 22 ) 23 24 type GlobPath struct { 25 Pattern string 26 Excludes []string 27 Files []string 28 Deps []string 29 Name string 30 } 31 32 func verifyGlob(fileName, pattern string, excludes []string, g GlobPath) { 33 if pattern != g.Pattern { 34 panic(fmt.Errorf("Mismatched patterns %q and %q for glob file %q", pattern, g.Pattern, fileName)) 35 } 36 if len(excludes) != len(g.Excludes) { 37 panic(fmt.Errorf("Mismatched excludes %v and %v for glob file %q", excludes, g.Excludes, fileName)) 38 } 39 40 for i := range excludes { 41 if g.Excludes[i] != excludes[i] { 42 panic(fmt.Errorf("Mismatched excludes %v and %v for glob file %q", excludes, g.Excludes, fileName)) 43 } 44 } 45 } 46 47 func (c *Context) glob(pattern string, excludes []string) ([]string, error) { 48 fileName := globToFileName(pattern, excludes) 49 50 // Try to get existing glob from the stored results 51 c.globLock.Lock() 52 g, exists := c.globs[fileName] 53 c.globLock.Unlock() 54 55 if exists { 56 // Glob has already been done, double check it is identical 57 verifyGlob(fileName, pattern, excludes, g) 58 return g.Files, nil 59 } 60 61 // Get a globbed file list 62 files, deps, err := c.fs.Glob(pattern, excludes) 63 if err != nil { 64 return nil, err 65 } 66 67 // Store the results 68 c.globLock.Lock() 69 if g, exists = c.globs[fileName]; !exists { 70 c.globs[fileName] = GlobPath{pattern, excludes, files, deps, fileName} 71 } 72 c.globLock.Unlock() 73 74 // Getting the list raced with another goroutine, throw away the results and use theirs 75 if exists { 76 verifyGlob(fileName, pattern, excludes, g) 77 return g.Files, nil 78 } 79 80 return files, nil 81 } 82 83 func (c *Context) Globs() []GlobPath { 84 fileNames := make([]string, 0, len(c.globs)) 85 for k := range c.globs { 86 fileNames = append(fileNames, k) 87 } 88 sort.Strings(fileNames) 89 90 globs := make([]GlobPath, len(fileNames)) 91 for i, fileName := range fileNames { 92 globs[i] = c.globs[fileName] 93 } 94 95 return globs 96 } 97 98 func globToString(pattern string) string { 99 ret := "" 100 for _, c := range pattern { 101 switch { 102 case c >= 'a' && c <= 'z', 103 c >= 'A' && c <= 'Z', 104 c >= '0' && c <= '9', 105 c == '_', c == '-', c == '/': 106 ret += string(c) 107 default: 108 ret += "_" 109 } 110 } 111 112 return ret 113 } 114 115 func globToFileName(pattern string, excludes []string) string { 116 name := globToString(pattern) 117 excludeName := "" 118 for _, e := range excludes { 119 excludeName += "__" + globToString(e) 120 } 121 122 // Prevent file names from reaching ninja's path component limit 123 if strings.Count(name, "/")+strings.Count(excludeName, "/") > 30 { 124 excludeName = fmt.Sprintf("___%x", md5.Sum([]byte(excludeName))) 125 } 126 127 return name + excludeName + ".glob" 128 }