Skip to content

Commit fe223c5

Browse files
committed
gen: prepare for move to Unicode 11
automatically update build tags option to skip tests Change-Id: Ic1e40c1a8308ed3641ba219a6e55b76dee98581f Reviewed-on: https://go-review.googlesource.com/c/text/+/154440 Run-TryBot: Marcel van Lohuizen <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent 5d731a3 commit fe223c5

File tree

3 files changed

+41
-20
lines changed

3 files changed

+41
-20
lines changed

gen.go

+5
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ var (
3434
verbose = flag.Bool("v", false, "verbose output")
3535
force = flag.Bool("force", false, "ignore failing dependencies")
3636
doCore = flag.Bool("core", false, "force an update to core")
37+
skipTest = flag.Bool("skiptest", false, "skip tests")
3738
excludeList = flag.String("exclude", "",
3839
"comma-separated list of packages to exclude")
3940

@@ -209,6 +210,10 @@ func generate(pkg string, deps ...*dependency) *dependency {
209210
return
210211
}
211212

213+
if *skipTest {
214+
return
215+
}
216+
212217
vprintf("=== TEST %s\n", pkg)
213218
args[0] = "test"
214219
cmd = exec.Command(filepath.Join(runtime.GOROOT(), "bin", "go"), args...)

internal/gen/code.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ func (w *CodeWriter) WriteGoFile(filename, pkg string) {
6666
func (w *CodeWriter) WriteVersionedGoFile(filename, pkg string) {
6767
tags := buildTags()
6868
if tags != "" {
69-
filename = insertVersion(filename, UnicodeVersion())
69+
pattern := fileToPattern(filename)
70+
updateBuildTags(pattern)
71+
filename = fmt.Sprintf(pattern, UnicodeVersion())
7072
}
7173
f, err := os.Create(filename)
7274
if err != nil {

internal/gen/gen.go

+33-19
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"os"
3232
"path"
3333
"path/filepath"
34+
"regexp"
3435
"strings"
3536
"sync"
3637
"unicode"
@@ -83,25 +84,21 @@ func CLDRVersion() string {
8384
}
8485

8586
var tags = []struct{ version, buildTags string }{
86-
{"10.0.0", "go1.10"},
87-
{"", "!go1.10"},
87+
{"9.0.0", "!go1.10"},
88+
{"10.0.0", "go1.10, !go1.13"},
89+
{"11.0.0", "go1.13"},
8890
}
8991

9092
// buildTags reports the build tags used for the current Unicode version.
9193
func buildTags() string {
9294
v := UnicodeVersion()
93-
for _, x := range tags {
94-
// We should do a numeric comparison, but including the collate package
95-
// would create an import cycle. We approximate it by assuming that
96-
// longer version strings are later.
97-
if len(x.version) <= len(v) {
98-
return x.buildTags
99-
}
100-
if len(x.version) == len(v) && x.version <= v {
101-
return x.buildTags
95+
for _, e := range tags {
96+
if e.version == v {
97+
return e.buildTags
10298
}
10399
}
104-
return tags[0].buildTags
100+
log.Fatalf("Unknown build tags for Unicode version %q.", v)
101+
return ""
105102
}
106103

107104
// IsLocal reports whether data files are available locally.
@@ -269,29 +266,46 @@ func WriteGoFile(filename, pkg string, b []byte) {
269266
}
270267
}
271268

272-
func insertVersion(filename, version string) string {
269+
func fileToPattern(filename string) string {
273270
suffix := ".go"
274271
if strings.HasSuffix(filename, "_test.go") {
275272
suffix = "_test.go"
276273
}
277-
return fmt.Sprint(filename[:len(filename)-len(suffix)], version, suffix)
274+
prefix := filename[:len(filename)-len(suffix)]
275+
return fmt.Sprint(prefix, "%s", suffix)
276+
}
277+
278+
func updateBuildTags(pattern string) {
279+
for _, t := range tags {
280+
oldFile := fmt.Sprintf(pattern, t.version)
281+
b, err := ioutil.ReadFile(oldFile)
282+
if err != nil {
283+
continue
284+
}
285+
build := fmt.Sprintf("// +build %s", t.buildTags)
286+
b = regexp.MustCompile(`// \+build .*`).ReplaceAll(b, []byte(build))
287+
err = ioutil.WriteFile(oldFile, b, 0644)
288+
if err != nil {
289+
log.Fatal(err)
290+
}
291+
}
278292
}
279293

280294
// WriteVersionedGoFile prepends a standard file comment, adds build tags to
281295
// version the file for the current Unicode version, and package statement to
282296
// the given bytes, applies gofmt, and writes them to a file with the given
283297
// name. It will call log.Fatal if there are any errors.
284298
func WriteVersionedGoFile(filename, pkg string, b []byte) {
285-
tags := buildTags()
286-
if tags != "" {
287-
filename = insertVersion(filename, UnicodeVersion())
288-
}
299+
pattern := fileToPattern(filename)
300+
updateBuildTags(pattern)
301+
filename = fmt.Sprintf(pattern, UnicodeVersion())
302+
289303
w, err := os.Create(filename)
290304
if err != nil {
291305
log.Fatalf("Could not create file %s: %v", filename, err)
292306
}
293307
defer w.Close()
294-
if _, err = WriteGo(w, pkg, tags, b); err != nil {
308+
if _, err = WriteGo(w, pkg, buildTags(), b); err != nil {
295309
log.Fatalf("Error writing file %s: %v", filename, err)
296310
}
297311
}

0 commit comments

Comments
 (0)