-
Notifications
You must be signed in to change notification settings - Fork 12
/
flat.go
274 lines (247 loc) · 6.33 KB
/
flat.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
package main
import (
"bufio"
"errors"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"time"
"gopkg.in/natefinch/lumberjack.v2"
)
var pragma string
type importRec struct {
FullPath string
Code []string
Uses map[string]bool
Created bool // has been depended on or processed
Processed bool // has been processed for includes
Resolved bool // all includes have been resolved
Written bool // written out
}
var imports map[string]importRec
func loadAndSplitFile(fileName string) (newFiles bool, err error) {
fileName = strings.Replace(fileName, "\"", "", 2) // remove quotes
fileName = strings.Replace(fileName, ";", "", 2) // and final semicolon
fileName = strings.TrimSpace(filepath.Clean(fileName)) // resolve ../ etc
thisPath := filepath.Dir(fileName)
fmt.Println("++++", thisPath)
_, err = os.Stat(thisPath)
if err != nil {
fmt.Println("err ", thisPath)
node, err := doWeHaveNodeModule(thisPath)
if err == nil {
fmt.Println("using ", node+"/node_modules/"+thisPath)
thisPath = node + "/node_modules/" + thisPath
fileName = node + "/node_modules/" + fileName
}
}
shortName := filepath.Base(fileName) // just the file name
if imports[shortName].Processed {
fmt.Println(shortName, " already done")
return
}
thisRec := importRec{FullPath: fileName, Created: true, Uses: make(map[string]bool)}
data, err := ioutil.ReadFile(fileName)
if err != nil {
if fileName[:1] == "." {
fmt.Println(fileName, err)
return
}
fileName = filepath.Clean("./" + fileName)
data, err = ioutil.ReadFile(fileName)
if err != nil {
dir, newerr := os.Getwd()
if newerr != nil {
log.Fatal(err)
}
fmt.Println(dir)
fmt.Println("*", "["+fileName+"]", err)
return
}
}
fmt.Println("Processing ", shortName)
contents := string(data)
lines := strings.Split(contents, "\n")
if len(pragma) == 0 {
pragma = lines[0]
}
noImports := true
for li, line := range lines {
if li == 0 {
continue // skip "pragma solidity ^0.4.x;"
}
if starts("import", line) {
noImports = false
afterImport := after("import ", line)
if afterImport[0] == '@' {
if !imports[afterImport].Created {
newFiles = true
imports[afterImport] = importRec{FullPath: afterImport,
Created: true,
Uses: make(map[string]bool)}
fmt.Println("--> uses new file ", afterImport)
}
thisRec.Uses[afterImport] = true
} else {
afta := thisPath + "/" + afterImport
afta = filepath.Clean(afta)
bafta := filepath.Base(afta)
if !imports[bafta].Created {
newFiles = true
imports[bafta] = importRec{FullPath: afta,
Created: true,
Uses: make(map[string]bool)}
fmt.Println("--> uses new file ", afta)
}
thisRec.Uses[bafta] = true
}
}
if starts("contract", line) || starts("library", line) || starts("interface", line) || starts("abstract contract", line) {
thisRec.Code = lines[li:]
fmt.Println("has ", len(lines[li:]), " lines")
break
}
}
thisRec.Processed = true
imports[shortName] = thisRec
thisRec.Resolved = noImports
if noImports {
fmt.Println(shortName, " has no dependencies")
}
return
}
func findCurrentParent() (string, error) {
cwd, err := os.Getwd()
if err != nil {
return "", err
}
return filepath.Dir(cwd), nil
}
func findDirectoryParent(cwd string) string {
return filepath.Dir(cwd)
}
func doWeHaveNodeModule(path string) (dir string, err error) {
start, err := os.Getwd()
if err != nil {
return "", err
}
for {
nodeDir := start + "/node_modules/" + path
f, err := os.Stat(nodeDir)
if err != nil {
start = findDirectoryParent(start)
} else if f.IsDir() {
return start, nil
}
if len(start) < 3 {
return "", errors.New("cannot find node")
}
}
}
var fName string
var oName string
func main() {
fmt.Println("Solididy File Flattener 1.20 (c) David Appleton 2018 to 2020 and beyond")
fmt.Println("contact : [email protected]")
fmt.Println("https://github.com/DaveAppleton/SolidityFlattery")
fmt.Println("released under Apache 2.0 licence")
flag.StringVar(&fName, "input", "", "base file to flatten")
flag.StringVar(&oName, "output", "", "output file")
flag.Parse()
if len(fName) == 0 {
flag.Usage()
os.Exit(0)
}
if len(oName) == 0 {
flag.Usage()
os.Exit(0)
}
if _, err := os.Stat(oName + ".sol"); err == nil {
fmt.Println("error : ", oName+".sol already exists")
fmt.Println("we can't have you accidentally deleting files!!!")
os.Exit(1)
}
log.SetOutput(&lumberjack.Logger{
Filename: "./" + oName + ".log",
MaxSize: 500, // megabytes
MaxBackups: 3,
MaxAge: 28, //days
})
oName += ".sol"
fmt.Println("ANALYSIS")
imports = make(map[string]importRec)
f, _ := os.Create(oName)
defer f.Close()
w := bufio.NewWriter(f)
log.Printf("pre processing %s\n", fName)
newFiles, err := loadAndSplitFile(fName)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// keep processing dependent files
for {
repeat := false
for _, iRec := range imports {
if iRec.Processed {
continue
}
log.Printf("pre processing %s\n", iRec.FullPath)
newFiles, err = loadAndSplitFile(iRec.FullPath)
repeat = repeat || newFiles
}
if !repeat {
break
}
}
absoluteFile, _ := filepath.Abs(fName)
fmt.Fprintln(w, pragma)
fmt.Fprintln(w, "// produced by the Solididy File Flattener (c) David Appleton 2018 - 2020 and beyond")
fmt.Fprintln(w, "// contact : [email protected]")
fmt.Fprintln(w, "// source : https://github.com/DaveAppleton/SolidityFlattery")
fmt.Fprintln(w, "// released under Apache 2.0 licence")
fmt.Fprintln(w, "// input ", absoluteFile)
fmt.Fprintln(w, "// flattened : ", time.Now().UTC().Format(time.RFC850))
fmt.Println("Writing output.")
for {
completed := true
for key, mp := range imports {
if mp.Written {
continue
}
completed = false
if mp.Resolved {
count := 0
for _, line := range mp.Code {
fmt.Fprintln(w, line)
count++
}
mp.Written = true
imports[key] = mp
log.Printf("Written %s (%d) lines\n", key, count)
continue
}
amResolved := true
for k2, _ := range mp.Uses {
if !imports[filepath.Base(k2)].Written {
amResolved = false
}
}
if amResolved {
mp.Resolved = true
imports[key] = mp
log.Println("Resolved ", key)
continue
}
log.Println(key, "remains unresolved")
}
if completed {
break
}
}
err = w.Flush()
}