public override async Task <bool> InstallPackageAsync( PackageIdentity packageIdentity, DownloadResourceResult downloadResourceResult, INuGetProjectContext nuGetProjectContext, CancellationToken token) { return(await Runtime.RunInMainThread(async() => { // Check if this NuGet package is already installed and should be removed. ProjectPackageReference existingPackageReference = project.FindPackageReference(packageIdentity); if (existingPackageReference != null) { if (ShouldRemoveExistingPackageReference(existingPackageReference, packageIdentity)) { project.PackageReferences.Remove(existingPackageReference); } else { nuGetProjectContext.Log( MessageLevel.Info, GettextCatalog.GetString("Package '{0}' already installed.", packageIdentity)); return true; } } bool developmentDependency = false; if (IsNuGetBuildPackagingPackage(packageIdentity)) { await GlobalPackagesExtractor.Extract(project.ParentSolution, packageIdentity, downloadResourceResult, token); developmentDependency = true; GenerateNuGetBuildPackagingTargets(packageIdentity); } var packageReference = ProjectPackageReference.Create(packageIdentity); if (developmentDependency) { packageReference.Metadata.SetValue("PrivateAssets", "All"); } project.PackageReferences.Add(packageReference); await SaveProject(); return true; })); }
public async Task GetReferences_ThreeProjectReferencesJsonNet_JsonNetReferenceAvailableToReferencingProjects() { string solutionFileName = Util.GetSampleProject("TransitiveProjectReferences", "TransitiveProjectReferences.sln"); solution = (Solution)await Services.ProjectService.ReadWorkspaceItem(Util.GetMonitor(), solutionFileName); var projectLibC = solution.FindProjectByName("LibC") as DotNetProject; var projectLibB = solution.FindProjectByName("LibB") as DotNetProject; var projectLibA = solution.FindProjectByName("LibA") as DotNetProject; //string fileName = GetType ().Assembly.Location; //var reference = ProjectReference.CreateAssemblyFileReference (fileName); //projectLibA.Items.Add (reference); var packageReference = ProjectPackageReference.Create("Newtonsoft.Json", "10.0.1"); projectLibA.Items.Add(packageReference); await projectLibA.SaveAsync(Util.GetMonitor()); CreateNuGetConfigFile(solution.BaseDirectory); var process = Process.Start("msbuild", $"/t:Restore /p:RestoreDisableParallel=true \"{solutionFileName}\""); Assert.IsTrue(process.WaitForExit(120000), "Timeout restoring NuGet packages."); Assert.AreEqual(0, process.ExitCode); var referencesLibC = await projectLibC.GetReferences(ConfigurationSelector.Default); var referencesLibB = await projectLibB.GetReferences(ConfigurationSelector.Default); var referencesLibA = await projectLibA.GetReferences(ConfigurationSelector.Default); var jsonNetReferenceLibC = referencesLibC.FirstOrDefault(r => r.FilePath.FileName == "Newtonsoft.Json.dll"); var jsonNetReferenceLibB = referencesLibB.FirstOrDefault(r => r.FilePath.FileName == "Newtonsoft.Json.dll"); var jsonNetReferenceLibA = referencesLibA.FirstOrDefault(r => r.FilePath.FileName == "Newtonsoft.Json.dll"); Assert.IsNotNull(jsonNetReferenceLibA); Assert.IsNotNull(jsonNetReferenceLibB); Assert.IsNotNull(jsonNetReferenceLibC); }
public void CanUpdatePackages_HasPackages_PackageReference() { var csharpProject = Services.ProjectService.CreateProject("C#") as DotNetProject; FilePath directory = Util.TmpDir; csharpProject.FileName = directory.Combine("CanUpdatePackagesProject.csproj"); // No package references. bool canUpdate = csharpProject.CanUpdatePackages(); bool hasPackages = csharpProject.HasPackages(); Assert.IsFalse(canUpdate); Assert.IsFalse(hasPackages); // One package reference. var packageReference = ProjectPackageReference.Create("Test", "1.0"); csharpProject.Items.Add(packageReference); canUpdate = csharpProject.CanUpdatePackages(); hasPackages = csharpProject.HasPackages(); Assert.IsTrue(canUpdate); Assert.IsTrue(hasPackages); }
public async Task CanUpdatePackages_HasPackages_PackageReferenceFromDirectoryProps() { var csharpProject = Services.ProjectService.CreateProject("C#") as DotNetProject; FilePath directory = Util.CreateTmpDir("DirectoryPropsTest"); csharpProject.FileName = directory.Combine("TestProject.csproj"); var buildPropsFileName = csharpProject.BaseDirectory.Combine("Build.props"); string xml = "<Project>\n" + " <ItemGroup>\n" + " <PackageReference Include=\"Newtonsoft.Json\" Version=\"8.0.1\" />\n" + " </ItemGroup>\n" + "</Project>"; File.WriteAllText(buildPropsFileName, xml); csharpProject.AddImportIfMissing("Build.props", null); await csharpProject.SaveAsync(Util.GetMonitor()); await csharpProject.MSBuildProject.EvaluateAsync(); // No package references in project. One imported. bool canUpdate = csharpProject.CanUpdatePackages(); bool hasPackages = csharpProject.HasPackages(); Assert.IsFalse(canUpdate); Assert.IsTrue(hasPackages); // One package reference. var packageReference = ProjectPackageReference.Create("Test", "1.0"); csharpProject.Items.Add(packageReference); canUpdate = csharpProject.CanUpdatePackages(); Assert.IsTrue(canUpdate); }
void AddDotNetProjectPackageReference(string packageId, string version) { var packageReference = ProjectPackageReference.Create(packageId, version); dotNetProject.Items.Add(packageReference); }
void AddPackageToProject(string packageId, string packageVersion) { var packageReference = ProjectPackageReference.Create(packageId, packageVersion); dotNetProject.Items.Add(packageReference); }