cleaned up app update

pull/6/head
kay.one 12 years ago
parent 420756bb47
commit cd2761d07d

@ -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<EnvironmentProvider>().SetupGet(c => c.SystemTemp).Returns(@"C:\Temp\");
Mocker.GetMock<ConfigFileProvider>().SetupGet(c => c.Guid).Returns(_clientGuid);
Mocker.GetMock<UpdateProvider>().Setup(c => c.GetAvailableUpdate(It.IsAny<Version>())).Returns(updatePackage);
Mocker.GetMock<UpdateService>().Setup(c => c.GetAvailableUpdate(It.IsAny<Version>())).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<UpdateProvider>().Setup(c => c.GetAvailableUpdate(It.IsAny<Version>())).Returns((UpdatePackage)null);
Mocker.GetMock<UpdateService>().Setup(c => c.GetAvailableUpdate(It.IsAny<Version>())).Returns((UpdatePackage)null);
StartUpdate();

@ -177,8 +177,6 @@
<Compile Include="ProviderTests\PostDownloadProviderTests\ProcessDownloadFixture.cs" />
<Compile Include="JobTests\TestJobs.cs" />
<Compile Include="JobTests\AppUpdateJobFixture.cs" />
<Compile Include="ProviderTests\UpdateProviderTests\GetUpdateLogFixture.cs" />
<Compile Include="ProviderTests\UpdateProviderTests\GetAvailableUpdateFixture.cs" />
<Compile Include="ProviderTests\XemCommunicationProviderTests\GetSceneTvdbMappingsFixture.cs" />
<Compile Include="ProviderTests\XemCommunicationProviderTests\GetXemSeriesIdsFixture.cs" />
<Compile Include="Services\ParseErrorServiceFixture.cs" />
@ -209,6 +207,8 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Qualities\QualityProfileFixture.cs" />
<Compile Include="TvTests\SeriesProviderTest.cs" />
<Compile Include="UpdateTests\GetAvailableUpdateFixture.cs" />
<Compile Include="UpdateTests\GetUpdateLogFixture.cs" />
<Compile Include="XbmcVersionTests.cs" />
</ItemGroup>
<ItemGroup>
@ -349,7 +349,9 @@
</None>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup />
<ItemGroup>
<Folder Include="ProviderTests\UpdateProviderTests\" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PreBuildEvent>

@ -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<UpdateProvider>
class GetAvailableUpdateFixture : CoreTest<UpdateService>
{
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";

@ -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<UpdateProvider>().UpdateLogFile();
var logs = Mocker.Resolve<UpdateService>().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<UpdateProvider>().UpdateLogFile();
var logs = Mocker.Resolve<UpdateService>().UpdateLogFile();
logs.Should().HaveCount(3);
}

@ -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)

@ -365,7 +365,6 @@
<Compile Include="Download\Clients\Sabnzbd\SabQueueItem.cs" />
<Compile Include="Download\Clients\Sabnzbd\SabVersionModel.cs" />
<Compile Include="Model\Twitter\TwitterAuthorizationModel.cs" />
<Compile Include="Model\UpdatePackage.cs" />
<Compile Include="Model\Xbmc\ActionType.cs" />
<Compile Include="Model\Xbmc\ActivePlayersDharmaResult.cs" />
<Compile Include="Model\Xbmc\ActivePlayersEdenResult.cs" />
@ -496,9 +495,6 @@
<Compile Include="Providers\TwitterProvider.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="Providers\UpdateProvider.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="Providers\XbmcProvider.cs">
<SubType>Code</SubType>
</Compile>
@ -525,6 +521,9 @@
<Compile Include="Tv\Series.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Tv\SeriesStatusType.cs" />
<Compile Include="Update\AvailableUpdateService.cs" />
<Compile Include="Update\UpdatePackage.cs" />
<Compile Include="Update\UpdateProvider.cs" />
</ItemGroup>
<ItemGroup>
<BootstrapperPackage Include=".NETFramework,Version=v4.0,Profile=Client">

@ -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(@"(?:\>)(?<filename>NzbDrone.+?(?<version>\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<UpdatePackage> GetAvailablePackages()
{
var updateList = new List<UpdatePackage>();
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<DateTime, string> UpdateLogFile()
{
var list = new Dictionary<DateTime, string>();
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;
}
}
}

@ -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<UpdatePackage> GetAvailablePackages();
}
public class AvailableUpdateService : IAvailableUpdateService
{
private readonly IConfigService _configService;
private readonly IHttpProvider _httpProvider;
private static readonly Regex ParseRegex = new Regex(@"(?:\>)(?<filename>NzbDrone.+?(?<version>\d+\.\d+\.\d+\.\d+).+?)(?:\<\/A\>)", RegexOptions.IgnoreCase);
public AvailableUpdateService(IConfigService configService, IHttpProvider httpProvider)
{
_configService = configService;
_httpProvider = httpProvider;
}
public IEnumerable<UpdatePackage> GetAvailablePackages()
{
var updateList = new List<UpdatePackage>();
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;
}
}
}

@ -1,7 +1,6 @@
using System;
using System.Linq;
namespace NzbDrone.Core.Model
namespace NzbDrone.Core.Update
{
public class UpdatePackage
{

@ -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<DateTime, string> 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<DateTime, string> UpdateLogFile()
{
var list = new Dictionary<DateTime, string>();
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;
}
}

@ -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"));

@ -2,6 +2,7 @@
<FileVersion>1</FileVersion>
<AutoEnableOnStartup>False</AutoEnableOnStartup>
<AllowParallelTestExecution>true</AllowParallelTestExecution>
<AllowTestsToRunInParallelWithThemselves>true</AllowTestsToRunInParallelWithThemselves>
<FrameworkUtilisationTypeForNUnit>UseDynamicAnalysis</FrameworkUtilisationTypeForNUnit>
<FrameworkUtilisationTypeForGallio>Disabled</FrameworkUtilisationTypeForGallio>
<FrameworkUtilisationTypeForMSpec>Disabled</FrameworkUtilisationTypeForMSpec>

Loading…
Cancel
Save