-
Notifications
You must be signed in to change notification settings - Fork 20
/
main.go
499 lines (446 loc) · 13.2 KB
/
main.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 main
import (
"bufio"
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"sort"
"strings"
)
import (
"github.com/pkg/errors"
)
import (
"github.com/dubbogo/tools/constant"
)
const (
GO_FILE_SUFFIX = ".go"
GO_ROOT = "GOROOT"
QUOTATION_MARK = "\""
PATH_SEPARATOR = "/"
IMPORT = "import"
GO_MOD = "go.mod"
)
var (
blankLine bool
currentWorkDir, _ = os.Getwd()
goRoot = os.Getenv(GO_ROOT) + "/src"
endBlocks = []string{"var", "const", "type", "func"}
projectRootPath string
projectName string
goPkgMap = make(map[string]struct{})
outerComments = make([]string, 0)
// record comments between importBlocks and endBlocks
innerComments = make([]string, 0)
ignorePath = []string{".git", ".idea", ".github", ".vscode", "vendor", "swagger", "docs"}
newLine = false
blockCount = 0
)
func init() {
flag.StringVar(&projectRootPath, "path", currentWorkDir, "the path need to be reformatted")
flag.StringVar(&projectName, "module", "", "project name, namely module name in the go.mod")
flag.BoolVar(&blankLine, "bl", true, "if true, it will split different import modules with a blank line")
}
func main() {
fmt.Println("imports-formatter:", constant.Version)
flag.Parse()
var err error
projectName, err = getProjectName(projectRootPath)
if err != nil {
panic(err)
}
err = preProcess(goRoot, goPkgMap)
if err != nil {
panic(err)
}
err = reformatImports(projectRootPath)
if err != nil {
panic(err)
}
}
func getProjectName(path string) (string, error) {
if projectName != "" {
return projectName, nil
}
fileInfos, err := ioutil.ReadDir(path)
if err != nil {
return "", err
}
for _, fileInfo := range fileInfos {
if fileInfo.Name() == GO_MOD {
f, err := os.OpenFile(path+PATH_SEPARATOR+fileInfo.Name(), os.O_RDONLY, 0644)
if err != nil {
return "", err
}
reader := bufio.NewReader(f)
for {
line, _, err := reader.ReadLine()
if err != nil {
return "", err
}
lineStr := strings.TrimSpace(string(line))
if strings.HasPrefix(lineStr, "module") {
return strings.Split(lineStr, " ")[1], nil
}
}
}
}
return "", err
}
func preProcess(path string, goPkgMap map[string]struct{}) error {
fileInfos, err := ioutil.ReadDir(path)
if err != nil {
return errors.WithStack(err)
}
dirs := make([]os.FileInfo, 0)
for _, fileInfo := range fileInfos {
if fileInfo.IsDir() {
dirs = append(dirs, fileInfo)
} else if strings.HasSuffix(fileInfo.Name(), GO_FILE_SUFFIX) {
goPkgMap[strings.TrimPrefix(path, goRoot+"/")] = struct{}{}
}
}
for _, dir := range dirs {
err := preProcess(path+PATH_SEPARATOR+dir.Name(), goPkgMap)
if err != nil {
return errors.WithStack(err)
}
}
return nil
}
func reformatImports(path string) error {
fileInfos, err := ioutil.ReadDir(path)
if err != nil {
return errors.WithStack(err)
}
dirs := make([]os.FileInfo, 0)
for _, fileInfo := range fileInfos {
if fileInfo.IsDir() && !ignore(fileInfo.Name()) {
dirs = append(dirs, fileInfo)
} else if strings.HasSuffix(fileInfo.Name(), GO_FILE_SUFFIX) {
clearData()
newLine = false
err = doReformat(path + PATH_SEPARATOR + fileInfo.Name())
if err != nil {
return errors.WithStack(err)
}
}
}
for _, dir := range dirs {
err := reformatImports(path + "/" + dir.Name())
if err != nil {
return errors.WithStack(err)
}
}
return nil
}
func doReformat(filePath string) error {
f, err := os.OpenFile(filePath, os.O_RDONLY, 0644)
defer func(f *os.File) {
err := f.Close()
if err != nil {
panic(errors.New(filePath + "encounter error:" + err.Error()))
}
}(f)
if err != nil {
return errors.New("open " + filePath + " encounter error:" + err.Error())
}
reader := bufio.NewReader(f)
beginImports := false
endImport := false
output := make([]byte, 0)
// processed import(organization) -> original import packages
rootImports := make(map[string][]string)
internalImports := make(map[string][]string)
thirdImports := make(map[string][]string)
for {
if len(outerComments) > 0 {
for _, c := range outerComments {
output = append(output, []byte(c+"\n")...)
}
outerComments = make([]string, 0)
}
line, _, err := reader.ReadLine()
if err != nil {
if err == io.EOF {
root := len(rootImports)
internal := len(internalImports)
third := len(thirdImports)
if root > 0 && internal > 0 && third > 0 {
blockCount = 3
} else if (root > 0 && internal > 0) || (root > 0 && third > 0) || (internal > 0 && third > 0) {
blockCount = 2
} else if root > 0 || internal > 0 || third > 0 {
blockCount = 1
}
break
}
return errors.New("read line of " + filePath + " encounter error:" + err.Error())
}
if endImport {
output = append(output, line...)
output = append(output, []byte("\n")...)
continue
}
// if import blocks end
for _, block := range endBlocks {
if strings.HasPrefix(string(line), block) {
endImport = true
beginImports = false
newLine = true
output = refreshImports(output, mergeImports(rootImports), false)
output = refreshImports(output, mergeImports(thirdImports), blankLine)
output = refreshImports(output, mergeImports(internalImports), false)
if len(innerComments) > 0 {
for _, c := range innerComments {
output = append(output, []byte(c+"\n")...)
}
}
break
}
}
lineStr := string(line)
if strings.HasPrefix(lineStr, IMPORT) {
beginImports = true
}
orgImportPkg := strings.TrimSpace(lineStr)
// single line comment
if strings.HasPrefix(orgImportPkg, "//") || (strings.HasPrefix(orgImportPkg, "/*") && strings.HasSuffix(orgImportPkg, "*/")) {
if beginImports {
innerComments = append(innerComments, lineStr)
} else {
outerComments = append(outerComments, lineStr)
}
continue
}
// multiple lines comment
if strings.HasPrefix(orgImportPkg, "/*") {
if beginImports {
innerComments = append(innerComments, lineStr)
commentLine, _, err := reader.ReadLine()
commentLineStr := string(commentLine)
for err == nil && !strings.HasSuffix(strings.TrimSpace(commentLineStr), "*/") {
innerComments = append(innerComments, commentLineStr)
commentLine, _, err = reader.ReadLine()
commentLineStr = string(commentLine)
}
if err == nil {
innerComments = append(innerComments, commentLineStr)
} else {
return errors.New("read line of " + filePath + " encounter error:" + err.Error())
}
} else {
outerComments = append(outerComments, lineStr)
commentLine, _, err := reader.ReadLine()
commentLineStr := string(commentLine)
for err == nil && !strings.HasSuffix(strings.TrimSpace(commentLineStr), "*/") {
outerComments = append(outerComments, commentLineStr)
commentLine, _, err = reader.ReadLine()
commentLineStr = string(commentLine)
}
if err == nil {
outerComments = append(outerComments, commentLineStr)
} else {
return errors.New("read line of " + filePath + " encounter error:" + err.Error())
}
}
continue
}
// collect imports
if beginImports && strings.Contains(orgImportPkg, QUOTATION_MARK) {
innerComments = innerComments[:0]
// single line import
if strings.HasPrefix(orgImportPkg, IMPORT+" ") {
orgImportPkg = strings.TrimPrefix(orgImportPkg, IMPORT+" ")
}
importKey := orgImportPkg
// process those imports that has alias
importKey = unwrapImport(importKey)
if _, ok := goPkgMap[importKey]; ok {
// go root import block
cacheImports(rootImports, importKey, []string{orgImportPkg})
} else if importKey == projectName || (strings.HasPrefix(importKey, projectName) && len(importKey) > len(projectName) && importKey[len(projectName)] == '/') {
/**
for project a
****************************
import (
a
a/b
aa
)
****************************
we need to combine a&a/b, and exclude aa.
importKey == projectName is for a
strings.HasPrefix(importKey, projectName) && len(importKey) > len(projectName) && importKey[len(projectName)] == '/' is for a/b
if we simply use strings.HasPrefix(importKey, projectName), it will recognize a&aa as the same project.
*/
// internal imports of the project
cacheImports(internalImports, importKey, []string{orgImportPkg})
} else {
// imports of the third projects
project, importsSegment := "", strings.Split(importKey, "/")
// like google.golang.org/grpc etc.
if len(importsSegment) == 2 {
project = strings.Join(importsSegment[:2], "/")
} else if len(importsSegment) > 2 {
project = strings.Join(importsSegment[:3], "/")
} else {
return errors.New("unexpected import format: " + orgImportPkg + " in file " + filePath)
}
cacheImports(thirdImports, project, []string{orgImportPkg})
}
continue
}
// to process `import (`
if beginImports {
continue
}
output = append(output, line...)
output = append(output, []byte("\n")...)
}
if !endImport {
output = refreshImports(output, mergeImports(rootImports), false)
output = refreshImports(output, mergeImports(thirdImports), blankLine)
output = refreshImports(output, mergeImports(internalImports), false)
if len(innerComments) > 0 {
for _, c := range innerComments {
output = append(output, []byte(c+"\n")...)
}
}
}
outF, err := os.OpenFile(filePath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
if err != nil {
return errors.New("open/create" + filePath + " encounter error:" + err.Error())
}
defer func(outF *os.File) {
err := outF.Close()
if err != nil {
}
}(outF)
writer := bufio.NewWriter(outF)
_, err = writer.Write(output)
if err != nil {
return errors.New("write " + filePath + " encounter error:" + err.Error())
}
err = writer.Flush()
if err != nil {
return errors.New("flush " + filePath + " encounter error:" + err.Error())
}
return nil
}
func unwrapImport(importStr string) string {
// exists alias
if !strings.HasPrefix(importStr, QUOTATION_MARK) {
importStr = strings.Fields(importStr)[1]
}
return strings.Trim(importStr, QUOTATION_MARK)
}
func cacheImports(m map[string][]string, key string, values []string) {
if oldValues, ok := m[key]; ok {
oldValues = append(oldValues, values...)
m[key] = oldValues
} else {
m[key] = values
}
}
func mergeImports(m map[string][]string) map[string][]string {
mergedMap := make(map[string][]string)
for key := range m {
mergedKeys := make([]string, len(m))
newMergedMap := make(map[string][]string)
mergedValues := make([]string, 0)
mergedValues = append(mergedValues, m[key]...)
rootKey := key
for mKey := range mergedMap {
if strings.HasPrefix(rootKey, mKey) {
rootKey = mKey
}
if strings.HasPrefix(key, mKey) || strings.HasPrefix(mKey, key) {
// mKey is a sub package of the module key || key is a sub package of the module mKey
mergedKeys = append(mergedKeys, mKey)
}
}
for mKey, value := range mergedMap {
target := false
for _, mKey1 := range mergedKeys {
if mKey == mKey1 {
target = true
mergedValues = append(mergedValues, mergedMap[mKey]...)
break
}
}
if !target {
cacheImports(newMergedMap, mKey, value)
}
}
cacheImports(newMergedMap, rootKey, mergedValues)
mergedMap = newMergedMap
}
return mergedMap
}
func refreshImports(content []byte, importsMap map[string][]string, blankLine bool) []byte {
if len(importsMap) <= 0 {
return content
}
blockCount--
content = append(content, []byte("import (\n")...)
sortedKeys := make([]string, 0, len(importsMap))
for key := range importsMap {
sortedKeys = append(sortedKeys, key)
}
sort.Strings(sortedKeys)
for idx, key := range sortedKeys {
value := importsMap[key]
content = doRefreshImports(content, value)
if blankLine && idx < len(sortedKeys)-1 {
content = append(content, []byte("\n")...)
}
}
if !newLine && blockCount == 0 && len(innerComments) <= 0 {
content = append(content, []byte(")\n")...)
} else {
content = append(content, []byte(")\n\n")...)
}
return content
}
func doRefreshImports(content []byte, imports []string) []byte {
sort.SliceStable(imports, func(i, j int) bool {
v1 := unwrapImport(imports[i])
v2 := unwrapImport(imports[j])
return v1 < v2
})
for _, rImport := range imports {
content = append(content, []byte("\t"+rImport+"\n")...)
}
return content
}
func clearData() {
innerComments = innerComments[:0]
outerComments = outerComments[:0]
}
func ignore(path string) bool {
for _, name := range ignorePath {
if name == path {
return true
}
}
return false
}