Skip to content

Commit

Permalink
fix: set-version is not restoring not-available packages after a purge
Browse files Browse the repository at this point in the history
  • Loading branch information
iadonkey committed Aug 20, 2024
1 parent fb90165 commit e5616d4
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 23 deletions.
20 changes: 10 additions & 10 deletions TwinpackCore/Core/PackageServerCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,24 +98,24 @@ public async Task<PackageItem> 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,
Expand Down
52 changes: 39 additions & 13 deletions TwinpackCore/Core/TwinpackService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
using static System.Net.Mime.MediaTypeNames;
using Twinpack.Configuration;
using System.Windows.Navigation;
using Microsoft.VisualStudio.PlatformUI;

namespace Twinpack.Core
{
Expand Down Expand Up @@ -511,7 +512,7 @@ private async Task<List<PackageItem>> AffectedPackagesAsync(List<PackageItem> 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);
Expand Down Expand Up @@ -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)
{
Expand All @@ -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
Expand All @@ -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<PackageItem>();
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<PackageItem>();
foreach (var project in _config.Projects)
{
foreach(var plc in project.Plcs)
Expand Down Expand Up @@ -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
{
Expand All @@ -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,
Expand All @@ -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();
Expand Down
28 changes: 28 additions & 0 deletions TwinpackTests/SystemTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<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 = "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("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")]
Expand Down

0 comments on commit e5616d4

Please sign in to comment.