-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibList.go
executable file
·459 lines (421 loc) · 13.1 KB
/
libList.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
// libList.go
// Source file auto-generated on Sun, 06 Oct 2019 23:05:32 using Gotk3ObjHandler v1.3.8 ©2018-19 H.F.M
/*
Copyright ©2019 H.F.M - Functions Library Manager
This program comes with absolutely no warranty. See the The MIT License (MIT) for details:
https://opensource.org/licenses/mit-license.php
*/
package main
import (
"bytes"
"encoding/json"
"errors"
"io"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"regexp"
"strings"
glco "github.com/hfmrow/genLib/crypto"
glfssf "github.com/hfmrow/genLib/files/scanFileDir"
gltsgssw "github.com/hfmrow/genLib/tools/goSources/sourceWalker"
)
// declIdx index that point to descriptions, used in treestore to track them.
type declIdx struct {
Idx int
DescPtr *shortDescription
}
// DeclIndexes: Contain all indexes pointing to descriptions.
type DeclIndexes struct {
Indexes []declIdx
Count int
}
// DeclIndxesNew:
func DeclIndxesNew(inDesc shortDescriptions) (di *DeclIndexes) {
di = new(DeclIndexes)
di.init(inDesc)
return
}
// init: build Indexes for all existing declarations.
func (di *DeclIndexes) init(inDesc shortDescriptions) {
for idx, d := range inDesc {
di.Indexes = append(di.Indexes, declIdx{
Idx: d.Idx,
DescPtr: &inDesc[idx]})
for idxM, dM := range d.Methods {
di.Indexes = append(di.Indexes, declIdx{
Idx: dM.Idx,
DescPtr: &d.Methods[idxM]})
}
}
di.Count = len(di.Indexes)
return
}
// GetDecl: Get declaration from index.
func (di *DeclIndexes) GetDescr(index int) (outDesc shortDescription, ok bool) {
for _, d := range di.Indexes {
if d.Idx == index {
return *d.DescPtr, true
}
}
return
}
// LibsInfos: Raw version of the analysed libraries.
type LibsInfos struct {
Shortcut, ImportPath string
Ast *gltsgssw.GoSourceFileStruct
}
// shortDescription: contain description of a declaration (function, method, structure).
type shortDescription struct {
Name string
NameFromSrc string
Shortcut string
File string
LineStart int
LineEnd int
Exported bool
Type string
Methods shortDescriptions
Comment string
Idx int
}
// Description: contain all specified libs that have been analysed.
type Description struct {
Libs []string
LibsMd5 string
ExludedDirs []string
changed bool
Desc shortDescriptions
RootLibs string
}
// shortDescriptions: define a structure to hold multiples descriptions.
type shortDescriptions []shortDescription
// String: function to complies with fuzzy search into structure.
func (e shortDescriptions) String(i int) string {
if len(e[i].NameFromSrc) > 0 {
return e[i].NameFromSrc
}
return e[i].Name
}
// Len: function to complies with fuzzy search into structure.
func (e shortDescriptions) Len() int {
return len(e)
}
// Read: Descriptions from file.
func (stru *Description) Read(filename string) (err error) {
var textFileBytes []byte
if textFileBytes, err = ioutil.ReadFile(filename); err == nil {
if err = json.Unmarshal(textFileBytes, &stru); err == nil {
if md5, err := stru.getMd5Libs(); md5 != stru.LibsMd5 {
if err != nil {
Logger.Log(err, "Read: Descriptions from file")
}
Logger.Log(err, "Some files in the library path have been modified\nBuilding a new AST data file ...")
stru.changed = true
}
}
}
return
}
// Write: Descriptions to file.
func (stru *Description) Write(filename string) (err error) {
var jsonData []byte
var out bytes.Buffer
if jsonData, err = json.Marshal(&stru); err == nil {
if err = json.Indent(&out, jsonData, "", "\t"); err == nil {
err = ioutil.WriteFile(filename, out.Bytes(), os.ModePerm)
}
}
Logger.Log(err, "Error while writing AST data file", filename)
return err
}
// initSourceLibs: Initiate and store the contents of the libraries
// or check if the files have been modified since the last time.
func initSourceLibs(src, srcToSkip []libs) (err error) {
var sources, subDirToSkip []string
for _, lib := range src {
if lib.Active {
sources = append(sources, lib.Path)
}
}
for _, lib := range srcToSkip {
if lib.Active {
subDirToSkip = append(subDirToSkip, lib.Path)
}
}
// Reset all if the is no library to scan
if len(sources) == 0 {
desc.Desc = desc.Desc[:0]
tvsTreeSearch.Clear()
return
}
// Compute description filename
descFilename := filepath.Join(filepath.Dir(optFilename), mainOptions.LastDescFilename)
// Try to read description file
if err = desc.Read(descFilename); err != nil || len(desc.Desc) == 0 {
desc.changed = true
}
// Compare libraries requested with saved ones.
if !reflect.DeepEqual(desc.Libs, sources) ||
!reflect.DeepEqual(desc.ExludedDirs, subDirToSkip) {
desc.changed = true
}
// If one of the two previous test fail, create new description file.
if desc.changed {
desc = Description{}
if len(sources) > 0 {
if sourcesFromAst, desc.LibsMd5, err = buildLibList(sources, append(subDirToSkip, mainOptions.DefaultExclude...)); err == nil {
var globalIdx int
for _, sfa := range sourcesFromAst {
for _, f := range sfa.Ast.Func {
desc.Desc = append(desc.Desc, shortDescription{
Name: f.Ident.Name,
NameFromSrc: "",
Shortcut: sfa.Shortcut,
File: f.File,
LineStart: f.Content.LineStart,
LineEnd: f.Content.LineEnd,
Exported: f.Exported,
Type: "func",
Comment: f.Content.Comment,
Idx: globalIdx})
globalIdx++
}
for _, f := range sfa.Ast.Struct {
var methods shortDescriptions
for _, m := range f.Methods { // Get methods.
methods = append(methods, shortDescription{
Name: m.Ident.Name,
NameFromSrc: f.Ident.Name,
File: m.File,
LineStart: m.Content.LineStart,
LineEnd: m.Content.LineEnd,
Exported: m.Exported,
Type: "method",
Comment: m.Content.Comment,
Idx: globalIdx})
globalIdx++
}
desc.Desc = append(desc.Desc, shortDescription{
Name: f.Ident.Name,
NameFromSrc: "",
Shortcut: sfa.Shortcut,
File: f.File,
LineStart: f.Content.LineStart,
LineEnd: f.Content.LineEnd,
Exported: f.Exported,
Type: "struct",
Methods: methods,
Comment: f.Content.Comment,
Idx: globalIdx})
globalIdx++
}
}
}
if err == io.EOF {
return
}
if err == nil {
desc.Libs = sources
desc.ExludedDirs = subDirToSkip
err = desc.Write(descFilename)
Logger.Log(err, "initSourceLibs/Write")
}
} else {
err = errors.New("There is no library to explore ...")
return
}
}
declIdexes = DeclIndxesNew(desc.Desc)
Logger.Log(err, "Error while reading AST data file. Building a new one ...")
return
}
// IsDirOrSymlinkDir: File is a directory or a symlinked directory ?
func IsDirOrSymlinkDir(slRoot string, slStat os.FileInfo) (slIsDir bool) {
var err error
var fName string
if slStat.IsDir() {
return true
} else if slStat.Mode()&os.ModeSymlink != 0 {
if fName, err = os.Readlink(filepath.Join(slRoot, slStat.Name())); err == nil {
if slStat, err = os.Stat(fName); err == nil {
if slStat.IsDir() {
return true
}
}
}
}
Logger.Log(err, "Unable to scan", fName)
return
}
// The purpose of this function is to generate a list of
// libraries contained in a specific directory and
// creating shortcut name to access them.
func buildLibList(sources, subDirToSkip []string) (sourcesFromAst []LibsInfos, md5 string, err error) {
var root string
var existing []string
var data []byte
if len(sources) > 0 {
for idx := 0; idx < len(sources); idx++ {
desc.RootLibs = filepath.Join(os.Getenv("GOPATH"), "src")
root = filepath.Join(desc.RootLibs, strings.TrimSpace(sources[idx]))
if _, err = os.Stat(root); err == nil {
rootPath := splitPath(root)
if err = filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err == nil {
if IsDirOrSymlinkDir(filepath.Dir(path), info) { // Is dir.
for _, toSkip := range subDirToSkip { // Skip unwanted directories.
if info.Name() == strings.TrimSpace(filepath.Base(toSkip)) {
return filepath.SkipDir
}
}
var infosFiles []os.FileInfo
// Scan files inside directory
if infosFiles, err = glfssf.ScanDirFileInfo(path); err != nil {
return err
}
// Get ast infos for each files.
gsfs, _ := gltsgssw.GoSourceFileStructNew()
for idx, osFile := range infosFiles {
if !osFile.IsDir() {
if filename := filepath.Join(path, osFile.Name()); filepath.Ext(filename) == ".go" /*|| filepath.Ext(filename) == ".c" || filepath.Ext(filename) == ".h"*/ {
if idx == 0 {
if err = gsfs.GoSourceFileStructureSetup(filename); err != nil {
return err
}
} else {
if err = gsfs.AppendFile(filename); err != nil {
return err
}
}
// Create md5 (same as "getMd5Libs" function) included here
// since when collecting informations we walk files too.
if data, err = ioutil.ReadFile(filename); err == nil {
md5 += glco.Md5String(string(data))
}
}
}
}
// Build shortcut and libs path (import style).
shortcut := computeShort(rootPath, splitPath(path), existing)
libInf := LibsInfos{
Ast: gsfs,
Shortcut: shortcut,
ImportPath: filepath.Join(removePathBefore(splitPath(path), "src", true)...),
}
sourcesFromAst = append(sourcesFromAst, libInf)
existing = append(existing, libInf.Shortcut)
return nil
}
}
return err
}); err != nil { // issue with formatted source
return
}
}
}
} else {
DlgErr(sts["missing"], errors.New(sts["noLibsToScan"]))
}
return sourcesFromAst, glco.Md5String(md5), err // generate global md5.
}
// getMd5Libs: Used to control integrity of already saved informations.
func (stru *Description) getMd5Libs() (md5 string, err error) {
var root string
var infosFiles []os.FileInfo
var data []byte
if len(stru.Libs) > 0 {
for idx := 0; idx < len(stru.Libs); idx++ {
root = filepath.Join(os.Getenv("GOPATH"), "src", stru.Libs[idx])
if _, err = os.Stat(root); err == nil {
err = filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err == nil {
if info.IsDir() { // Is dir
for _, toSkip := range stru.ExludedDirs { // Skip unwanted directories.
if info.Name() == toSkip {
return filepath.SkipDir
}
}
// Scan for "*.go" files inside directory.
if infosFiles, err = glfssf.ScanDirFileInfo(path); err != nil {
Logger.Log(err, "getMd5Libs/Walk/ScanDirFileInfo")
return err
}
for _, osFile := range infosFiles {
if !osFile.IsDir() && osFile.Mode()&os.ModeSymlink == 0 { // not dir & not symlink ?
if filename := filepath.Join(path, osFile.Name()); filepath.Ext(filename) == ".go" { // Is *.go file
if data, err = ioutil.ReadFile(filename); err == nil {
md5 += glco.Md5String(string(data)) // Concatenate md5 of each files.
}
}
}
}
return nil
}
}
return err
})
}
}
} else {
err = errors.New("Missing directories to be analysed ...")
}
return glco.Md5String(md5), err // generate global md5.
}
// computeShort: Create shortcut name for the library
func computeShort(rootPath, newPath, existing []string) (outShortCut string) {
unableBuildShortCut := "Error"
upperChar := regexp.MustCompile(`([[:upper:]])`)
newPath = removePathBefore(newPath, rootPath[len(rootPath)-1])
for _, name := range newPath {
outShortCut += name[:1]
// Search for uppercase character in name, if it found, it
// included in short name rather than the last char of the name.
if found := upperChar.FindAllString(name, 1); len(found) > 0 {
outShortCut += found[0]
} else {
outShortCut += name[len(name)-1:]
}
outShortCut = strings.ToLower(outShortCut)
}
// Search for duplicate shortcut and compute a new one if found.
subChar := 0
name := newPath[len(newPath)-1]
for _, existingName := range existing {
if outShortCut == existingName && !(outShortCut == unableBuildShortCut) {
subChar++
outShortCut = outShortCut[:len(outShortCut)-1]
if a, b := len(name)-(1+subChar), len(name)-subChar; a >= 0 && b < len(name) {
outShortCut += name[a:b]
} else {
outShortCut = unableBuildShortCut
}
}
}
return
}
// splitPath: make a slice from a string path.
func splitPath(path string) (outSlice []string) {
// remove leading and ending PathSeparator.
path = strings.Trim(path, string(os.PathSeparator))
return strings.Split(path, string(os.PathSeparator))
}
// removePathBefore: remove directories before or after the chosen one.
func removePathBefore(path []string, at string, after ...bool) []string {
var afterMark bool
if len(after) > 0 {
afterMark = after[0]
}
for idx := len(path) - 1; idx >= 0; idx-- {
if path[idx] == at {
if afterMark {
path = path[idx+1:]
} else {
path = path[idx:]
}
break
}
}
return path
}