🌐 AI搜索 & 代理 主页
blob: 50e0205e84c21c7f913fa742b1cbed8f07e5b978 [file] [log] [blame]
Miguel Molina0acfd382017-01-27 20:39:071// Copyright 2016 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package dep
6
7import (
8 "fmt"
9 "os"
10 "path/filepath"
11
Ibrahim AshShohail68475b72017-05-13 01:05:3512 "github.com/golang/dep/internal/fs"
Dave Cheney9ea84892017-05-10 04:13:2213 "github.com/golang/dep/internal/gps"
Miguel Molina0acfd382017-01-27 20:39:0714)
15
16var errProjectNotFound = fmt.Errorf("could not find project %s, use dep init to initiate a manifest", ManifestName)
Sunny47302cf2017-05-12 13:14:5017var errVendorBackupFailed = fmt.Errorf("Failed to create vendor backup. File with same name exists.")
Miguel Molina0acfd382017-01-27 20:39:0718
Miguel Molina0acfd382017-01-27 20:39:0719// findProjectRoot searches from the starting directory upwards looking for a
20// manifest file until we get to the root of the filesystem.
21func findProjectRoot(from string) (string, error) {
22 for {
23 mp := filepath.Join(from, ManifestName)
24
25 _, err := os.Stat(mp)
26 if err == nil {
27 return from, nil
28 }
29 if !os.IsNotExist(err) {
30 // Some err other than non-existence - return that out
31 return "", err
32 }
33
34 parent := filepath.Dir(from)
35 if parent == from {
36 return "", errProjectNotFound
37 }
38 from = parent
39 }
40}
41
Jordan Kragec2491a62017-05-31 19:48:4842// A Project holds a Manifest and optional Lock for a project.
Miguel Molina0acfd382017-01-27 20:39:0743type Project struct {
44 // AbsRoot is the absolute path to the root directory of the project.
45 AbsRoot string
Ibrahim AshShohail71a26072017-06-14 22:30:2546 // ResolvedAbsRoot is the resolved absolute path to the root directory of the project.
47 // If AbsRoot is not a symlink, then ResolvedAbsRoot should equal AbsRoot.
48 ResolvedAbsRoot string
Miguel Molina0acfd382017-01-27 20:39:0749 // ImportRoot is the import path of the project's root directory.
50 ImportRoot gps.ProjectRoot
51 Manifest *Manifest
Jordan Kragec2491a62017-05-31 19:48:4852 Lock *Lock // Optional
Miguel Molina0acfd382017-01-27 20:39:0753}
54
Ibrahim AshShohail3826bb02017-06-17 00:51:0855// SetRoot sets the project AbsRoot and ResolvedAbsRoot. If root is a not symlink, ResolvedAbsRoot will be set to root.
Ibrahim AshShohail91660342017-06-14 23:36:0156func (p *Project) SetRoot(root string) error {
Ibrahim AshShohail3826bb02017-06-17 00:51:0857 rroot, err := filepath.EvalSymlinks(root)
Ibrahim AshShohailb98d7132017-06-16 18:25:5858 if err != nil {
Ibrahim AshShohail91660342017-06-14 23:36:0159 return err
Ibrahim AshShohailb98d7132017-06-16 18:25:5860 }
61
Ibrahim AshShohail3826bb02017-06-17 00:51:0862 p.ResolvedAbsRoot, p.AbsRoot = rroot, root
Ibrahim AshShohail91660342017-06-14 23:36:0163 return nil
Ibrahim AshShohail5bf92052017-06-13 12:50:1264}
65
Miguel Molina0acfd382017-01-27 20:39:0766// MakeParams is a simple helper to create a gps.SolveParameters without setting
67// any nils incorrectly.
68func (p *Project) MakeParams() gps.SolveParameters {
69 params := gps.SolveParameters{
sam boyer8438fd42017-04-15 05:06:4470 RootDir: p.AbsRoot,
sam boyer00a5b762017-04-15 05:23:1671 ProjectAnalyzer: Analyzer{},
Miguel Molina0acfd382017-01-27 20:39:0772 }
73
74 if p.Manifest != nil {
75 params.Manifest = p.Manifest
76 }
77
78 if p.Lock != nil {
79 params.Lock = p.Lock
80 }
81
82 return params
83}
Sunny45162e52017-05-11 16:56:1484
85// BackupVendor looks for existing vendor directory and if it's not empty,
86// creates a backup of it to a new directory with the provided suffix.
87func BackupVendor(vpath, suffix string) (string, error) {
88 // Check if there's a non-empty vendor directory
Ibrahim AshShohail68475b72017-05-13 01:05:3589 vendorExists, err := fs.IsNonEmptyDir(vpath)
mstrong78703842017-06-18 13:16:0290 if err != nil && !os.IsNotExist(err) {
Sunny45162e52017-05-11 16:56:1491 return "", err
92 }
93 if vendorExists {
Jonathan Stacks98250d42017-05-20 15:22:0994 // vpath is a full filepath. We need to split it to prefix the backup dir
95 // with an "_"
96 vpathDir, name := filepath.Split(vpath)
97 vendorbak := filepath.Join(vpathDir, "_"+name+"-"+suffix)
Sunny45162e52017-05-11 16:56:1498 // Check if a directory with same name exists
99 if _, err = os.Stat(vendorbak); os.IsNotExist(err) {
Sunny44700342017-06-01 10:22:29100 // Copy existing vendor to vendor-{suffix}
101 if err := fs.CopyDir(vpath, vendorbak); err != nil {
Sunny45162e52017-05-11 16:56:14102 return "", err
103 }
104 return vendorbak, nil
Sunny45162e52017-05-11 16:56:14105 }
Sunny1709f382017-05-13 21:47:27106 return "", errVendorBackupFailed
Sunny45162e52017-05-11 16:56:14107 }
108
109 return "", nil
110}