-
Notifications
You must be signed in to change notification settings - Fork 13
/
compile.go
104 lines (89 loc) · 1.97 KB
/
compile.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 main
import (
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"path"
"strings"
"time"
"github.com/straightdave/lesphina/v2"
)
const (
defaultTimeout = 60 * time.Second
)
func mustCompileDir(dir string, mock, verbose bool) {
if err := compileDir(dir, mock, verbose); err != nil {
log.Fatal(err)
}
}
func compileDir(dir string, mock, verbose bool) error {
_, err := exec.LookPath("go")
if err != nil {
log.Println(yellow("Go compiler seems not installed. Please check your Go toolchain."))
return err
}
cDir, err := os.Getwd()
if err != nil {
log.Println("failed to get working dir:", err.Error())
return err
}
if !path.IsAbs(dir) {
dir = path.Join(cDir, dir)
}
// change dir
log.Println("change dir to", dir)
err = os.Chdir(dir)
if err != nil {
log.Println("failed to change dir:", err.Error())
return err
}
// change dir back
defer func() {
log.Println("change dir back to", cDir)
err = os.Chdir(cDir)
if err != nil {
log.Println("failed to change dir:", err.Error())
}
}()
log.Println("Build ...")
raw, err := exec.Command("go", "build").CombinedOutput()
fmt.Println(string(raw))
return err
}
func listDepsOfCurrentPackage2() ([]string, error) {
cdir, err := os.Getwd()
if err != nil {
return nil, err
}
files, err := ioutil.ReadDir(cdir)
if err != nil {
return nil, err
}
resMap := make(map[string]bool)
for _, f := range files {
debug("analyzing file: %s\n", f.Name())
if !f.IsDir() && strings.HasSuffix(f.Name(), ".go") {
l, err := lesphina.Read(f.Name())
if err != nil {
debug("failed to analyze file: %s\n", f.Name())
return nil, err
}
for _, i := range l.Meta.Imports {
path := strings.Trim(i.Name, ` "`)
spl := strings.Split(path, `/`)
if len(spl) > 1 && strings.Contains(spl[0], `.`) {
// possibly a non-built-in packages
debug("dep: %v\n", path)
resMap[path] = true
}
}
}
}
var res []string
for key := range resMap {
res = append(res, key)
}
return res, nil
}