private SemanticVersion ResolvePackageVersion(string projectFile) { var projectFolder = Path.GetDirectoryName(projectFile); var projectLockFile = Path.Combine(projectFolder, LockFileFormat.LockFileName); if (!File.Exists(projectLockFile)) { _reports.WriteError("The project.lock.json file is missing. Restore the packages to generate it."); return(null); } var lockFileReader = new LockFileFormat(); var lockFile = lockFileReader.Read(projectLockFile); var librariesForPackage = lockFile ?.Libraries .Where(lib => string.Equals(_packageId, lib.Name, StringComparison.OrdinalIgnoreCase)); // Projects can reference multiple versions of the same library if (librariesForPackage.Count() > 1) { _reports.WriteError($"The project references multiple versions of the '{_packageId}'."); return(null); } var packageVersion = librariesForPackage.FirstOrDefault()?.Version; if (packageVersion == null) { _reports.WriteError($"The project is not referencing the '{_packageId}' package. Sources can be retrieved only for packages used by the project."); return(null); } return(packageVersion); }
public static IReadOnlyList <PackageIdentity> GetOrderedProjectDependencies( BuildIntegratedNuGetProject buildIntegratedProject) { var results = new List <PackageIdentity>(); var lockFilePath = ProjectJsonPathUtilities.GetLockFilePath(buildIntegratedProject.JsonConfigPath); var lockFileFormat = new LockFileFormat(); // Read the lock file to find the full closure of dependencies if (File.Exists(lockFilePath)) { var lockFile = lockFileFormat.Read(lockFilePath); var dependencies = new HashSet <PackageDependencyInfo>(PackageIdentity.Comparer); foreach (var target in lockFile.Targets) { foreach (var targetLibrary in target.Libraries) { var identity = new PackageIdentity(targetLibrary.Name, targetLibrary.Version); var dependency = new PackageDependencyInfo(identity, targetLibrary.Dependencies); dependencies.Add(dependency); } } // Sort dependencies var sortedDependencies = SortPackagesByDependencyOrder(dependencies); results.AddRange(sortedDependencies); } return(results); }
public async Task NETCoreProject2Project_VerifyLibFilesUnderCompile(string path, string expected) { // Arrange using (var cacheContext = new SourceCacheContext()) using (var pathContext = new SimpleTestPathContext()) { var logger = new TestLogger(); var sources = new List <PackageSource>(); sources.Add(new PackageSource(pathContext.PackageSource)); var spec1 = NETCoreRestoreTestUtility.GetProject(projectName: "projectA", framework: "netstandard1.6"); var spec2 = NETCoreRestoreTestUtility.GetProject(projectName: "projectB", framework: "netstandard1.3"); var specs = new[] { spec1, spec2 }; // Create fake projects, the real data is in the specs var projects = NETCoreRestoreTestUtility.CreateProjectsFromSpecs(pathContext, specs); // Link projects spec1.RestoreMetadata.TargetFrameworks.Single().ProjectReferences.Add(new ProjectRestoreReference() { ProjectPath = projects[1].ProjectPath, ProjectUniqueName = spec2.RestoreMetadata.ProjectUniqueName, }); var projectDir = Path.GetDirectoryName(spec2.RestoreMetadata.ProjectPath); var absolutePath = Path.Combine(projectDir, "bin", "debug", "b.dll"); spec2.RestoreMetadata.Files.Add(new ProjectRestoreMetadataFile(path, Path.Combine(projectDir, absolutePath))); // Create dg file var dgFile = new DependencyGraphSpec(); dgFile.AddProject(spec1); dgFile.AddProject(spec2); dgFile.AddRestore(spec1.RestoreMetadata.ProjectUniqueName); dgFile.Save(Path.Combine(pathContext.WorkingDirectory, "out.dg")); var lockFormat = new LockFileFormat(); // Act var summaries = await NETCoreRestoreTestUtility.RunRestore(pathContext, logger, sources, dgFile, cacheContext); var success = summaries.All(s => s.Success); // Assert Assert.True(success, "Failed: " + string.Join(Environment.NewLine, logger.Messages)); var assetsFile = lockFormat.Read(Path.Combine(spec1.RestoreMetadata.OutputPath, LockFileFormat.AssetsFileName)); var projectBTarget = assetsFile.Targets.Single().Libraries.Single(e => e.Type == "project"); // Verify compile and runtime Assert.Equal(expected, string.Join("|", projectBTarget.CompileTimeAssemblies.Select(e => e.Path))); Assert.Equal(expected, string.Join("|", projectBTarget.RuntimeAssemblies.Select(e => e.Path))); } }
public static bool RequiresPackageRestore(string functionPath) { string projectFilePath = Path.Combine(functionPath, DotNetConstants.ProjectFileName); if (!File.Exists(projectFilePath)) { // If there's no project file, we can just return from here // as there's nothing to restore return(false); } string lockFilePath = Path.Combine(functionPath, DotNetConstants.ProjectLockFileName); if (!File.Exists(lockFilePath)) { // If have a project.json and no lock file, we need to // restore the packages, just return true and skip validation return(true); } // This mimics the logic used by Nuget to validate a lock file against a given project file. // In order to determine whether we have a match, we: // - Read the project frameworks and their dependencies, // extracting the appropriate version range using the lock file format // - Read the lock file depenency groups // - Ensure that each project dependency matches a dependency in the lock file for the // appropriate group matching the framework (including non-framework specific/project wide dependencies) LockFile lockFile = null; try { var reader = new LockFileFormat(); lockFile = reader.Read(lockFilePath); } catch (FileFormatException) { return(true); } var projectDependencies = GetProjectDependencies(projectFilePath) .Select(d => d.ToLockFileDependencyGroupString()) .OrderBy(d => d, StringComparer.OrdinalIgnoreCase) .ToList(); var dependencyGroups = lockFile.ProjectFileDependencyGroups .Where(d => string.Equals(d.FrameworkName, FrameworkConstants.CommonFrameworks.NetStandard20.DotNetFrameworkName, StringComparison.OrdinalIgnoreCase)) .Aggregate(new List <string>(), (a, d) => { a.AddRange(d.Dependencies.Where(name => !name.StartsWith("NETStandard.Library", StringComparison.OrdinalIgnoreCase))); return(a); }); return(!projectDependencies.SequenceEqual(dependencyGroups.OrderBy(d => d, StringComparer.OrdinalIgnoreCase))); }
public async Task Restore_TamperedPackage_FailsAsync() { // Arrange var nupkg = new SimpleTestPackageContext("A", "1.0.0"); using (var pathContext = new SimpleTestPathContext()) using (var testCertificate = new X509Certificate2(_trustedTestCert.Source.Cert)) { // Set up solution, project, and packages var solution = new SimpleTestSolutionContext(pathContext.SolutionRoot); var projectA = SimpleTestProjectContext.CreateNETCore( "a", pathContext.SolutionRoot, NuGetFramework.Parse("NETStandard2.0")); var packageX = new SimpleTestPackageContext("X", "9.0.0"); var signedPackagePath = await SignedArchiveTestUtility.AuthorSignPackageAsync(testCertificate, packageX, pathContext.PackageSource); SignedArchiveTestUtility.TamperWithPackage(signedPackagePath); projectA.AddPackageToAllFrameworks(packageX); solution.Projects.Add(projectA); solution.Create(pathContext.SolutionRoot); var args = new string[] { projectA.ProjectPath, "-Source", pathContext.PackageSource }; // Act var result = RunRestore(_nugetExePath, pathContext, expectedExitCode: 1, additionalArgs: args); var reader = new LockFileFormat(); var lockFile = reader.Read(projectA.AssetsFileOutputPath); var errors = lockFile.LogMessages.Where(m => m.Level == LogLevel.Error); var warnings = lockFile.LogMessages.Where(m => m.Level == LogLevel.Warning); // Assert result.ExitCode.Should().Be(1); result.Errors.Should().Contain(_NU3008); result.AllOutput.Should().Contain($"WARNING: {_NU3027}"); errors.Count().Should().Be(1); errors.First().Code.Should().Be(NuGetLogCode.NU3008); errors.First().Message.Should().Be(_NU3008Message); errors.First().LibraryId.Should().Be(packageX.ToString()); warnings.Count().Should().Be(1); warnings.First().Code.Should().Be(NuGetLogCode.NU3027); warnings.First().Message.Should().Be(_NU3027Message); warnings.First().LibraryId.Should().Be("X.9.0.0"); } }
public static void UpdateDependencies(SolutionProject project, bool directDependencies, bool flattenedDependencies) { if (flattenedDependencies) { project.FlattenedDependencies.Clear(); } if (directDependencies) { project.DirectDependencies.Clear(); } var projectAssetsJsonPath = Path.Combine(project.FullPath.GetFullDirectory(), @"obj", LockFileFormat.AssetsFileName); if (File.Exists(projectAssetsJsonPath)) { var format = new LockFileFormat(); var projectAssets = format.Read(projectAssetsJsonPath); // Update dependencies if (flattenedDependencies) { foreach (var library in projectAssets.Libraries) { var projectDependency = new Dependency(library.Name, library.Version.ToPackageVersion(), library.Type == "project" ? DependencyType.Project : DependencyType.Package) { MSBuildProject = library.Type == "project" ? library.MSBuildProject : null }; project.FlattenedDependencies.Add(projectDependency); // Try to resolve package if already loaded projectDependency.Package = project.Session.Packages.Find(projectDependency); } } if (directDependencies) { foreach (var projectReference in projectAssets.PackageSpec.RestoreMetadata.TargetFrameworks.First().ProjectReferences) { var projectName = new UFile(projectReference.ProjectUniqueName).GetFileNameWithoutExtension(); project.DirectDependencies.Add(new DependencyRange(projectName, null, DependencyType.Project) { MSBuildProject = projectReference.ProjectPath }); } foreach (var dependency in projectAssets.PackageSpec.TargetFrameworks.First().Dependencies) { if (dependency.AutoReferenced) { continue; } project.DirectDependencies.Add(new DependencyRange(dependency.Name, dependency.LibraryRange.VersionRange.ToPackageVersionRange(), DependencyType.Package)); } } } }
public static async Task <LockFile> ReadWithLock(this LockFileFormat subject, string path) { return(await ConcurrencyUtilities.ExecuteWithFileLockedAsync( path, lockedToken => { var lockFile = FileAccessRetrier.RetryOnFileAccessFailure(() => subject.Read(path)); return lockFile; }, CancellationToken.None)); }
public override bool Execute() { var assetsFilePath = string.Empty; if (!string.IsNullOrEmpty(ProjectAssetsFileAbsolutePath) && File.Exists(ProjectAssetsFileAbsolutePath)) { assetsFilePath = ProjectAssetsFileAbsolutePath; } else { assetsFilePath = Path.Combine(RestoreOutputAbsolutePath, LockFileFormat.AssetsFileName); } if (!File.Exists(assetsFilePath)) { throw new InvalidOperationException(string.Format( CultureInfo.CurrentCulture, Strings.AssetsFileNotFound, assetsFilePath)); } // The assets file is necessary for project and package references. Pack should not do any traversal, // so we leave that work up to restore (which produces the assets file). var lockFileFormat = new LockFileFormat(); var assetsFile = lockFileFormat.Read(assetsFilePath); if (assetsFile.PackageSpec == null) { throw new InvalidOperationException(string.Format( CultureInfo.CurrentCulture, Strings.AssetsFileDoesNotHaveValidPackageSpec, assetsFilePath)); } var projectDirectory = Path.GetDirectoryName(assetsFile.PackageSpec.RestoreMetadata.ProjectPath); // Using the libraries section of the assets file, the library name and version for the project path. var projectPathToLibraryIdentities = assetsFile .Libraries .Where(library => library.MSBuildProject != null) .Select(library => new TaskItem(Path.GetFullPath(Path.Combine( projectDirectory, PathUtility.GetPathWithDirectorySeparator(library.MSBuildProject))))); if (projectPathToLibraryIdentities != null) { ProjectReferences = projectPathToLibraryIdentities.ToArray(); } else { ProjectReferences = Array.Empty <ITaskItem>(); } return(true); }
/// <summary> /// Read lock file /// </summary> public static LockFile GetLockFileOrNull(string lockFilePath) { LockFile lockFile = null; var lockFileFormat = new LockFileFormat(); // Read the lock file to find the full closure of dependencies if (File.Exists(lockFilePath)) { lockFile = lockFileFormat.Read(lockFilePath); } return(lockFile); }
private static ImmutableArray <PackageReference> InitializeAssemblyRegistry(string functionDirectory) { var builder = ImmutableArray <PackageReference> .Empty.ToBuilder(); string fileName = Path.Combine(functionDirectory, DotNetConstants.ProjectLockFileName); if (File.Exists(fileName)) { LockFile lockFile = null; try { var reader = new LockFileFormat(); lockFile = reader.Read(fileName); var target = lockFile.Targets .FirstOrDefault(t => string.Equals(t.TargetFramework.DotNetFrameworkName, FrameworkConstants.CommonFrameworks.NetStandard20.DotNetFrameworkName)); if (target != null) { string nugetCachePath = PackageManager.GetNugetPackagesPath(); // Get all the referenced libraries for the target, excluding the framework references we add by default var libraries = target.Libraries.Where(s => !DotNetConstants.FrameworkReferences.Contains(s.Name)); foreach (var library in libraries) { var package = new PackageReference(library.Name, library.Version.ToFullString()); var assemblies = GetAssembliesList(library.CompileTimeAssemblies, nugetCachePath, package); package.CompileTimeAssemblies.AddRange(assemblies); assemblies = GetAssembliesList(library.RuntimeAssemblies, nugetCachePath, package); package.RuntimeAssemblies.AddRange(assemblies); var frameworkAssemblies = library.FrameworkAssemblies.ToDictionary(a => new AssemblyName(a)); package.FrameworkAssemblies.AddRange(frameworkAssemblies); builder.Add(package); } } } catch (FileFormatException) { return(ImmutableArray <PackageReference> .Empty); } } return(builder.ToImmutableArray()); }
private static bool TryReadLockFile(string directory, out LockFile lockFile) { lockFile = null; string file = Path.Combine(directory, LockFileFormat.LockFileName); if (File.Exists(file)) { using (var stream = File.OpenRead(file)) { lockFile = LockFileFormat.Read(stream); } return(true); } return(false); }
public async Task StandaloneProject_BasicRestore() { // Arrange using (var pathContext = new SimpleTestPathContext()) { var logger = new TestLogger(); var dgFile = new DependencyGraphSpec(); var projectJson = @" { ""frameworks"": { ""netstandard1.6"": { ""dependencies"": { ""a"": ""1.0.0"" } } } }"; var spec = JsonPackageSpecReader.GetPackageSpec(projectJson, "x", Path.Combine(pathContext.SolutionRoot, "project.json")); spec.RestoreMetadata = new ProjectRestoreMetadata(); spec.RestoreMetadata.ProjectStyle = ProjectStyle.Standalone; spec.RestoreMetadata.OutputPath = Path.Combine(pathContext.SolutionRoot, "x"); spec.RestoreMetadata.ProjectUniqueName = "x"; spec.RestoreMetadata.ProjectName = "x"; spec.RestoreMetadata.ProjectPath = Path.Combine(pathContext.SolutionRoot, "x.csproj"); dgFile.AddProject(spec); dgFile.AddRestore("x"); await SimpleTestPackageUtility.CreateFolderFeedV3Async(pathContext.PackageSource, new PackageIdentity("a", NuGetVersion.Parse("1.0.0"))); // Act var result = await CommandsTestUtility.RunSingleRestore(dgFile, pathContext, logger); var path = Path.Combine(spec.RestoreMetadata.OutputPath, "project.assets.json"); // Assert Assert.True(result.Success, "Failed: " + string.Join(Environment.NewLine, logger.Messages)); Assert.True(File.Exists(path)); var lockFormat = new LockFileFormat(); var lockFile = lockFormat.Read(path); Assert.Equal(NuGetFramework.Parse("netstandard1.6"), lockFile.Targets.Single().TargetFramework); Assert.Equal("a", lockFile.Targets.Single().Libraries.Single().Name); } }
public async Task GivenAProjectIsUsedOverAPackageVerifyNoDowngradeWarningAsync() { // Arrange using (var pathContext = new SimpleTestPathContext()) { // Set up solution, project, and packages var solution = new SimpleTestSolutionContext(pathContext.SolutionRoot); var projectA = SimpleTestProjectContext.CreateNETCore( "a", pathContext.SolutionRoot, NuGetFramework.Parse("NETStandard1.5")); var projectB = SimpleTestProjectContext.CreateNETCore( "b", pathContext.SolutionRoot, NuGetFramework.Parse("NETStandard1.5")); // A -> B projectA.AddProjectToAllFrameworks(projectB); var packageX = new SimpleTestPackageContext("x", "9.0.0"); var packageB = new SimpleTestPackageContext("b", "9.0.0"); packageX.Dependencies.Add(packageB); await SimpleTestPackageUtility.CreatePackagesAsync(pathContext.PackageSource, packageX, packageB); // PackageB will be overridden by ProjectB projectA.AddPackageToAllFrameworks(packageX); solution.Projects.Add(projectA); solution.Projects.Add(projectB); solution.Create(pathContext.SolutionRoot); // Act var r = Util.Restore(pathContext, projectA.ProjectPath); var output = r.Item2 + " " + r.Item3; var reader = new LockFileFormat(); var lockFileObj = reader.Read(projectA.AssetsFileOutputPath); // Assert Assert.NotNull(lockFileObj); Assert.Equal(0, lockFileObj.LogMessages.Count()); Assert.DoesNotContain("downgrade", output, StringComparison.OrdinalIgnoreCase); } }
public ImmutableArray <PackageReference> FindUnresolvedPackageReferences(ProjectFileInfo projectFile) { if (projectFile.PackageReferences.Length == 0) { return(ImmutableArray <PackageReference> .Empty); } // If the lock file does not exist, all of the package references are unresolved. if (!File.Exists(projectFile.ProjectAssetsFile)) { return(projectFile.PackageReferences); } var lockFileFormat = new LockFileFormat(); var lockFile = lockFileFormat.Read(projectFile.ProjectAssetsFile); return(FindUnresolvedPackageReferencesInLockFile(projectFile, lockFile)); }
public AssetsFileDependenciesSnapshot UpdateFromAssetsFile(string path) { Requires.NotNull(path, nameof(path)); try { // Parse the file using var fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read | FileShare.Delete, 4096 * 10, FileOptions.SequentialScan); LockFile lockFile = LockFileFormat.Read(fileStream, path); return(new AssetsFileDependenciesSnapshot(lockFile, this)); } catch { return(this); } }
public ModuleLoaderContext(IServiceProvider serviceProvider, string projectDirectory) { ProjectDirectory = projectDirectory; RootDirectory = Microsoft.Framework.Runtime.ProjectResolver.ResolveRootDirectory(ProjectDirectory); // A new project resolver is required. you cannot reuse the one from the // parent service provider as that will be for the parent context. ProjectResolver = new ProjectResolver(ProjectDirectory, RootDirectory); var referenceAssemblyDependencyResolver = new ReferenceAssemblyDependencyResolver(new FrameworkReferenceResolver()); // Need to pass through package directory incase you download a package from the gallary, this needs to know about it var nuGetDependencyProvider = new NuGetDependencyResolver(new PackageRepository( NuGetDependencyResolver.ResolveRepositoryPath(RootDirectory))); var gacDependencyResolver = new GacDependencyResolver(); var projectDepencyProvider = new ProjectReferenceDependencyProvider(ProjectResolver); var unresolvedDependencyProvider = new UnresolvedDependencyProvider(); var projectLockJsonPath = Path.Combine(ProjectDirectory, LockFileFormat.LockFileName); if (File.Exists(projectLockJsonPath)) { var lockFileFormat = new LockFileFormat(); var lockFile = lockFileFormat.Read(projectLockJsonPath); nuGetDependencyProvider.ApplyLockFile(lockFile); } DependencyWalker = new DependencyWalker(new IDependencyProvider[] { projectDepencyProvider, nuGetDependencyProvider, referenceAssemblyDependencyResolver, gacDependencyResolver, unresolvedDependencyProvider }); LibraryExportProvider = new CompositeLibraryExportProvider(new ILibraryExportProvider[] { new ModuleProjectLibraryExportProvider( ProjectResolver, serviceProvider), referenceAssemblyDependencyResolver, gacDependencyResolver, nuGetDependencyProvider }); }
public async Task GivenAPackageDowngradeVerifyDowngradeWarningAsync() { // Arrange using (var pathContext = new SimpleTestPathContext()) { // Set up solution, project, and packages var solution = new SimpleTestSolutionContext(pathContext.SolutionRoot); var projectA = SimpleTestProjectContext.CreateNETCore( "a", pathContext.SolutionRoot, NuGetFramework.Parse("NETStandard1.5")); var packageB = new SimpleTestPackageContext("b", "9.0.0"); var packageI1 = new SimpleTestPackageContext("i", "9.0.0"); var packageI2 = new SimpleTestPackageContext("i", "1.0.0"); packageB.Dependencies.Add(packageI1); await SimpleTestPackageUtility.CreatePackagesAsync(pathContext.PackageSource, packageB, packageI1, packageI2); projectA.AddPackageToAllFrameworks(packageB); projectA.AddPackageToAllFrameworks(packageI2); solution.Projects.Add(projectA); solution.Create(pathContext.SolutionRoot); // Act var r = Util.Restore(pathContext, projectA.ProjectPath); var output = r.Item2 + " " + r.Item3; var reader = new LockFileFormat(); var lockFileObj = reader.Read(projectA.AssetsFileOutputPath); // Assert Assert.NotNull(lockFileObj); Assert.Equal(1, lockFileObj.LogMessages.Count()); Assert.Contains("Detected package downgrade: i from 9.0.0 to 1.0.0", lockFileObj.LogMessages.First().Message, StringComparison.OrdinalIgnoreCase); Assert.Contains("Detected package downgrade: i from 9.0.0 to 1.0.0", output, StringComparison.OrdinalIgnoreCase); } }
public static async Task <PackageVersion> GetPackageVersion(string fullPath) { try { // Solution file: extract projects var solutionDirectory = Path.GetDirectoryName(fullPath) ?? ""; var solution = Stride.Core.VisualStudio.Solution.FromFile(fullPath); foreach (var project in solution.Projects) { if (project.TypeGuid == VisualStudio.KnownProjectTypeGuid.CSharp || project.TypeGuid == VisualStudio.KnownProjectTypeGuid.CSharpNewSystem) { var projectPath = project.FullPath; var projectAssetsJsonPath = Path.Combine(Path.GetDirectoryName(projectPath), @"obj", LockFileFormat.AssetsFileName); #if !STRIDE_LAUNCHER && !STRIDE_VSPACKAGE if (!File.Exists(projectAssetsJsonPath)) { var log = new Stride.Core.Diagnostics.LoggerResult(); await VSProjectHelper.RestoreNugetPackages(log, projectPath); } #endif if (File.Exists(projectAssetsJsonPath)) { var format = new LockFileFormat(); var projectAssets = format.Read(projectAssetsJsonPath); foreach (var library in projectAssets.Libraries) { if ((library.Type == "package" || library.Type == "project") && (library.Name == "Stride.Engine" || library.Name == "Xenko.Engine")) { return(new PackageVersion((string)library.Version.ToString())); } } } } } } catch (Exception e) { e.Ignore(); } return(null); }
public static async Task <LockFile> ReadWithLock(this LockFileFormat subject, string path) { if (!File.Exists(path)) { throw new GracefulException(string.Join( Environment.NewLine, $"File not found `{path}`.", "The project may not have been restored or restore failed - run `dotnet restore`")); } return(await ConcurrencyUtilities.ExecuteWithFileLockedAsync( path, lockedToken => { var lockFile = FileAccessRetrier.RetryOnFileAccessFailure(() => subject.Read(path)); return lockFile; }, CancellationToken.None)); }
public static async Task <LockFile> ReadWithLock(this LockFileFormat subject, string path) { return(await ConcurrencyUtilities.ExecuteWithFileLockedAsync( path, lockedToken => { if (!File.Exists(path)) { throw new GracefulException(string.Join( Environment.NewLine, string.Format(Resources.GetString("FileNotFound"), path), Resources.GetString("ProjectNotRestoredOrRestoreFailed"))); } var lockFile = FileAccessRetrier.RetryOnFileAccessFailure(() => subject.Read(path), Resources.GetString("CouldNotAccessAssetsFile")); return lockFile; }, CancellationToken.None)); }
public static async Task <LockFile> ReadWithLock(this LockFileFormat subject, string path) { if (!File.Exists(path)) { throw new GracefulException(string.Join( Environment.NewLine, string.Format(LocalizableStrings.FileNotFound, path), LocalizableStrings.ProjectNotRestoredOrRestoreFailed)); } return(await ConcurrencyUtilities.ExecuteWithFileLockedAsync( path, lockedToken => { var lockFile = FileAccessRetrier.RetryOnFileAccessFailure(() => subject.Read(path)); return lockFile; }, CancellationToken.None)); }
private LockFile GetAssetsFile(IPackTaskRequest <IMSBuildItem> request) { if (request.PackItem == null) { throw new PackagingException(NuGetLogCode.NU5028, Strings.NoPackItemProvided); } string assetsFilePath = Path.Combine(request.RestoreOutputPath, LockFileFormat.AssetsFileName); if (!File.Exists(assetsFilePath)) { throw new InvalidOperationException(string.Format( CultureInfo.CurrentCulture, Strings.AssetsFileNotFound, assetsFilePath)); } // The assets file is necessary for project and package references. Pack should not do any traversal, // so we leave that work up to restore (which produces the assets file). var lockFileFormat = new LockFileFormat(); return(lockFileFormat.Read(assetsFilePath)); }
public void GivenAnUnknownPackageVerifyError() { // Arrange using (var pathContext = new SimpleTestPathContext()) { // Set up solution, project, and packages var solution = new SimpleTestSolutionContext(pathContext.SolutionRoot); var projectA = SimpleTestProjectContext.CreateNETCore( "a", pathContext.SolutionRoot, NuGetFramework.Parse("NETStandard1.5")); var packageB = new SimpleTestPackageContext("b", "9.0.0"); projectA.AddPackageToAllFrameworks(packageB); solution.Projects.Add(projectA); solution.Create(pathContext.SolutionRoot); // Act var r = Util.Restore(pathContext, projectA.ProjectPath, expectedExitCode: 1); var output = r.Item2 + " " + r.Item3; var reader = new LockFileFormat(); var lockFileObj = reader.Read(projectA.AssetsFileOutputPath); // Assert Assert.NotNull(lockFileObj); Assert.Equal(1, lockFileObj.LogMessages.Count()); Assert.Contains("Unable to find package b. No packages exist with this id in source(s): source", lockFileObj.LogMessages.First().Message, StringComparison.OrdinalIgnoreCase); Assert.Contains("Unable to find package b. No packages exist with this id in source(s): source", output, StringComparison.OrdinalIgnoreCase); } }
public async Task Project2ProjectInLockFile_VerifyProjectsReferencesInLibAndTargets() { // Arrange var sources = new List <PackageSource>(); var project1Json = @" { ""version"": ""1.0.0"", ""frameworks"": { ""net45"": {} } }"; var project2Json = @" { ""version"": ""1.0.0"", ""frameworks"": { ""net45"": {} } }"; var project3Json = @" { ""version"": ""1.0.0"", ""dependencies"": { }, ""frameworks"": { ""net45"": {} } }"; using (var packagesDir = TestDirectory.Create()) using (var workingDir = TestDirectory.Create()) { var project1 = new DirectoryInfo(Path.Combine(workingDir, "projects", "project1")); var project2 = new DirectoryInfo(Path.Combine(workingDir, "projects", "project2")); var project3 = new DirectoryInfo(Path.Combine(workingDir, "projects", "project3")); project1.Create(); project2.Create(); project3.Create(); File.WriteAllText(Path.Combine(project1.FullName, "project.json"), project1Json); File.WriteAllText(Path.Combine(project2.FullName, "project.json"), project2Json); File.WriteAllText(Path.Combine(project3.FullName, "project.json"), project3Json); var project1Path = Path.Combine(project1.FullName, "project1.csproj"); var project2Path = Path.Combine(project2.FullName, "project2.csproj"); var project3Path = Path.Combine(project3.FullName, "project3.csproj"); File.WriteAllText(project1Path, string.Empty); File.WriteAllText(project2Path, string.Empty); File.WriteAllText(project3Path, string.Empty); var specPath1 = Path.Combine(project1.FullName, "project.json"); var specPath2 = Path.Combine(project2.FullName, "project.json"); var specPath3 = Path.Combine(project3.FullName, "project.json"); var spec1 = JsonPackageSpecReader.GetPackageSpec(project1Json, "project1", specPath1); var spec2 = JsonPackageSpecReader.GetPackageSpec(project1Json, "project2", specPath2); var spec3 = JsonPackageSpecReader.GetPackageSpec(project1Json, "project3", specPath3); var logger = new TestLogger(); var restoreContext = new RestoreArgs() { GlobalPackagesFolder = packagesDir, Log = logger, CacheContext = new SourceCacheContext() }; // Modify specs for netcore spec3 = spec3.WithTestRestoreMetadata(); spec3.FilePath = project3Path; spec3.RestoreMetadata.ProjectPath = project3Path; spec3.RestoreMetadata.ProjectUniqueName = project3Path; spec2 = spec2.WithTestRestoreMetadata().WithTestProjectReference(spec3); spec2.FilePath = project2Path; spec2.RestoreMetadata.ProjectPath = project2Path; spec2.RestoreMetadata.ProjectUniqueName = project2Path; spec1 = spec1.WithTestRestoreMetadata().WithTestProjectReference(spec2); spec1.FilePath = project1Path; spec1.RestoreMetadata.ProjectPath = project1Path; spec1.RestoreMetadata.ProjectUniqueName = project1Path; var request = await ProjectJsonTestHelpers.GetRequestAsync(restoreContext, spec1, spec2, spec3); request.LockFilePath = Path.Combine(project1.FullName, "project.assets.json"); var format = new LockFileFormat(); // Act var command = new RestoreCommand(request); var result = await command.ExecuteAsync(); await result.CommitAsync(logger, CancellationToken.None); Assert.True(result.Success, logger.ShowMessages()); var lockFile = format.Read(request.LockFilePath, logger); var project1Lib = lockFile.GetLibrary("project1", NuGetVersion.Parse("1.0.0")); var project2Lib = lockFile.GetLibrary("project2", NuGetVersion.Parse("1.0.0")); var project3Lib = lockFile.GetLibrary("project3", NuGetVersion.Parse("1.0.0")); var project2Target = lockFile.GetTarget(FrameworkConstants.CommonFrameworks.Net45, runtimeIdentifier: null) .Libraries .Where(lib => lib.Name == "project2") .Single(); var project3Target = lockFile.GetTarget(FrameworkConstants.CommonFrameworks.Net45, runtimeIdentifier: null) .Libraries .Where(lib => lib.Name == "project3") .Single(); // Assert Assert.True(result.Success); Assert.Equal(2, lockFile.Libraries.Count); Assert.Equal("project", project2Lib.Type); Assert.Equal("project", project3Lib.Type); Assert.Equal("../project2/project2.csproj", project2Lib.Path); Assert.Equal("../project3/project3.csproj", project3Lib.Path); Assert.Equal("../project2/project2.csproj", project2Lib.MSBuildProject); Assert.Equal("../project3/project3.csproj", project3Lib.MSBuildProject); Assert.Equal(1, project2Target.Dependencies.Count); Assert.Equal("project3", project2Target.Dependencies.Single().Id); Assert.Equal("1.0.0", project2Target.Dependencies.Single().VersionRange.ToLegacyShortString()); Assert.Equal(0, project3Target.Dependencies.Count); Assert.Equal(".NETFramework,Version=v4.5", project2Target.Framework); Assert.Equal(".NETFramework,Version=v4.5", project3Target.Framework); } }
public async Task Project2ProjectInLockFile_VerifyProjectsUnderProjectFileDependencyGroups_External() { // Arrange var sources = new List <PackageSource>(); var projectJson = @" { ""version"": ""1.0.0"", ""dependencies"": { }, ""frameworks"": { ""net45"": {} } }"; using (var packagesDir = TestDirectory.Create()) using (var workingDir = TestDirectory.Create()) { var project1 = new DirectoryInfo(Path.Combine(workingDir, "projects", "project1")); var project2 = new DirectoryInfo(Path.Combine(workingDir, "projects", "project2")); var project3 = new DirectoryInfo(Path.Combine(workingDir, "projects", "project3")); project1.Create(); project2.Create(); project3.Create(); File.WriteAllText(Path.Combine(project1.FullName, "project.json"), projectJson); File.WriteAllText(Path.Combine(project2.FullName, "project.json"), projectJson); File.WriteAllText(Path.Combine(project3.FullName, "project.json"), projectJson); File.WriteAllText(Path.Combine(project1.FullName, "project1.csproj"), string.Empty); File.WriteAllText(Path.Combine(project2.FullName, "project2.csproj"), string.Empty); File.WriteAllText(Path.Combine(project3.FullName, "project3.csproj"), string.Empty); var specPath1 = Path.Combine(project1.FullName, "project.json"); var specPath2 = Path.Combine(project2.FullName, "project.json"); var specPath3 = Path.Combine(project3.FullName, "project.json"); var spec1 = JsonPackageSpecReader.GetPackageSpec(projectJson, "project1", specPath1); var spec2 = JsonPackageSpecReader.GetPackageSpec(projectJson, "project2", specPath2); var spec3 = JsonPackageSpecReader.GetPackageSpec(projectJson, "project3", specPath3); var logger = new TestLogger(); var request = new TestRestoreRequest(spec1, sources, packagesDir, logger); request.ExternalProjects.Add(new ExternalProjectReference( "project1", spec1, Path.Combine(project1.FullName, "project1.xproj"), new string[] { "project2" })); request.ExternalProjects.Add(new ExternalProjectReference( "project2", spec2, Path.Combine(project2.FullName, "project2.csproj"), new string[] { "project3" })); request.ExternalProjects.Add(new ExternalProjectReference( "project3", spec3, Path.Combine(project3.FullName, "project3.csproj"), new string[] { })); request.LockFilePath = Path.Combine(project1.FullName, "project.lock.json"); var format = new LockFileFormat(); // Act var command = new RestoreCommand(request); var result = await command.ExecuteAsync(); await result.CommitAsync(logger, CancellationToken.None); var lockFile = format.Read(request.LockFilePath, logger); var project1Lib = lockFile.GetLibrary("project1", NuGetVersion.Parse("1.0.0")); var project2Lib = lockFile.GetLibrary("project2", NuGetVersion.Parse("1.0.0")); var project3Lib = lockFile.GetLibrary("project3", NuGetVersion.Parse("1.0.0")); var project2Target = lockFile.GetTarget(FrameworkConstants.CommonFrameworks.Net45, runtimeIdentifier: null) .Libraries .Where(lib => lib.Name == "project2") .Single(); var project3Target = lockFile.GetTarget(FrameworkConstants.CommonFrameworks.Net45, runtimeIdentifier: null) .Libraries .Where(lib => lib.Name == "project3") .Single(); // Assert Assert.True(result.Success); Assert.Equal(2, lockFile.Libraries.Count); Assert.Equal("project", project2Lib.Type); Assert.Equal("project", project3Lib.Type); Assert.Equal("../project2/project.json", project2Lib.Path); Assert.Equal("../project3/project.json", project3Lib.Path); Assert.Equal("../project2/project2.csproj", project2Lib.MSBuildProject); Assert.Equal("../project3/project3.csproj", project3Lib.MSBuildProject); Assert.Equal(1, project2Target.Dependencies.Count); Assert.Equal("project3", project2Target.Dependencies.Single().Id); Assert.Equal("1.0.0", project2Target.Dependencies.Single().VersionRange.ToLegacyShortString()); Assert.Equal(0, project3Target.Dependencies.Count); Assert.Equal(".NETFramework,Version=v4.5", project2Target.Framework); Assert.Equal(".NETFramework,Version=v4.5", project3Target.Framework); } }
public async Task Project2ProjectInLockFile_VerifySnapshotVersions() { // Arrange var sources = new List <PackageSource>(); var projectJson = @" { ""version"": ""2.0.0"", ""dependencies"": { }, ""frameworks"": { ""net45"": {} } }"; var project2Json = @" { ""version"": ""2.0.0-*"", ""description"": ""Proj2 Class Library"", ""authors"": [ ""author"" ], ""tags"": [ """" ], ""projectUrl"": """", ""licenseUrl"": """", ""dependencies"": { ""project3"": ""2.0.0-*"" }, ""frameworks"": { ""net45"": { } } }"; var project3Json = @" { ""version"": ""2.0.0-*"", ""description"": ""Proj3 Class Library"", ""authors"": [ ""author"" ], ""tags"": [ """" ], ""projectUrl"": """", ""licenseUrl"": """", ""frameworks"": { ""net45"": { } } }"; var globalJson = @" { ""projects"": [ ""projects"" ] }"; using (var packagesDir = TestDirectory.Create()) using (var workingDir = TestDirectory.Create()) { var project1 = new DirectoryInfo(Path.Combine(workingDir, "projects", "project1")); var project2 = new DirectoryInfo(Path.Combine(workingDir, "projects", "project2")); var project3 = new DirectoryInfo(Path.Combine(workingDir, "projects", "project3")); project1.Create(); project2.Create(); project3.Create(); File.WriteAllText(Path.Combine(project1.FullName, "project.json"), projectJson); File.WriteAllText(Path.Combine(project2.FullName, "project.json"), project2Json); File.WriteAllText(Path.Combine(project3.FullName, "project.json"), project3Json); File.WriteAllText(Path.Combine(workingDir, "global.json"), globalJson); File.WriteAllText(Path.Combine(project1.FullName, "project1.csproj"), string.Empty); File.WriteAllText(Path.Combine(project2.FullName, "project2.xproj"), string.Empty); File.WriteAllText(Path.Combine(project2.FullName, "project3.xproj"), string.Empty); var specPath1 = Path.Combine(project1.FullName, "project.json"); var specPath2 = Path.Combine(project2.FullName, "project.json"); var specPath3 = Path.Combine(project3.FullName, "project.json"); var spec1 = JsonPackageSpecReader.GetPackageSpec(projectJson, "project1", specPath1); var spec2 = JsonPackageSpecReader.GetPackageSpec(project2Json, "project2", specPath2); var spec3 = JsonPackageSpecReader.GetPackageSpec(project3Json, "project3", specPath3); var logger = new TestLogger(); var request = new TestRestoreRequest(spec1, sources, packagesDir, logger); request.ExternalProjects.Add(new ExternalProjectReference( "project1", spec1, Path.Combine(project1.FullName, "project1.csproj"), new string[] { "project2" })); request.ExternalProjects.Add(new ExternalProjectReference( "project2", spec2, Path.Combine(project2.FullName, "project2.xproj"), new string[] { "project3" })); request.ExternalProjects.Add(new ExternalProjectReference( "project3", spec3, Path.Combine(project2.FullName, "project3.xproj"), new string[] { })); request.LockFilePath = Path.Combine(project1.FullName, "project.lock.json"); var format = new LockFileFormat(); // Act var command = new RestoreCommand(request); var result = await command.ExecuteAsync(); await result.CommitAsync(logger, CancellationToken.None); var lockFile = format.Read(request.LockFilePath, logger); var project2Lib = lockFile.GetLibrary("project2", NuGetVersion.Parse("2.0.0")); var project3Lib = lockFile.GetLibrary("project3", NuGetVersion.Parse("2.0.0")); var project2Target = lockFile.GetTarget(FrameworkConstants.CommonFrameworks.Net45, runtimeIdentifier: null) .Libraries .Single(lib => lib.Name == "project2"); var project3Target = lockFile.GetTarget(FrameworkConstants.CommonFrameworks.Net45, runtimeIdentifier: null) .Libraries .Single(lib => lib.Name == "project3"); // Assert Assert.True(result.Success); Assert.Equal("2.0.0", project2Target.Version.ToString()); Assert.Equal("2.0.0", project3Target.Version.ToString()); Assert.Equal("2.0.0", project2Lib.Version.ToString()); Assert.Equal("2.0.0", project3Lib.Version.ToString()); Assert.Equal("2.0.0", project2Target.Dependencies.Single().VersionRange.ToLegacyShortString()); } }
public async Task ExecuteCommandAsync(ListPackageArgs listPackageArgs) { if (!File.Exists(listPackageArgs.Path)) { Console.Error.WriteLine(string.Format(CultureInfo.CurrentCulture, Strings.ListPkg_ErrorFileNotFound, listPackageArgs.Path)); return; } //If the given file is a solution, get the list of projects //If not, then it's a project, which is put in a list var projectsPaths = Path.GetExtension(listPackageArgs.Path).Equals(".sln") ? MSBuildAPIUtility.GetProjectsFromSolution(listPackageArgs.Path).Where(f => File.Exists(f)) : new List <string>(new string[] { listPackageArgs.Path }); var autoReferenceFound = false; var msBuild = new MSBuildAPIUtility(listPackageArgs.Logger); //Print sources, but not for generic list (which is offline) if (listPackageArgs.ReportType != ReportType.Default) { Console.WriteLine(); Console.WriteLine(Strings.ListPkg_SourcesUsedDescription); ProjectPackagesPrintUtility.PrintSources(listPackageArgs.PackageSources); Console.WriteLine(); } foreach (var projectPath in projectsPaths) { //Open project to evaluate properties for the assets //file and the name of the project var project = MSBuildAPIUtility.GetProject(projectPath); if (!MSBuildAPIUtility.IsPackageReferenceProject(project)) { Console.Error.WriteLine(string.Format(CultureInfo.CurrentCulture, Strings.Error_NotPRProject, projectPath)); Console.WriteLine(); continue; } var projectName = project.GetPropertyValue(ProjectName); var assetsPath = project.GetPropertyValue(ProjectAssetsFile); // If the file was not found, print an error message and continue to next project if (!File.Exists(assetsPath)) { Console.Error.WriteLine(string.Format(CultureInfo.CurrentCulture, Strings.Error_AssetsFileNotFound, projectPath)); Console.WriteLine(); } else { var lockFileFormat = new LockFileFormat(); var assetsFile = lockFileFormat.Read(assetsPath); // Assets file validation if (assetsFile.PackageSpec != null && assetsFile.Targets != null && assetsFile.Targets.Count != 0) { // Get all the packages that are referenced in a project var packages = msBuild.GetResolvedVersions(project.FullPath, listPackageArgs.Frameworks, assetsFile, listPackageArgs.IncludeTransitive, includeProjects: listPackageArgs.ReportType == ReportType.Default); // If packages equals null, it means something wrong happened // with reading the packages and it was handled and message printed // in MSBuildAPIUtility function, but we need to move to the next project if (packages != null) { // No packages means that no package references at all were found in the current framework if (!packages.Any()) { Console.WriteLine(string.Format(Strings.ListPkg_NoPackagesFoundForFrameworks, projectName)); } else { if (listPackageArgs.ReportType != ReportType.Default) // generic list package is offline -- no server lookups { PopulateSourceRepositoryCache(listPackageArgs); await GetRegistrationMetadataAsync(packages, listPackageArgs); await AddLatestVersionsAsync(packages, listPackageArgs); } bool printPackages = FilterPackages(packages, listPackageArgs); // Filter packages for dedicated reports, inform user if none if (listPackageArgs.ReportType != ReportType.Default && !printPackages) { switch (listPackageArgs.ReportType) { case ReportType.Outdated: Console.WriteLine(string.Format(Strings.ListPkg_NoUpdatesForProject, projectName)); break; case ReportType.Deprecated: Console.WriteLine(string.Format(Strings.ListPkg_NoDeprecatedPackagesForProject, projectName)); break; case ReportType.Vulnerable: Console.WriteLine(string.Format(Strings.ListPkg_NoVulnerablePackagesForProject, projectName)); break; } } printPackages = printPackages || ReportType.Default == listPackageArgs.ReportType; if (printPackages) { var hasAutoReference = false; ProjectPackagesPrintUtility.PrintPackages(packages, projectName, listPackageArgs, ref hasAutoReference); autoReferenceFound = autoReferenceFound || hasAutoReference; } } } } else { Console.WriteLine(string.Format(Strings.ListPkg_ErrorReadingAssetsFile, assetsPath)); } // Unload project ProjectCollection.GlobalProjectCollection.UnloadProject(project); } } // Print a legend message for auto-reference markers used if (autoReferenceFound) { Console.WriteLine(Strings.ListPkg_AutoReferenceDescription); } }
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); }
private static void Process(FileInfo file) { var json = JObject.Parse(File.ReadAllText(file.FullName)); var format = new LockFileFormat(); var lockFile = format.Read(Path.Combine(file.DirectoryName, "project.lock.json")); var projects = new HashSet<string>(); foreach (var lib in lockFile.Libraries) { if (lib.Type == "project") { projects.Add(lib.Name); } } var dependencies = new List<JProperty>(); var rootDeps = json["dependencies"] as JObject; if (rootDeps != null) { foreach (var dep in rootDeps.Properties()) { dependencies.Add(dep); } } var frameworks = json["frameworks"] as JObject; if (frameworks != null) { foreach (var fw in frameworks.Properties()) { var fwDeps = fw.Value["dependencies"] as JObject; if (fwDeps != null) { foreach (var dep in fwDeps.Properties()) { dependencies.Add(dep); } } } } foreach (var dep in dependencies) { if (projects.Contains(dep.Name)) { if (dep.Value.Type == JTokenType.String) { var newVal = new JObject(); newVal.Add("target", "project"); dep.Value = newVal; } else { var jObj = dep.Value as JObject; var targetProp = jObj.Properties().SingleOrDefault(p => p.Name == "target"); if (targetProp == null) { dep.Value["target"] = "project"; } var versionProp = jObj.Properties().SingleOrDefault(p => p.Name == "version"); if (versionProp != null) { versionProp.Remove(); } } } } File.WriteAllText(file.FullName, json.ToString(), Encoding.UTF8); }
public async Task RestoreTargets_RestoreWithRuntimes() { // Arrange var sources = new List <PackageSource>(); var project1Json = @" { ""version"": ""1.0.0"", ""description"": """", ""authors"": [ ""author"" ], ""tags"": [ """" ], ""projectUrl"": """", ""licenseUrl"": """", ""frameworks"": { ""netstandardapp1.5"": { ""dependencies"": { ""packageA"": ""1.0.0"" } } } }"; using (var workingDir = TestDirectory.Create()) { var packagesDir = new DirectoryInfo(Path.Combine(workingDir, "globalPackages")); var packageSource = new DirectoryInfo(Path.Combine(workingDir, "packageSource")); var project1 = new DirectoryInfo(Path.Combine(workingDir, "projects", "project1")); packagesDir.Create(); packageSource.Create(); project1.Create(); sources.Add(new PackageSource(packageSource.FullName)); File.WriteAllText(Path.Combine(project1.FullName, "project.json"), project1Json); var specPath1 = Path.Combine(project1.FullName, "project.json"); var spec1 = JsonPackageSpecReader.GetPackageSpec(project1Json, "project1", specPath1); var logger = new TestLogger(); var request = new TestRestoreRequest(spec1, sources, packagesDir.FullName, logger); request.LockFilePath = Path.Combine(project1.FullName, "project.lock.json"); request.RequestedRuntimes.Add("win7-x86"); var packageA = new SimpleTestPackageContext() { Id = "packageA" }; packageA.AddFile("lib/netstandard1.5/a.dll"); packageA.AddFile("native/a.dll"); packageA.AddFile("runtimes/unix/native/a.dll"); packageA.AddFile("runtimes/unix/lib/netstandard1.5/a.dll"); packageA.AddFile("runtimes/win7/lib/netstandard1.5/a.dll"); packageA.AddFile("runtimes/win7-x86/lib/netstandard1.5/a.dll"); packageA.AddFile("runtimes/win7-x86/lib/netstandard1.5/en-us/a.resources.dll"); SimpleTestPackageUtility.CreatePackages(packageSource.FullName, packageA); // Act var command = new RestoreCommand(request); var result = await command.ExecuteAsync(); await result.CommitAsync(logger, CancellationToken.None); var format = new LockFileFormat(); var lockFile = format.Read(request.LockFilePath); var targetLib = lockFile.Targets.Single(graph => graph.RuntimeIdentifier == null).Libraries.Single(); var ridTargetLib = lockFile.Targets.Single(graph => graph.RuntimeIdentifier != null).Libraries.Single(); // Assert Assert.True(result.Success); Assert.Equal(5, targetLib.RuntimeTargets.Count); Assert.Equal("runtimes/unix/lib/netstandard1.5/a.dll", targetLib.RuntimeTargets[0].Path); Assert.Equal("runtime", targetLib.RuntimeTargets[0].Properties["assetType"]); Assert.Equal("unix", targetLib.RuntimeTargets[0].Properties["rid"]); Assert.Equal("runtimes/win7-x86/lib/netstandard1.5/en-us/a.resources.dll", targetLib.RuntimeTargets[3].Path); Assert.Equal("resource", targetLib.RuntimeTargets[3].Properties["assetType"]); Assert.Equal("win7-x86", targetLib.RuntimeTargets[3].Properties["rid"]); Assert.Equal("runtimes/unix/native/a.dll", targetLib.RuntimeTargets[1].Path); Assert.Equal("native", targetLib.RuntimeTargets[1].Properties["assetType"]); Assert.Equal("unix", targetLib.RuntimeTargets[1].Properties["rid"]); // This section does not exist for RID graphs Assert.Equal(0, ridTargetLib.RuntimeTargets.Count); } }
protected IEnumerable <RuntimeLibrary> GetNugetReferences(string projectFilePath, Project project) { Logger.WriteInfo("Adding nuget references."); ICollection <RuntimeLibrary> runtimeLibraries = new List <RuntimeLibrary>(); LockFileFormat lockFileFormat = new LockFileFormat(); string lockFileFilePath = Path.Combine(Path.GetDirectoryName(projectFilePath), project.GetPropertyValue(PropertyNames.ProjectAssetsFile)); if (!File.Exists(lockFileFilePath)) { Logger.WriteError($"Lock file {lockFileFilePath} not found. Run dotnet restore before executing Automaty."); throw new AutomatyException(); } LockFile lockFile = lockFileFormat.Read(lockFileFilePath); string targetFramework = project.GetPropertyValue(PropertyNames.TargetFramework); if (string.IsNullOrEmpty(targetFramework)) { Logger.WriteDebug("Multi targeting project assembly detected."); targetFramework = GetSuitableTargetFramework(project.GetPropertyValue(PropertyNames.TargetFrameworks)); Logger.WriteDebug($"Using target framework {targetFramework}."); } LockFileTarget lockFileTarget = lockFile.GetTarget(NuGetUtils.ParseFrameworkName(targetFramework), string.Empty); NuGetPackageResolver nuGetPackageResolver = NuGetPackageResolver.CreateResolver(lockFile, Path.GetDirectoryName(projectFilePath)); // Add nuget references foreach (LockFileTargetLibrary library in lockFileTarget.Libraries) { if (library.Type != LibraryType.Package) { continue; } string packageDirectory = nuGetPackageResolver.GetPackageDirectory(library.Name, library.Version); foreach (LockFileItem file in library.RuntimeAssemblies.Where(file => !NuGetUtils.IsPlaceholderFile(file.Path))) { string filePath = Path.GetFullPath(Path.Combine(packageDirectory, file.Path)); Logger.WriteDebug($"Adding \"{filePath}\"."); runtimeLibraries.Add(new RuntimeLibrary { Name = library.Name, DirectoryName = Path.GetDirectoryName(filePath), FileName = Path.GetFileName(filePath) }); } } return(runtimeLibraries); }
private bool PrunePackages(PublishRoot root) { var resolver = new DefaultPackagePathResolver(root.TargetPackagesPath); // Special cases (for backwards compat) var specialFolders = new List<string> { "native", "InteropAssemblies", "redist", "runtimes" }; if (!root.NoSource) { // 'shared' folder is build time dependency, so we only copy it when deploying with source specialFolders.Add("shared"); } var lockFilePath = Path.GetFullPath(Path.Combine(ApplicationBasePath, LockFileFormat.LockFileName)); var format = new LockFileFormat(); root.LockFile = format.Read(lockFilePath); var keep = new HashSet<string>(); foreach (var target in root.LockFile.Targets) { foreach (var library in target.Libraries) { var packagesDir = resolver.GetInstallPath(library.Name, library.Version); var manifest = resolver.GetManifestFilePath(library.Name, library.Version); keep.Add(manifest); foreach (var path in library.RuntimeAssemblies) { keep.Add(CombinePath(packagesDir, path)); } foreach (var path in library.CompileTimeAssemblies) { keep.Add(CombinePath(packagesDir, path)); } foreach (var path in library.NativeLibraries) { keep.Add(CombinePath(packagesDir, path)); } foreach (var path in library.ResourceAssemblies) { keep.Add(CombinePath(packagesDir, path)); } foreach (var specialFolder in specialFolders) { var specialFolderPath = CombinePath(packagesDir, specialFolder); if (!Directory.Exists(specialFolderPath)) { continue; } keep.AddRange(Directory.EnumerateFiles(specialFolderPath, "*.*", SearchOption.AllDirectories)); } } } foreach (var package in root.Packages) { var packageDir = resolver.GetInstallPath(package.Library.Name, package.Library.Version); var packageFiles = Directory.EnumerateFiles(packageDir, "*.*", SearchOption.AllDirectories); foreach (var file in packageFiles) { if (!keep.Contains(file)) { File.Delete(file); } } root.Operations.DeleteEmptyFolders(packageDir); } return true; }
private void PurgeOrphanPackages() { _reports.Verbose.WriteLine("Removing orphaned packages..."); // Find the packages still used by the command scripts var applicationPackagesStillUsed = _commandsRepo.Commands .Select(cmd => _commandsRepo.FindCommandOwner(cmd)) .Distinct(); // Get all the installed packages var packagesRepo = new PackageRepository( _commandsRepo.PackagesRoot, caseSensitivePackagesName: false); // Key = package<id, version>, Value = bool (true if used, false otherwise) var usedPackages = packagesRepo .GetAllPackages() .SelectMany(pack => pack.Value) .ToDictionary( pack => pack, _ => false); var lockFileFormat = new LockFileFormat(); // Mark all the packages still in used by using the dependencies in the lock file foreach(var applicationPackage in applicationPackagesStillUsed) { var appLockFileFullPath = Path.Combine( _commandsRepo.PackagesRoot.Root, _commandsRepo.PathResolver.GetPackageDirectory(applicationPackage.Id, applicationPackage.Version), InstallBuilder.CommandsFolderName, LockFileFormat.LockFileName); if (!File.Exists(appLockFileFullPath)) { _reports.Verbose.WriteLine("Lock file {0} not found. This package will be removed if it is not used by another application", appLockFileFullPath); // The lock file is missing, probably the app is not installed correctly // unless someone else is using it, we'll remove it continue; } var lockFile = lockFileFormat.Read(appLockFileFullPath); foreach(var dependency in lockFile.PackageLibraries) { var dependencyPackage = new NuGet.PackageInfo( _commandsRepo.PackagesRoot, dependency.Name, dependency.Version, null, null); if (usedPackages.ContainsKey(dependencyPackage)) { // Mark the dependency as used usedPackages[dependencyPackage] = true; } } } // Now it's time to remove those unused packages var unusedPackages = usedPackages .Where(pack => !pack.Value) .Select(pack => pack.Key); foreach (var package in unusedPackages) { packagesRepo.RemovePackage(package); _reports.Verbose.WriteLine("Removed orphaned package: {0} {1}", package.Id, package.Version); } }
public PackageBuilder GetPackageBuilder(IPackTaskRequest <IMSBuildItem> request) { // Load the assets JSON file produced by restore. var assetsFilePath = Path.Combine(request.RestoreOutputPath, LockFileFormat.AssetsFileName); if (!File.Exists(assetsFilePath)) { throw new PackagingException(NuGetLogCode.NU5023, string.Format( CultureInfo.CurrentCulture, Strings.AssetsFileNotFound, assetsFilePath)); } var builder = new PackageBuilder(request.Deterministic) { Id = request.PackageId, Description = request.Description, Title = request.Title, Copyright = request.Copyright, ReleaseNotes = request.ReleaseNotes, RequireLicenseAcceptance = request.RequireLicenseAcceptance, PackageTypes = ParsePackageTypes(request) }; if (request.DevelopmentDependency) { builder.DevelopmentDependency = true; } if (request.PackageVersion != null) { NuGetVersion version; if (!NuGetVersion.TryParse(request.PackageVersion, out version)) { throw new PackagingException(NuGetLogCode.NU5024, string.Format( CultureInfo.CurrentCulture, Strings.InvalidPackageVersion, request.PackageVersion)); } builder.Version = version; } else { builder.Version = new NuGetVersion("1.0.0"); } if (request.Authors != null) { builder.Authors.AddRange(request.Authors); } if (request.Tags != null) { builder.Tags.AddRange(request.Tags); } Uri tempUri; if (Uri.TryCreate(request.LicenseUrl, UriKind.Absolute, out tempUri)) { builder.LicenseUrl = tempUri; } if (Uri.TryCreate(request.ProjectUrl, UriKind.Absolute, out tempUri)) { builder.ProjectUrl = tempUri; } if (Uri.TryCreate(request.IconUrl, UriKind.Absolute, out tempUri)) { builder.IconUrl = tempUri; } if (!string.IsNullOrEmpty(request.RepositoryUrl) || !string.IsNullOrEmpty(request.RepositoryType)) { builder.Repository = new RepositoryMetadata( request.RepositoryType, request.RepositoryUrl, request.RepositoryBranch, request.RepositoryCommit); } builder.LicenseMetadata = BuildLicenseMetadata(request); builder.Icon = request.PackageIcon; if (request.MinClientVersion != null) { Version version; if (!Version.TryParse(request.MinClientVersion, out version)) { throw new PackagingException(NuGetLogCode.NU5022, string.Format( CultureInfo.CurrentCulture, Strings.InvalidMinClientVersion, request.MinClientVersion)); } builder.MinClientVersion = version; } // The assets file is necessary for project and package references. Pack should not do any traversal, // so we leave that work up to restore (which produces the assets file). var lockFileFormat = new LockFileFormat(); var assetsFile = lockFileFormat.Read(assetsFilePath); if (assetsFile.PackageSpec == null) { throw new PackagingException(NuGetLogCode.NU5025, string.Format( CultureInfo.CurrentCulture, Strings.AssetsFileDoesNotHaveValidPackageSpec, assetsFilePath)); } var projectRefToVersionMap = new Dictionary <string, string>(PathUtility.GetStringComparerBasedOnOS()); if (request.ProjectReferencesWithVersions != null && request.ProjectReferencesWithVersions.Any()) { projectRefToVersionMap = request .ProjectReferencesWithVersions .ToDictionary(msbuildItem => msbuildItem.Identity, msbuildItem => msbuildItem.GetProperty("ProjectVersion"), PathUtility.GetStringComparerBasedOnOS()); } var nuGetFrameworkComparer = new NuGetFrameworkFullComparer(); var frameworksWithSuppressedDependencies = new HashSet <NuGetFramework>(nuGetFrameworkComparer); if (request.FrameworksWithSuppressedDependencies != null && request.FrameworksWithSuppressedDependencies.Any()) { frameworksWithSuppressedDependencies = new HashSet <NuGetFramework>(request.FrameworksWithSuppressedDependencies .Select(t => NuGetFramework.Parse(t.Identity)).ToList(), nuGetFrameworkComparer); } PopulateProjectAndPackageReferences(builder, assetsFile, projectRefToVersionMap, frameworksWithSuppressedDependencies); PopulateFrameworkAssemblyReferences(builder, request); PopulateFrameworkReferences(builder, assetsFile); return(builder); }
private SemanticVersion ResolvePackageVersion(string projectFile) { var projectFolder = Path.GetDirectoryName(projectFile); var projectLockFile = Path.Combine(projectFolder, LockFileFormat.LockFileName); if (!File.Exists(projectLockFile)) { _reports.WriteError("The project.lock.json file is missing. Restore the packages to generate it."); return null; } var lockFileReader = new LockFileFormat(); var lockFile = lockFileReader.Read(projectLockFile); var librariesForPackage = lockFile ?.Libraries .Where(lib => string.Equals(_packageId, lib.Name, StringComparison.OrdinalIgnoreCase)); // Projects can reference multiple versions of the same library if (librariesForPackage.Count() > 1) { _reports.WriteError($"The project references multiple versions of the '{_packageId}'."); return null; } var packageVersion = librariesForPackage.FirstOrDefault()?.Version; if (packageVersion == null) { _reports.WriteError($"The project is not referencing the '{_packageId}' package. Sources can be retrieved only for packages used by the project."); return null; } return packageVersion; }