-
Notifications
You must be signed in to change notification settings - Fork 29
/
gow.go
157 lines (140 loc) · 4.43 KB
/
gow.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
Copyright 2021 The GoPlus Authors (goplus.org)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package gogen
import (
"go/ast"
"go/token"
"go/types"
"io"
"log"
"os"
"syscall"
"github.com/goplus/gogen/internal/go/format"
"github.com/goplus/gogen/internal/go/printer"
)
// ----------------------------------------------------------------------------
// TypeAST returns the AST of specified typ.
func TypeAST(pkg *Package, typ types.Type) ast.Expr {
return toType(pkg, typ)
}
// ASTFile returns AST of a file by its fname.
// If fname is not provided, it returns AST of the default (NOT current) file.
func (p *Package) ASTFile(fname ...string) *ast.File {
f, ok := p.File(fname...)
if !ok {
return nil
}
if debugWriteFile {
log.Println("==> ASTFile", f.Name())
}
decls := f.getDecls(p)
file := &ast.File{Name: ident(p.Types.Name()), Decls: decls, Imports: getImports(decls)}
return file
}
func getImports(decls []ast.Decl) []*ast.ImportSpec {
if len(decls) > 0 {
if decl, ok := decls[0].(*ast.GenDecl); ok && decl.Tok == token.IMPORT {
n := len(decl.Specs)
ret := make([]*ast.ImportSpec, n)
for i, spec := range decl.Specs {
ret[i] = spec.(*ast.ImportSpec)
}
return ret
}
}
return nil
}
// CommentedASTFile returns commented AST of a file by its fname.
// If fname is not provided, it returns AST of the default (NOT current) file.
func (p *Package) CommentedASTFile(fname ...string) *printer.CommentedNodes {
f := p.ASTFile(fname...)
if f == nil {
return nil
}
return &printer.CommentedNodes{
Node: f,
CommentedStmts: p.commentedStmts,
}
}
// WriteTo writes a file named fname to dst.
// If fname is not provided, it writes the default (NOT current) file.
func (p *Package) WriteTo(dst io.Writer, fname ...string) (err error) {
file := p.CommentedASTFile(fname...)
if file == nil {
return syscall.ENOENT
}
fset := token.NewFileSet()
return format.Node(dst, fset, file)
}
// GeneratedHeader is the default string that the source file generated by gogen start with.
// Change it if you want to make it different.
var (
GeneratedHeader = "// Code generated by gogen; DO NOT EDIT.\n\n"
)
// WriteFile writes a file named fname.
// If fname is not provided, it writes the default (NOT current) file.
func (p *Package) WriteFile(file string, fname ...string) (err error) {
ast := p.CommentedASTFile(fname...)
if ast == nil {
return syscall.ENOENT
}
if debugWriteFile {
log.Println("WriteFile", file)
}
f, err := os.Create(file)
if err != nil {
return
}
err = syscall.EFAULT
defer func() {
f.Close()
if err != nil {
os.Remove(file)
}
}()
if GeneratedHeader != "" {
f.WriteString(GeneratedHeader)
}
fset := token.NewFileSet()
return format.Node(f, fset, ast)
}
// ----------------------------------------------------------------------------
// ASTFile returns AST of a file by its fname.
// If fname is not provided, it returns AST of the default (NOT current) file.
//
// Deprecated: Use pkg.ASTFile instead.
func ASTFile(pkg *Package, fname ...string) *ast.File {
return pkg.ASTFile(fname...)
}
// CommentedASTFile returns commented AST of a file by its fname.
// If fname is not provided, it returns AST of the default (NOT current) file.
//
// Deprecated: Use pkg.CommentedASTFile instead.
func CommentedASTFile(pkg *Package, fname ...string) *printer.CommentedNodes {
return pkg.CommentedASTFile(fname...)
}
// WriteTo writes a file named fname to dst.
// If fname is not provided, it writes the default (NOT current) file.
//
// Deprecated: Use pkg.WriteTo instead.
func WriteTo(dst io.Writer, pkg *Package, fname ...string) (err error) {
return pkg.WriteTo(dst, fname...)
}
// WriteFile writes a file named fname.
// If fname is not provided, it writes the default (NOT current) file.
//
// Deprecated: Use pkg.WriteTo instead.
func WriteFile(file string, pkg *Package, fname ...string) (err error) {
return pkg.WriteFile(file, fname...)
}
// ----------------------------------------------------------------------------