diff --git a/Chocolatey Explorer/Chocolatey Explorer.sln b/Chocolatey Explorer/Chocolatey Explorer.sln index 5fd765b75..473183c78 100644 --- a/Chocolatey Explorer/Chocolatey Explorer.sln +++ b/Chocolatey Explorer/Chocolatey Explorer.sln @@ -7,6 +7,11 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Chocolatey.Explorer.Test", EndProject Project("{930C7802-8A8C-48F9-8165-68863BCCD9DD}") = "Setup_ChocolateyGUI", "..\Setup_ChocolateyGUI\Setup_ChocolateyGUI.wixproj", "{1772C1E5-8896-4E28-933A-9413EB588A82}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{0256ACD9-FC12-4C32-9795-5104410FAED0}" + ProjectSection(SolutionItems) = preProject + .nuget\packages.config = .nuget\packages.config + EndProjectSection +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU diff --git a/Chocolatey Explorer/Services/PackagesService/CachedPackagesService.cs b/Chocolatey Explorer/Services/PackagesService/CachedPackagesService.cs index 1ab049739..784b4f367 100644 --- a/Chocolatey Explorer/Services/PackagesService/CachedPackagesService.cs +++ b/Chocolatey Explorer/Services/PackagesService/CachedPackagesService.cs @@ -12,8 +12,8 @@ class CachedPackagesService : IPackagesService, ICacheable { private static readonly ILog log = LogManager.GetLogger(typeof(CachedPackagesService)); - public delegate void FinishedDelegate(IList packages); public event PackagesService.FinishedDelegate RunFinshed; + public event PackagesService.FailedDelegate RunFailed; private readonly IPackagesService packagesService; private IList availablePackageCache; @@ -22,6 +22,7 @@ class CachedPackagesService : IPackagesService, ICacheable public CachedPackagesService() { packagesService = new ODataPackagesService(); + packagesService.RunFailed += PackagesServiceRunFailed; } public void InvalidateCache() @@ -95,5 +96,11 @@ public void OnUncachedInstalledRunFinished(IList packages) packagesService.RunFinshed -= OnUncachedInstalledRunFinished; OnRunFinshed(packages); } + + private void PackagesServiceRunFailed(System.Exception exc) + { + if (RunFailed != null) + RunFailed(exc); + } } } diff --git a/Chocolatey Explorer/Services/PackagesService/IPackagesService.cs b/Chocolatey Explorer/Services/PackagesService/IPackagesService.cs index 51cbf810b..5d8a9ab02 100644 --- a/Chocolatey Explorer/Services/PackagesService/IPackagesService.cs +++ b/Chocolatey Explorer/Services/PackagesService/IPackagesService.cs @@ -3,6 +3,7 @@ namespace Chocolatey.Explorer.Services public interface IPackagesService { event PackagesService.FinishedDelegate RunFinshed; + event PackagesService.FailedDelegate RunFailed; void ListOfPackages(); void ListOfInstalledPackages(); } diff --git a/Chocolatey Explorer/Services/PackagesService/ODataPackagesService.cs b/Chocolatey Explorer/Services/PackagesService/ODataPackagesService.cs index 21d9e3c9e..fc3b9601a 100644 --- a/Chocolatey Explorer/Services/PackagesService/ODataPackagesService.cs +++ b/Chocolatey Explorer/Services/PackagesService/ODataPackagesService.cs @@ -5,6 +5,7 @@ using System.Xml; using Chocolatey.Explorer.Model; using log4net; +using System.Threading.Tasks; namespace Chocolatey.Explorer.Services { @@ -17,8 +18,8 @@ public class ODataPackagesService : IPackagesService private readonly IPackageVersionXMLParser _xmlParser; private readonly ChocolateyLibDirHelper _libDirHelper; - public delegate void PackagesServiceFinishedDelegate(IList packages); public event PackagesService.FinishedDelegate RunFinshed; + public event PackagesService.FailedDelegate RunFailed; public ODataPackagesService(): this(new SourceService(), new PackageVersionXMLParser()) { @@ -107,17 +108,18 @@ private XmlDocument LoadFeedDoc(int skip) return null; } - public void ListOfInstalledPackages() - { - log.Info("Getting list of installed packages"); - var thread = new Thread(ListOfInstalledPackagsThread) {IsBackground = true}; - thread.Start(); - } - - private void ListOfInstalledPackagsThread() - { - OnRunFinshed(_libDirHelper.ReloadFromDir()); - } + public void ListOfInstalledPackages() + { + log.Info("Getting list of installed packages"); + Task.Factory.StartNew(() => _libDirHelper.ReloadFromDir()) + .ContinueWith((task) => + { + if (!task.IsFaulted) + OnRunFinshed(task.Result); + else if (task.IsFaulted && RunFailed != null) + RunFailed(task.Exception); + }); + } private void OnRunFinshed(IList packages) { diff --git a/Chocolatey Explorer/Services/PackagesService/PackagesService.cs b/Chocolatey Explorer/Services/PackagesService/PackagesService.cs index cdd4df003..e5c51ff8a 100644 --- a/Chocolatey Explorer/Services/PackagesService/PackagesService.cs +++ b/Chocolatey Explorer/Services/PackagesService/PackagesService.cs @@ -4,6 +4,8 @@ using Chocolatey.Explorer.Model; using Chocolatey.Explorer.Powershell; using log4net; +using System; +using System.Threading.Tasks; namespace Chocolatey.Explorer.Services { @@ -19,6 +21,9 @@ public class PackagesService : IPackagesService public delegate void FinishedDelegate(IList packages); public event FinishedDelegate RunFinshed; + public delegate void FailedDelegate(Exception exc); + public event FailedDelegate RunFailed; + public PackagesService(): this(new RunAsync(), new SourceService()) { } @@ -42,13 +47,13 @@ public void ListOfPackages() public void ListOfInstalledPackages() { log.Info("Getting list of installed packages"); - var thread = new Thread(ListOfInstalledPackagsThread) {IsBackground = true}; - thread.Start(); - } - - private void ListOfInstalledPackagsThread() - { - OnRunFinshed(_libDirHelper.ReloadFromDir()); + Task.Factory.StartNew(() => _libDirHelper.ReloadFromDir()) + .ContinueWith((task) => { + if (!task.IsFaulted) + OnRunFinshed(task.Result); + else if (task.IsFaulted && RunFailed != null) + RunFailed(task.Exception); + }); } private void OutputChanged(string line) diff --git a/Chocolatey Explorer/View/PackageManager.cs b/Chocolatey Explorer/View/PackageManager.cs index 8f9a44013..f731490ff 100644 --- a/Chocolatey Explorer/View/PackageManager.cs +++ b/Chocolatey Explorer/View/PackageManager.cs @@ -38,6 +38,7 @@ public PackageManager(IPackagesService packagesService, IPackageVersionService p _packagesService.RunFinshed += PackagesServiceRunFinished; _packageService.LineChanged += PackageServiceLineChanged; _packageService.RunFinshed += PackageServiceRunFinished; + _packagesService.RunFailed += PackagesService_RunFailed; ClearStatus(); PackageGrid.Focus(); UpdateInstallUninstallButtonLabel(); @@ -103,6 +104,18 @@ private void PackagesServiceRunFinished(IList packages) } } + private void PackagesService_RunFailed(Exception exc) + { + //TODO - should we do something to prevent them from using more of the app nd getting more errors? + if (exc is ChocolateyVersionUnknownException || (exc is AggregateException || exc.InnerException is IChocolateyService)) + { + var result = MessageBox.Show("Chocolatey version could not be detected. Either Chocolatey is not installed or we cannot access it.", "Chocolatey not found"); + Application.Exit(); + } + else + MessageBox.Show(String.Format("An unexpected error occurred (good thing you're technical, eh?)\n{0}: {1}", exc.GetType().Name, exc.Message), "Unexpected Application Error"); + } + private void VersionChangedHandler(PackageVersion version) { if (this.InvokeRequired) @@ -264,7 +277,7 @@ private void QueryInstalledPackages() packageTabControl.SelectedTab = tabInstalled; lblProgressbar.Style = ProgressBarStyle.Marquee; PackageGrid.DataSource = new List(); - _packagesService.ListOfInstalledPackages(); + _packagesService.ListOfInstalledPackages(); } }