github.com/sdboyer/gps@v0.16.3/result.go (about)

     1  package gps
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path/filepath"
     7  )
     8  
     9  // A Solution is returned by a solver run. It is mostly just a Lock, with some
    10  // additional methods that report information about the solve run.
    11  type Solution interface {
    12  	Lock
    13  	Attempts() int
    14  }
    15  
    16  type solution struct {
    17  	// A list of the projects selected by the solver.
    18  	p []LockedProject
    19  
    20  	// The number of solutions that were attempted
    21  	att int
    22  
    23  	// The hash digest of the input opts
    24  	hd []byte
    25  }
    26  
    27  // WriteDepTree takes a basedir and a Lock, and exports all the projects
    28  // listed in the lock to the appropriate target location within the basedir.
    29  //
    30  // If the goal is to populate a vendor directory, basedir should be the absolute
    31  // path to that vendor directory, not its parent (a project root, typically).
    32  //
    33  // It requires a SourceManager to do the work, and takes a flag indicating
    34  // whether or not to strip vendor directories contained in the exported
    35  // dependencies.
    36  func WriteDepTree(basedir string, l Lock, sm SourceManager, sv bool) error {
    37  	if l == nil {
    38  		return fmt.Errorf("must provide non-nil Lock to WriteDepTree")
    39  	}
    40  
    41  	err := os.MkdirAll(basedir, 0777)
    42  	if err != nil {
    43  		return err
    44  	}
    45  
    46  	// TODO(sdboyer) parallelize
    47  	for _, p := range l.Projects() {
    48  		to := filepath.FromSlash(filepath.Join(basedir, string(p.Ident().ProjectRoot)))
    49  
    50  		err = sm.ExportProject(p.Ident(), p.Version(), to)
    51  		if err != nil {
    52  			removeAll(basedir)
    53  			return fmt.Errorf("error while exporting %s: %s", p.Ident().ProjectRoot, err)
    54  		}
    55  		if sv {
    56  			filepath.Walk(to, stripVendor)
    57  		}
    58  		// TODO(sdboyer) dump version metadata file
    59  	}
    60  
    61  	return nil
    62  }
    63  
    64  func (r solution) Projects() []LockedProject {
    65  	return r.p
    66  }
    67  
    68  func (r solution) Attempts() int {
    69  	return r.att
    70  }
    71  
    72  func (r solution) InputHash() []byte {
    73  	return r.hd
    74  }