private async Task CommitAssetsFileAsync( LockFileFormat lockFileFormat, IRestoreResult result, ILogger log, bool toolCommit, CancellationToken token) { // Commit targets/props to disk before the assets file. // Visual Studio typically watches the assets file for changes // and begins a reload when that file changes. var buildFilesToWrite = result.MSBuildOutputFiles .Where(e => BuildAssetsUtils.HasChanges(e.Content, e.Path, log)); BuildAssetsUtils.WriteFiles(buildFilesToWrite, log); // Avoid writing out the lock file if it is the same to avoid triggering an intellisense // update on a restore with no actual changes. if (result.PreviousLockFile == null || !result.PreviousLockFile.Equals(result.LockFile)) { if (toolCommit) { if (result.LockFilePath != null && result.LockFile != null) { log.LogInformation(string.Format(CultureInfo.CurrentCulture, Strings.Log_ToolWritingLockFile, result.LockFilePath)); await FileUtility.ReplaceWithLock( (outputPath) => lockFileFormat.Write(outputPath, result.LockFile), result.LockFilePath); } } else { log.LogInformation(string.Format(CultureInfo.CurrentCulture, Strings.Log_WritingLockFile, result.LockFilePath)); FileUtility.Replace( (outputPath) => lockFileFormat.Write(outputPath, result.LockFile), result.LockFilePath); } } else { if (toolCommit) { log.LogInformation(string.Format(CultureInfo.CurrentCulture, Strings.Log_ToolSkippingAssetsFile, result.LockFilePath)); } else { log.LogInformation(string.Format(CultureInfo.CurrentCulture, Strings.Log_SkippingAssetsFile, result.LockFilePath)); } } }
private static async Task CommitAsync( LockFileFormat lockFileFormat, IRestoreResult result, ILogger log, bool forceWrite, bool toolCommit, CancellationToken token) { // Don't write the lock file if it is Locked AND we're not re-locking the file if (!result.LockFile.IsLocked || result.RelockFile || toolCommit) { // Avoid writing out the lock file if it is the same to avoid triggering an intellisense // update on a restore with no actual changes. if (forceWrite || result.PreviousLockFile == null || !result.PreviousLockFile.Equals(result.LockFile)) { if (toolCommit) { log.LogDebug($"Writing tool lock file to disk. Path: {result.LockFilePath}"); await ConcurrencyUtilities.ExecuteWithFileLockedAsync( result.LockFilePath, lockedToken => { var lockFileDirectory = Path.GetDirectoryName(result.LockFilePath); Directory.CreateDirectory(lockFileDirectory); lockFileFormat.Write(result.LockFilePath, result.LockFile); return(Task.FromResult(0)); }, token); } else { log.LogMinimal($"Writing lock file to disk. Path: {result.LockFilePath}"); lockFileFormat.Write(result.LockFilePath, result.LockFile); } } else { if (toolCommit) { log.LogDebug($"Tool lock file has not changed. Skipping lock file write. Path: {result.LockFilePath}"); } else { log.LogMinimal($"Lock file has not changed. Skipping lock file write. Path: {result.LockFilePath}"); } } } }
private static async Task CommitAsync( LockFileFormat lockFileFormat, IRestoreResult result, ILogger log, bool forceWrite, bool toolCommit, CancellationToken token) { // Commit targets/props to disk before the assets file. // Visual Studio typically watches the assets file for changes // and begins a reload when that file changes. var buildFilesToWrite = result.MSBuildOutputFiles .Where(e => forceWrite || BuildAssetsUtils.HasChanges(e.Content, e.Path, log)); BuildAssetsUtils.WriteFiles(buildFilesToWrite, log); // Avoid writing out the lock file if it is the same to avoid triggering an intellisense // update on a restore with no actual changes. if (forceWrite || result.PreviousLockFile == null || !result.PreviousLockFile.Equals(result.LockFile)) { if (toolCommit) { if (result.LockFilePath != null && result.LockFile != null) { log.LogDebug($"Writing tool lock file to disk. Path: {result.LockFilePath}"); await FileUtility.ReplaceWithLock( (outputPath) => lockFileFormat.Write(outputPath, result.LockFile), result.LockFilePath); } } else { log.LogMinimal($"Writing lock file to disk. Path: {result.LockFilePath}"); FileUtility.Replace( (outputPath) => lockFileFormat.Write(outputPath, result.LockFile), result.LockFilePath); } } else { if (toolCommit) { log.LogDebug($"Tool lock file has not changed. Skipping lock file write. Path: {result.LockFilePath}"); } else { log.LogMinimal($"Lock file has not changed. Skipping lock file write. Path: {result.LockFilePath}"); } } }
public void UWPRestore_ReadLockFileRoundTrip() { using (var workingDir = TestDirectory.Create()) { // Arrange var expectedStream = GetResource("NuGet.Commands.FuncTest.compiler.resources.uwpBlankAppV2.json"); JObject json = null; var format = new LockFileFormat(); using (var reader = new StreamReader(expectedStream)) { json = JObject.Parse(reader.ReadToEnd()); } var path = Path.Combine(workingDir, "project.lock.json"); // Act var lockFile = format.Parse(json.ToString(), path); format.Write(path, lockFile); var jsonOutput = JObject.Parse(File.ReadAllText(path)); // Assert Assert.Equal(json.ToString(), jsonOutput.ToString()); } }
private static void WriteLockFile( LockFileFormat lockFileFormat, IRestoreResult result, bool createDirectory) { if (createDirectory) { } lockFileFormat.Write(result.LockFilePath, result.LockFile); }
private void WriteLockFile(string dir, string libName, string version) { var lockFile = new LockFile { Islocked = false, Libraries = new List <LockFileLibrary> { new LockFileLibrary { Name = libName, Version = new NuGet.SemanticVersion(version), Sha512 = "TestSha" } } }; var lockFormat = new LockFileFormat(); lockFormat.Write($"{dir}/project.lock.json", lockFile); }
private void WriteLockFile(string projectLockFilePath, HashSet <Library> graphItems, NuGetv3LocalRepository repository) { var excludes = new[] { ".nuspec", ".nupkg", ".sha512" }; var lockFile = new LockFile(); lockFile.Islocked = false; foreach (var item in graphItems.OrderBy(x => x.Name)) { var package = repository.FindPackagesById(item.Name).FirstOrDefault(p => p.Version == item.Version); if (package != null) { NuspecReader nuspecReader = null; using (var stream = File.OpenRead(package.ManifestPath)) { nuspecReader = new NuspecReader(stream); } var library = new LockFileLibrary(); library.Name = item.Name; library.Version = item.Version; library.FrameworkReferenceGroups = nuspecReader.GetFrameworkReferenceGroups().ToList(); library.ReferenceGroups = nuspecReader.GetReferenceGroups().ToList(); library.DependencyGroups = nuspecReader.GetDependencyGroups().ToList(); library.Files = Directory.EnumerateFiles(package.ExpandedPath, "*.*", SearchOption.AllDirectories) .Where(path => !excludes.Contains(Path.GetExtension(path))) .Select(path => path.Substring(package.ExpandedPath.Length).TrimStart(Path.DirectorySeparatorChar).Replace(Path.DirectorySeparatorChar, '/')) .ToList(); lockFile.Libraries.Add(library); } } var lockFileFormat = new LockFileFormat(); lockFileFormat.Write(projectLockFilePath, lockFile); }
private void WriteLockFile(string dir, string libName, string version) { var lockFile = new LockFile { Islocked = false, Libraries = new List<LockFileLibrary> { new LockFileLibrary { Name = libName, Version = new NuGet.SemanticVersion(version), Sha512 = "TestSha" } } }; var lockFormat = new LockFileFormat(); lockFormat.Write($"{dir}/project.lock.json", lockFile); }
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.BaseDirectory); var nugetRepository = Repository.Factory.GetCoreV3(request.PackagesDirectory); var context = new RemoteWalkContext(); context.ProjectLibraryProviders.Add( new LocalDependencyProvider( new PackageSpecReferenceDependencyProvider(projectResolver))); 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>(); foreach (var framework in frameworks) { graphs.Add(await WalkDependencies(projectRange, framework, remoteWalker, context)); } 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) { foreach (var graph in graphs) { var runtimeSpecificGraphs = await WalkRuntimeDependencies(projectRange, graph, request.Project.RuntimeGraph, remoteWalker, context, localRepository); 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); return(new RestoreResult(true, graphs, lockFile)); }
private void UpdateLockFile(PublishRoot root) { var lockFileFormat = new LockFileFormat(); string lockFilePath; if (root.NoSource) { lockFilePath = Path.Combine(TargetPath, "root", LockFileFormat.LockFileName); } else { lockFilePath = Path.Combine(TargetPath, LockFileFormat.LockFileName); } LockFile lockFile; if (File.Exists(lockFilePath)) { lockFile = lockFileFormat.Read(lockFilePath); } else { lockFile = new LockFile { Islocked = false }; var project = GetCurrentProject(); // Restore dependency groups for future lockfile validation lockFile.ProjectFileDependencyGroups.Add(new ProjectFileDependencyGroup( string.Empty, project.Dependencies.Select(x => x.LibraryRange.ToString()))); foreach (var frameworkInfo in project.GetTargetFrameworks()) { lockFile.ProjectFileDependencyGroups.Add(new ProjectFileDependencyGroup( frameworkInfo.FrameworkName.ToString(), frameworkInfo.Dependencies.Select(x => x.LibraryRange.ToString()))); } } if (root.NoSource) { // The dependency group shared by all frameworks should only contain the main nupkg (i.e. entrypoint) lockFile.ProjectFileDependencyGroups.RemoveAll(g => string.IsNullOrEmpty(g.FrameworkName)); lockFile.ProjectFileDependencyGroups.Add(new ProjectFileDependencyGroup( string.Empty, new[] { _libraryDescription.LibraryRange.ToString() })); } var repository = new PackageRepository(root.TargetPackagesPath); var resolver = new DefaultPackagePathResolver(root.TargetPackagesPath); // For dependency projects that were published to nupkgs // Add them to lockfile to ensure the contents of lockfile are still valid using (var sha512 = SHA512.Create()) { foreach (var publishProject in root.Projects.Where(p => p.IsPackage)) { var packageInfo = repository.FindPackagesById(publishProject.Name) .SingleOrDefault(); if (packageInfo == null) { root.Reports.Information.WriteLine("Unable to locate published package {0} in {1}", publishProject.Name.Yellow(), repository.RepositoryRoot); continue; } var package = packageInfo.Package; var nupkgPath = resolver.GetPackageFilePath(package.Id, package.Version); var project = publishProject.GetCurrentProject(); lockFile.Libraries.Add(LockFileUtils.CreateLockFileLibraryForProject( project, package, sha512, project.GetTargetFrameworks().Select(f => f.FrameworkName), resolver)); } } lockFileFormat.Write(lockFilePath, lockFile); }
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); }