forked from gpmgo/gopm
/
gopath.go
293 lines (257 loc) · 7.21 KB
/
gopath.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// Copyright 2013-2014 gopm authors.
//
// Licensed under the Apache License, Version 2.0 (the "License"): you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
package cmd
import (
"errors"
"go/build"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"github.com/Unknwon/com"
"github.com/codegangsta/cli"
"github.com/gpmgo/gopm/doc"
"github.com/gpmgo/gopm/log"
)
var isWindowsXP = false
func getGopmPkgs(dirPath string, isTest bool) (pkgs map[string]*doc.Pkg, err error) {
absPath, err := filepath.Abs(dirPath)
if err != nil {
log.Error("", "Fail to get absolute path of work directory:")
log.Fatal("", "\t"+err.Error())
}
var builds map[string]string
if com.IsFile(absPath + "/" + doc.GOPM_FILE_NAME) {
gf := doc.NewGopmfile(absPath)
if builds, err = gf.GetSection("deps"); err != nil {
builds = nil
}
}
imports := doc.GetAllImports([]string{dirPath}, ".", false, false)
pkgs = make(map[string]*doc.Pkg)
for _, name := range imports {
if name == "C" {
continue
}
if !doc.IsGoRepoPath(name) {
if builds != nil {
if info, ok := builds[name]; ok {
// Check version. there should chek
// local first because d:\ contains :
if com.IsDir(info) {
pkgs[name] = &doc.Pkg{
ImportPath: name,
Type: doc.LOCAL,
Value: info,
}
continue
} else if i := strings.Index(info, ":"); i > -1 {
pkgs[name] = &doc.Pkg{
ImportPath: name,
Type: info[:i],
Value: info[i+1:],
}
continue
}
}
}
pkgs[name] = doc.NewDefaultPkg(name)
}
}
return pkgs, nil
}
func pkgInCache(name string, cachePkgs map[string]*doc.Pkg) bool {
_, ok := cachePkgs[name]
return ok
}
func autoLink(oldPath, newPath string) error {
newPPath, _ := filepath.Split(newPath)
os.MkdirAll(newPPath, os.ModePerm)
return makeLink(oldPath, newPath)
}
func getChildPkgs(ctx *cli.Context, cpath string, ppkg *doc.Pkg, cachePkgs map[string]*doc.Pkg, isTest bool) error {
log.Trace("Current Path: 1 %s", cpath)
pkgs, err := getGopmPkgs(cpath, isTest)
if err != nil {
return errors.New("Fail to get gopmfile deps: " + err.Error())
}
for name, pkg := range pkgs {
pkg.RootPath = doc.GetProjectPath(pkg.ImportPath)
if !pkgInCache(pkg.RootPath, cachePkgs) {
var newPath string
if !build.IsLocalImport(name) && pkg.Type != doc.LOCAL {
suf := versionSuffix(pkg.Value)
pkgPath := strings.Replace(
pkg.ImportPath, pkg.RootPath, pkg.RootPath+suf, 1)
newPath = filepath.Join(installRepoPath, pkgPath)
if len(suf) == 0 && !ctx.Bool("remote") &&
com.IsDir(filepath.Join(installGopath, pkgPath)) {
newPath = filepath.Join(installGopath, pkgPath)
}
if pkgName != "" && strings.HasPrefix(pkg.ImportPath, pkgName) {
newPath = filepath.Join(curPath, strings.TrimPrefix(pkg.ImportPath, pkgName))
} else {
if !com.IsExist(newPath) || ctx.Bool("update") {
node := doc.NewNode(pkg.ImportPath, pkg.ImportPath,
pkg.Type, pkg.Value, true)
nodes := []*doc.Node{node}
downloadPackages(ctx, nodes)
// TODO: Should handler download failed
}
}
} else {
if pkg.Type == doc.LOCAL {
newPath, err = filepath.Abs(pkg.Value)
} else {
newPath, err = filepath.Abs(name)
}
if err != nil {
return err
}
}
cachePkgs[pkg.RootPath] = pkg
err = getChildPkgs(ctx, newPath, pkg, cachePkgs, false)
if err != nil {
return err
}
}
}
return nil
}
var pkgName string
var curPath string
var newCurPath string
var newGoPath string
var oldGoPath string
func execCmd(gopath, curPath string, args ...string) error {
cwd, err := os.Getwd()
if err != nil {
log.Error("", "Fail to get work directory:")
log.Fatal("", "\t"+err.Error())
}
log.Log("Changing work directory to %s", curPath)
err = os.Chdir(curPath)
if err != nil {
log.Error("", "Fail to change work directory:")
log.Fatal("", "\t"+err.Error())
}
defer func() {
log.Log("Changing work directory back to %s", cwd)
os.Chdir(cwd)
}()
err = os.Chdir(curPath)
if err != nil {
log.Error("", "Fail to change work directory:")
log.Fatal("", "\t"+err.Error())
}
oldGoPath = os.Getenv("GOPATH")
log.Log("Setting GOPATH to %s", gopath)
sep := ":"
if runtime.GOOS == "windows" {
sep = ";"
}
err = os.Setenv("GOPATH", gopath+sep+oldGoPath)
if err != nil {
log.Error("", "Fail to setting GOPATH:")
log.Fatal("", "\t"+err.Error())
}
defer func() {
log.Log("Setting GOPATH back to %s", oldGoPath)
os.Setenv("GOPATH", oldGoPath)
}()
cmd := exec.Command(args[0], args[1:]...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
log.Log("===== application outputs start =====\n")
err = cmd.Run()
log.Log("====== application outputs end ======")
return err
}
func genNewGoPath(ctx *cli.Context, isTest bool) {
var err error
curPath, err = os.Getwd()
if err != nil {
log.Error("", "Fail to get work directory:")
log.Fatal("", "\t"+err.Error())
}
log.Trace("Current Path: 0 %s", curPath)
installRepoPath = doc.HomeDir + "/repos"
if com.IsFile(curPath + "/" + doc.GOPM_FILE_NAME) {
log.Trace("Loading gopmfile...")
gf := doc.NewGopmfile(curPath)
var err error
pkgName, err = gf.GetValue("target", "path")
if err == nil {
log.Log("Target name: %s", pkgName)
}
}
if len(pkgName) == 0 {
_, pkgName = filepath.Split(curPath)
}
cachePkgs := make(map[string]*doc.Pkg)
if err = getChildPkgs(ctx, curPath, nil, cachePkgs, isTest); err != nil {
log.Error("", "Fail to get child pakcages:")
log.Fatal("", "\t"+err.Error())
}
newGoPath = filepath.Join(curPath, doc.VENDOR)
newGoPathSrc := filepath.Join(newGoPath, "src")
os.RemoveAll(newGoPathSrc)
os.MkdirAll(newGoPathSrc, os.ModePerm)
for name, pkg := range cachePkgs {
suf := versionSuffix(pkg.Value)
var oldPath string
if pkg.Type == doc.LOCAL {
oldPath, _ = filepath.Abs(pkg.Value)
} else {
oldPath = filepath.Join(installRepoPath, name) + suf
}
newPath := filepath.Join(newGoPathSrc, name)
paths := strings.Split(name, "/")
var isExistP, isCurChild bool
if name == pkgName {
continue
}
for i := 0; i < len(paths)-1; i++ {
pName := strings.Join(paths[:len(paths)-1-i], "/")
if _, ok := cachePkgs[pName]; ok {
isExistP = true
break
}
if pkgName == pName {
isCurChild = true
break
}
}
if isCurChild {
continue
}
if !isExistP && (len(pkg.Value) > 0 || ctx.Bool("remote") ||
!com.IsDir(filepath.Join(installGopath, pkg.ImportPath))) {
log.Log("Linking %s", name+suf)
err = autoLink(oldPath, newPath)
if err != nil {
log.Error("", "Fail to make link:")
log.Fatal("", "\t"+err.Error())
}
}
}
newCurPath = filepath.Join(newGoPathSrc, pkgName)
log.Log("Linking %s", pkgName)
err = autoLink(curPath, newCurPath)
if err != nil {
log.Error("", "Fail to make link:")
log.Fatal("", "\t"+err.Error())
}
}