|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "os" |
| 8 | + "os/exec" |
| 9 | + "path/filepath" |
| 10 | + "runtime" |
| 11 | + "strings" |
| 12 | +) |
| 13 | + |
| 14 | +func main() { |
| 15 | + // Process softfloat64.go file |
| 16 | + processSoftFloat64File() |
| 17 | + |
| 18 | + // Process softfloat64_test.go file |
| 19 | + processSoftFloat64TestFile() |
| 20 | + |
| 21 | + // Run mvdan.cc/gofumpt |
| 22 | + gofumpt() |
| 23 | + |
| 24 | + fmt.Println("Files processed successfully.") |
| 25 | +} |
| 26 | + |
| 27 | +func processSoftFloat64File() { |
| 28 | + // Read source file |
| 29 | + content, err := os.ReadFile(fmt.Sprintf("%s/src/runtime/softfloat64.go", runtime.GOROOT())) |
| 30 | + if err != nil { |
| 31 | + log.Fatal("Error reading source file:", err) |
| 32 | + } |
| 33 | + |
| 34 | + // Prepare header |
| 35 | + header := `// Code generated by github.com/gnolang/gno/gnovm/pkg/gnolang/internal/softfloat/gen. DO NOT EDIT. |
| 36 | +// This file is copied from $GOROOT/src/runtime/softfloat64.go. |
| 37 | +// It is the software floating point implementation used by the Go runtime. |
| 38 | +
|
| 39 | +` |
| 40 | + |
| 41 | + // Combine header with content |
| 42 | + newContent := header + string(content) |
| 43 | + |
| 44 | + // Replace package name |
| 45 | + newContent = strings.Replace(newContent, "package runtime", "package softfloat", 1) |
| 46 | + |
| 47 | + // Write to destination file |
| 48 | + err = os.WriteFile("runtime_softfloat64.go", []byte(newContent), 0o644) |
| 49 | + if err != nil { |
| 50 | + log.Fatal("Error writing to destination file:", err) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +func processSoftFloat64TestFile() { |
| 55 | + // Read source test file |
| 56 | + content, err := os.ReadFile(fmt.Sprintf("%s/src/runtime/softfloat64_test.go", runtime.GOROOT())) |
| 57 | + if err != nil { |
| 58 | + log.Fatal("Error reading source test file:", err) |
| 59 | + } |
| 60 | + |
| 61 | + // Prepare header |
| 62 | + header := `// Code generated by github.com/gnolang/gno/gnovm/pkg/gnolang/internal/softfloat/gen. DO NOT EDIT. |
| 63 | +// This file is copied from $GOROOT/src/runtime/softfloat64_test.go. |
| 64 | +// It is the tests for the software floating point implementation |
| 65 | +// used by the Go runtime. |
| 66 | +
|
| 67 | +` |
| 68 | + |
| 69 | + // Combine header with content |
| 70 | + newContent := header + string(content) |
| 71 | + |
| 72 | + // Replace package name and imports |
| 73 | + newContent = strings.Replace(newContent, "package runtime_test", "package softfloat_test", 1) |
| 74 | + newContent = strings.Replace(newContent, "\t. \"runtime\"", "\t\"runtime\"", 1) |
| 75 | + newContent = strings.Replace(newContent, "GOARCH", "runtime.GOARCH", 1) |
| 76 | + |
| 77 | + newContent = strings.Replace(newContent, "import (", "import (\n\t. \"github.com/gnolang/gno/gnovm/pkg/gnolang/internal/softfloat\"", 1) |
| 78 | + |
| 79 | + // Write to destination file |
| 80 | + err = os.WriteFile("runtime_softfloat64_test.go", []byte(newContent), 0o644) |
| 81 | + if err != nil { |
| 82 | + log.Fatal("Error writing to destination test file:", err) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +func gitRoot() (string, error) { |
| 87 | + wd, err := os.Getwd() |
| 88 | + if err != nil { |
| 89 | + return "", err |
| 90 | + } |
| 91 | + p := wd |
| 92 | + for { |
| 93 | + if s, e := os.Stat(filepath.Join(p, ".git")); e == nil && s.IsDir() { |
| 94 | + return p, nil |
| 95 | + } |
| 96 | + |
| 97 | + if strings.HasSuffix(p, string(filepath.Separator)) { |
| 98 | + return "", errors.New("root git not found") |
| 99 | + } |
| 100 | + |
| 101 | + p = filepath.Dir(p) |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +func gofumpt() { |
| 106 | + rootPath, err := gitRoot() |
| 107 | + if err != nil { |
| 108 | + log.Fatal("error finding git root:", err) |
| 109 | + } |
| 110 | + |
| 111 | + cmd := exec.Command("go", "run", "-modfile", filepath.Join(strings.TrimSpace(rootPath), "misc/devdeps/go.mod"), "mvdan.cc/gofumpt", "-w", ".") |
| 112 | + _, err = cmd.Output() |
| 113 | + if err != nil { |
| 114 | + log.Fatal("error gofumpt:", err) |
| 115 | + } |
| 116 | +} |
0 commit comments