Move all file handling into main.go.

This commit is contained in:
Greg 2018-08-06 12:11:15 -04:00
parent 62c409292b
commit 33821928c9
2 changed files with 33 additions and 26 deletions

View File

@ -1,16 +1,14 @@
package main package main
import ( import (
"path/filepath" "io"
"log" "log"
"go/ast" "go/ast"
"go/format" "go/format"
"go/parser" "go/parser"
"go/token" "go/token"
"golang.org/x/tools/go/ast/astutil" "golang.org/x/tools/go/ast/astutil"
"os"
"regexp" "regexp"
"strings"
) )
type subspec struct { type subspec struct {
@ -91,16 +89,6 @@ var (
) )
func init() { func init() {
fs,err := filepath.Glob("pgen*.go")
if err != nil {
log.Fatal("Glob error")
}
for _,f := range(fs) {
err := os.Remove(f)
if err != nil && os.IsExist(err) {
log.Fatal("Removing ",f,err)
}
}
qexp = regexp.MustCompile(`"`) qexp = regexp.MustCompile(`"`)
} }
@ -153,24 +141,15 @@ func (g *Generator) Import(name string) {
astutil.AddImport(g.fset, g.nodes, name) astutil.AddImport(g.fset, g.nodes, name)
} }
func (g *Generator) Save() { func (g *Generator) Save(of io.Writer) {
if g.nodes == nil { if g.nodes == nil {
return return
} }
var of *os.File if g.name != "main" {
var err error
if g.name == "main" {
of, err = os.Create("pgen.go")
} else {
oname := strings.Join([]string{"pgen_",g.name,".go"},"")
of, err = os.Create(oname)
g.nodes.Name = ast.NewIdent(g.name) g.nodes.Name = ast.NewIdent(g.name)
} }
if err != nil {
log.Fatal("Cannot open pgen.go")
}
err = format.Node(of,g.fset,g.nodes) err := format.Node(of,g.fset,g.nodes)
if err != nil { if err != nil {
log.Fatal("Generate error:",err) log.Fatal("Generate error:",err)
} }

View File

@ -7,6 +7,8 @@ import (
"go/token" "go/token"
"go/types" "go/types"
"log" "log"
"os"
"path/filepath"
"regexp" "regexp"
"strings" "strings"
) )
@ -104,6 +106,18 @@ func init() {
imps = make(map[string]string) imps = make(map[string]string)
needImps = make(map[string]bool) needImps = make(map[string]bool)
fset = token.NewFileSet() fset = token.NewFileSet()
// clear out old generated files
fs,err := filepath.Glob("pgen*.go")
if err != nil {
log.Fatal("Glob error")
}
for _,f := range(fs) {
err := os.Remove(f)
if err != nil && os.IsExist(err) {
log.Fatal("Removing ",f,err)
}
}
} }
func main() { func main() {
@ -116,6 +130,9 @@ func main() {
log.Print("Processing package ",pkg.Name) log.Print("Processing package ",pkg.Name)
ts = make(map[string]tps) ts = make(map[string]tps)
chkpkg(pkg) chkpkg(pkg)
if len(ts) == 0 {
continue
}
g := NewGenerator(pkg.Name) g := NewGenerator(pkg.Name)
for _,v := range ts { for _,v := range ts {
g.Add(v.fun, v.name, v.typ) g.Add(v.fun, v.name, v.typ)
@ -123,9 +140,20 @@ func main() {
for name := range needImps { for name := range needImps {
g.Import(imps[name]) g.Import(imps[name])
} }
var of *os.File
var err error
if pkg.Name == "main" {
of, err = os.Create("pgen.go")
} else {
oname := strings.Join([]string{"pgen_",pkg.Name,".go"},"")
of, err = os.Create(oname)
}
if err != nil {
log.Fatal("Cannot open output file: ",err)
}
// process template for each type identified and // process template for each type identified and
// generate output // generate output
g.Save() g.Save(of)
} }
} }