Move func WuffsRoot to package generate

This commit is contained in:
Nigel Tao committed 2017-12-28 11:02:49 +11:00
1 parent f5c6204f09
commit d92e90d4f0
4 files changed
+35 -20

No files matched your search

+32
View File
@@ -15,11 +15,15 @@
package generate
import (
"errors"
"flag"
"fmt"
"go/build"
"io/ioutil"
"os"
"path/filepath"
"strings"
"sync"
"github.com/google/wuffs/lang/ast"
"github.com/google/wuffs/lang/check"
@@ -120,3 +124,31 @@ func ParseFiles(tm *token.Map, filenames []string, opts *parse.Options) (files [
}
return files, nil
}
var cachedWuffsRoot struct {
mu sync.Mutex
value string
}
func WuffsRoot() (string, error) {
cachedWuffsRoot.mu.Lock()
value := cachedWuffsRoot.value
cachedWuffsRoot.mu.Unlock()
if value != "" {
return value, nil
}
// TODO: look for a WUFFSROOT environment variable?
for _, p := range filepath.SplitList(build.Default.GOPATH) {
p = filepath.Join(p, "src", "github.com", "google", "wuffs")
if o, err := os.Stat(p); err == nil && o.IsDir() {
cachedWuffsRoot.mu.Lock()
cachedWuffsRoot.value = p
cachedWuffsRoot.mu.Unlock()
return p, nil
}
}
return "", errors.New("could not find Wuffs root directory")
}