From e5616d4be0f8c7e98c40ce1dd5ff10608f3a00cb Mon Sep 17 00:00:00 2001 From: Donkey Date: Tue, 20 Aug 2024 13:02:05 +0200 Subject: [PATCH] fix: set-version is not restoring not-available packages after a purge --- TwinpackCore/Core/PackageServerCollection.cs | 20 ++++---- TwinpackCore/Core/TwinpackService.cs | 52 +++++++++++++++----- TwinpackTests/SystemTest.cs | 28 +++++++++++ 3 files changed, 77 insertions(+), 23 deletions(-) diff --git a/TwinpackCore/Core/PackageServerCollection.cs b/TwinpackCore/Core/PackageServerCollection.cs index ffce7ac..c5289cd 100644 --- a/TwinpackCore/Core/PackageServerCollection.cs +++ b/TwinpackCore/Core/PackageServerCollection.cs @@ -98,24 +98,24 @@ public async Task FetchPackageAsync(string projectName, string plcN catalogItem.PackageServer = packageServer; // if some data is not present, try to resolve the information - if(item.Branch == null || item.Configuration == null || item.Target == null || item.DistributorName == null) - { - var resolvedPackageVersion = await packageServer.ResolvePackageVersionAsync( + PackageVersionGetResponse resolvedPackageVersion = await packageServer.ResolvePackageVersionAsync( new PlcLibrary { Name = item.Name, DistributorName = item.DistributorName, Version = item.Version }, item.Target, item.Configuration, item.Branch, cancellationToken: cancellationToken); - if(resolvedPackageVersion?.Name != null) - { - item.Branch = resolvedPackageVersion?.Branch; - item.Configuration = resolvedPackageVersion?.Configuration; - item.Target = resolvedPackageVersion?.Target; - item.DistributorName = resolvedPackageVersion?.DistributorName; - } + if (resolvedPackageVersion?.Name != null && (item.Branch == null || item.Configuration == null || item.Target == null || item.DistributorName == null)) + { + item.Branch = resolvedPackageVersion?.Branch; + item.Configuration = resolvedPackageVersion?.Configuration; + item.Target = resolvedPackageVersion?.Target; + item.DistributorName = resolvedPackageVersion?.DistributorName; } + if (resolvedPackageVersion == null) + continue; + // try to get the installed package, if we can't find it at least try to resolve it var packageVersion = await packageServer.GetPackageVersionAsync(new PlcLibrary { DistributorName = item.DistributorName, Name = item.Name, Version = item.Version }, item.Branch, item.Configuration, item.Target, diff --git a/TwinpackCore/Core/TwinpackService.cs b/TwinpackCore/Core/TwinpackService.cs index 50065eb..4aa5da6 100644 --- a/TwinpackCore/Core/TwinpackService.cs +++ b/TwinpackCore/Core/TwinpackService.cs @@ -24,6 +24,7 @@ using static System.Net.Mime.MediaTypeNames; using Twinpack.Configuration; using System.Windows.Navigation; +using Microsoft.VisualStudio.PlatformUI; namespace Twinpack.Core { @@ -511,7 +512,7 @@ private async Task> AffectedPackagesAsync(List pa } if (package.PackageVersion?.Name == null) - break; + continue; if (cache.Any(x => x.ProjectName == package.ProjectName && x.PlcName == package.PlcName && x.PackageVersion.Name == package.PackageVersion.Name) == false) cache.Add(package); @@ -599,9 +600,9 @@ public async System.Threading.Tasks.Task SetPackageVersionAsync(string version, // set the version of all plcs in the project(s) var plcs = _config.Projects - .Where(x => options.ProjectName == null || x.Name == options.ProjectName) + .Where(x => options?.ProjectName == null || x.Name == options?.ProjectName) .SelectMany(x => x.Plcs) - .Where(x => options.PlcName == null || x.Name == options.PlcName); + .Where(x => options?.PlcName == null || x.Name == options?.PlcName); foreach(var plc in plcs) { @@ -612,7 +613,7 @@ public async System.Threading.Tasks.Task SetPackageVersionAsync(string version, } // also include all framework packages if required - if (options.SyncFrameworkPackages) + if (options?.SyncFrameworkPackages == true) { // resolve the plcs as packages var affectedPackages = plcs.Select(x => new PackageItem @@ -637,16 +638,40 @@ public async System.Threading.Tasks.Task SetPackageVersionAsync(string version, affectedPackages = await AffectedPackagesAsync(affectedPackages, cancellationToken); var frameworks = affectedPackages - .Where(x => x.PackageVersion.Framework != null && plcs.Any(y => y.Name == x.PackageVersion.Name)) - .Select(x => x.PackageVersion.Framework).Distinct().ToList(); + .Where(x => x.PackageVersion?.Framework != null && plcs.Any(y => y.Name == x.PackageVersion?.Name)) + .Select(x => x.PackageVersion?.Framework).Distinct().ToList(); - var frameworkPackages = affectedPackages.Where(x => frameworks.Contains(x.PackageVersion.Framework)).ToList(); + var frameworkPackages = affectedPackages.Where(x => frameworks.Contains(x.PackageVersion?.Framework)).ToList(); if (frameworkPackages.Any()) _logger.Info("Synchronizing framework packages"); - var existingPackages = affectedPackages.Where(x => !frameworks.Contains(x.PackageVersion.Framework)).ToList(); - var notExistingPackages = new List(); + var nonFrameworkPackages = affectedPackages.Where(x => !frameworks.Contains(x.PackageVersion?.Framework)).ToList(); + var configuredPackages = plcs + .SelectMany( + x => x.Packages.Select(y => new PackageItem + { + ProjectName = x.ProjectName, + PlcName = x.Name, + Config = y + })) + .Where(x => frameworkPackages.Any(y => x.Config.Name == y.Config.Name) == false) + .Select( + x => new PackageItem(x) + { + Package = nonFrameworkPackages.FirstOrDefault(y => y.ProjectName == x.ProjectName && y.PlcName == x.PlcName && y.Config.Name == y.Config.Name).Package, + PackageVersion = new PackageVersionGetResponse(nonFrameworkPackages.FirstOrDefault(z => z.ProjectName == x.ProjectName && z.PlcName == x.PlcName && z.Config.Name == x.Config.Name).PackageVersion) + { + Version = x.Config.Version, + Branch = x.Config.Branch, + Target = x.Config.Target, + Configuration = x.Config.Configuration + } + } + ) + .ToList(); + + var frameworkPackagesToAdd = new List(); foreach (var project in _config.Projects) { foreach(var plc in project.Plcs) @@ -675,7 +700,7 @@ public async System.Threading.Tasks.Task SetPackageVersionAsync(string version, plcPackage.Configuration = requestedPackage?.Configuration; // since the package actually exists, we can add it to the plcproj file - existingPackages.Add(new PackageItem(affectedPackage) { ProjectName = project.Name, PlcName = plc.Name, Package = null, PackageVersion = null, Config = plcPackage }); + configuredPackages.Add(new PackageItem(affectedPackage) { ProjectName = project.Name, PlcName = plc.Name, Package = null, PackageVersion = null, Config = plcPackage }); } else { @@ -687,7 +712,7 @@ public async System.Threading.Tasks.Task SetPackageVersionAsync(string version, // only a headless interface allows to add not existing packages if (_automationInterface is AutomationInterfaceHeadless) { - notExistingPackages.Add(new PackageItem(affectedPackage) + frameworkPackagesToAdd.Add(new PackageItem(affectedPackage) { ProjectName = project.Name, PlcName = plc.Name, @@ -706,8 +731,9 @@ public async System.Threading.Tasks.Task SetPackageVersionAsync(string version, } } - await AddPackagesAsync(existingPackages); - await AddPackagesAsync(notExistingPackages, new AddPackageOptions { SkipDownload = true, IncludeDependencies = false }); + // add the packages accordingly to the configuration, which might not exist on the serer + await AddPackagesAsync(configuredPackages); + await AddPackagesAsync(frameworkPackagesToAdd, new AddPackageOptions { SkipDownload = true, IncludeDependencies = false }); } _automationInterface?.SaveAll(); diff --git a/TwinpackTests/SystemTest.cs b/TwinpackTests/SystemTest.cs index b4417a7..3646a21 100644 --- a/TwinpackTests/SystemTest.cs +++ b/TwinpackTests/SystemTest.cs @@ -314,6 +314,34 @@ public async Task AddPackage_WithPackageThatIsNotInstalled_Async() Assert.AreEqual("1.2.3.4", plcprojPackage.Version); } + [DataTestMethod] + [DataRow(true)] + [DataRow(false)] + [DataRow(null)] + public async Task SetPackageVersion_NotAPackage_Async(bool? syncFrameworkPackages) + { + var packageServers = new PackageServerCollection { _packageServer }; + var twinpack = new TwinpackService(packageServers, automationInterface: _automationInterface, config: _config); + + // cleanup + await twinpack.RemovePackagesAsync(new List { new PackageItem { ProjectName = "TestProject", PlcName = "Plc1", Config = new ConfigPlcPackage { Name = "PlcLibrary1" } } }); + + await twinpack.AddPackagesAsync(new List { new PackageItem { ProjectName = "TestProject", PlcName = "Plc1", Config = new ConfigPlcPackage { Name = "PlcLibrary1" } } }); + await twinpack.AddPackagesAsync(new List { new PackageItem { ProjectName = "TestProject", PlcName = "Plc1", Config = new ConfigPlcPackage { Name = "Tc3_Module" } } }); + + // act - add package, including dependencies + await twinpack.SetPackageVersionAsync("1.1.1.1", syncFrameworkPackages == null ? null : new TwinpackService.SetPackageVersionOptions { SyncFrameworkPackages = true }); + + // check if config was updated correctly + var plc = _config.Projects.FirstOrDefault(x => x.Name == "TestProject").Plcs.FirstOrDefault(x => x.Name == "Plc1"); + Assert.AreEqual("1.1.1.1", plc?.Version); + + // check if plcproj references were not updated + var plcproj = await ConfigFactory.CreateFromSolutionFileAsync(_config.WorkingDirectory, continueWithoutSolution: false, packageServers: packageServers); + var plcprojPlc = plcproj.Projects.FirstOrDefault(x => x.Name == "TestProject").Plcs.FirstOrDefault(x => x.Name == "Plc1"); + Assert.AreEqual("1.1.1.1", plcprojPlc?.Version); + } + [DataTestMethod] [DataRow("3.3.3.3", "3.3.3.5", "3.3.3.7")] [DataRow("2.3.3.3", "3.3.3.5", "3.3.3.7")]