public LocalPackageSourceInfo(NuGetv3LocalRepository repository, LocalPackageInfo package) { if (repository == null) { throw new ArgumentNullException(nameof(repository)); } if (package == null) { throw new ArgumentNullException(nameof(package)); } Repository = repository; Package = package; }
public NuGetDependencyResolver(string packagesPath) { _repository = new NuGetv3LocalRepository(packagesPath, checkPackageIdCase: false); }
public NuGetDependencyResolver(NuGetv3LocalRepository repository) { _repository = repository; }
private Task<RestoreTargetGraph[]> WalkRuntimeDependencies(LibraryRange projectRange, RestoreTargetGraph graph, RuntimeGraph projectRuntimeGraph, RemoteDependencyWalker walker, RemoteWalkContext context, NuGetv3LocalRepository localRepository) { // Load runtime specs _log.LogVerbose("Scanning packages for runtime.json files..."); var runtimeGraph = projectRuntimeGraph; graph.Graph.ForEach(node => { var match = node?.Item?.Data?.Match; if (match == null) { return; } // Locate the package in the local repository var package = localRepository.FindPackagesById(match.Library.Name).FirstOrDefault(p => p.Version == match.Library.Version); if (package != null) { var nextGraph = LoadRuntimeGraph(package); if (nextGraph != null) { _log.LogVerbose($"Merging in runtimes defined in {match.Library}"); runtimeGraph = RuntimeGraph.Merge(runtimeGraph, nextGraph); } } }); var resultGraphs = new List<Task<RestoreTargetGraph>>(); foreach (var runtimeName in projectRuntimeGraph.Runtimes.Keys) { _log.LogInformation($"Restoring packages for {graph.Framework} on {runtimeName}"); resultGraphs.Add(WalkDependencies(projectRange, graph.Framework, runtimeName, runtimeGraph, walker, context)); } return Task.WhenAll(resultGraphs); }
public async Task<RestoreResult> ExecuteAsync(RestoreRequest request) { if (request.Project.TargetFrameworks.Count == 0) { _log.LogError("The project does not specify any target frameworks!"); return new RestoreResult(success: false, restoreGraphs: Enumerable.Empty<RestoreTargetGraph>()); } var projectLockFilePath = string.IsNullOrEmpty(request.LockFilePath) ? Path.Combine(request.Project.BaseDirectory, LockFileFormat.LockFileName) : request.LockFilePath; _log.LogInformation($"Restoring packages for '{request.Project.FilePath}'"); _log.LogWarning("TODO: Read and use lock file"); // Load repositories var projectResolver = new PackageSpecResolver(request.Project); var nugetRepository = Repository.Factory.GetCoreV3(request.PackagesDirectory); var context = new RemoteWalkContext(); ExternalProjectReference exterenalProjectReference = null; if (request.ExternalProjects.Any()) { exterenalProjectReference = new ExternalProjectReference( request.Project.Name, request.Project.FilePath, request.ExternalProjects.Select(p => p.Name)); } context.ProjectLibraryProviders.Add( new LocalDependencyProvider( new PackageSpecReferenceDependencyProvider(projectResolver, exterenalProjectReference))); if (request.ExternalProjects != null) { context.ProjectLibraryProviders.Add( new LocalDependencyProvider( new ExternalProjectReferenceDependencyProvider(request.ExternalProjects))); } context.LocalLibraryProviders.Add( new SourceRepositoryDependencyProvider(nugetRepository, _log)); foreach (var provider in request.Sources.Select(s => CreateProviderFromSource(s, request.NoCache))) { context.RemoteLibraryProviders.Add(provider); } var remoteWalker = new RemoteDependencyWalker(context); var projectRange = new LibraryRange() { Name = request.Project.Name, VersionRange = new VersionRange(request.Project.Version), TypeConstraint = LibraryTypes.Project }; // Resolve dependency graphs var frameworks = request.Project.TargetFrameworks.Select(f => f.FrameworkName).ToList(); var graphs = new List<RestoreTargetGraph>(); var frameworkTasks = new List<Task<RestoreTargetGraph>>(); foreach (var framework in frameworks) { frameworkTasks.Add(WalkDependencies(projectRange, framework, remoteWalker, context)); } graphs.AddRange(await Task.WhenAll(frameworkTasks)); if (graphs.Any(g => g.InConflict)) { _log.LogError("Failed to resolve conflicts"); return new RestoreResult(success: false, restoreGraphs: graphs); } // Install the runtime-agnostic packages var allInstalledPackages = new HashSet<LibraryIdentity>(); var localRepository = new NuGetv3LocalRepository(request.PackagesDirectory, checkPackageIdCase: false); await InstallPackages(graphs, request.PackagesDirectory, allInstalledPackages, request.MaxDegreeOfConcurrency); // Resolve runtime dependencies var runtimeGraphs = new List<RestoreTargetGraph>(); if (request.Project.RuntimeGraph.Runtimes.Count > 0) { var runtimeTasks = new List<Task<RestoreTargetGraph[]>>(); foreach (var graph in graphs) { runtimeTasks.Add(WalkRuntimeDependencies(projectRange, graph, request.Project.RuntimeGraph, remoteWalker, context, localRepository)); } foreach (var runtimeSpecificGraphs in await Task.WhenAll(runtimeTasks)) { runtimeGraphs.AddRange(runtimeSpecificGraphs); } graphs.AddRange(runtimeGraphs); if (runtimeGraphs.Any(g => g.InConflict)) { _log.LogError("Failed to resolve conflicts"); return new RestoreResult(success: false, restoreGraphs: graphs); } // Install runtime-specific packages await InstallPackages(runtimeGraphs, request.PackagesDirectory, allInstalledPackages, request.MaxDegreeOfConcurrency); } else { _log.LogVerbose("Skipping runtime dependency walk, no runtimes defined in project.json"); } // Build the lock file var repository = new NuGetv3LocalRepository(request.PackagesDirectory, checkPackageIdCase: false); var lockFile = CreateLockFile(request.Project, graphs, repository); var lockFileFormat = new LockFileFormat(); lockFileFormat.Write(projectLockFilePath, lockFile); // Generate Targets/Props files WriteTargetsAndProps(request.Project, graphs, repository); return new RestoreResult(true, graphs, lockFile); }
private LockFile CreateLockFile(PackageSpec project, List<RestoreTargetGraph> targetGraphs, NuGetv3LocalRepository repository) { var lockFile = new LockFile(); using (var sha512 = SHA512.Create()) { foreach (var item in targetGraphs.SelectMany(g => g.Flattened).Distinct().OrderBy(x => x.Data.Match.Library)) { var library = item.Data.Match.Library; var packageInfo = repository.FindPackagesById(library.Name) .FirstOrDefault(p => p.Version == library.Version); if (packageInfo == null) { continue; } var lockFileLib = CreateLockFileLibrary( packageInfo, sha512, correctedPackageName: library.Name); lockFile.Libraries.Add(lockFileLib); } } // Use empty string as the key of dependencies shared by all frameworks lockFile.ProjectFileDependencyGroups.Add(new ProjectFileDependencyGroup( string.Empty, project.Dependencies.Select(x => x.LibraryRange.ToString()))); foreach (var frameworkInfo in project.TargetFrameworks) { lockFile.ProjectFileDependencyGroups.Add(new ProjectFileDependencyGroup( frameworkInfo.FrameworkName.ToString(), frameworkInfo.Dependencies.Select(x => x.LibraryRange.ToString()))); } // Add the targets foreach (var targetGraph in targetGraphs) { var target = new LockFileTarget(); target.TargetFramework = targetGraph.Framework; target.RuntimeIdentifier = targetGraph.RuntimeIdentifier; foreach (var library in targetGraph.Flattened.Select(g => g.Key).OrderBy(x => x)) { var packageInfo = repository.FindPackagesById(library.Name) .FirstOrDefault(p => p.Version == library.Version); if (packageInfo == null) { continue; } var targetLibrary = CreateLockFileTargetLibrary( packageInfo, targetGraph, new DefaultPackagePathResolver(repository.RepositoryRoot), correctedPackageName: library.Name); target.Libraries.Add(targetLibrary); } lockFile.Targets.Add(target); } return lockFile; }
private void GenerateImportsFile(NuGetv3LocalRepository repository, string path, List<string> imports) { var ns = XNamespace.Get("http://schemas.microsoft.com/developer/msbuild/2003"); var doc = new XDocument( new XDeclaration("1.0", "utf-8", "no"), new XElement(ns + "Project", new XAttribute("ToolsVersion", "14.0"), new XElement(ns + "PropertyGroup", new XAttribute("Condition", "'$(NuGetPackageRoot)' == ''"), new XElement(ns + "NuGetPackageRoot", repository.RepositoryRoot)), new XElement(ns + "ImportGroup", imports.Select(i => new XElement(ns + "Import", new XAttribute("Project", Path.Combine("$(NuGetPackageRoot)", i)), new XAttribute("Condition", $"Exists('{Path.Combine("$(NuGetPackageRoot)", i)}')")))))); using (var output = new FileStream(path, FileMode.Create, FileAccess.ReadWrite, FileShare.None)) { doc.Save(output); } }
private void WriteTargetsAndProps(PackageSpec project, List<RestoreTargetGraph> targetGraphs, NuGetv3LocalRepository repository) { // Get the runtime-independent graphs var tfmGraphs = targetGraphs.Where(g => string.IsNullOrEmpty(g.RuntimeIdentifier)).ToList(); if (tfmGraphs.Count > 1) { var name = $"{project.Name}.nuget.targets"; var path = Path.Combine(project.BaseDirectory, name); _log.LogInformation($"Generating MSBuild file {name}"); GenerateMSBuildErrorFile(path); return; } var graph = tfmGraphs[0]; var pathResolver = new DefaultPackagePathResolver(repository.RepositoryRoot); var targets = new List<string>(); var props = new List<string>(); foreach (var library in graph.Flattened.Distinct().OrderBy(g => g.Data.Match.Library)) { var package = repository.FindPackagesById(library.Key.Name).FirstOrDefault(p => p.Version == library.Key.Version); if (package != null) { var criteria = graph.Conventions.Criteria.ForFramework(graph.Framework); var contentItemCollection = new ContentItemCollection(); using (var nupkgStream = File.OpenRead(package.ZipPath)) { var reader = new PackageReader(nupkgStream); contentItemCollection.Load(reader.GetFiles()); } // Find MSBuild thingies var buildItems = contentItemCollection.FindBestItemGroup(criteria, graph.Conventions.Patterns.MSBuildFiles); if (buildItems != null) { // We need to additionally filter to items that are named "{packageId}.targets" and "{packageId}.props" // Filter by file name here and we'll filter by extension when we add things to the lists. var items = buildItems.Items .Where(item => Path.GetFileNameWithoutExtension(item.Path).Equals(package.Id, StringComparison.OrdinalIgnoreCase)) .ToList(); targets.AddRange(items .Select(c => c.Path) .Where(path => Path.GetExtension(path).Equals(".targets", StringComparison.OrdinalIgnoreCase)) .Select(path => Path.Combine(pathResolver.GetPackageDirectory(package.Id, package.Version), path.Replace('/', Path.DirectorySeparatorChar)))); props.AddRange(items .Select(c => c.Path) .Where(path => Path.GetExtension(path).Equals(".props", StringComparison.OrdinalIgnoreCase)) .Select(path => Path.Combine(pathResolver.GetPackageDirectory(package.Id, package.Version), path.Replace('/', Path.DirectorySeparatorChar)))); } } } // Generate the files as needed var targetsName = $"{project.Name}.nuget.targets"; var propsName = $"{project.Name}.nuget.props"; var targetsPath = Path.Combine(project.BaseDirectory, targetsName); var propsPath = Path.Combine(project.BaseDirectory, propsName); if (targets.Any()) { _log.LogInformation($"Generating MSBuild file {targetsName}"); GenerateImportsFile(repository, targetsPath, targets); } else if (File.Exists(targetsPath)) { File.Delete(targetsPath); } if (props.Any()) { _log.LogInformation($"Generating MSBuild file {propsName}"); GenerateImportsFile(repository, propsPath, props); } else if (File.Exists(propsPath)) { File.Delete(propsPath); } }