aboutsummaryrefslogtreecommitdiff
path: root/vendor/golang.org/x/text/internal/gen/gen.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/golang.org/x/text/internal/gen/gen.go')
-rw-r--r--vendor/golang.org/x/text/internal/gen/gen.go92
1 files changed, 70 insertions, 22 deletions
diff --git a/vendor/golang.org/x/text/internal/gen/gen.go b/vendor/golang.org/x/text/internal/gen/gen.go
index dfaa278..9eb2987 100644
--- a/vendor/golang.org/x/text/internal/gen/gen.go
+++ b/vendor/golang.org/x/text/internal/gen/gen.go
@@ -22,6 +22,7 @@ import (
"bytes"
"flag"
"fmt"
+ "go/build"
"go/format"
"io"
"io/ioutil"
@@ -30,6 +31,7 @@ import (
"os"
"path"
"path/filepath"
+ "sync"
"unicode"
"golang.org/x/text/unicode/cldr"
@@ -48,12 +50,6 @@ var (
cldrVersion = flag.String("cldr",
getEnv("CLDR_VERSION", cldr.Version),
"cldr version to use")
- // Allow an environment variable to specify the local directory.
- // go generate doesn't allow specifying arguments; this is a useful
- // alternative to specifying a local mirror.
- localDir = flag.String("local",
- os.Getenv("UNICODE_DIR"),
- "directory containing local data files; for debugging only.")
)
func getEnv(name, def string) string {
@@ -87,9 +83,12 @@ func CLDRVersion() string {
return *cldrVersion
}
-// IsLocal reports whether the user specified a local directory.
+// IsLocal reports whether data files are available locally.
func IsLocal() bool {
- return *localDir != ""
+ if _, err := os.Stat(localReadmeFile()); err != nil {
+ return false
+ }
+ return true
}
// OpenUCDFile opens the requested UCD file. The file is specified relative to
@@ -124,29 +123,78 @@ func OpenIANAFile(path string) io.ReadCloser {
return Open(*iana, "iana", path)
}
+var (
+ dirMutex sync.Mutex
+ localDir string
+)
+
+const permissions = 0755
+
+func localReadmeFile() string {
+ p, err := build.Import("golang.org/x/text", "", build.FindOnly)
+ if err != nil {
+ log.Fatalf("Could not locate package: %v", err)
+ }
+ return filepath.Join(p.Dir, "DATA", "README")
+}
+
+func getLocalDir() string {
+ dirMutex.Lock()
+ defer dirMutex.Unlock()
+
+ readme := localReadmeFile()
+ dir := filepath.Dir(readme)
+ if _, err := os.Stat(readme); err != nil {
+ if err := os.MkdirAll(dir, permissions); err != nil {
+ log.Fatalf("Could not create directory: %v", err)
+ }
+ ioutil.WriteFile(readme, []byte(readmeTxt), permissions)
+ }
+ return dir
+}
+
+const readmeTxt = `Generated by golang.org/x/text/internal/gen. DO NOT EDIT.
+
+This directory contains downloaded files used to generate the various tables
+in the golang.org/x/text subrepo.
+
+Note that the language subtag repo (iana/assignments/language-subtag-registry)
+and all other times in the iana subdirectory are not versioned and will need
+to be periodically manually updated. The easiest way to do this is to remove
+the entire iana directory. This is mostly of concern when updating the language
+package.
+`
+
// Open opens subdir/path if a local directory is specified and the file exists,
// where subdir is a directory relative to the local root, or fetches it from
// urlRoot/path otherwise. It will call log.Fatal if there are any errors.
func Open(urlRoot, subdir, path string) io.ReadCloser {
- if *localDir != "" {
- path = filepath.FromSlash(path)
- if f, err := os.Open(filepath.Join(*localDir, subdir, path)); err == nil {
- return f
- }
- }
- return get(urlRoot, path)
+ file := filepath.Join(getLocalDir(), subdir, filepath.FromSlash(path))
+ return open(file, urlRoot, path)
}
func openUnicode(path string) io.ReadCloser {
- if *localDir != "" {
- path = filepath.FromSlash(path)
- f, err := os.Open(filepath.Join(*localDir, path))
- if err != nil {
- log.Fatal(err)
- }
+ file := filepath.Join(getLocalDir(), filepath.FromSlash(path))
+ return open(file, *url, path)
+}
+
+// TODO: automatically periodically update non-versioned files.
+
+func open(file, urlRoot, path string) io.ReadCloser {
+ if f, err := os.Open(file); err == nil {
return f
}
- return get(*url, path)
+ r := get(urlRoot, path)
+ defer r.Close()
+ b, err := ioutil.ReadAll(r)
+ if err != nil {
+ log.Fatalf("Could not download file: %v", err)
+ }
+ os.MkdirAll(filepath.Dir(file), permissions)
+ if err := ioutil.WriteFile(file, b, permissions); err != nil {
+ log.Fatalf("Could not create file: %v", err)
+ }
+ return ioutil.NopCloser(bytes.NewReader(b))
}
func get(root, path string) io.ReadCloser {