forked from tools/godep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
save.go
210 lines (193 loc) · 4.9 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
package main
import (
"errors"
"github.com/kr/fs"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"sort"
"strings"
)
var cmdSave = &Command{
Usage: "save [-copy] [packages]",
Short: "list current dependencies to a file",
Long: `
Save writes a list of the dependencies of the named packages along
with the exact source control revision of each dependency. Output
is a JSON document with the following structure:
type Godeps struct {
ImportPath string
GoVersion string // Abridged output of 'go version'.
Packages []string // Arguments to godep save, if any.
Deps []struct {
ImportPath string
Comment string // Tag or description of commit.
Rev string // VCS-specific commit ID.
}
}
If flag -copy is given, the list is written to Godeps/Godeps.json,
and source code for all dependencies is copied into Godeps.
Otherwise, the list alone is written to file Godeps.
For more about specifying packages, see 'go help packages'.
`,
Run: runSave,
}
var flagCopy bool
func init() {
cmdSave.Flag.BoolVar(&flagCopy, "copy", false, "copy source code")
}
func runSave(cmd *Command, args []string) {
// Remove Godeps before listing packages, so that args
// such as ./... don't match anything in there.
if err := os.RemoveAll("Godeps"); err != nil {
log.Fatalln(err)
}
g := &Godeps{
ImportPath: MustLoadPackages(".")[0].ImportPath,
GoVersion: mustGoVersion(),
}
if len(args) > 0 {
g.Packages = args
} else {
args = []string{"."}
}
a := MustLoadPackages(args...)
err := g.Load(a)
if err != nil {
log.Fatalln(err)
}
if a := badSandboxVCS(g.Deps); a != nil && !flagCopy {
log.Println("Unsupported sandbox VCS:", strings.Join(a, ", "))
log.Printf("Instead, run: godep save -copy %s", strings.Join(args, " "))
os.Exit(1)
}
if g.Deps == nil {
g.Deps = make([]Dependency, 0) // produce json [], not null
}
manifest := "Godeps"
if flagCopy {
manifest = filepath.Join("Godeps", "Godeps.json")
// We use a name starting with "_" so the go tool
// ignores this directory when traversing packages
// starting at the project's root. For example,
// godep go list ./...
workspace := filepath.Join("Godeps", "_workspace")
err = copySrc(workspace, g)
if err != nil {
log.Fatalln(err)
}
path := filepath.Join("Godeps", "Readme")
err = writeFile(path, strings.TrimSpace(Readme)+"\n")
if err != nil {
log.Println(err)
}
writeVCSIgnore(workspace)
}
f, err := os.Create(manifest)
if err != nil {
log.Fatalln(err)
}
_, err = g.WriteTo(f)
if err != nil {
log.Fatalln(err)
}
err = f.Close()
if err != nil {
log.Fatalln(err)
}
}
// badSandboxVCS returns a list of VCSes that don't work
// with the `godep go` sandbox code.
func badSandboxVCS(deps []Dependency) (a []string) {
for _, d := range deps {
if d.vcs.CreateCmd == "" {
a = append(a, d.vcs.vcs.Name)
}
}
sort.Strings(a)
return uniq(a)
}
func copySrc(dir string, g *Godeps) error {
ok := true
for _, dep := range g.Deps {
w := fs.Walk(dep.pkg.Dir)
for w.Step() {
if w.Err() != nil {
log.Println(w.Err())
ok = false
continue
}
if c := w.Stat().Name()[0]; c == '.' || c == '_' {
// Skip directories using a rule similar to how
// the go tool enumerates packages.
// See $GOROOT/src/cmd/go/main.go:/matchPackagesInFs
w.SkipDir()
}
if w.Stat().IsDir() {
continue
}
dst := filepath.Join(dir, w.Path()[len(dep.pkg.Root)+1:])
if err := copyFile(dst, w.Path()); err != nil {
log.Println(err)
ok = false
}
}
}
if !ok {
return errors.New("error copying source code")
}
return nil
}
// copyFile copies a regular file from src to dst.
// dst is opened with os.Create.
func copyFile(dst, src string) error {
r, err := os.Open(src)
if err != nil {
return err
}
defer r.Close()
err = os.MkdirAll(filepath.Dir(dst), 0777)
if err != nil {
return err
}
w, err := os.Create(dst)
if err != nil {
return err
}
_, err = io.Copy(w, r)
err1 := w.Close()
if err == nil {
err = err1
}
return err
}
// Func writeVCSIgnore writes "ignore" files inside dir for known VCSs,
// so that dir/pkg and dir/bin don't accidentally get committed.
// It logs any errors it encounters.
func writeVCSIgnore(dir string) {
// Currently git is the only VCS for which we know how to do this.
// Mercurial and Bazaar have similar mechasims, but they apparently
// require writing files outside of dir.
const ignore = "/pkg\n/bin\n"
name := filepath.Join(dir, ".gitignore")
err := writeFile(name, ignore)
if err != nil {
log.Println(err)
}
}
// writeFile is like ioutil.WriteFile but it creates
// intermediate directories with os.MkdirAll.
func writeFile(name, body string) error {
err := os.MkdirAll(filepath.Dir(name), 0777)
if err != nil {
return err
}
return ioutil.WriteFile(name, []byte(body), 0666)
}
const Readme = `
This directory tree is generated automatically by godep.
Please do not edit.
See https://github.com/kr/godep for more information.
`