-
Notifications
You must be signed in to change notification settings - Fork 0
/
concat.go
231 lines (189 loc) · 4.9 KB
/
concat.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
package goconcat
import (
"bytes"
"fmt"
"go/ast"
"go/format"
"go/token"
"io/ioutil"
"log"
"os"
"regexp"
"strings"
"github.com/pkg/errors"
)
func GoConcat(options *Options) error {
err := validateOptions(options)
if err != nil {
log.Println(err)
return errors.WithStack(err)
}
filePaths, err := GetFilePaths(options)
if err != nil {
log.Println(err)
return errors.WithStack(err)
}
filesToConcat, fileSet, err := ParseASTFiles(filePaths)
if err != nil {
log.Println(err)
return errors.WithStack(err)
}
filesToSort, err := getFilesToSort(filesToConcat, options, fileSet)
if err != nil {
log.Println(err)
return errors.WithStack(err)
}
for _, file := range filesToSort {
originialPackage := file.Name.Name
destinationPath := anyToString(options.Destination)
if destinationPath == rootDirectory {
destinationPath = goconcat
}
var pkgName string
if splitStrings := strings.Split(destinationPath, "/"); len(splitStrings) > 0 {
pkgName = splitStrings[len(splitStrings)-1]
} else {
pkgName = destinationPath
}
file.Name.Name = pkgName
var buf bytes.Buffer
if err := format.Node(&buf, fileSet, file); err != nil {
log.Println(err)
return errors.WithStack(err)
}
isValid := destinationDirIsValid(options.RootPath, destinationPath)
if !isValid {
if err := os.Mkdir(destinationPath, os.ModePerm); err != nil {
log.Println(err)
return errors.WithStack(err)
}
}
var fileName string
if !options.SplitFilesByPackage {
fileName = goconcat
} else {
fileName = originialPackage
}
finalPath := getDestinationPath(destinationPath, file.Name.Name, FileGo, options, filePaths, fileName)
if err := ioutil.WriteFile(finalPath, buf.Bytes(), os.ModePerm); err != nil {
log.Println(err)
return errors.WithStack(err)
}
}
if options.DeleteOldFiles {
if err := DeleteFiles(filePaths); err != nil {
log.Println(err)
return errors.WithStack(err)
}
}
return nil
}
func getDestinationPath(
destination string,
packageName string,
fileType FileType,
options *Options,
filePaths []string,
fileName string,
) string {
file := anyToString(fileType)
if options.MockeryDestination {
findPackage := regexp.MustCompile(packageName)
var splitPath []string
for _, path := range filePaths {
if findPackage.Match([]byte(path)) {
splitPath = strings.Split(path, packageName)
}
}
if splitPath[0] == "" {
return packageName + "/" + fmt.Sprintf("mocks_%s", packageName) + file
}
return splitPath[0] + packageName + "/" + fmt.Sprintf("mocks_%s", packageName) + file
}
if options.SplitFilesByPackage {
return "./" + destination + "/" + fileName + file
}
return "./" + destination + "/" + packageName + file
}
func validateOptions(options *Options) error {
if options.RootPath == "" {
return errors.WithStack(errNoRootPath)
}
if options.FilePrefix != nil && len(options.FilePrefix) < 1 {
return errors.WithStack(errNoPrefix)
}
return nil
}
func concatImports(targetFile *ast.File, fileSet *token.FileSet, importStrings []string) {
existingImports := make(map[string]string)
for _, v := range targetFile.Imports {
existingImports[v.Path.Value] = v.Path.Value
}
for _, importString := range importStrings {
// skip import if it already exists
if _, ok := existingImports[importString]; ok {
continue
}
addImportToTargetFile(targetFile, importString)
}
ast.SortImports(fileSet, targetFile)
}
func addImportToTargetFile(targetFile *ast.File, target string) {
for _, decl := range targetFile.Decls {
switch decl.(type) {
case *ast.GenDecl:
genDecl := decl.(*ast.GenDecl)
if genDecl.Tok == token.IMPORT {
spec := &ast.ImportSpec{
Path: &ast.BasicLit{
Value: target,
},
}
genDecl.Specs = append(genDecl.Specs, spec)
}
}
}
}
func getSpecsAndIndices(file *ast.File, tok token.Token) (specs []ast.Spec, declIndex []int) {
for index, decl := range file.Decls {
switch decl.(type) {
case *ast.GenDecl:
genDecl := decl.(*ast.GenDecl)
if genDecl.Tok == tok {
declIndex = append(declIndex, index)
specs = append(specs, genDecl.Specs...)
}
}
}
return specs, declIndex
}
func concatSpecs(file *ast.File, specs []ast.Spec, tok token.Token) {
for _, decl := range file.Decls {
switch decl.(type) {
case *ast.GenDecl:
genDecl := decl.(*ast.GenDecl)
if genDecl.Tok == tok {
genDecl.Specs = append(genDecl.Specs, specs...)
}
}
}
}
// removes decl from ast file
func removeDecl(file *ast.File, indices []int) {
// get the first index of decl as the base decl
indices = removeFromSlice(indices, 0)
file.Decls = returnAllButIndices(file.Decls, indices)
}
func getFuncDeclFromFiles(files []*ast.File) []ast.Decl {
var funcs []ast.Decl
for _, file := range files {
for _, decl := range file.Decls {
switch decl.(type) {
case *ast.FuncDecl:
funcDecl := decl.(*ast.FuncDecl)
funcs = append(funcs, funcDecl)
}
}
}
return funcs
}