github.com/joey-fossa/fossa-cli@v0.7.34-0.20190708193710-569f1e8679f0/buildtools/dotnet/dotnet.go (about)

     1  package dotnet
     2  
     3  import (
     4  	"path/filepath"
     5  	"strings"
     6  
     7  	"github.com/apex/log"
     8  
     9  	"github.com/fossas/fossa-cli/exec"
    10  	"github.com/fossas/fossa-cli/graph"
    11  	"github.com/fossas/fossa-cli/pkg"
    12  )
    13  
    14  type DotNET struct {
    15  	Cmd     string
    16  	Version string
    17  }
    18  
    19  func (d *DotNET) Build(dir string) error {
    20  	_, _, err := exec.Run(exec.Cmd{
    21  		Name: d.Cmd,
    22  		Argv: []string{"restore"},
    23  		Dir:  dir,
    24  	})
    25  	return err
    26  }
    27  
    28  func Path(s string) string {
    29  	return filepath.Join(strings.Split(s, "\\")...)
    30  }
    31  
    32  func ResolveStrategy(target, dir string) (graph.Deps, error) {
    33  	lockfile, err := readLockfile(filepath.Join(dir, "obj", "project.assets.json"))
    34  	if err != nil {
    35  		return graph.Deps{}, err
    36  	}
    37  
    38  	// Compute project graph.
    39  	projects := make(map[string]Manifest)
    40  	err = Projects(projects, target)
    41  	if err != nil {
    42  		return graph.Deps{}, err
    43  	}
    44  
    45  	// Compute package graph.
    46  	deps := make(map[pkg.ID]pkg.Package)
    47  	packagesOutput(projects, lockfile, deps, target)
    48  
    49  	root := projects[target]
    50  	rootID := pkg.ID{
    51  		Type:     pkg.NuGet,
    52  		Name:     root.Name(),
    53  		Revision: root.Version(),
    54  		Location: target,
    55  	}
    56  	imports := deps[rootID].Imports
    57  	deleteProjects(deps, projects)
    58  
    59  	return graph.Deps{
    60  		Direct:     imports,
    61  		Transitive: deps,
    62  	}, nil
    63  
    64  }
    65  
    66  func packagesOutput(projects map[string]Manifest, lockfile Lockfile, deps map[pkg.ID]pkg.Package, dep string) pkg.ID {
    67  	if project, ok := projects[Path(dep)]; ok {
    68  		log.Debugf("%#v", project)
    69  		name := project.Name()
    70  		version := project.Version()
    71  
    72  		id := pkg.ID{
    73  			Type:     pkg.NuGet,
    74  			Name:     name,
    75  			Revision: version,
    76  			Location: dep,
    77  		}
    78  
    79  		var imports []pkg.Import
    80  		for _, ref := range project.packages() {
    81  			imports = append(imports, pkg.Import{
    82  				Target:   ref.Include + "@" + ref.Version,
    83  				Resolved: packagesOutput(projects, lockfile, deps, ref.Include),
    84  			})
    85  		}
    86  		for _, ref := range project.projects() {
    87  			packagesOutput(projects, lockfile, deps, filepath.Join(filepath.Dir(dep), Path(ref.Include)))
    88  		}
    89  
    90  		deps[id] = pkg.Package{
    91  			ID:      id,
    92  			Imports: imports,
    93  		}
    94  
    95  		return id
    96  	}
    97  
    98  	name := dep
    99  	version := lockfile.resolve(name)
   100  
   101  	id := pkg.ID{
   102  		Type:     pkg.NuGet,
   103  		Name:     name,
   104  		Revision: version,
   105  	}
   106  
   107  	var imports []pkg.Import
   108  	for p, version := range lockfile.imports(dep) {
   109  		imports = append(imports, pkg.Import{
   110  			Target:   p + "@" + version,
   111  			Resolved: packagesOutput(projects, lockfile, deps, p),
   112  		})
   113  	}
   114  
   115  	deps[id] = pkg.Package{
   116  		ID:      id,
   117  		Imports: imports,
   118  	}
   119  
   120  	return id
   121  }
   122  
   123  func deleteProjects(deps map[pkg.ID]pkg.Package, projects map[string]Manifest) {
   124  	for project, manifest := range projects {
   125  		delete(deps, pkg.ID{
   126  			Type:     pkg.NuGet,
   127  			Name:     manifest.Name(),
   128  			Revision: manifest.Name(),
   129  			Location: project,
   130  		})
   131  	}
   132  }