github.com/masterminds/glide@v0.13.4-0.20190710143844-b94b39d657d8/godep/strip/strip.go (about)

     1  // Package strip removes Godeps/_workspace and undoes the Godep rewrites. This
     2  // essentially removes the old style (pre-vendor) Godep vendoring.
     3  //
     4  // Note, this functionality is deprecated. Once more projects use the Godep
     5  // support for the core vendoring this will no longer be needed.
     6  package strip
     7  
     8  import (
     9  	"bytes"
    10  	"go/ast"
    11  	"go/parser"
    12  	"go/printer"
    13  	"go/token"
    14  	"os"
    15  	"path/filepath"
    16  	"strconv"
    17  	"strings"
    18  
    19  	"github.com/Masterminds/glide/msg"
    20  )
    21  
    22  var godepMark = map[string]bool{}
    23  
    24  var vPath = "vendor"
    25  
    26  // GodepWorkspace removes any Godeps/_workspace directories and makes sure
    27  // any rewrites are undone.
    28  // Note, this is not concuccency safe.
    29  func GodepWorkspace(v string) error {
    30  	vPath = v
    31  	if _, err := os.Stat(vPath); err != nil {
    32  		if os.IsNotExist(err) {
    33  			msg.Debug("Vendor directory does not exist.")
    34  		}
    35  
    36  		return err
    37  	}
    38  
    39  	err := filepath.Walk(vPath, stripGodepWorkspaceHandler)
    40  	if err != nil {
    41  		return err
    42  	}
    43  
    44  	// Walk the marked projects to make sure rewrites are undone.
    45  	for k := range godepMark {
    46  		msg.Info("Removing Godep rewrites for %s", k)
    47  		err := filepath.Walk(k, rewriteGodepfilesHandler)
    48  		if err != nil {
    49  			return err
    50  		}
    51  	}
    52  
    53  	return nil
    54  }
    55  
    56  func stripGodepWorkspaceHandler(path string, info os.FileInfo, err error) error {
    57  	// Skip the base vendor directory
    58  	if path == vPath {
    59  		return nil
    60  	}
    61  
    62  	name := info.Name()
    63  	p := filepath.Dir(path)
    64  	pn := filepath.Base(p)
    65  	if name == "_workspace" && pn == "Godeps" {
    66  		if _, err := os.Stat(path); err == nil {
    67  			if info.IsDir() {
    68  				// Marking this location to make sure rewrites are undone.
    69  				pp := filepath.Dir(p)
    70  				godepMark[pp] = true
    71  
    72  				msg.Info("Removing: %s", path)
    73  				if err := os.RemoveAll(path); err != nil {
    74  					return err
    75  				}
    76  				return filepath.SkipDir
    77  			}
    78  
    79  			msg.Debug("%s is not a directory. Skipping removal", path)
    80  			return nil
    81  		}
    82  	}
    83  	return nil
    84  }
    85  
    86  func rewriteGodepfilesHandler(path string, info os.FileInfo, err error) error {
    87  	name := info.Name()
    88  
    89  	if info.IsDir() {
    90  		if name == "testdata" || name == "vendor" {
    91  			return filepath.SkipDir
    92  		}
    93  		return nil
    94  	}
    95  
    96  	if e := filepath.Ext(path); e != ".go" {
    97  		return nil
    98  	}
    99  
   100  	fset := token.NewFileSet()
   101  	f, err := parser.ParseFile(fset, path, nil, parser.ParseComments)
   102  	if err != nil {
   103  		return err
   104  	}
   105  
   106  	var changed bool
   107  	for _, s := range f.Imports {
   108  		n, err := strconv.Unquote(s.Path.Value)
   109  		if err != nil {
   110  			return err
   111  		}
   112  		q := rewriteGodepImport(n)
   113  		if q != name {
   114  			s.Path.Value = strconv.Quote(q)
   115  			changed = true
   116  		}
   117  	}
   118  	if !changed {
   119  		return nil
   120  	}
   121  
   122  	printerConfig := &printer.Config{Mode: printer.TabIndent | printer.UseSpaces, Tabwidth: 8}
   123  	var buffer bytes.Buffer
   124  	if err = printerConfig.Fprint(&buffer, fset, f); err != nil {
   125  		return err
   126  	}
   127  	fset = token.NewFileSet()
   128  	f, err = parser.ParseFile(fset, name, &buffer, parser.ParseComments)
   129  	ast.SortImports(fset, f)
   130  	tpath := path + ".temp"
   131  	t, err := os.Create(tpath)
   132  	if err != nil {
   133  		return err
   134  	}
   135  	if err = printerConfig.Fprint(t, fset, f); err != nil {
   136  		return err
   137  	}
   138  	if err = t.Close(); err != nil {
   139  		return err
   140  	}
   141  
   142  	msg.Debug("Rewriting Godep imports for %s", path)
   143  
   144  	// This is required before the rename on windows.
   145  	if err = os.Remove(path); err != nil {
   146  		return err
   147  	}
   148  	return os.Rename(tpath, path)
   149  }
   150  
   151  func rewriteGodepImport(n string) string {
   152  	if !strings.Contains(n, "Godeps/_workspace/src") {
   153  		return n
   154  	}
   155  
   156  	i := strings.LastIndex(n, "Godeps/_workspace/src")
   157  
   158  	return strings.TrimPrefix(n[i:], "Godeps/_workspace/src/")
   159  }