-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgopackage.go
147 lines (114 loc) · 3 KB
/
gopackage.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
package dependency
import (
"go/build"
"os"
"path/filepath"
"strings"
"time"
"srcd.works/go-git.v4/plumbing"
"jrubin.io/zb/lib/zbcontext"
)
type GoPackage struct {
*build.Package
Hash *plumbing.Hash
Path string
ProjectImportPath string
dependencies []Dependency
}
var _ Dependency = (*GoPackage)(nil)
func (pkg *GoPackage) Name() string {
return pkg.Path
}
func (pkg *GoPackage) Build(ctx zbcontext.Context) error {
if !pkg.IsCommand() {
return pkg.Install(ctx)
}
path := pkg.Name()
if rel, err := filepath.Rel(zbcontext.CWD, path); err == nil {
path = rel
}
args := []string{"build"}
args = append(args, pkg.BuildArgs(ctx)...)
args = append(args, "-o", path)
args = append(args, pkg.ImportPath)
if err := ctx.GoExec(args...); err != nil {
return err
}
return ctx.Touch(pkg.Name())
}
func (pkg *GoPackage) BuildArgs(ctx zbcontext.Context) []string {
return ctx.BuildArgs(pkg.Package, pkg.Hash)
}
func (pkg *GoPackage) Install(ctx zbcontext.Context) error {
args := []string{"install"}
args = append(args, pkg.BuildArgs(ctx)...)
args = append(args, pkg.ImportPath)
if err := ctx.GoExec(args...); err != nil {
return err
}
return ctx.Touch(pkg.Name())
}
func (pkg *GoPackage) ModTime() time.Time {
i, err := os.Stat(pkg.Path)
if err != nil {
return time.Time{}
}
return i.ModTime()
}
func (pkg *GoPackage) files(ctx zbcontext.Context) []Dependency {
var files []string
files = append(files, pkg.GoFiles...)
files = append(files, pkg.CgoFiles...)
files = append(files, pkg.CFiles...)
files = append(files, pkg.CXXFiles...)
files = append(files, pkg.MFiles...)
files = append(files, pkg.HFiles...)
files = append(files, pkg.FFiles...)
files = append(files, pkg.SFiles...)
files = append(files, pkg.SwigFiles...)
files = append(files, pkg.SwigCXXFiles...)
files = append(files, pkg.SysoFiles...)
// files = append(files, pkg.TestGoFiles...)
// files = append(files, pkg.XTestGoFiles...)
gofiles := make([]Dependency, len(files))
for i, f := range files {
gofiles[i] = NewGoFile(ctx, pkg, filepath.Join(pkg.Dir, f))
}
return gofiles
}
func (pkg *GoPackage) packages(ctx zbcontext.Context) ([]Dependency, error) {
var pkgs []Dependency
imports := pkg.Imports
for _, i := range imports {
if !strings.Contains(i, ".") {
// skip standard library packages and "C"
continue
}
p, err := ctx.Import(i, pkg.Dir)
if err != nil {
return nil, err
}
pkgs = append(pkgs, &GoPackage{
ProjectImportPath: pkg.ProjectImportPath,
Path: p.PkgObj,
Package: p,
Hash: pkg.Hash,
})
}
return pkgs, nil
}
func (pkg *GoPackage) Buildable() bool {
return true
}
func (pkg *GoPackage) Dependencies(ctx zbcontext.Context) ([]Dependency, error) {
if pkg.dependencies != nil {
return pkg.dependencies, nil
}
pkgs, err := pkg.packages(ctx)
if err != nil {
return nil, err
}
pkg.dependencies = pkgs
pkg.dependencies = append(pkg.dependencies, pkg.files(ctx)...)
return pkg.dependencies, nil
}