-
Notifications
You must be signed in to change notification settings - Fork 1
/
pre.go
104 lines (88 loc) · 2.13 KB
/
pre.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
package prs
import (
"fmt"
"log"
"os"
"path"
"strings"
)
func DiscoverPackages(main string) Packages {
var pathsToLook []string
cwd, err := os.Getwd()
if err != nil {
panic(err)
}
pathsToLook = append(pathsToLook, cwd)
cwdfbd := path.Join(cwd, "fbd")
_, err = os.Stat(cwdfbd)
if err == nil {
pathsToLook = append(pathsToLook, cwdfbd)
}
fbdpath := os.Getenv("FBDPATH")
if len(fbdpath) != 0 {
pathsToLook = append(pathsToLook, strings.Split(fbdpath, ":")...)
}
// TODO: Add support for $HOME/local/.lib/fbd
dbgMsg := fmt.Sprintf("debug: looking for packages in following %d directories:\n", len(pathsToLook))
for _, path := range pathsToLook {
dbgMsg += fmt.Sprintf(" %s\n", path)
}
log.Print(dbgMsg)
packages := make(Packages)
for _, checkPath := range pathsToLook {
content, err := os.ReadDir(checkPath)
if err != nil {
panic(err)
}
for _, c := range content {
pkgPath := path.Join(checkPath, c.Name())
if !c.IsDir() {
fileInfo, err := os.Lstat(pkgPath)
if err != nil {
panic(err)
}
if fileInfo.Mode()&os.ModeSymlink != 0 {
pointee, err := os.Readlink(path.Join(checkPath, c.Name()))
if err != nil {
panic(err)
}
pkgPath = path.Join(checkPath, pointee)
} else {
continue
}
}
pkgName := strings.TrimPrefix(c.Name(), "fbd-")
innerContent, err := os.ReadDir(path.Join(checkPath, c.Name()))
if err != nil {
panic(err)
}
for _, ic := range innerContent {
if ic.IsDir() {
continue
}
fileName := ic.Name()
if strings.HasSuffix(fileName, ".fbd") {
pkg := Package{Name: pkgName, Path: pkgPath}
packages[pkgName] = append(packages[pkgName], &pkg)
break
}
}
}
}
// Add main file.
var tmp []*Package
tmp = append(tmp, &Package{Name: "main", Path: main})
packages["main"] = tmp
pkgsCount := 0
for _, pkgs := range packages {
pkgsCount += len(pkgs)
}
dbgMsg = fmt.Sprintf("debug: found following %d packages:\n", pkgsCount)
for _, pkgs := range packages {
for _, pkg := range pkgs {
dbgMsg += fmt.Sprintf(" %s: %s\n", pkg.Name, pkg.Path)
}
}
log.Print(dbgMsg)
return packages
}