public async Task Invoke(NuGetVersion newVersion, string packageSource, PackageInProject currentPackage) { var dirName = currentPackage.Path.Info.DirectoryName; var sources = GetSourcesCommandLine(_sources); await _externalProcess.Run(dirName, "dotnet", $"restore {sources}", true); await _externalProcess.Run(dirName, "dotnet", $"remove package {currentPackage.Id}", true); await _externalProcess.Run(dirName, "dotnet", $"add package {currentPackage.Id} -v {newVersion} -s {packageSource}", true); }
public async Task Invoke(NuGetVersion newVersion, string packageSource, PackageInProject currentPackage) { var dirName = currentPackage.Path.Info.DirectoryName; var nuget = NuGetPath.FindExecutable(); var sources = GetSourcesCommandLine(_sources); var arguments = $"update packages.config -Id {currentPackage.Id} -Version {newVersion} {sources}"; _logger.Verbose(arguments); await _externalProcess.Run(dirName, nuget, arguments, true); }
private PackageUpdateSet BuildPackageUpdateSet(PackageInProject pip) { var package = new PackageIdentity(pip.Id, new NuGetVersion("1.4.5")); var latest = new PackageSearchMedatadata(package, new PackageSource("http://none"), null, null); var updates = new PackageLookupResult(VersionChange.Major, latest, null, null); return(new PackageUpdateSet(updates, new List <PackageInProject> { pip })); }
public static void IsPopulated(PackageInProject package) { Assert.That(package, Is.Not.Null); Assert.That(package.PackageVersionRange, Is.Not.Null); Assert.That(package.Version, Is.Not.Null); Assert.That(package.Identity, Is.Not.Null); Assert.That(package.Path, Is.Not.Null); Assert.That(package.Id, Is.Not.Empty); Assert.That(package.Version.ToString(), Is.Not.Empty); Assert.That(package.ProjectReferences, Is.Not.Null); }
public async Task Invoke(NuGetVersion newVersion, string packageSource, PackageInProject currentPackage) { var dirName = currentPackage.Path.Info.DirectoryName; var sources = GetSourcesCommandLine(_sources); var updateCommand = $"cd {dirName}" + $" & dotnet restore {sources}" + $" & dotnet remove package {currentPackage.Id}" + $" & dotnet add package {currentPackage.Id} -v {newVersion} -s {packageSource}"; _logger.Verbose(updateCommand); await _externalProcess.Run(updateCommand, true); }
public static PackageUpdateSet MakePackageUpdateSet(string packageName) { var fooPackage = new PackageIdentity(packageName, new NuGetVersion("1.2.3")); var latest = new PackageSearchMedatadata(fooPackage, new PackageSource("http://none"), null, Enumerable.Empty <PackageDependency>()); var packages = new PackageLookupResult(VersionChange.Major, latest, null, null); var path = new PackagePath("c:\\foo", "bar", PackageReferenceType.ProjectFile); var pip = new PackageInProject(fooPackage, path, null); return(new PackageUpdateSet(packages, new List <PackageInProject> { pip })); }
public static PackageUpdateSet MakeUpdateSet(string packageName, string version = "1.2.3", PackageReferenceType packageRefType = PackageReferenceType.ProjectFile) { var packageId = new PackageIdentity(packageName, new NuGetVersion(version)); var latest = new PackageSearchMedatadata( packageId, OfficialPackageSource(), null, Enumerable.Empty <PackageDependency>()); var packages = new PackageLookupResult(VersionChange.Major, latest, null, null); var pip = new PackageInProject(packageId, MakePackagePath(packageRefType), null) .InList(); return(new PackageUpdateSet(packages, pip)); }
public static void IsPopulated(PackageInProject package) { Assert.That(package, Is.Not.Null); if (package == null) { throw new ArgumentNullException(nameof(package)); } Assert.That(package.PackageVersionRange, Is.Not.Null); Assert.That(package.Version, Is.Not.Null); Assert.That(package.Identity, Is.Not.Null); Assert.That(package.Path, Is.Not.Null); Assert.That(package.Id, Is.Not.Empty); Assert.That(package.Version?.ToString(), Is.Not.Empty); Assert.That(package.ProjectReferences, Is.Not.Null); }
public Task Invoke(PackageInProject currentPackage, NuGetVersion newVersion, PackageSource packageSource, NuGetSources allSources) { XDocument xml; using (var xmlInput = File.OpenRead(currentPackage.Path.FullName)) { xml = XDocument.Load(xmlInput); } using (var xmlOutput = File.Open(currentPackage.Path.FullName, FileMode.Truncate)) { UpdateFile(xmlOutput, newVersion, currentPackage, xml); } return(Task.CompletedTask); }
private void UpdateVersionTo(PackageInProject currentPackage, IEnumerable <XElement> elements, string newVersion) { foreach (var dependencyToUpdate in elements) { _logger.Detailed( $"Updating directory-level dependencies: {currentPackage.Id} in path {currentPackage.Path.FullName}"); var attribute = dependencyToUpdate.Attribute("Version"); if (attribute != null) { attribute.Value = newVersion; } else { dependencyToUpdate.Element("Version").Value = newVersion; } } }
private static PackageInProject PackageFor(string packageId, string packageVersion, string relativePath, PackageInProject refProject = null) { relativePath = relativePath.Replace("{sep}", $"{Path.DirectorySeparatorChar}", StringComparison.OrdinalIgnoreCase); var basePath = "c_temp" + Path.DirectorySeparatorChar + "test"; var refs = new List <string>(); if (refProject != null) { refs.Add(refProject.Path.FullName); } return(new PackageInProject( new PackageIdentity(packageId, new NuGetVersion(packageVersion)), new PackagePath(basePath, relativePath, PackageReferenceType.ProjectFile), refs)); }
public async Task ShouldUpdateDotnetClassicProject() { const string oldPackageVersion = "5.2.3"; const string newPackageVersion = "5.2.4"; const string expectedPackageString = "<package id=\"Microsoft.AspNet.WebApi.Client\" version=\"{packageVersion}\" targetFramework=\"net47\" />"; const string testFolder = nameof(ShouldUpdateDotnetClassicProject); var testProject = $"{testFolder}.csproj"; var tempFolder = UniqueTemporaryFolder(); var workDirectory = Path.Combine(tempFolder.FullPath, testFolder); Directory.CreateDirectory(workDirectory); var packagesFolder = Path.Combine(workDirectory, "packages"); Directory.CreateDirectory(packagesFolder); var projectContents = _testDotNetClassicProject.Replace("{packageVersion}", oldPackageVersion, StringComparison.OrdinalIgnoreCase); var projectPath = Path.Combine(workDirectory, testProject); await File.WriteAllTextAsync(projectPath, projectContents); var packagesConfigContents = _testPackagesConfig.Replace("{packageVersion}", oldPackageVersion, StringComparison.OrdinalIgnoreCase); var packagesConfigPath = Path.Combine(workDirectory, "packages.config"); await File.WriteAllTextAsync(packagesConfigPath, packagesConfigContents); await File.WriteAllTextAsync(Path.Combine(workDirectory, "nuget.config"), _nugetConfig); var logger = Substitute.For <INuKeeperLogger>(); var command = new NuGetUpdatePackageCommand(logger, new NuGetPath(logger), new ExternalProcess(logger)); var packageToUpdate = new PackageInProject("Microsoft.AspNet.WebApi.Client", oldPackageVersion, new PackagePath(workDirectory, testProject, PackageReferenceType.PackagesConfig)); await command.Invoke(packageToUpdate, new NuGetVersion(newPackageVersion), new PackageSource(NuGetConstants.V3FeedUrl), NuGetSources.GlobalFeed); var contents = await File.ReadAllTextAsync(packagesConfigPath); Assert.That(contents, Does.Contain(expectedPackageString.Replace("{packageVersion}", newPackageVersion, StringComparison.OrdinalIgnoreCase))); Assert.That(contents, Does.Not.Contain(expectedPackageString.Replace("{packageVersion}", oldPackageVersion, StringComparison.OrdinalIgnoreCase))); tempFolder.TryDelete(); }
public PackageInProject?Read( string?id, string?version, PackagePath path, IEnumerable <string>?projectReferences) { if (path == null) { return(null); } if (string.IsNullOrWhiteSpace(id)) { _logger.Normal($"Skipping package with no id specified in file '{path.FullName}'."); return(null); } if (string.IsNullOrWhiteSpace(version)) { _logger.Normal($"Skipping package '{id}' with no version specified in file '{path.FullName}'."); return(null); } var packageVersionRange = PackageVersionRange.Parse(id, version); if (packageVersionRange == null) { _logger.Normal($"Skipping package '{id}' with version '{version}' that could not be parsed in file '{path.FullName}'."); return(null); } var pip = new PackageInProject(packageVersionRange, path, projectReferences); var singleVersion = pip.Identity; if (singleVersion == null) { _logger.Normal($"Skipping package '{id}' with version range '{version}' that is not a single version in file '{path.FullName}'."); return(null); } return(pip); }
public async Task Invoke(PackageInProject currentPackage, NuGetVersion newVersion, PackageSource packageSource, NuGetSources allSources) { if (currentPackage == null) { throw new ArgumentNullException(nameof(currentPackage)); } if (allSources == null) { throw new ArgumentNullException(nameof(allSources)); } var projectPath = currentPackage.Path.Info.DirectoryName; var nuget = _nuGetPath.Executable; if (string.IsNullOrWhiteSpace(nuget)) { _logger.Normal("Cannot find NuGet.exe for package update"); return; } var sources = allSources.CommandLine("-Source"); var updateCommand = $"update packages.config -Id {currentPackage.Id} -Version {newVersion} {sources} -NonInteractive"; if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { if (await _monoExecutor.CanRun()) { await _monoExecutor.Run(projectPath, nuget, updateCommand, true); } else { _logger.Error("Cannot run NuGet.exe. It requires either Windows OS Platform or Mono installation"); } } else { await _externalProcess.Run(projectPath, nuget, updateCommand, true); } }
public async Task Invoke(PackageInProject currentPackage, NuGetVersion newVersion, PackageSource packageSource, NuGetSources allSources) { var projectPath = currentPackage.Path.Info.DirectoryName; var projectFileName = currentPackage.Path.Info.Name; var sourceUrl = packageSource.SourceUri.ToString(); var sources = allSources.CommandLine("-s"); var restoreCommand = $"restore {projectFileName} {sources}"; await _externalProcess.Run(projectPath, "dotnet", restoreCommand, true); if (currentPackage.Path.PackageReferenceType == PackageReferenceType.ProjectFileOldStyle) { var removeCommand = $"remove {projectFileName} package {currentPackage.Id}"; await _externalProcess.Run(projectPath, "dotnet", removeCommand, true); } var addCommand = $"add {projectFileName} package {currentPackage.Id} -v {newVersion} -s {sourceUrl}"; await _externalProcess.Run(projectPath, "dotnet", addCommand, true); }
public async Task ShouldUpdateConditionOnTaskImport() { var workDirectory = Path.Combine(TestContext.CurrentContext.WorkDirectory, nameof(ShouldUpdateConditionOnTaskImport)); Directory.CreateDirectory(workDirectory); var projectName = nameof(ShouldUpdateConditionOnTaskImport) + ".csproj"; var projectPath = Path.Combine(workDirectory, projectName); await File.WriteAllTextAsync(projectPath, _testWebApiProject); var subject = new UpdateProjectImportsCommand(); var package = new PackageInProject("acme", "1", new PackagePath(workDirectory, projectName, PackageReferenceType.ProjectFileOldStyle)); await subject.Invoke(package, null, null, NuGetSources.GlobalFeed); var updatedContents = await File.ReadAllTextAsync(projectPath); Assert.That(updatedContents, Does.Not.Contain(_unpatchedImport)); Assert.That(updatedContents, Does.Contain(_patchedImport)); }
private void UpdateNuspec(Stream fileContents, NuGetVersion newVersion, PackageInProject currentPackage, XDocument xml) { var packagesNode = xml.Element("package")?.Element("metadata")?.Element("dependencies"); if (packagesNode == null) { return; } var packageNodeList = packagesNode.Elements() .Where(x => x.Name == "dependency" && x.Attributes("id") .Any(a => a.Value == currentPackage.Id)); foreach (var dependencyToUpdate in packageNodeList) { _logger.Detailed($"Updating nuspec depenencies: {currentPackage.Id} in path {currentPackage.Path.FullName}"); dependencyToUpdate.Attribute("version").Value = newVersion.ToString(); } xml.Save(fileContents); }
private void UpdateFile(Stream fileContents, NuGetVersion newVersion, PackageInProject currentPackage, XDocument xml) { var packagesNode = xml.Element("Project")?.Elements("ItemGroup"); if (packagesNode == null) { return; } var packageRefs = IncludesOrUpdates(currentPackage, packagesNode.Elements("PackageReference")); UpdateVersionTo(currentPackage, packageRefs, newVersion.ToString()); var packageVersions = IncludesOrUpdates(currentPackage, packagesNode.Elements("PackageVersion")); UpdateVersionTo(currentPackage, packageVersions, newVersion.ToString()); var packageDownloads = IncludesOrUpdates(currentPackage, packagesNode.Elements("PackageDownload")); UpdateVersionTo(currentPackage, packageDownloads, $"[{newVersion}]"); xml.Save(fileContents); }
public async Task Invoke(PackageInProject currentPackage, NuGetVersion newVersion, PackageSource packageSource, NuGetSources allSources) { if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { _logger.Normal("Cannot run NuGet.exe package update as OS Platform is not Windows"); return; } var projectPath = currentPackage.Path.Info.DirectoryName; var nuget = NuGetPath.FindExecutable(); if (string.IsNullOrWhiteSpace(nuget)) { _logger.Normal("Cannot find NuGet exe for package update"); return; } var sources = allSources.CommandLine("-Source"); var updateCommand = $"update packages.config -Id {currentPackage.Id} -Version {newVersion} {sources}"; await _externalProcess.Run(projectPath, nuget, updateCommand, true); }
public Task Invoke(PackageInProject currentPackage, NuGetVersion newVersion, PackageSource packageSource, NuGetSources allSources) { if (currentPackage == null) { throw new ArgumentNullException(nameof(currentPackage)); } var projectsToUpdate = new Stack <string>(); projectsToUpdate.Push(currentPackage.Path.FullName); while (projectsToUpdate.Count > 0) { var currentProject = projectsToUpdate.Pop(); XDocument xml; using (var projectContents = File.Open(currentProject, FileMode.Open, FileAccess.ReadWrite)) { xml = XDocument.Load(projectContents); } var projectsToCheck = UpdateConditionsOnProjects(xml, currentProject); foreach (var potentialProject in projectsToCheck) { var fullPath = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(currentProject), potentialProject)); if (File.Exists(fullPath)) { projectsToUpdate.Push(fullPath); } } } return(Task.CompletedTask); }
private async Task ExecuteValidUpdateTest( string testProjectContents, PackageReferenceType packageReferenceType, [CallerMemberName] string memberName = "") { const string oldPackageVersion = "5.2.3"; const string newPackageVersion = "5.2.4"; const string expectedPackageString = "<PackageReference Include=\"Microsoft.AspNet.WebApi.Client\" Version=\"{packageVersion}\" />"; var testFolder = memberName; var testProject = $"{memberName}.csproj"; var workDirectory = Path.Combine(_tempFolder.FullPath, testFolder); Directory.CreateDirectory(workDirectory); var projectContents = testProjectContents.Replace("{packageVersion}", oldPackageVersion); var projectPath = Path.Combine(workDirectory, testProject); await File.WriteAllTextAsync(projectPath, projectContents); var command = new DotNetUpdatePackageCommand(new ExternalProcess(Substitute.For <INuKeeperLogger>())); var packageToUpdate = new PackageInProject("Microsoft.AspNet.WebApi.Client", oldPackageVersion, new PackagePath(workDirectory, testProject, packageReferenceType)); await command.Invoke(packageToUpdate, new NuGetVersion(newPackageVersion), new PackageSource(NuGetConstants.V3FeedUrl), NuGetSources.GlobalFeed); var contents = await File.ReadAllTextAsync(projectPath); Assert.That(contents, Does.Contain(expectedPackageString.Replace("{packageVersion}", newPackageVersion))); Assert.That(contents, Does.Not.Contain(expectedPackageString.Replace("{packageVersion}", oldPackageVersion))); _tempFolder.TryDelete(); }
private void UpdateFile(Stream fileContents, NuGetVersion newVersion, PackageInProject currentPackage, XDocument xml) { var packagesNode = xml.Element("Project")?.Elements("ItemGroup"); if (packagesNode == null) { return; } var packageNodeList = packagesNode.Elements("PackageReference") .Where(x => (x.Attributes("Include").Any(a => a.Value == currentPackage.Id) || x.Attributes("Update").Any(a => a.Value == currentPackage.Id))); foreach (var dependencyToUpdate in packageNodeList) { _logger.Detailed( $"Updating directory-level dependencies: {currentPackage.Id} in path {currentPackage.Path.FullName}"); dependencyToUpdate.Attribute("Version").Value = newVersion.ToString(); } xml.Save(fileContents); }
public async Task Invoke(PackageInProject currentPackage, NuGetVersion newVersion, PackageSource packageSource, NuGetSources allSources) { await Invoke(currentPackage.Path.Info, allSources); }
private bool Match(PackageInProject a, PackageInProject b) { return(a.Path.FullName == b.Path.FullName); }
private void LogVersionedMetapackage(PackageInProject metaPackage) { _logger.Error($"Metapackage '{metaPackage.Id}' has version {metaPackage.Version} in {metaPackage.Path.FullName}, " + "but should not have explicit version."); }