| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
package main |
| 14 |
|
| 15 |
import ( |
| 16 |
"bytes" |
| 17 |
"go/build/constraint" |
| 18 |
"go/scanner" |
| 19 |
"go/token" |
| 20 |
"log" |
| 21 |
"os" |
| 22 |
"path/filepath" |
| 23 |
"runtime" |
| 24 |
"strings" |
| 25 |
) |
| 26 |
|
| 27 |
func main() { |
| 28 |
dir := filepath.Join(runtime.GOROOT(), "src", "cmd", "go", "internal", "robustio") |
| 29 |
|
| 30 |
entries, err := os.ReadDir(dir) |
| 31 |
if err != nil { |
| 32 |
log.Fatalf("reading the robustio dir: %v", err) |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
fileContent := make(map[string][]byte) |
| 37 |
windowsImport := []byte("\t\"internal/syscall/windows\"\n") |
| 38 |
foundWindowsImport := false |
| 39 |
for _, entry := range entries { |
| 40 |
if strings.HasSuffix(entry.Name(), ".go") { |
| 41 |
pth := filepath.Join(dir, entry.Name()) |
| 42 |
content, err := os.ReadFile(pth) |
| 43 |
if err != nil { |
| 44 |
log.Fatalf("reading %q: %v", entry.Name(), err) |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
if entry.Name() == "robustio_windows.go" && bytes.Contains(content, windowsImport) { |
| 50 |
foundWindowsImport = true |
| 51 |
content = bytes.Replace(content, windowsImport, nil, 1) |
| 52 |
content = bytes.Replace(content, []byte("windows.ERROR_SHARING_VIOLATION"), []byte("ERROR_SHARING_VIOLATION"), -1) |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
if bytes.Contains(content, []byte("os.ReadFile(")) { |
| 62 |
content = bytes.Replace(content, []byte("\"os\""), []byte("\"io/ioutil\"\n\t\"os\""), 1) |
| 63 |
content = bytes.Replace(content, []byte("os.ReadFile("), []byte("os.ReadFile("), -1) |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
content = addPlusBuildConstraints(content) |
| 68 |
|
| 69 |
fileContent[entry.Name()] = content |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
if !foundWindowsImport { |
| 74 |
log.Fatal("missing expected import of internal/syscall/windows in robustio_windows.go") |
| 75 |
} |
| 76 |
|
| 77 |
for name, content := range fileContent { |
| 78 |
if err := os.WriteFile(name, content, 0644); err != nil { |
| 79 |
log.Fatalf("writing %q: %v", name, err) |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
func addPlusBuildConstraints(src []byte) []byte { |
| 89 |
var s scanner.Scanner |
| 90 |
fset := token.NewFileSet() |
| 91 |
file := fset.AddFile("", fset.Base(), len(src)) |
| 92 |
s.Init(file, src, nil , scanner.ScanComments) |
| 93 |
|
| 94 |
result := make([]byte, 0, len(src)) |
| 95 |
lastInsertion := 0 |
| 96 |
for { |
| 97 |
pos, tok, lit := s.Scan() |
| 98 |
if tok == token.EOF { |
| 99 |
break |
| 100 |
} |
| 101 |
if tok == token.COMMENT { |
| 102 |
if c, err := constraint.Parse(lit); err == nil { |
| 103 |
plusBuild, err := constraint.PlusBuildLines(c) |
| 104 |
if err != nil { |
| 105 |
log.Fatalf("computing +build constraint for %q: %v", lit, err) |
| 106 |
} |
| 107 |
insertAt := file.Offset(pos) + len(lit) |
| 108 |
result = append(result, src[lastInsertion:insertAt]...) |
| 109 |
result = append(result, []byte("\n"+strings.Join(plusBuild, "\n"))...) |
| 110 |
lastInsertion = insertAt |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
result = append(result, src[lastInsertion:]...) |
| 115 |
return result |
| 116 |
} |