From cd2761d07d8bb9b8c6812c3b4203e3d43158cf89 Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Sat, 13 Apr 2013 16:57:10 -0700 Subject: [PATCH] cleaned up app update --- .../JobTests/AppUpdateJobFixture.cs | 5 +- NzbDrone.Core.Test/NzbDrone.Core.Test.csproj | 8 +- .../GetAvailableUpdateFixture.cs | 7 +- .../GetUpdateLogFixture.cs | 10 +- NzbDrone.Core/Lifecycle/AppUpdateJob.cs | 9 +- NzbDrone.Core/NzbDrone.Core.csproj | 7 +- NzbDrone.Core/Providers/UpdateProvider.cs | 93 ------------------- .../Update/AvailableUpdateService.cs | 46 +++++++++ .../{Model => Update}/UpdatePackage.cs | 3 +- NzbDrone.Core/Update/UpdateProvider.cs | 68 ++++++++++++++ NzbDrone.Update/Providers/UpdateProvider.cs | 2 +- NzbDrone.ncrunchsolution | 1 + 12 files changed, 140 insertions(+), 119 deletions(-) rename NzbDrone.Core.Test/{ProviderTests/UpdateProviderTests => UpdateTests}/GetAvailableUpdateFixture.cs (91%) rename NzbDrone.Core.Test/{ProviderTests/UpdateProviderTests => UpdateTests}/GetUpdateLogFixture.cs (82%) delete mode 100644 NzbDrone.Core/Providers/UpdateProvider.cs create mode 100644 NzbDrone.Core/Update/AvailableUpdateService.cs rename NzbDrone.Core/{Model => Update}/UpdatePackage.cs (79%) create mode 100644 NzbDrone.Core/Update/UpdateProvider.cs diff --git a/NzbDrone.Core.Test/JobTests/AppUpdateJobFixture.cs b/NzbDrone.Core.Test/JobTests/AppUpdateJobFixture.cs index 73021d293..8ba040873 100644 --- a/NzbDrone.Core.Test/JobTests/AppUpdateJobFixture.cs +++ b/NzbDrone.Core.Test/JobTests/AppUpdateJobFixture.cs @@ -12,6 +12,7 @@ using NzbDrone.Core.Lifecycle; using NzbDrone.Core.Model; using NzbDrone.Core.Providers; using NzbDrone.Core.Test.Framework; +using NzbDrone.Core.Update; using NzbDrone.Test.Common; namespace NzbDrone.Core.Test.JobTests @@ -35,7 +36,7 @@ namespace NzbDrone.Core.Test.JobTests { Mocker.GetMock().SetupGet(c => c.SystemTemp).Returns(@"C:\Temp\"); Mocker.GetMock().SetupGet(c => c.Guid).Returns(_clientGuid); - Mocker.GetMock().Setup(c => c.GetAvailableUpdate(It.IsAny())).Returns(updatePackage); + Mocker.GetMock().Setup(c => c.GetAvailableUpdate(It.IsAny())).Returns(updatePackage); } @@ -126,7 +127,7 @@ namespace NzbDrone.Core.Test.JobTests [Test] public void when_no_updates_are_available_should_return_without_error_or_warnings() { - Mocker.GetMock().Setup(c => c.GetAvailableUpdate(It.IsAny())).Returns((UpdatePackage)null); + Mocker.GetMock().Setup(c => c.GetAvailableUpdate(It.IsAny())).Returns((UpdatePackage)null); StartUpdate(); diff --git a/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj b/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj index ece43a531..a4d41f31a 100644 --- a/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj +++ b/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj @@ -177,8 +177,6 @@ - - @@ -209,6 +207,8 @@ + + @@ -349,7 +349,9 @@ - + + + diff --git a/NzbDrone.Core.Test/ProviderTests/UpdateProviderTests/GetAvailableUpdateFixture.cs b/NzbDrone.Core.Test/UpdateTests/GetAvailableUpdateFixture.cs similarity index 91% rename from NzbDrone.Core.Test/ProviderTests/UpdateProviderTests/GetAvailableUpdateFixture.cs rename to NzbDrone.Core.Test/UpdateTests/GetAvailableUpdateFixture.cs index e21103daf..e861a9671 100644 --- a/NzbDrone.Core.Test/ProviderTests/UpdateProviderTests/GetAvailableUpdateFixture.cs +++ b/NzbDrone.Core.Test/UpdateTests/GetAvailableUpdateFixture.cs @@ -1,14 +1,13 @@ -using System; +using System; using FluentAssertions; using NUnit.Framework; using NzbDrone.Common; using NzbDrone.Core.Configuration; -using NzbDrone.Core.Providers; using NzbDrone.Core.Test.Framework; -namespace NzbDrone.Core.Test.ProviderTests.UpdateProviderTests +namespace NzbDrone.Core.Test.UpdateTests { - class GetAvailableUpdateFixture : CoreTest + class GetAvailableUpdateFixture : CoreTest { private static readonly Version LatestTestVersion = new Version("0.6.0.3"); private const string LATEST_TEST_URL = "http://update.nzbdrone.com/_test/NzbDrone.master.0.6.0.3.zip"; diff --git a/NzbDrone.Core.Test/ProviderTests/UpdateProviderTests/GetUpdateLogFixture.cs b/NzbDrone.Core.Test/UpdateTests/GetUpdateLogFixture.cs similarity index 82% rename from NzbDrone.Core.Test/ProviderTests/UpdateProviderTests/GetUpdateLogFixture.cs rename to NzbDrone.Core.Test/UpdateTests/GetUpdateLogFixture.cs index cd029786f..4752e7e6f 100644 --- a/NzbDrone.Core.Test/ProviderTests/UpdateProviderTests/GetUpdateLogFixture.cs +++ b/NzbDrone.Core.Test/UpdateTests/GetUpdateLogFixture.cs @@ -1,13 +1,11 @@ -using System; -using System.Collections.Generic; +using System; using System.IO; using FluentAssertions; using NUnit.Framework; using NzbDrone.Common; -using NzbDrone.Core.Providers; using NzbDrone.Core.Test.Framework; -namespace NzbDrone.Core.Test.ProviderTests.UpdateProviderTests +namespace NzbDrone.Core.Test.UpdateTests { class GetUpdateLogFixture : CoreTest { @@ -43,7 +41,7 @@ namespace NzbDrone.Core.Test.ProviderTests.UpdateProviderTests .Setup(c => c.FolderExists(UpdateLogFolder)) .Returns(false); - var logs = Mocker.Resolve().UpdateLogFile(); + var logs = Mocker.Resolve().UpdateLogFile(); logs.Should().BeEmpty(); } @@ -51,7 +49,7 @@ namespace NzbDrone.Core.Test.ProviderTests.UpdateProviderTests [Test] public void get_logs_should_return_list_of_files_in_log_folder() { - var logs = Mocker.Resolve().UpdateLogFile(); + var logs = Mocker.Resolve().UpdateLogFile(); logs.Should().HaveCount(3); } diff --git a/NzbDrone.Core/Lifecycle/AppUpdateJob.cs b/NzbDrone.Core/Lifecycle/AppUpdateJob.cs index d82aa4562..b62d04893 100644 --- a/NzbDrone.Core/Lifecycle/AppUpdateJob.cs +++ b/NzbDrone.Core/Lifecycle/AppUpdateJob.cs @@ -7,12 +7,13 @@ using NzbDrone.Common; using NzbDrone.Core.Jobs; using NzbDrone.Core.Model.Notification; using NzbDrone.Core.Providers; +using NzbDrone.Core.Update; namespace NzbDrone.Core.Lifecycle { public class AppUpdateJob : IJob { - private readonly UpdateProvider _updateProvider; + private readonly UpdateService _updateService; private readonly EnvironmentProvider _environmentProvider; private readonly DiskProvider _diskProvider; private readonly IHttpProvider _httpProvider; @@ -22,10 +23,10 @@ namespace NzbDrone.Core.Lifecycle private static readonly Logger logger = LogManager.GetCurrentClassLogger(); - public AppUpdateJob(UpdateProvider updateProvider, EnvironmentProvider environmentProvider, DiskProvider diskProvider, + public AppUpdateJob(UpdateService updateService, EnvironmentProvider environmentProvider, DiskProvider diskProvider, IHttpProvider httpProvider, ProcessProvider processProvider, ArchiveProvider archiveProvider, ConfigFileProvider configFileProvider) { - _updateProvider = updateProvider; + _updateService = updateService; _environmentProvider = environmentProvider; _diskProvider = diskProvider; _httpProvider = httpProvider; @@ -48,7 +49,7 @@ namespace NzbDrone.Core.Lifecycle { notification.CurrentMessage = "Checking for updates"; - var updatePackage = _updateProvider.GetAvailableUpdate(_environmentProvider.Version); + var updatePackage = _updateService.GetAvailableUpdate(_environmentProvider.Version); //No updates available if (updatePackage == null) diff --git a/NzbDrone.Core/NzbDrone.Core.csproj b/NzbDrone.Core/NzbDrone.Core.csproj index 1ad63e50f..19856bfde 100644 --- a/NzbDrone.Core/NzbDrone.Core.csproj +++ b/NzbDrone.Core/NzbDrone.Core.csproj @@ -365,7 +365,6 @@ - @@ -496,9 +495,6 @@ Code - - Code - Code @@ -525,6 +521,9 @@ + + + diff --git a/NzbDrone.Core/Providers/UpdateProvider.cs b/NzbDrone.Core/Providers/UpdateProvider.cs deleted file mode 100644 index d50f4e64a..000000000 --- a/NzbDrone.Core/Providers/UpdateProvider.cs +++ /dev/null @@ -1,93 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Globalization; -using System.IO; -using System.Linq; -using System.Text.RegularExpressions; -using NLog; -using NzbDrone.Common; -using NzbDrone.Core.Configuration; -using NzbDrone.Core.Model; - - -namespace NzbDrone.Core.Providers -{ - public class UpdateProvider - { - private readonly IHttpProvider _httpProvider; - private readonly IConfigService _configService; - private readonly EnvironmentProvider _environmentProvider; - - private readonly DiskProvider _diskProvider; - private readonly Logger _logger; - - private static readonly Regex parseRegex = new Regex(@"(?:\>)(?NzbDrone.+?(?\d+\.\d+\.\d+\.\d+).+?)(?:\<\/A\>)", RegexOptions.IgnoreCase); - - - public UpdateProvider(IHttpProvider httpProvider, IConfigService configService, - EnvironmentProvider environmentProvider, DiskProvider diskProvider, Logger logger) - { - _httpProvider = httpProvider; - _configService = configService; - _environmentProvider = environmentProvider; - _diskProvider = diskProvider; - _logger = logger; - } - - public UpdateProvider() - { - - } - - private IEnumerable GetAvailablePackages() - { - var updateList = new List(); - var updateUrl = _configService.UpdateUrl; - var rawUpdateList = _httpProvider.DownloadString(updateUrl); - var matches = parseRegex.Matches(rawUpdateList); - - foreach (Match match in matches) - { - var updatePackage = new UpdatePackage(); - updatePackage.FileName = match.Groups["filename"].Value; - updatePackage.Url = updateUrl + updatePackage.FileName; - updatePackage.Version = new Version(match.Groups["version"].Value); - updateList.Add(updatePackage); - } - - return updateList; - } - - public virtual UpdatePackage GetAvailableUpdate(Version currentVersion) - { - var latestAvailable = GetAvailablePackages().OrderByDescending(c => c.Version).FirstOrDefault(); - - if (latestAvailable != null && latestAvailable.Version > currentVersion) - { - _logger.Debug("An update is available ({0}) => ({1})", currentVersion, latestAvailable.Version); - return latestAvailable; - } - - _logger.Trace("No updates available"); - return null; - } - - public virtual Dictionary UpdateLogFile() - { - var list = new Dictionary(); - CultureInfo provider = CultureInfo.InvariantCulture; - - if (_diskProvider.FolderExists(_environmentProvider.GetUpdateLogFolder())) - { - var files = _diskProvider.GetFiles(_environmentProvider.GetUpdateLogFolder(), SearchOption.TopDirectoryOnly).ToList(); - - foreach (var file in files.Select(c => new FileInfo(c)).OrderByDescending(c => c.Name)) - { - list.Add(DateTime.ParseExact(file.Name.Replace(file.Extension, string.Empty), "yyyy.MM.dd-H-mm", provider), file.FullName); - } - } - - return list; - } - } -} diff --git a/NzbDrone.Core/Update/AvailableUpdateService.cs b/NzbDrone.Core/Update/AvailableUpdateService.cs new file mode 100644 index 000000000..f8226abca --- /dev/null +++ b/NzbDrone.Core/Update/AvailableUpdateService.cs @@ -0,0 +1,46 @@ +using System; +using System.Collections.Generic; +using System.Text.RegularExpressions; +using NzbDrone.Common; +using NzbDrone.Core.Configuration; + +namespace NzbDrone.Core.Update +{ + public interface IAvailableUpdateService + { + IEnumerable GetAvailablePackages(); + } + + public class AvailableUpdateService : IAvailableUpdateService + { + private readonly IConfigService _configService; + private readonly IHttpProvider _httpProvider; + + private static readonly Regex ParseRegex = new Regex(@"(?:\>)(?NzbDrone.+?(?\d+\.\d+\.\d+\.\d+).+?)(?:\<\/A\>)", RegexOptions.IgnoreCase); + + public AvailableUpdateService(IConfigService configService, IHttpProvider httpProvider) + { + _configService = configService; + _httpProvider = httpProvider; + } + + public IEnumerable GetAvailablePackages() + { + var updateList = new List(); + var updateUrl = _configService.UpdateUrl; + var rawUpdateList = _httpProvider.DownloadString(updateUrl); + var matches = ParseRegex.Matches(rawUpdateList); + + foreach (Match match in matches) + { + var updatePackage = new UpdatePackage(); + updatePackage.FileName = match.Groups["filename"].Value; + updatePackage.Url = updateUrl + updatePackage.FileName; + updatePackage.Version = new Version(match.Groups["version"].Value); + updateList.Add(updatePackage); + } + + return updateList; + } + } +} \ No newline at end of file diff --git a/NzbDrone.Core/Model/UpdatePackage.cs b/NzbDrone.Core/Update/UpdatePackage.cs similarity index 79% rename from NzbDrone.Core/Model/UpdatePackage.cs rename to NzbDrone.Core/Update/UpdatePackage.cs index 00cfde5bd..e600e2a83 100644 --- a/NzbDrone.Core/Model/UpdatePackage.cs +++ b/NzbDrone.Core/Update/UpdatePackage.cs @@ -1,7 +1,6 @@ using System; -using System.Linq; -namespace NzbDrone.Core.Model +namespace NzbDrone.Core.Update { public class UpdatePackage { diff --git a/NzbDrone.Core/Update/UpdateProvider.cs b/NzbDrone.Core/Update/UpdateProvider.cs new file mode 100644 index 000000000..9675b1501 --- /dev/null +++ b/NzbDrone.Core/Update/UpdateProvider.cs @@ -0,0 +1,68 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.IO; +using System.Linq; +using NLog; +using NzbDrone.Common; +using NzbDrone.Core.Update; + +namespace NzbDrone.Core.Update +{ + public interface IUpdateService + { + UpdatePackage GetAvailableUpdate(Version currentVersion); + Dictionary UpdateLogFile(); + } +} + +public class UpdateService : IUpdateService +{ + private readonly IAvailableUpdateService _availableUpdateService; + private readonly EnvironmentProvider _environmentProvider; + + private readonly DiskProvider _diskProvider; + private readonly Logger _logger; + + + public UpdateService(IAvailableUpdateService availableUpdateService, EnvironmentProvider environmentProvider, DiskProvider diskProvider, Logger logger) + { + _availableUpdateService = availableUpdateService; + _environmentProvider = environmentProvider; + _diskProvider = diskProvider; + _logger = logger; + } + + public UpdatePackage GetAvailableUpdate(Version currentVersion) + { + var latestAvailable = _availableUpdateService.GetAvailablePackages().OrderByDescending(c => c.Version).FirstOrDefault(); + + if (latestAvailable != null && latestAvailable.Version > currentVersion) + { + _logger.Debug("An update is available ({0}) => ({1})", currentVersion, latestAvailable.Version); + return latestAvailable; + } + + _logger.Trace("No updates available"); + return null; + } + + public Dictionary UpdateLogFile() + { + var list = new Dictionary(); + CultureInfo provider = CultureInfo.InvariantCulture; + + if (_diskProvider.FolderExists(_environmentProvider.GetUpdateLogFolder())) + { + var files = _diskProvider.GetFiles(_environmentProvider.GetUpdateLogFolder(), SearchOption.TopDirectoryOnly).ToList(); + + foreach (var file in files.Select(c => new FileInfo(c)).OrderByDescending(c => c.Name)) + { + list.Add(DateTime.ParseExact(file.Name.Replace(file.Extension, string.Empty), "yyyy.MM.dd-H-mm", provider), file.FullName); + } + } + + return list; + } +} + diff --git a/NzbDrone.Update/Providers/UpdateProvider.cs b/NzbDrone.Update/Providers/UpdateProvider.cs index ef13ebc01..f507b7852 100644 --- a/NzbDrone.Update/Providers/UpdateProvider.cs +++ b/NzbDrone.Update/Providers/UpdateProvider.cs @@ -125,7 +125,7 @@ namespace NzbDrone.Update.Providers logger.Info("Starting NzbDrone service"); _serviceProvider.Start(ServiceProvider.NZBDRONE_SERVICE_NAME); } - else if(appType == AppType.Console) + else if (appType == AppType.Console) { logger.Info("Starting NzbDrone with Console"); _processProvider.Start(Path.Combine(targetFolder, "NzbDrone.Console.exe")); diff --git a/NzbDrone.ncrunchsolution b/NzbDrone.ncrunchsolution index 6cb47a29a..444b34b1d 100644 --- a/NzbDrone.ncrunchsolution +++ b/NzbDrone.ncrunchsolution @@ -2,6 +2,7 @@ 1 False true + true UseDynamicAnalysis Disabled Disabled