This repository has been archived by the owner on Aug 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
save.go
232 lines (199 loc) · 4.56 KB
/
save.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
package main
import (
"fmt"
"go/build"
"os"
"path"
"path/filepath"
"sort"
"strings"
"time"
"github.com/Masterminds/vcs"
)
var (
excludePath = "example"
)
type context struct {
soFar map[string]bool
ctx build.Context
dir string
}
type GoPackage struct {
Name string
ImportPath string
VcsRepo string
VcsCommand string
Revision string
Hash string
UpdateDate time.Time
Vendored []*VendoredPackage
}
type VendoredPackage struct {
ImportPath string
PkgDir string
}
func save(pkgName, goPath, nixFile string, depsFile string, testImports bool, buildTags []string) error {
pkg, err := NewPackage(pkgName, goPath)
if err != nil {
return err
}
deps, err := findDeps(pkgName, goPath, testImports, buildTags)
if err != nil {
return err
}
pkgsSoFar := make(map[string]bool)
var depsPkgs []*NixDependency
for _, dep := range deps {
p, err := NewPackage(dep, goPath)
if err != nil {
return fmt.Errorf("Can't create package for: %v", dep)
}
if p == nil || p.VcsRepo == pkg.VcsRepo {
continue
}
if !pkgsSoFar[p.ImportPath] {
depsPkgs = append(depsPkgs, &NixDependency{
GoPackagePath: p.ImportPath,
Fetch: &FetchGit{
Type: "git",
Url: p.VcsRepo,
Rev: p.Revision,
Sha256: p.Hash,
},
})
pkgsSoFar[p.ImportPath] = true
}
}
if err := saveDeps(depsPkgs, depsFile); err != nil {
return fmt.Errorf("Error while saving %v: %v", depsFile, err)
}
pkgDef := struct {
Pkg *GoPackage
BuildTags string
Version string
}{pkg, strings.Join(buildTags, ","), version}
if err = writeFromTemplate(nixFile, "default.nix", pkgDef); err != nil {
return fmt.Errorf("Error while writing %v: %v", nixFile, err)
}
return nil
}
func NewPackage(importPath string, goPath string) (*GoPackage, error) {
fullPath, err := goPackageDir(importPath, goPath)
if err != nil {
return nil, err
}
repoRoot, err := repoRoot(fullPath)
if err != nil {
return nil, fmt.Errorf("Cannot find repo root for %v: %v",
fullPath, err)
}
pkgRoot, _ := trimGopath(goPath, repoRoot)
if strings.Contains(pkgRoot, "/vendor/") {
return nil, nil
}
repo, err := vcs.NewRepo("", repoRoot)
if err != nil {
return nil, fmt.Errorf("Error while creating repo for %v: %v",
repoRoot, err)
}
revision, err := repo.Version()
if err != nil {
return nil, err
}
updateDate, err := repo.Date()
if err != nil {
return nil, err
}
return &GoPackage{
Name: nixName(pkgRoot),
ImportPath: pkgRoot,
VcsRepo: repo.Remote(),
VcsCommand: string(repo.Vcs()),
Revision: revision,
Hash: calculateHash("file://"+repo.LocalPath(), string(repo.Vcs())),
UpdateDate: updateDate,
}, nil
}
func goPackageDir(importPath, goPath string) (string, error) {
for _, goPathDir := range filepath.SplitList(goPath) {
expectedPath := filepath.Clean(goPathDir + "/src/" + importPath)
if _, err := os.Stat(expectedPath); err == nil {
return expectedPath, nil
}
}
return "", fmt.Errorf("Cannot find package %v dir in GOPATH: %v",
importPath, goPath)
}
func repoRoot(pth string) (string, error) {
_, err := vcs.DetectVcsFromFS(pth)
if err == vcs.ErrCannotDetectVCS {
if pth == "/" {
return pth, err
}
return repoRoot(path.Dir(pth))
}
if err != nil {
return pth, fmt.Errorf("Error while detecting repo root for %v: %v",
pth, err)
}
return pth, nil
}
func findDeps(name, gopath string, testImports bool, buildTags []string) ([]string, error) {
ctx := build.Default
ctx.BuildTags = buildTags
if gopath != "" {
ctx.GOPATH = gopath
}
c := &context{
soFar: make(map[string]bool),
ctx: ctx,
dir: gopath + "/src/" + name,
}
if err := c.find(name, testImports); err != nil {
return nil, err
}
var deps []string
for p := range c.soFar {
if p != name {
deps = append(deps, p)
}
}
sort.Strings(deps)
return deps, nil
}
func (c *context) find(name string, testImports bool) error {
if name == "C" {
return nil
}
pkg, err := c.ctx.Import(name, c.dir, 0)
if err != nil {
return err
}
if pkg.Goroot {
return nil
}
if name != "." {
c.soFar[pkg.ImportPath] = true
}
if strings.Contains(c.dir, "/vendor") {
return nil
}
imports := pkg.Imports
if testImports {
imports = append(imports, pkg.TestImports...)
}
for _, imp := range imports {
if !c.soFar[imp] {
topDir := c.dir
repoRoot, _ := repoRoot(c.ctx.GOPATH + "/src/" + imp)
if f, err := os.Stat(repoRoot + "/vendor"); err == nil && f.IsDir() {
c.dir = repoRoot + "/vendor"
}
if err := c.find(imp, testImports); err != nil {
return err
}
c.dir = topDir
}
}
return nil
}