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

-15
View File
@@ -19,11 +19,7 @@
package commonflags
import (
"errors"
"go/build"
"os"
"path"
"path/filepath"
)
const (
@@ -66,14 +62,3 @@ func IsAlphaNumericIsh(s string) bool {
func IsValidUsePath(s string) bool {
return s == path.Clean(s) && s != "" && s[0] != '.' && s[0] != '/'
}
func WuffsRoot() (string, error) {
// 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() {
return p, nil
}
}
return "", errors.New("could not find Wuffs root directory")
}
+1 -3
View File
@@ -32,8 +32,6 @@ import (
"github.com/google/wuffs/lang/check"
"github.com/google/wuffs/lang/generate"
cf "github.com/google/wuffs/cmd/commonflags"
a "github.com/google/wuffs/lang/ast"
t "github.com/google/wuffs/lang/token"
)
@@ -613,7 +611,7 @@ func (g *gen) writeUse(b *buffer, n *a.Use) error {
if g.wuffsRoot == "" {
var err error
g.wuffsRoot, err = cf.WuffsRoot()
g.wuffsRoot, err = generate.WuffsRoot()
if err != nil {
return err
}
+2 -2
View File
@@ -23,7 +23,7 @@ import (
"sort"
"strings"
cf "github.com/google/wuffs/cmd/commonflags"
"github.com/google/wuffs/lang/generate"
)
var commands = []struct {
@@ -63,7 +63,7 @@ func main1() error {
flag.Usage = usage
flag.Parse()
wuffsRoot, err := cf.WuffsRoot()
wuffsRoot, err := generate.WuffsRoot()
if err != nil {
return err
}
+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")
}