diff options
| author | Roy Ben-Shabat <Roy@Twine-s.com> | 2019-12-05 11:30:45 +0200 |
|---|---|---|
| committer | Roy Ben-Shabat <Roy@Twine-s.com> | 2019-12-05 11:30:45 +0200 |
| commit | cd467ed8a6afec8dcf50d8dc71c75d9d445f7489 (patch) | |
| tree | 0adc1655c1d11c434061993d47ef77e461284f7f /Software/Visual_Studio/PPC/Tango.PPC.Common/UpdatePackages | |
| parent | e8c9c0b649f31bf5170be409cdf6925aa9fc11b6 (diff) | |
| download | Tango-cd467ed8a6afec8dcf50d8dc71c75d9d445f7489.tar.gz Tango-cd467ed8a6afec8dcf50d8dc71c75d9d445f7489.zip | |
Working on PPC packages...
Diffstat (limited to 'Software/Visual_Studio/PPC/Tango.PPC.Common/UpdatePackages')
7 files changed, 139 insertions, 9 deletions
diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/UpdatePackages/DefaultPackageRunner.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/UpdatePackages/DefaultPackageRunner.cs index 882896f04..e1323916b 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.Common/UpdatePackages/DefaultPackageRunner.cs +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/UpdatePackages/DefaultPackageRunner.cs @@ -6,9 +6,14 @@ using System.IO; using System.Linq; using System.Reflection; using System.Text; +using System.Threading; using System.Threading.Tasks; using Tango.Core; +using Tango.Core.DI; using Tango.Core.Helpers; +using Tango.PPC.Common.Application; +using Tango.PPC.Common.Connection; +using Tango.PPC.Common.Notifications; namespace Tango.PPC.Common.UpdatePackages { @@ -87,11 +92,16 @@ namespace Tango.PPC.Common.UpdatePackages File.WriteAllText(_configFile, json); } - public Task Run(PackageType type, String packagesFolder) + public Task<PackageRunnerResult> Run(PackageType type, String packagesFolder) { - return Task.Factory.StartNew(() => + return Task.Factory.StartNew<PackageRunnerResult>(() => { + PackageRunnerResult result = new PackageRunnerResult(); + PackageContext context = new PackageContext(); + context.ApplicationManager = TangoIOC.Default.GetInstance<IPPCApplicationManager>(); + context.MachineProvider = TangoIOC.Default.GetInstance<IMachineProvider>(); + context.NotificationProvider = TangoIOC.Default.GetInstance<INotificationProvider>(); LogManager.Log($"Running {type}-update packages..."); @@ -163,19 +173,27 @@ namespace Tango.PPC.Common.UpdatePackages { try { - OnPackageRuns(att.Name, installedPackage.State); + OnPackageRuns(att.Name, installedPackage.State, installedPackage.Type); + installedPackage.InstallationDate = DateTime.Now; packageInstance.Run(context).GetAwaiter().GetResult(); installedPackage.State = PackageInstallationState.Installed; installedPackage.FailedReason = null; - OnPackageRuns(att.Name, installedPackage.State); + OnPackageRuns(att.Name, installedPackage.State, installedPackage.Type); LogManager.Log("Package installed successfully."); + + if (att.RequiresRestart) + { + result.RestartRequired = true; + } + + Thread.Sleep(2000); } catch (Exception ex) { LogManager.Log(ex, "Package installation failed."); installedPackage.State = PackageInstallationState.Failed; installedPackage.FailedReason = ex.FlattenMessage(); - OnPackageRuns(att.Name, installedPackage.State); + OnPackageRuns(att.Name, installedPackage.State, installedPackage.Type); continue; } } @@ -210,15 +228,97 @@ namespace Tango.PPC.Common.UpdatePackages { LogManager.Log(ex, "Error saving packages file!"); } + + return result; + }); + } + + public Task<bool> IsPackageInstallationRequired(PackageType type, String packagesFolder) + { + return Task.Factory.StartNew<bool>(() => + { + LogManager.Log("Checking if any package installation is required..."); + + _packagesFile = GetPackagesFile().Result; + + //Get all packages in folder. + foreach (var packageFile in Directory.GetFiles(packagesFolder, "*.dll")) + { + LogManager.Log($"Loading assembly '{Path.GetFileName(packageFile)}'..."); + + Assembly asm; + + //Load assembly and investigate for types based on package type. + try + { + asm = Assembly.LoadFile(packageFile); + } + catch (Exception ex) + { + LogManager.Log(ex, "Error loading assembly!"); + continue; + } + + try + { + foreach (var packageType in asm.GetTypes().Where( + x => typeof(IPPCPackage).IsAssignableFrom(x) && + x.GetCustomAttribute<PPCPackageAttribute>() != null && + x.GetCustomAttribute<PPCPackageAttribute>().Type == type)) + { + LogManager.Log($"Checking package '{packageType.FullName}'..."); + + try + { + //Getting installed package from file. + var installedPackage = _packagesFile.PackageInstallations.SingleOrDefault(x => x.PackageName == packageType.FullName); + + //Check if requires installation. + if (installedPackage == null || installedPackage.State != PackageInstallationState.Installed) + { + if (installedPackage == null) + { + LogManager.Log("Package was never installed."); + return true; + } + else + { + LogManager.Log($"Package installation state is '{installedPackage.State}' due to {installedPackage.FailedReason}"); + return true; + } + } + else + { + LogManager.Log("Package is already installed."); + } + } + catch (Exception ex) + { + LogManager.Log(ex, "Error in handling the package!"); + continue; + } + } + } + catch (Exception ex) + { + LogManager.Log(ex, "Error investigating assembly!"); + continue; + } + } + + LogManager.Log("No packages to install."); + + return false; }); } - protected virtual void OnPackageRuns(String packageName, PackageInstallationState state) + protected virtual void OnPackageRuns(String packageName, PackageInstallationState state, PackageType type) { PackageStateChanged?.Invoke(this, new PackageStateChangedEventArgs() { PackageName = packageName, State = state, + PackageType = type, }); } } diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/UpdatePackages/IPackageRunner.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/UpdatePackages/IPackageRunner.cs index 03c583dca..8a3490f8d 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.Common/UpdatePackages/IPackageRunner.cs +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/UpdatePackages/IPackageRunner.cs @@ -10,6 +10,7 @@ namespace Tango.PPC.Common.UpdatePackages { event EventHandler<PackageStateChangedEventArgs> PackageStateChanged; Task<PackagesFile> GetPackagesFile(); - Task Run(PackageType type, String packagesFolder); + Task<PackageRunnerResult> Run(PackageType type, String packagesFolder); + Task<bool> IsPackageInstallationRequired(PackageType type, String packagesFolder); } } diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/UpdatePackages/PPCPackageAttribute.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/UpdatePackages/PPCPackageAttribute.cs index 7ae4ea52d..3186b57b1 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.Common/UpdatePackages/PPCPackageAttribute.cs +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/UpdatePackages/PPCPackageAttribute.cs @@ -13,10 +13,13 @@ namespace Tango.PPC.Common.UpdatePackages public PackageType Type { get; set; } - public PPCPackageAttribute(PackageType type, String name) + public bool RequiresRestart { get; set; } + + public PPCPackageAttribute(PackageType type, String name, bool requiresRestart) { Type = type; Name = name; + RequiresRestart = requiresRestart; } } } diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/UpdatePackages/PackageContext.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/UpdatePackages/PackageContext.cs index cf96ff026..4cfe49af8 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.Common/UpdatePackages/PackageContext.cs +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/UpdatePackages/PackageContext.cs @@ -3,11 +3,16 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using Tango.PPC.Common.Application; +using Tango.PPC.Common.Connection; +using Tango.PPC.Common.Notifications; namespace Tango.PPC.Common.UpdatePackages { public class PackageContext { - + public IPPCApplicationManager ApplicationManager { get; set; } + public IMachineProvider MachineProvider { get; set; } + public INotificationProvider NotificationProvider { get; set; } } } diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/UpdatePackages/PackageInstallation.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/UpdatePackages/PackageInstallation.cs index bcffb1b6e..bf00915bd 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.Common/UpdatePackages/PackageInstallation.cs +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/UpdatePackages/PackageInstallation.cs @@ -12,8 +12,15 @@ namespace Tango.PPC.Common.UpdatePackages public PackageType Type { get; set; } + public DateTime InstallationDate { get; set; } + public PackageInstallationState State { get; set; } public String FailedReason { get; set; } + + public PackageInstallation() + { + InstallationDate = DateTime.Now; + } } } diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/UpdatePackages/PackageRunnerResult.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/UpdatePackages/PackageRunnerResult.cs new file mode 100644 index 000000000..55f3a490a --- /dev/null +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/UpdatePackages/PackageRunnerResult.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Tango.PPC.Common.UpdatePackages +{ + public class PackageRunnerResult + { + public bool RestartRequired { get; set; } + } +} diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/UpdatePackages/PackageStateChangedEventArgs.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/UpdatePackages/PackageStateChangedEventArgs.cs index 62eb00e5e..9c8cbe2c0 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.Common/UpdatePackages/PackageStateChangedEventArgs.cs +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/UpdatePackages/PackageStateChangedEventArgs.cs @@ -10,5 +10,6 @@ namespace Tango.PPC.Common.UpdatePackages { public PackageInstallationState State { get; set; } public String PackageName { get; set; } + public PackageType PackageType { get; set; } } } |
