Skip to content
Permalink
Browse files

rewrite gen.go to use panic

Makes the program much simpler. We don't need fancy error handling,
anyway.
  • Loading branch information...
mvdan committed Jun 27, 2019
1 parent c16825b commit f9f86e09f4aedc3a7d24f8957e259c95685ccd43
Showing with 43 additions and 71 deletions.
  1. +43 −71 gen.go
114 gen.go
@@ -6,9 +6,6 @@
package main

import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
@@ -18,15 +15,6 @@ import (
)

func main() {
if err := run(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}

func run() error {
flag.Parse()

cfg := &packages.Config{Mode: packages.NeedName | packages.NeedFiles}
pkgs, err := packages.Load(cfg,
"cmd/gofmt",
@@ -40,60 +28,64 @@ func run() error {
"golang.org/x/tools/internal/semver",
)
if err != nil {
return err
panic(err)
}
for _, pkg := range pkgs {
var err error
switch pkg.PkgPath {
case "cmd/gofmt":
err = copyGofmt(pkg.GoFiles)
copyGofmt(pkg.GoFiles)
case "golang.org/x/tools/cmd/goimports":
err = copyGoimports(pkg.GoFiles)
copyGoimports(pkg.GoFiles)
default:
parts := strings.Split(pkg.PkgPath, "/")
dir := filepath.Join(append([]string{"gofumports"}, parts[3:]...)...)
err = copyInternal(pkg.GoFiles, dir)
}
if err != nil {
return err
copyInternal(pkg.GoFiles, dir)
}
}
return nil
}

func copyGofmt(files []string) error {
func readFile(path string) string {
body, err := ioutil.ReadFile(path)
if err != nil {
panic(err)
}
return string(body)
}

func writeFile(path, body string) {
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
panic(err)
}
if err := ioutil.WriteFile(path, []byte(body), 0644); err != nil {
panic(err)
}
}

func copyGofmt(files []string) {
const extraSrc = `
// This is the only gofumpt change on gofmt's codebase, besides changing
// the name in the usage text.
internal.Gofumpt(fileSet, file)
`

for _, fpath := range files {
bodyBytes, err := ioutil.ReadFile(fpath)
if err != nil {
return err
}
body := string(bodyBytes) // to simplify operations later
name := filepath.Base(fpath)
for _, path := range files {
body := readFile(path)
name := filepath.Base(path)
switch name {
case "doc.go":
continue // we have our own
case "gofmt.go":
i := strings.Index(body, "res, err := format(")
if i < 0 {
return fmt.Errorf("could not insert the gofumpt source code")
panic("could not insert the gofumpt source code")
}
body = body[:i] + "\n" + extraSrc + "\n" + body[i:]
}
body = strings.Replace(body, "gofmt", "gofumpt", -1)
if err := ioutil.WriteFile(name, []byte(body), 0644); err != nil {
return err
}
writeFile(name, body)
}
return nil
}

func copyGoimports(files []string) error {
func copyGoimports(files []string) {
const extraSrc = `
// This is the only gofumpt change on goimports's codebase, besides
// changing the name in the usage text.
@@ -102,58 +94,38 @@ func copyGoimports(files []string) error {
return err
}
`

for _, fpath := range files {
bodyBytes, err := ioutil.ReadFile(fpath)
if err != nil {
return err
}
bodyBytes = fixImports(bodyBytes)
body := string(bodyBytes) // to simplify operations later
name := filepath.Base(fpath)
for _, path := range files {
body := readFile(path)
body = fixImports(body)
name := filepath.Base(path)
switch name {
case "doc.go":
continue // we have our own
case "goimports.go":
i := strings.Index(body, "if !bytes.Equal")
if i < 0 {
return fmt.Errorf("could not insert the gofumports source code")
panic("could not insert the gofumports source code")
}
body = body[:i] + "\n" + extraSrc + "\n" + body[i:]
}
body = strings.Replace(body, "goimports", "gofumports", -1)

dst := filepath.Join("gofumports", name)
if err := ioutil.WriteFile(dst, []byte(body), 0644); err != nil {
return err
}
writeFile(filepath.Join("gofumports", name), body)
}
return nil
}

func copyInternal(files []string, dir string) error {
if err := os.MkdirAll(dir, 0755); err != nil {
return err
}
for _, fpath := range files {
body, err := ioutil.ReadFile(fpath)
if err != nil {
return err
}
func copyInternal(files []string, dir string) {
for _, path := range files {
body := readFile(path)
body = fixImports(body)

name := filepath.Base(fpath)
dst := filepath.Join(dir, name)
if err := ioutil.WriteFile(dst, []byte(body), 0644); err != nil {
return err
}
name := filepath.Base(path)
writeFile(filepath.Join(dir, name), body)
}
return nil
}

func fixImports(body []byte) []byte {
return bytes.Replace(body,
[]byte("golang.org/x/tools/internal/"),
[]byte("mvdan.cc/gofumpt/gofumports/internal/"),
func fixImports(body string) string {
return strings.Replace(body,
"golang.org/x/tools/internal/",
"mvdan.cc/gofumpt/gofumports/internal/",
-1)
}

0 comments on commit f9f86e0

Please sign in to comment.
You can’t perform that action at this time.