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, NullLogger.Instance); // Create a project var project = new Project(packageSpec.Name, packageSpec.FilePath, packageSpec.RestoreMetadata.Sources.Select(s => s.SourceUri).ToList(), packageSpec.Version); projects.Add(project); // Get the target frameworks with their dependencies foreach (var targetFrameworkInformation in packageSpec.TargetFrameworks) { var targetFramework = new TargetFramework(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 => string.Equals(library.Name, projectDependency.Name, StringComparison.OrdinalIgnoreCase)); bool isDevelopmentDependency = false; if (projectLibrary != null) { // Determine whether this is a development dependency var packageIdentity = new PackageIdentity(projectLibrary.Name, projectLibrary.Version); var packageInfo = LocalFolderUtility.GetPackageV3(packageSpec.RestoreMetadata.PackagesPath, packageIdentity, NullLogger.Instance); if (packageInfo != null) { isDevelopmentDependency = packageInfo.GetReader().GetDevelopmentDependency(); } } var dependency = new Dependency(projectDependency.Name, projectDependency.LibraryRange.VersionRange, projectLibrary?.Version, projectDependency.AutoReferenced, false, isDevelopmentDependency); targetFramework.Dependencies.Add(dependency); // Process transitive dependencies for the library if (includeTransitiveDependencies) { AddDependencies(targetFramework, projectLibrary, target, 1, transitiveDepth); } } } } } return(projects); }
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); }