| 1 |
> |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
package main |
| 7 |
|
| 8 |
import ( |
| 9 |
"bytes" |
| 10 |
"encoding/json" |
| 11 |
"io" |
| 12 |
"os" |
| 13 |
"os/exec" |
| 14 |
"path" |
| 15 |
"path/filepath" |
| 16 |
"strings" |
| 17 |
) |
| 18 |
|
| 19 |
var ( |
| 20 |
modulePath = "mvdan.cc/gofumpt" |
| 21 |
vendorDir = filepath.Join("internal", "govendor") |
| 22 |
) |
| 23 |
|
| 24 |
|
| 25 |
var toVendor = []string{ |
| 26 |
"go/format", |
| 27 |
"go/printer", |
| 28 |
"go/doc/comment", |
| 29 |
"internal/diff", |
| 30 |
} |
| 31 |
|
| 32 |
func main() { |
| 33 |
catch(os.RemoveAll(vendorDir)) |
| 34 |
|
| 35 |
catch(os.MkdirAll(vendorDir, 0o777)) |
| 36 |
out, err := exec.Command("go", "env", "GOVERSION").Output() |
| 37 |
catch(err) |
| 38 |
catch(os.WriteFile(filepath.Join(vendorDir, "version.txt"), out, 0o666)) |
| 39 |
|
| 40 |
oldnew := []string{ |
| 41 |
"//go:generate", "//disabled go:generate", |
| 42 |
} |
| 43 |
for _, pkgPath := range toVendor { |
| 44 |
oldnew = append(oldnew, pkgPath, path.Join(modulePath, vendorDir, pkgPath)) |
| 45 |
} |
| 46 |
replacer := strings.NewReplacer(oldnew...) |
| 47 |
|
| 48 |
listArgs := append([]string{"list", "-json"}, toVendor...) |
| 49 |
out, err = exec.Command("go", listArgs...).Output() |
| 50 |
catch(err) |
| 51 |
|
| 52 |
type Package struct { |
| 53 |
Dir string |
| 54 |
ImportPath string |
| 55 |
GoFiles []string |
| 56 |
} |
| 57 |
dec := json.NewDecoder(bytes.NewReader(out)) |
| 58 |
for { |
| 59 |
var pkg Package |
| 60 |
err := dec.Decode(&pkg) |
| 61 |
if err == io.EOF { |
| 62 |
break |
| 63 |
} |
| 64 |
catch(err) |
| 65 |
|
| 66 |
|
| 67 |
dstPkg := strings.TrimPrefix(pkg.ImportPath, "internal/") |
| 68 |
|
| 69 |
dstDir := filepath.Join(vendorDir, filepath.FromSlash(dstPkg)) |
| 70 |
catch(os.MkdirAll(dstDir, 0o777)) |
| 71 |
|
| 72 |
|
| 73 |
for _, goFile := range pkg.GoFiles { |
| 74 |
srcBytes, err := os.ReadFile(filepath.Join(pkg.Dir, goFile)) |
| 75 |
catch(err) |
| 76 |
|
| 77 |
src := replacer.Replace(string(srcBytes)) |
| 78 |
|
| 79 |
dst := filepath.Join(dstDir, goFile) |
| 80 |
catch(os.WriteFile(dst, []byte(src), 0o666)) |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
func catch(err error) { |
| 86 |
if err != nil { |
| 87 |
panic(err) |
| 88 |
} |
| 89 |
} |