private void AddDependencies(Project.TargetFramework targetFramework, LockFileTargetLibrary parentLibrary, LockFileTarget target, int level, int transitiveDepth)
        {
            if (parentLibrary?.Dependencies != null)
            {
                foreach (var packageDependency in parentLibrary.Dependencies)
                {
                    var childLibrary = target.Libraries.FirstOrDefault(library => library.Name == packageDependency.Id);

                    // Only add library and process child dependencies if we have not come across this dependency before
                    if (!targetFramework.Dependencies.Any(dependency => dependency.Name == packageDependency.Id))
                    {
                        var childDependency = new Project.Dependency
                        {
                            Name            = packageDependency.Id,
                            VersionRange    = packageDependency.VersionRange,
                            ResolvedVersion = childLibrary?.Version,
                            IsTransitive    = true
                        };
                        targetFramework.Dependencies.Add(childDependency);

                        // Process the dependency for this project depency
                        if (level < transitiveDepth)
                        {
                            AddDependencies(targetFramework, childLibrary, target, level + 1, transitiveDepth);
                        }
                    }
                }
            }
        }
Beispiel #2
0
        private void AddDependencies(Project.Dependency parentDependency, LockFileTargetLibrary parentLibrary, LockFileTarget target, int level, int transitiveDepth)
        {
            if (parentLibrary?.Dependencies != null)
            {
                foreach (var packageDependency in parentLibrary.Dependencies)
                {
                    var childLibrary = target.Libraries.FirstOrDefault(library => library.Name == packageDependency.Id);

                    var childDependency = new Project.Dependency
                    {
                        Name            = packageDependency.Id,
                        VersionRange    = packageDependency.VersionRange,
                        ResolvedVersion = childLibrary?.Version
                    };
                    parentDependency.Dependencies.Add(childDependency);

                    // Process the dependency for this project depency
                    if (level < transitiveDepth)
                    {
                        AddDependencies(childDependency, childLibrary, target, level + 1, transitiveDepth);
                    }
                }
            }
        }
Beispiel #3
0
        public List <Project> AnalyzeProject(string projectPath, bool includeTransitiveDependencies, int transitiveDepth)
        {
            var dependencyGraph = _dependencyGraphService.GenerateDependencyGraph(projectPath);

            if (dependencyGraph == null)
            {
                return(null);
            }

            var projects = new List <Project>();

            foreach (var packageSpec in dependencyGraph.Projects.Where(p => p.RestoreMetadata.ProjectStyle == ProjectStyle.PackageReference))
            {
                // Restore the packages
                _dotNetRestoreService.Restore(packageSpec.FilePath);

                // Load the lock file
                string lockFilePath = _fileSystem.Path.Combine(packageSpec.RestoreMetadata.OutputPath, "project.assets.json");
                var    lockFile     = LockFileUtilities.GetLockFile(lockFilePath, NuGet.Common.NullLogger.Instance);

                // Create a project
                var project = new Project
                {
                    Name     = packageSpec.Name,
                    Sources  = packageSpec.RestoreMetadata.Sources.Select(s => s.SourceUri).ToList(),
                    FilePath = packageSpec.FilePath
                };
                projects.Add(project);

                // Get the target frameworks with their dependencies
                foreach (var targetFrameworkInformation in packageSpec.TargetFrameworks)
                {
                    var targetFramework = new Project.TargetFramework
                    {
                        Name = targetFrameworkInformation.FrameworkName,
                    };
                    project.TargetFrameworks.Add(targetFramework);

                    var target = lockFile.Targets.FirstOrDefault(t => t.TargetFramework.Equals(targetFrameworkInformation.FrameworkName));

                    if (target != null)
                    {
                        foreach (var projectDependency in targetFrameworkInformation.Dependencies)
                        {
                            var projectLibrary = target.Libraries.FirstOrDefault(library => library.Name == projectDependency.Name);

                            var dependency = new Project.Dependency
                            {
                                Name            = projectDependency.Name,
                                VersionRange    = projectDependency.LibraryRange.VersionRange,
                                ResolvedVersion = projectLibrary?.Version,
                                AutoReferenced  = projectDependency.AutoReferenced
                            };
                            targetFramework.Dependencies.Add(dependency);

                            // Process transitive dependencies for the library
                            if (includeTransitiveDependencies)
                            {
                                AddDependencies(dependency, projectLibrary, target, 1, transitiveDepth);
                            }
                        }
                    }
                }
            }

            return(projects);
        }