From 70bb33599ad40252ff62c95cf216b764e1a5c1a5 Mon Sep 17 00:00:00 2001 From: Donkey Date: Sat, 14 Sep 2024 22:22:16 +0200 Subject: [PATCH] refactor: moved update specific packages/framework logic into TwinpackService --- TwinpackCore/Core/TwinpackService.cs | 62 ++++++++++++- TwinpackTests/SystemTest.cs | 43 +++++++++ TwinpackTests/TwinpackServiceTest.cs | 92 ++++++++++++++++--- .../Dialogs/CatalogWindow.xaml.cs | 10 +- 4 files changed, 191 insertions(+), 16 deletions(-) diff --git a/TwinpackCore/Core/TwinpackService.cs b/TwinpackCore/Core/TwinpackService.cs index 23a3082..a263a34 100644 --- a/TwinpackCore/Core/TwinpackService.cs +++ b/TwinpackCore/Core/TwinpackService.cs @@ -65,6 +65,18 @@ public class RestorePackageOptions : AddPackageOptions public List ExcludedPackages; } + public class UpdatePackageFilters + { + public string ProjectName; + public string PlcName; + public string[] Packages; + public string[] Frameworks; + public string[] Versions; + public string[] Branches; + public string[] Configurations; + public string[] Targets; + } + public class UpdatePackageOptions : AddPackageOptions { public bool IncludeProvidedPackages = false; @@ -366,14 +378,56 @@ public async System.Threading.Tasks.Task> RestorePackagesAsync .ToList(); } - public async System.Threading.Tasks.Task> UpdatePackagesAsync(UpdatePackageOptions options = default, CancellationToken cancellationToken = default) + public async System.Threading.Tasks.Task> UpdatePackagesAsync(UpdatePackageFilters filters = default, UpdatePackageOptions options = default, CancellationToken cancellationToken = default) { - var usedPackages = await RetrieveUsedPackagesAsync(token: cancellationToken); - var packages = usedPackages.Select(x => new PackageItem(x) { Package = x.Used, PackageVersion = x.Update }).ToList(); + var usedPackages = await RetrieveUsedPackagesAsync(); + List packages; + if (filters.Packages != null || filters.Frameworks != null) + { + packages = usedPackages.Where( + x => (filters.ProjectName == null || filters.ProjectName == x.ProjectName) && + (filters.ProjectName == null || filters.ProjectName == x.ProjectName) && + (filters.Packages == null || filters.Packages.Any(y => y == x.Update?.Name)) && + (filters.Frameworks == null || filters.Frameworks.Any(y => y == x.Update?.Framework))) + .Select(x => new PackageItem(x) { Package = new Protocol.Api.PackageGetResponse(x.Update), PackageVersion = x.Update }).ToList(); + + foreach (var package in packages) + { + var i = filters.Packages != null && package.Package.Name != null ? Array.IndexOf(filters.Packages, package.Package.Name) : -1; + if (i >= 0) + { + package.Config.Version = filters.Versions?.ElementAtOrDefault(i) ?? package.PackageVersion.Version; + package.Config.Branch = filters.Branches?.ElementAtOrDefault(i) ?? package.PackageVersion.Branch; + package.Config.Configuration = filters.Configurations?.ElementAtOrDefault(i) ?? package.PackageVersion.Configuration; + package.Config.Target = filters.Targets?.ElementAtOrDefault(i) ?? package.PackageVersion.Target; + } + + i = filters.Frameworks != null && package.PackageVersion.Framework != null ? Array.IndexOf(filters.Frameworks, package.PackageVersion.Framework) : -1; + if (i >= 0) + { + package.Config.Version = filters.Versions?.ElementAtOrDefault(i) ?? package.PackageVersion.Version; + package.Config.Branch = filters.Branches?.ElementAtOrDefault(i) ?? package.PackageVersion.Branch; + package.Config.Configuration = filters.Configurations?.ElementAtOrDefault(i) ?? package.PackageVersion.Configuration; + package.Config.Target = filters.Targets?.ElementAtOrDefault(i) ?? package.PackageVersion.Target; + } + } + + // force new retrievable of metadata + foreach (var package in packages) + { + package.Package = null; + package.PackageVersion = null; + } + } + else + { + packages = usedPackages.Select(x => new PackageItem(x) { Package = new Protocol.Api.PackageGetResponse(x.Update), PackageVersion = x.Update }).ToList(); + } + return await UpdatePackagesAsync(packages, options, cancellationToken); } - public async System.Threading.Tasks.Task> UpdatePackagesAsync(List packages, UpdatePackageOptions options = default, CancellationToken cancellationToken = default) + protected async System.Threading.Tasks.Task> UpdatePackagesAsync(List packages, UpdatePackageOptions options = default, CancellationToken cancellationToken = default) { var usedPackages = await RetrieveUsedPackagesAsync(token: cancellationToken); if (packages.Any(x => !usedPackages.Any(y => x.Config.Name == x.Config.Name))) diff --git a/TwinpackTests/SystemTest.cs b/TwinpackTests/SystemTest.cs index b647250..0056b9a 100644 --- a/TwinpackTests/SystemTest.cs +++ b/TwinpackTests/SystemTest.cs @@ -453,6 +453,49 @@ public async Task SetPackageVersion_VersionExistingOnPackageServers_Async(string Assert.AreEqual(null, plcprojPackages.FirstOrDefault(x => x.Name == "Tc3_Module")?.Version); } + [DataTestMethod] + [DataRow("1.2.3.3")] + [DataRow("1.2.3.4")] + public async Task SetPackageVersion_VersionExistingOnPackageServers_NoSolution_Async(string newVersion) + { + var packageServers = new PackageServerCollection { _packageServer }; + var twinpack = new TwinpackService(packageServers, automationInterface: _automationInterface, config: _config); + + // cleanup + 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(newVersion, new TwinpackService.SetPackageVersionOptions + { + ProjectName = "TestProject", + PlcName = "Plc1", + SyncFrameworkPackages = true, + PreferredFrameworkBranch = "My Special Branch", + PreferredFrameworkTarget = "My Special Target", + PreferredFrameworkConfiguration = "My Special Configuration", + }); + + // check if config was updated correctly + var plc = _config.Projects.FirstOrDefault(x => x.Name == "TestProject").Plcs.FirstOrDefault(x => x.Name == "Plc1"); + var configPackages = plc.Packages; + Assert.AreEqual(newVersion, plc?.Version); + Assert.AreEqual(newVersion, configPackages.FirstOrDefault(x => x.Name == "PlcLibrary1")?.Version); + Assert.AreEqual("main", configPackages.FirstOrDefault(x => x.Name == "PlcLibrary1")?.Branch); + Assert.AreEqual("TC3.1", configPackages.FirstOrDefault(x => x.Name == "PlcLibrary1")?.Target); + Assert.AreEqual("Release", configPackages.FirstOrDefault(x => x.Name == "PlcLibrary1")?.Configuration); + Assert.AreEqual(null, configPackages.FirstOrDefault(x => x.Name == "Tc3_Module")?.Version); + + // check if plcproj was updated as well + 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"); + var plcprojPackages = plcprojPlc.Packages; + var plcprojPackage = plcprojPackages.FirstOrDefault(x => x.Name == "PlcLibrary1"); + Assert.AreEqual(newVersion, plcprojPlc?.Version); + Assert.AreEqual(newVersion, plcprojPackages.FirstOrDefault(x => x.Name == "PlcLibrary1")?.Version); + Assert.AreEqual(null, plcprojPackages.FirstOrDefault(x => x.Name == "Tc3_Module")?.Version); + } + [DataTestMethod] [DataRow("NewTitle", "NewCompany", "6.6.6.6", "NewTitle", "NewCompany", "6.6.6.6")] diff --git a/TwinpackTests/TwinpackServiceTest.cs b/TwinpackTests/TwinpackServiceTest.cs index 7fe066d..b1b8e62 100644 --- a/TwinpackTests/TwinpackServiceTest.cs +++ b/TwinpackTests/TwinpackServiceTest.cs @@ -1098,12 +1098,80 @@ public async Task RestoreAsync_IncludedProvidedPackages_IncludedDependencies() Assert.AreEqual("1.2.3.4", auxExternalPackage.PackageVersion.Version); } + [DataTestMethod] + [DataRow("1.5.0.2", "1.5.0.2")] + [DataRow("1.5.0.3", "1.5.0.3")] + [DataRow(null, "1.5.0.3")] + [DataRow("1.5.0.100", "1.5.0.100")] + public async Task UpdateAsync_NoIncludedProvidedPackages_NoIncludedDependencies_WithFilters1(string version, string expectedVersion) + { + Config config; + var twinpack = BuildMultiProjectConfig(out config); + + var packages = await twinpack.UpdatePackagesAsync( + new TwinpackService.UpdatePackageFilters + { + ProjectName = "ZPlatform Project", + PlcName = "ZPlatform", + Packages = new[] { "ZCore" }, + Versions = new[] { version } + }, + new TwinpackService.UpdatePackageOptions + { + IncludeProvidedPackages = false, + IncludeDependencies = false, + ForceDownload = false + }); + + + Assert.AreEqual(0, packages.Count()); + Assert.IsFalse(config.Projects.First(x => x.Name == "ZCore Project").Plcs.First(x => x.Name == "ZCore").Packages.Any() ); + Assert.AreEqual(expectedVersion, config.Projects.First(x => x.Name == "ZPlatform Project").Plcs.First(x => x.Name == "ZPlatform").Packages.First(x => x.Name == "ZCore").Version ); + Assert.AreEqual("1.2.3.4", config.Projects.First(x => x.Name == "ZPlatform Project").Plcs.First(x => x.Name == "ZPlatform").Packages.First(x => x.Name == "ExternalLib1").Version ); + Assert.AreEqual("1.5.0.1", config.Projects.First(x => x.Name == "ZAux Project").Plcs.First(x => x.Name == "ZAux").Packages.First(x => x.Name == "ZCore").Version ); + Assert.AreEqual("1.5.0.1", config.Projects.First(x => x.Name == "ZAux Project").Plcs.First(x => x.Name == "ZAux").Packages.First(x => x.Name == "ZPlatform").Version ); + } + + [DataTestMethod] + [DataRow("1.5.0.2", "1.5.0.2")] + [DataRow("1.5.0.3", "1.5.0.3")] + [DataRow(null, "1.5.0.2")] + public async Task UpdateAsync_NoIncludedProvidedPackages_NoIncludedDependencies_WithFilters2(string version, string expectedVersion) + { + Config config; + var twinpack = BuildMultiProjectConfig(out config); + + var packages = await twinpack.UpdatePackagesAsync( + new TwinpackService.UpdatePackageFilters + { + ProjectName = "ZAux Project", + PlcName = "ZAux", + Packages = new[] { "ZPlatform" }, + Versions = new[] { version } + }, + new TwinpackService.UpdatePackageOptions + { + IncludeProvidedPackages = false, + IncludeDependencies = false, + ForceDownload = false + }); + + + Assert.AreEqual(0, packages.Count()); + Assert.IsFalse(config.Projects.First(x => x.Name == "ZCore Project").Plcs.First(x => x.Name == "ZCore").Packages.Any()); + Assert.AreEqual("1.5.0.1", config.Projects.First(x => x.Name == "ZPlatform Project").Plcs.First(x => x.Name == "ZPlatform").Packages.First(x => x.Name == "ZCore").Version); + Assert.AreEqual("1.2.3.4", config.Projects.First(x => x.Name == "ZPlatform Project").Plcs.First(x => x.Name == "ZPlatform").Packages.First(x => x.Name == "ExternalLib1").Version); + Assert.AreEqual("1.5.0.1", config.Projects.First(x => x.Name == "ZAux Project").Plcs.First(x => x.Name == "ZAux").Packages.First(x => x.Name == "ZCore").Version); + Assert.AreEqual(expectedVersion, config.Projects.First(x => x.Name == "ZAux Project").Plcs.First(x => x.Name == "ZAux").Packages.First(x => x.Name == "ZPlatform").Version); + } + [TestMethod] public async Task UpdateAsync_NoIncludedProvidedPackages_NoIncludedDependencies() { - var twinpack = BuildMultiProjectConfig(); + var twinpack = BuildMultiProjectConfig(out _); var packages = await twinpack.UpdatePackagesAsync( + new TwinpackService.UpdatePackageFilters { }, new TwinpackService.UpdatePackageOptions { IncludeProvidedPackages = false, @@ -1124,9 +1192,10 @@ public async Task UpdateAsync_NoIncludedProvidedPackages_NoIncludedDependencies( [TestMethod] public async Task UpdateAsync_IncludedProvidedPackages_NoIncludedDependencies() { - var twinpack = BuildMultiProjectConfig(); + var twinpack = BuildMultiProjectConfig(out _); var packages = await twinpack.UpdatePackagesAsync( + new TwinpackService.UpdatePackageFilters { }, new TwinpackService.UpdatePackageOptions { IncludeProvidedPackages = true, @@ -1138,8 +1207,8 @@ public async Task UpdateAsync_IncludedProvidedPackages_NoIncludedDependencies() var platformCorePackage = packages.FirstOrDefault(x => x.PlcName == "ZPlatform" && x.Config.Name == "ZCore"); Assert.AreEqual("ZCore", platformCorePackage.PackageVersion.Name); - Assert.AreEqual("1.5.0.2", platformCorePackage.Config.Version); - Assert.AreEqual("1.5.0.2", platformCorePackage.PackageVersion.Version); + Assert.AreEqual("1.5.0.3", platformCorePackage.Config.Version); + Assert.AreEqual("1.5.0.3", platformCorePackage.PackageVersion.Version); var platformExternalPackage = packages.FirstOrDefault(x => x.PlcName == "ZPlatform" && x.Config.Name == "ExternalLib1"); Assert.AreEqual("ExternalLib1", platformExternalPackage.PackageVersion.Name); @@ -1148,8 +1217,8 @@ public async Task UpdateAsync_IncludedProvidedPackages_NoIncludedDependencies() var auxCorePackage = packages.FirstOrDefault(x => x.PlcName == "ZAux" && x.Config.Name == "ZCore"); Assert.AreEqual("ZCore", auxCorePackage.PackageVersion.Name); - Assert.AreEqual("1.5.0.2", auxCorePackage.Config.Version); - Assert.AreEqual("1.5.0.2", auxCorePackage.PackageVersion.Version); + Assert.AreEqual("1.5.0.3", auxCorePackage.Config.Version); + Assert.AreEqual("1.5.0.3", auxCorePackage.PackageVersion.Version); var auxPlatformPackage = packages.FirstOrDefault(x => x.PlcName == "ZAux" && x.Config.Name == "ZPlatform"); Assert.AreEqual("ZPlatform", auxPlatformPackage.PackageVersion.Name); @@ -1160,9 +1229,10 @@ public async Task UpdateAsync_IncludedProvidedPackages_NoIncludedDependencies() [TestMethod] public async Task UpdateAsync_IncludedProvidedPackages_IncludedDependencies() { - var twinpack = BuildMultiProjectConfig(); + var twinpack = BuildMultiProjectConfig(out _); var packages = await twinpack.UpdatePackagesAsync( + new TwinpackService.UpdatePackageFilters { }, new TwinpackService.UpdatePackageOptions { IncludeProvidedPackages = true, @@ -1174,8 +1244,8 @@ public async Task UpdateAsync_IncludedProvidedPackages_IncludedDependencies() var platformCorePackage = packages.FirstOrDefault(x => x.PlcName == "ZPlatform" && x.Config.Name == "ZCore"); Assert.AreEqual("ZCore", platformCorePackage.PackageVersion.Name); - Assert.AreEqual("1.5.0.2", platformCorePackage.Config.Version); - Assert.AreEqual("1.5.0.2", platformCorePackage.PackageVersion.Version); + Assert.AreEqual("1.5.0.3", platformCorePackage.Config.Version); + Assert.AreEqual("1.5.0.3", platformCorePackage.PackageVersion.Version); var platformExternalPackage = packages.FirstOrDefault(x => x.PlcName == "ZPlatform" && x.Config.Name == "ExternalLib1"); Assert.AreEqual("ExternalLib1", platformExternalPackage.PackageVersion.Name); @@ -1184,8 +1254,8 @@ public async Task UpdateAsync_IncludedProvidedPackages_IncludedDependencies() var auxCorePackage = packages.FirstOrDefault(x => x.PlcName == "ZAux" && x.Config.Name == "ZCore"); Assert.AreEqual("ZCore", auxCorePackage.PackageVersion.Name); - Assert.AreEqual("1.5.0.2", auxCorePackage.Config.Version); - Assert.AreEqual("1.5.0.2", auxCorePackage.PackageVersion.Version); + Assert.AreEqual("1.5.0.3", auxCorePackage.Config.Version); + Assert.AreEqual("1.5.0.3", auxCorePackage.PackageVersion.Version); var auxPlatformPackage = packages.FirstOrDefault(x => x.PlcName == "ZAux" && x.Config.Name == "ZPlatform"); Assert.AreEqual("ZPlatform", auxPlatformPackage.PackageVersion.Name); diff --git a/TwinpackVsixShared/Dialogs/CatalogWindow.xaml.cs b/TwinpackVsixShared/Dialogs/CatalogWindow.xaml.cs index 0affc18..f2231d0 100644 --- a/TwinpackVsixShared/Dialogs/CatalogWindow.xaml.cs +++ b/TwinpackVsixShared/Dialogs/CatalogWindow.xaml.cs @@ -686,7 +686,15 @@ public async void UpdateAllPackageButton_Click(object sender, RoutedEventArgs e) _context.Dte.ExecuteCommand("File.SaveAll"); - await _twinpack.UpdatePackagesAsync(new TwinpackService.UpdatePackageOptions { ForceDownload = ForcePackageVersionDownload, IncludeDependencies = AddDependencies, IncludeProvidedPackages = true }, Token); + await _twinpack.UpdatePackagesAsync( + new TwinpackService.UpdatePackageFilters { }, + new TwinpackService.UpdatePackageOptions + { + ForceDownload = ForcePackageVersionDownload, + IncludeDependencies = AddDependencies, + IncludeProvidedPackages = true + }, + Token); await UpdateCatalogAsync(); _context.Dte.ExecuteCommand("File.SaveAll");