Skip to content

Commit

Permalink
refactor: moved update specific packages/framework logic into Twinpac…
Browse files Browse the repository at this point in the history
…kService
  • Loading branch information
iadonkey committed Sep 14, 2024
1 parent d7eebe7 commit 70bb335
Show file tree
Hide file tree
Showing 4 changed files with 191 additions and 16 deletions.
62 changes: 58 additions & 4 deletions TwinpackCore/Core/TwinpackService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@ public class RestorePackageOptions : AddPackageOptions
public List<string> 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;
Expand Down Expand Up @@ -366,14 +378,56 @@ public async System.Threading.Tasks.Task<List<PackageItem>> RestorePackagesAsync
.ToList();
}

public async System.Threading.Tasks.Task<List<PackageItem>> UpdatePackagesAsync(UpdatePackageOptions options = default, CancellationToken cancellationToken = default)
public async System.Threading.Tasks.Task<List<PackageItem>> 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<PackageItem> 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<List<PackageItem>> UpdatePackagesAsync(List<PackageItem> packages, UpdatePackageOptions options = default, CancellationToken cancellationToken = default)
protected async System.Threading.Tasks.Task<List<PackageItem>> UpdatePackagesAsync(List<PackageItem> 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)))
Expand Down
43 changes: 43 additions & 0 deletions TwinpackTests/SystemTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PackageItem> { new PackageItem { ProjectName = "TestProject", PlcName = "Plc1", Config = new ConfigPlcPackage { Name = "PlcLibrary1" } } });
await twinpack.AddPackagesAsync(new List<PackageItem> { 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")]
Expand Down
92 changes: 81 additions & 11 deletions TwinpackTests/TwinpackServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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,
Expand All @@ -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);
Expand All @@ -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);
Expand Down
10 changes: 9 additions & 1 deletion TwinpackVsixShared/Dialogs/CatalogWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit 70bb335

Please sign in to comment.