/
format.go
169 lines (154 loc) · 3.88 KB
/
format.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
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright 2018 henrylee2cn. All Rights Reserved.
//
// 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 aster
import (
"bytes"
"fmt"
"go/ast"
"go/format"
"os"
"path/filepath"
"github.com/henrylee2cn/goutil"
)
// Store formats the module codes and writes to the local files.
func (m *Module) Store() (first error) {
codes, first := m.Format()
if first != nil {
return first
}
for _, v := range codes {
for kk, vv := range v {
first = writeFile(kk, vv)
if first != nil {
return first
}
}
}
return
}
// Store formats the package codes and writes to the local files.
func (p *Package) Store() (first error) {
codes, first := p.Format()
if first != nil {
return
}
for k, v := range codes {
first = writeFile(k, v)
if first != nil {
return first
}
}
return
}
// Store formats the file codes and writes to the local file.
func (f *File) Store() (err error) {
code, err := f.Format()
if err != nil {
return
}
return writeFile(f.Filename, code)
}
// Format format the package and returns the string.
// @codes <packageName,<fileName,code>>
func (m *Module) Format() (codes map[string]map[string]string, first error) {
codes = make(map[string]map[string]string, len(m.Packages))
for k, v := range m.Packages {
subcodes, err := v.Format()
if err != nil {
first = err
return
}
codes[k] = subcodes
}
return
}
// Format format the package and returns the string.
// @codes <fileName,code>
func (p *Package) Format() (codes map[string]string, first error) {
codes = make(map[string]string, len(p.Files))
var code string
for k, v := range p.Files {
code, first = v.Format()
if first != nil {
return
}
codes[k] = code
}
return
}
// Format formats the file and returns the string.
func (f *File) Format() (string, error) {
return f.FormatNode(f.File)
}
// String returns the formated file text.
func (f *File) String() string {
s, err := f.Format()
if err != nil {
return fmt.Sprintf("// Formatting error: %s", err.Error())
}
return s
}
// FormatNode formats the node and returns the string.
func (m *Module) FormatNode(node ast.Node) (string, error) {
var dst bytes.Buffer
err := format.Node(&dst, m.FileSet, node)
if err != nil {
return "", err
}
return goutil.BytesToString(dst.Bytes()), nil
}
// FormatNode formats the node and returns the string.
func (p *Package) FormatNode(node ast.Node) (string, error) {
var dst bytes.Buffer
err := format.Node(&dst, p.FileSet, node)
if err != nil {
return "", err
}
return goutil.BytesToString(dst.Bytes()), nil
}
// FormatNode formats the node and returns the string.
func (f *File) FormatNode(node ast.Node) (string, error) {
var dst bytes.Buffer
err := format.Node(&dst, f.FileSet, node)
if err != nil {
return "", err
}
return goutil.BytesToString(dst.Bytes()), nil
}
// TryFormatNode formats the node and returns the string,
// returns the default string if fail.
func (f *File) TryFormatNode(node ast.Node, defaultValue ...string) string {
code, err := f.FormatNode(node)
if err != nil && len(defaultValue) > 0 {
return defaultValue[0]
}
return code
}
func writeFile(filename, text string) error {
filename, err := filepath.Abs(filename)
if err != nil {
return err
}
dir := filepath.Dir(filename)
err = os.MkdirAll(dir, 0777)
if err != nil {
return err
}
f, err := os.Create(filename)
if err != nil {
return err
}
_, err = f.Write(goutil.StringToBytes(text))
return err
}