github.com/ryanfowler/glide@v0.12.1-0.20160826135910-1071a8c18883/action/rebuild.go (about)

     1  package action
     2  
     3  import (
     4  	"os"
     5  	"os/exec"
     6  	"path"
     7  	"path/filepath"
     8  	"strings"
     9  
    10  	"github.com/Masterminds/glide/cfg"
    11  	"github.com/Masterminds/glide/msg"
    12  	gpath "github.com/Masterminds/glide/path"
    13  )
    14  
    15  // Rebuild rebuilds '.a' files for a project.
    16  //
    17  // Prior to Go 1.4, this could substantially reduce time on incremental compiles.
    18  // It remains to be seen whether this is tremendously beneficial to modern Go
    19  // programs.
    20  func Rebuild() {
    21  	msg.Warn("The rebuild command is deprecated and will be removed in a future version")
    22  	msg.Warn("Use the go install command instead")
    23  	conf := EnsureConfig()
    24  	vpath, err := gpath.Vendor()
    25  	if err != nil {
    26  		msg.Die("Could not get vendor path: %s", err)
    27  	}
    28  
    29  	msg.Info("Building dependencies.\n")
    30  
    31  	if len(conf.Imports) == 0 {
    32  		msg.Info("No dependencies found. Nothing built.\n")
    33  		return
    34  	}
    35  
    36  	for _, dep := range conf.Imports {
    37  		if err := buildDep(dep, vpath); err != nil {
    38  			msg.Warn("Failed to build %s: %s\n", dep.Name, err)
    39  		}
    40  	}
    41  }
    42  
    43  func buildDep(dep *cfg.Dependency, vpath string) error {
    44  	if len(dep.Subpackages) == 0 {
    45  		buildPath(dep.Name)
    46  	}
    47  
    48  	for _, pkg := range dep.Subpackages {
    49  		if pkg == "**" || pkg == "..." {
    50  			//Info("Building all packages in %s\n", dep.Name)
    51  			buildPath(path.Join(dep.Name, "..."))
    52  		} else {
    53  			paths, err := resolvePackages(vpath, dep.Name, pkg)
    54  			if err != nil {
    55  				msg.Warn("Error resolving packages: %s", err)
    56  			}
    57  			buildPaths(paths)
    58  		}
    59  	}
    60  
    61  	return nil
    62  }
    63  
    64  func resolvePackages(vpath, pkg, subpkg string) ([]string, error) {
    65  	sdir, _ := os.Getwd()
    66  	if err := os.Chdir(filepath.Join(vpath, pkg, subpkg)); err != nil {
    67  		return []string{}, err
    68  	}
    69  	defer os.Chdir(sdir)
    70  	p, err := filepath.Glob(path.Join(vpath, pkg, subpkg))
    71  	if err != nil {
    72  		return []string{}, err
    73  	}
    74  	for k, v := range p {
    75  		nv := strings.TrimPrefix(v, vpath)
    76  		p[k] = strings.TrimPrefix(nv, string(filepath.Separator))
    77  	}
    78  	return p, nil
    79  }
    80  
    81  func buildPaths(paths []string) error {
    82  	for _, path := range paths {
    83  		if err := buildPath(path); err != nil {
    84  			return err
    85  		}
    86  	}
    87  
    88  	return nil
    89  }
    90  
    91  func buildPath(path string) error {
    92  	msg.Info("Running go build %s\n", path)
    93  	// . in a filepath.Join is removed so it needs to be prepended separately.
    94  	p := "." + string(filepath.Separator) + filepath.Join("vendor", path)
    95  	out, err := exec.Command(goExecutable(), "install", p).CombinedOutput()
    96  	if err != nil {
    97  		msg.Warn("Failed to run 'go install' for %s: %s", path, string(out))
    98  	}
    99  	return err
   100  }