From 6b0a24e28e9000b4ec855ede5c36d7428b55ebdb Mon Sep 17 00:00:00 2001 From: Keivan Beigi Date: Thu, 27 Jun 2013 17:04:52 -0700 Subject: [PATCH] broke up EnvironmentProvider into different services --- NzbDrone.Api/Client/ClientSettings.cs | 13 +- NzbDrone.Api/Frontend/MediaCoverMapper.cs | 9 +- NzbDrone.Api/Frontend/StaticResourceMapper.cs | 9 +- NzbDrone.Api/System/SystemModule.cs | 32 ++-- NzbDrone.Api/Update/UpdateModule.cs | 8 +- NzbDrone.App.Test/RouterTest.cs | 13 +- .../ConfigFileProviderTest.cs | 3 +- NzbDrone.Common.Test/DiskProviderFixture.cs | 3 +- .../EnviromentProviderTest.cs | 16 +- .../EnviromentProviderTest.cs | 21 +++ .../NzbDrone.Common.Test.csproj | 2 + NzbDrone.Common.Test/PathExtensionFixture.cs | 19 +-- .../EnsureThat/EnsureStringExtensions.cs | 3 +- .../EnvironmentInfo/AppDirectoryInfo.cs | 38 +++++ NzbDrone.Common/EnvironmentInfo/BuildInfo.cs | 35 ++++ NzbDrone.Common/EnvironmentInfo/OsInfo.cs | 36 +++++ .../EnvironmentInfo/RuntimeInfo.cs | 65 ++++++++ NzbDrone.Common/HttpProvider.cs | 7 +- NzbDrone.Common/IDiskProvider.cs | 3 +- NzbDrone.Common/IEnvironmentProvider.cs | 149 ------------------ .../ApplicationLogLayoutRenderer.cs | 3 +- .../Instrumentation/ExceptronTarget.cs | 5 +- .../Instrumentation/LogglyTarget.cs | 11 +- .../UpdateLogLayoutRenderer.cs | 3 +- NzbDrone.Common/NzbDrone.Common.csproj | 5 +- NzbDrone.Common/PathExtensions.cs | 53 ++++--- NzbDrone.Common/RestProvider.cs | 22 +-- NzbDrone.Core.Test/Framework/CoreTest.cs | 2 +- .../MediaCoverServiceFixture.cs | 5 +- NzbDrone.Core.Test/NzbDrone.Core.Test.csproj | 1 - .../ParserTests/ParserFixture.cs | 20 +-- .../UpdateTests/GetUpdateLogFixture.cs | 57 ------- .../UpdateTests/UpdateServiceFixture.cs | 21 +-- .../Configuration/ConfigFileProvider.cs | 9 +- NzbDrone.Core/MediaCover/MediaCoverService.cs | 5 +- NzbDrone.Core/NzbDrone.Core.csproj | 3 +- NzbDrone.Core/Providers/BackupProvider.cs | 16 +- NzbDrone.Core/Update/InstallUpdateService.cs | 86 ++++++++++ NzbDrone.Core/Update/UpdateCheckService.cs | 38 +++++ NzbDrone.Core/Update/UpdateService.cs | 134 ---------------- NzbDrone.Integration.Test/IntegrationTest.cs | 28 ++-- NzbDrone.Test.Common/TestBase.cs | 12 +- .../InstallUpdateServiceFixture.cs | 3 +- .../UpdateProviderStartFixture.cs | 6 +- NzbDrone.Update/Program.cs | 5 +- .../UpdateEngine/BackupAndRestore.cs | 11 +- .../UpdateEngine/InstallUpdateService.cs | 13 +- NzbDrone.sln.DotSettings | 1 + NzbDrone/AppMain.cs | 3 +- NzbDrone/ApplicationServer.cs | 12 +- NzbDrone/Host/UrlAclAdapter.cs | 7 +- NzbDrone/MainAppContainerBuilder.cs | 9 +- NzbDrone/Router.cs | 9 +- NzbDrone/SysTray/SysTrayApp.cs | 7 +- 54 files changed, 549 insertions(+), 560 deletions(-) create mode 100644 NzbDrone.Common.Test/EnvironmentTests/EnviromentProviderTest.cs create mode 100644 NzbDrone.Common/EnvironmentInfo/AppDirectoryInfo.cs create mode 100644 NzbDrone.Common/EnvironmentInfo/BuildInfo.cs create mode 100644 NzbDrone.Common/EnvironmentInfo/OsInfo.cs create mode 100644 NzbDrone.Common/EnvironmentInfo/RuntimeInfo.cs delete mode 100644 NzbDrone.Common/IEnvironmentProvider.cs delete mode 100644 NzbDrone.Core.Test/UpdateTests/GetUpdateLogFixture.cs create mode 100644 NzbDrone.Core/Update/InstallUpdateService.cs create mode 100644 NzbDrone.Core/Update/UpdateCheckService.cs delete mode 100644 NzbDrone.Core/Update/UpdateService.cs diff --git a/NzbDrone.Api/Client/ClientSettings.cs b/NzbDrone.Api/Client/ClientSettings.cs index b62bddf30..872cd1fd0 100644 --- a/NzbDrone.Api/Client/ClientSettings.cs +++ b/NzbDrone.Api/Client/ClientSettings.cs @@ -5,6 +5,7 @@ using System.Text; using System.Text.RegularExpressions; using NzbDrone.Common; +using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Common.Messaging; using NzbDrone.Common.Serializer; using NzbDrone.Core.Lifecycle; @@ -13,24 +14,24 @@ namespace NzbDrone.Api.Client { public class ClientSettings : IHandle { - private readonly EnvironmentProvider _environmentProvider; + private readonly IAppDirectoryInfo _appDirectoryInfo; private static readonly Regex VersionRegex = new Regex(@"(?<=Version:\s')(.*)(?=')", RegexOptions.IgnoreCase | RegexOptions.Compiled); private static readonly Regex BuildDateRegex = new Regex(@"(?<=BuildDate:\s)('.*')", RegexOptions.IgnoreCase | RegexOptions.Compiled); - public ClientSettings(EnvironmentProvider environmentProvider) + public ClientSettings(IAppDirectoryInfo appDirectoryInfo) { - _environmentProvider = environmentProvider; + _appDirectoryInfo = appDirectoryInfo; } public void Handle(ApplicationStartedEvent message) { //TODO: Update the APIKey (when we have it) - var appFile = Path.Combine(_environmentProvider.StartUpPath, "UI", "app.js"); + var appFile = Path.Combine(_appDirectoryInfo.StartUpPath, "UI", "app.js"); var contents = File.ReadAllText(appFile); - var version = _environmentProvider.Version; - var date = _environmentProvider.BuildDateTime; + var version = BuildInfo.Version; + var date = BuildInfo.BuildDateTime; contents = VersionRegex.Replace(contents, version.ToString()); contents = BuildDateRegex.Replace(contents, date.ToUniversalTime().ToJson()); diff --git a/NzbDrone.Api/Frontend/MediaCoverMapper.cs b/NzbDrone.Api/Frontend/MediaCoverMapper.cs index ed757e266..fe74862fe 100644 --- a/NzbDrone.Api/Frontend/MediaCoverMapper.cs +++ b/NzbDrone.Api/Frontend/MediaCoverMapper.cs @@ -1,15 +1,16 @@ using System.IO; using NzbDrone.Common; +using NzbDrone.Common.EnvironmentInfo; namespace NzbDrone.Api.Frontend { public class MediaCoverMapper : IMapHttpRequestsToDisk { - private readonly IEnvironmentProvider _environmentProvider; + private readonly IAppDirectoryInfo _appDirectoryInfo; - public MediaCoverMapper(IEnvironmentProvider environmentProvider) + public MediaCoverMapper(IAppDirectoryInfo appDirectoryInfo) { - _environmentProvider = environmentProvider; + _appDirectoryInfo = appDirectoryInfo; } public string Map(string resourceUrl) @@ -17,7 +18,7 @@ public string Map(string resourceUrl) var path = resourceUrl.Replace('/', Path.DirectorySeparatorChar); path = path.Trim(Path.DirectorySeparatorChar).ToLower(); - return Path.Combine(_environmentProvider.GetAppDataPath(), path); + return Path.Combine(_appDirectoryInfo.GetAppDataPath(), path); } public bool CanHandle(string resourceUrl) diff --git a/NzbDrone.Api/Frontend/StaticResourceMapper.cs b/NzbDrone.Api/Frontend/StaticResourceMapper.cs index f04421efe..dac512688 100644 --- a/NzbDrone.Api/Frontend/StaticResourceMapper.cs +++ b/NzbDrone.Api/Frontend/StaticResourceMapper.cs @@ -2,12 +2,13 @@ using System.IO; using System.Linq; using NzbDrone.Common; +using NzbDrone.Common.EnvironmentInfo; namespace NzbDrone.Api.Frontend { public class StaticResourceMapper : IMapHttpRequestsToDisk { - private readonly IEnvironmentProvider _environmentProvider; + private readonly IAppDirectoryInfo _appDirectoryInfo; private static readonly string[] Extensions = new[] { ".css", ".js", @@ -24,9 +25,9 @@ public class StaticResourceMapper : IMapHttpRequestsToDisk ".eot" }; - public StaticResourceMapper(IEnvironmentProvider environmentProvider) + public StaticResourceMapper(IAppDirectoryInfo appDirectoryInfo) { - _environmentProvider = environmentProvider; + _appDirectoryInfo = appDirectoryInfo; } public string Map(string resourceUrl) @@ -35,7 +36,7 @@ public string Map(string resourceUrl) path = path.Trim(Path.DirectorySeparatorChar).ToLower(); - return Path.Combine(_environmentProvider.StartUpPath, "ui", path); + return Path.Combine(_appDirectoryInfo.StartUpPath, "ui", path); } public bool CanHandle(string resourceUrl) diff --git a/NzbDrone.Api/System/SystemModule.cs b/NzbDrone.Api/System/SystemModule.cs index 266721510..b4f2e714d 100644 --- a/NzbDrone.Api/System/SystemModule.cs +++ b/NzbDrone.Api/System/SystemModule.cs @@ -2,19 +2,21 @@ using Nancy.Routing; using NzbDrone.Common; using NzbDrone.Api.Extensions; -using System.Linq; +using NzbDrone.Common.EnvironmentInfo; namespace NzbDrone.Api.System { public class SystemModule : NzbDroneApiModule { - private readonly IEnvironmentProvider _environmentProvider; + private readonly IAppDirectoryInfo _appDirectoryInfo; + private readonly IRuntimeInfo _runtimeInfo; private readonly IRouteCacheProvider _routeCacheProvider; - public SystemModule(IEnvironmentProvider environmentProvider, IRouteCacheProvider routeCacheProvider) + public SystemModule(IAppDirectoryInfo appDirectoryInfo, IRuntimeInfo runtimeInfo, IRouteCacheProvider routeCacheProvider) : base("system") { - _environmentProvider = environmentProvider; + _appDirectoryInfo = appDirectoryInfo; + _runtimeInfo = runtimeInfo; _routeCacheProvider = routeCacheProvider; Get["/status"] = x => GetStatus(); Get["/routes"] = x => GetRoutes(); @@ -24,17 +26,17 @@ private Response GetStatus() { return new { - Version = _environmentProvider.Version.ToString(), - AppData = _environmentProvider.GetAppDataPath(), - IsAdmin = _environmentProvider.IsAdmin, - IsUserInteractive = _environmentProvider.IsUserInteractive, - BuildTime = _environmentProvider.BuildDateTime, - StartupPath = _environmentProvider.StartUpPath, - OsVersion = _environmentProvider.GetOsVersion().ToString(), - IsMono = EnvironmentProvider.IsMono, - IsProduction = EnvironmentProvider.IsProduction, - IsDebug = EnvironmentProvider.IsDebug, - IsLinux = EnvironmentProvider.IsLinux, + Version = BuildInfo.Version.ToString(), + BuildTime = BuildInfo.BuildDateTime, + IsDebug = BuildInfo.IsDebug, + IsProduction = RuntimeInfo.IsProduction, + IsAdmin = _runtimeInfo.IsAdmin, + IsUserInteractive = _runtimeInfo.IsUserInteractive, + StartupPath = _appDirectoryInfo.StartUpPath, + AppData = _appDirectoryInfo.GetAppDataPath(), + OsVersion = OsInfo.Version.ToString(), + IsMono = OsInfo.IsMono, + IsLinux = OsInfo.IsLinux, }.AsResponse(); } diff --git a/NzbDrone.Api/Update/UpdateModule.cs b/NzbDrone.Api/Update/UpdateModule.cs index aab842eea..6fdb1ca50 100644 --- a/NzbDrone.Api/Update/UpdateModule.cs +++ b/NzbDrone.Api/Update/UpdateModule.cs @@ -8,17 +8,17 @@ namespace NzbDrone.Api.Update { public class UpdateModule : NzbDroneRestModule { - private readonly IUpdateService _updateService; + private readonly ICheckUpdateService _checkUpdateService; - public UpdateModule(IUpdateService updateService) + public UpdateModule(ICheckUpdateService checkUpdateService) { - _updateService = updateService; + _checkUpdateService = checkUpdateService; GetResourceAll = GetAvailableUpdate; } private List GetAvailableUpdate() { - var update = _updateService.AvailableUpdate(); + var update = _checkUpdateService.AvailableUpdate(); var response = new List(); if (update != null) diff --git a/NzbDrone.App.Test/RouterTest.cs b/NzbDrone.App.Test/RouterTest.cs index c394b5d07..d670f6dce 100644 --- a/NzbDrone.App.Test/RouterTest.cs +++ b/NzbDrone.App.Test/RouterTest.cs @@ -3,6 +3,7 @@ using Moq; using NUnit.Framework; using NzbDrone.Common; +using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Test.Common; namespace NzbDrone.App.Test @@ -48,7 +49,7 @@ public void Route_should_call_install_service_when_application_mode_is_install() serviceProviderMock.Setup(c => c.Install(ServiceProvider.NZBDRONE_SERVICE_NAME)); serviceProviderMock.Setup(c => c.ServiceExist(ServiceProvider.NZBDRONE_SERVICE_NAME)).Returns(false); serviceProviderMock.Setup(c => c.Start(ServiceProvider.NZBDRONE_SERVICE_NAME)); - Mocker.GetMock().SetupGet(c => c.IsUserInteractive).Returns(true); + Mocker.GetMock().SetupGet(c => c.IsUserInteractive).Returns(true); Subject.Route(ApplicationModes.InstallService); @@ -61,7 +62,7 @@ public void Route_should_call_uninstall_service_when_application_mode_is_uninsta { var serviceProviderMock = Mocker.GetMock(); serviceProviderMock.Setup(c => c.UnInstall(ServiceProvider.NZBDRONE_SERVICE_NAME)); - Mocker.GetMock().SetupGet(c => c.IsUserInteractive).Returns(true); + Mocker.GetMock().SetupGet(c => c.IsUserInteractive).Returns(true); serviceProviderMock.Setup(c => c.ServiceExist(ServiceProvider.NZBDRONE_SERVICE_NAME)).Returns(true); Subject.Route(ApplicationModes.UninstallService); @@ -72,7 +73,7 @@ public void Route_should_call_uninstall_service_when_application_mode_is_uninsta [Test] public void Route_should_call_console_service_when_application_mode_is_console() { - Mocker.GetMock().SetupGet(c => c.IsUserInteractive).Returns(true); + Mocker.GetMock().SetupGet(c => c.IsUserInteractive).Returns(true); Mocker.GetMock().SetupGet(c => c.IsConsoleApplication).Returns(true); Subject.Route(ApplicationModes.Console); @@ -87,7 +88,7 @@ public void Route_should_call_console_service_when_application_mode_is_console() [TestCase(ApplicationModes.Help)] public void Route_should_call_service_start_when_run_in_service_more(ApplicationModes applicationModes) { - var envMock = Mocker.GetMock(); + var envMock = Mocker.GetMock(); var serviceProvider = Mocker.GetMock(); envMock.SetupGet(c => c.IsUserInteractive).Returns(false); @@ -105,7 +106,7 @@ public void show_error_on_install_if_service_already_exist() { var consoleMock = Mocker.GetMock(); var serviceMock = Mocker.GetMock(); - Mocker.GetMock().SetupGet(c => c.IsUserInteractive).Returns(true); + Mocker.GetMock().SetupGet(c => c.IsUserInteractive).Returns(true); consoleMock.Setup(c => c.PrintServiceAlreadyExist()); serviceMock.Setup(c => c.ServiceExist(ServiceProvider.NZBDRONE_SERVICE_NAME)).Returns(true); @@ -120,7 +121,7 @@ public void show_error_on_uninstall_if_service_doesnt_exist() { var consoleMock = Mocker.GetMock(); var serviceMock = Mocker.GetMock(); - Mocker.GetMock().SetupGet(c => c.IsUserInteractive).Returns(true); + Mocker.GetMock().SetupGet(c => c.IsUserInteractive).Returns(true); consoleMock.Setup(c => c.PrintServiceDoestExist()); serviceMock.Setup(c => c.ServiceExist(ServiceProvider.NZBDRONE_SERVICE_NAME)).Returns(false); diff --git a/NzbDrone.Common.Test/ConfigFileProviderTest.cs b/NzbDrone.Common.Test/ConfigFileProviderTest.cs index 95232e40f..bde0dbad0 100644 --- a/NzbDrone.Common.Test/ConfigFileProviderTest.cs +++ b/NzbDrone.Common.Test/ConfigFileProviderTest.cs @@ -1,6 +1,7 @@ using System.IO; using FluentAssertions; using NUnit.Framework; +using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Common.Model; using NzbDrone.Core.Configuration; using NzbDrone.Test.Common; @@ -17,7 +18,7 @@ public void SetUp() WithTempAsAppPath(); //Reset config file - var configFile = Mocker.Resolve().GetConfigPath(); + var configFile = Mocker.Resolve().GetConfigPath(); if (File.Exists(configFile)) File.Delete(configFile); diff --git a/NzbDrone.Common.Test/DiskProviderFixture.cs b/NzbDrone.Common.Test/DiskProviderFixture.cs index 236641a9f..a76b017e4 100644 --- a/NzbDrone.Common.Test/DiskProviderFixture.cs +++ b/NzbDrone.Common.Test/DiskProviderFixture.cs @@ -3,6 +3,7 @@ using System.Linq; using FluentAssertions; using NUnit.Framework; +using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Test.Common; namespace NzbDrone.Common.Test @@ -165,7 +166,7 @@ public void empty_folder_should_return_folder_modified_date() [Test] public void folder_should_return_correct_value_for_last_write() { - var appPath = new EnvironmentProvider().WorkingDirectory; + var appPath = new AppDirectoryInfo().WorkingDirectory; TestLogger.Info("Path is: {0}", appPath); diff --git a/NzbDrone.Common.Test/EnviromentProviderTest.cs b/NzbDrone.Common.Test/EnviromentProviderTest.cs index 66ab9e06d..34c5688ae 100644 --- a/NzbDrone.Common.Test/EnviromentProviderTest.cs +++ b/NzbDrone.Common.Test/EnviromentProviderTest.cs @@ -1,13 +1,13 @@ -using System; -using System.IO; +using System.IO; using FluentAssertions; using NUnit.Framework; +using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Test.Common; namespace NzbDrone.Common.Test { [TestFixture] - public class EnvironmentProviderTest : TestBase + public class IAppDirectoryInfoTest : TestBase { [Test] @@ -30,15 +30,7 @@ public void ApplicationPath_should_not_be_empty() [Test] public void IsProduction_should_return_false_when_run_within_nunit() { - EnvironmentProvider.IsProduction.Should().BeFalse(); + RuntimeInfo.IsProduction.Should().BeFalse(); } - - [TestCase("0.0.0.0")] - [TestCase("1.0.0.0")] - public void Application_version_should_not_be_default(string version) - { - Subject.Version.Should().NotBe(new Version(version)); - } - } } diff --git a/NzbDrone.Common.Test/EnvironmentTests/EnviromentProviderTest.cs b/NzbDrone.Common.Test/EnvironmentTests/EnviromentProviderTest.cs new file mode 100644 index 000000000..826cf9520 --- /dev/null +++ b/NzbDrone.Common.Test/EnvironmentTests/EnviromentProviderTest.cs @@ -0,0 +1,21 @@ +using System; +using FluentAssertions; +using NUnit.Framework; +using NzbDrone.Common.EnvironmentInfo; +using NzbDrone.Test.Common; + +namespace NzbDrone.Common.Test.EnvironmentTests +{ + [TestFixture] + public class BuildInfoTest : TestBase + { + + [TestCase("0.0.0.0")] + [TestCase("1.0.0.0")] + public void Application_version_should_not_be_default(string version) + { + BuildInfo.Version.Should().NotBe(new Version(version)); + } + + } +} diff --git a/NzbDrone.Common.Test/NzbDrone.Common.Test.csproj b/NzbDrone.Common.Test/NzbDrone.Common.Test.csproj index d365c1381..bf27bbb24 100644 --- a/NzbDrone.Common.Test/NzbDrone.Common.Test.csproj +++ b/NzbDrone.Common.Test/NzbDrone.Common.Test.csproj @@ -82,6 +82,7 @@ + @@ -123,6 +124,7 @@ + diff --git a/NzbDrone.Common.Test/PathExtensionFixture.cs b/NzbDrone.Common.Test/PathExtensionFixture.cs index 6c313190e..2f1110b4b 100644 --- a/NzbDrone.Common.Test/PathExtensionFixture.cs +++ b/NzbDrone.Common.Test/PathExtensionFixture.cs @@ -2,6 +2,7 @@ using FluentAssertions; using Moq; using NUnit.Framework; +using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Test.Common; using NzbDrone.Test.Common.Categories; @@ -11,9 +12,9 @@ namespace NzbDrone.Common.Test public class PathExtensionFixture : TestBase { - private EnvironmentProvider GetEnvironmentProvider() + private IAppDirectoryInfo GetIAppDirectoryInfo() { - var fakeEnvironment = new Mock(); + var fakeEnvironment = new Mock(); fakeEnvironment.SetupGet(c => c.WorkingDirectory).Returns(@"C:\NzbDrone\"); @@ -73,44 +74,44 @@ public void normalize_path_exception_null() [Test] public void AppDataDirectory_path_test() { - GetEnvironmentProvider().GetAppDataPath().Should().BeEquivalentTo(@"C:\NzbDrone\"); + GetIAppDirectoryInfo().GetAppDataPath().Should().BeEquivalentTo(@"C:\NzbDrone\"); } [Test] public void Config_path_test() { - GetEnvironmentProvider().GetConfigPath().Should().BeEquivalentTo(@"C:\NzbDrone\Config.xml"); + GetIAppDirectoryInfo().GetConfigPath().Should().BeEquivalentTo(@"C:\NzbDrone\Config.xml"); } [Test] public void Sanbox() { - GetEnvironmentProvider().GetUpdateSandboxFolder().Should().BeEquivalentTo(@"C:\Temp\Nzbdrone_update\"); + GetIAppDirectoryInfo().GetUpdateSandboxFolder().Should().BeEquivalentTo(@"C:\Temp\Nzbdrone_update\"); } [Test] public void GetUpdatePackageFolder() { - GetEnvironmentProvider().GetUpdatePackageFolder().Should().BeEquivalentTo(@"C:\Temp\Nzbdrone_update\NzbDrone\"); + GetIAppDirectoryInfo().GetUpdatePackageFolder().Should().BeEquivalentTo(@"C:\Temp\Nzbdrone_update\NzbDrone\"); } [Test] public void GetUpdateClientFolder() { - GetEnvironmentProvider().GetUpdateClientFolder().Should().BeEquivalentTo(@"C:\Temp\Nzbdrone_update\NzbDrone\NzbDrone.Update\"); + GetIAppDirectoryInfo().GetUpdateClientFolder().Should().BeEquivalentTo(@"C:\Temp\Nzbdrone_update\NzbDrone\NzbDrone.Update\"); } [Test] public void GetUpdateClientExePath() { - GetEnvironmentProvider().GetUpdateClientExePath().Should().BeEquivalentTo(@"C:\Temp\Nzbdrone_update\NzbDrone.Update.exe"); + GetIAppDirectoryInfo().GetUpdateClientExePath().Should().BeEquivalentTo(@"C:\Temp\Nzbdrone_update\NzbDrone.Update.exe"); } [Test] public void GetUpdateLogFolder() { - GetEnvironmentProvider().GetUpdateLogFolder().Should().BeEquivalentTo(@"C:\NzbDrone\UpdateLogs\"); + GetIAppDirectoryInfo().GetUpdateLogFolder().Should().BeEquivalentTo(@"C:\NzbDrone\UpdateLogs\"); } } } diff --git a/NzbDrone.Common/EnsureThat/EnsureStringExtensions.cs b/NzbDrone.Common/EnsureThat/EnsureStringExtensions.cs index 734b5cb96..308721d56 100644 --- a/NzbDrone.Common/EnsureThat/EnsureStringExtensions.cs +++ b/NzbDrone.Common/EnsureThat/EnsureStringExtensions.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Text.RegularExpressions; using NzbDrone.Common.EnsureThat.Resources; +using NzbDrone.Common.EnvironmentInfo; namespace NzbDrone.Common.EnsureThat { @@ -105,7 +106,7 @@ public static Param IsValidPath(this Param param) if (string.IsNullOrWhiteSpace(param.Value)) throw ExceptionFactory.CreateForParamValidation(param.Name, ExceptionMessages.EnsureExtensions_IsNotNullOrWhiteSpace); - if (EnvironmentProvider.IsLinux) + if (OsInfo.IsLinux) { if (!param.Value.StartsWith(Path.DirectorySeparatorChar.ToString())) { diff --git a/NzbDrone.Common/EnvironmentInfo/AppDirectoryInfo.cs b/NzbDrone.Common/EnvironmentInfo/AppDirectoryInfo.cs new file mode 100644 index 000000000..5c9a61ab8 --- /dev/null +++ b/NzbDrone.Common/EnvironmentInfo/AppDirectoryInfo.cs @@ -0,0 +1,38 @@ +using System; +using System.IO; +using System.Reflection; + +namespace NzbDrone.Common.EnvironmentInfo +{ + public interface IAppDirectoryInfo + { + string WorkingDirectory { get; } + string SystemTemp { get; } + string StartUpPath { get; } + } + + public class AppDirectoryInfo : IAppDirectoryInfo + { + public string WorkingDirectory + { + get { return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData, Environment.SpecialFolderOption.Create), "NzbDrone"); } + } + + public string StartUpPath + { + get + { + var path = new FileInfo(Assembly.GetExecutingAssembly().Location).Directory.FullName; + return path; + } + } + + public String SystemTemp + { + get + { + return Path.GetTempPath(); + } + } + } +} \ No newline at end of file diff --git a/NzbDrone.Common/EnvironmentInfo/BuildInfo.cs b/NzbDrone.Common/EnvironmentInfo/BuildInfo.cs new file mode 100644 index 000000000..56a010dd0 --- /dev/null +++ b/NzbDrone.Common/EnvironmentInfo/BuildInfo.cs @@ -0,0 +1,35 @@ +using System; +using System.IO; +using System.Reflection; + +namespace NzbDrone.Common.EnvironmentInfo +{ + public static class BuildInfo + { + public static Version Version + { + get { return Assembly.GetExecutingAssembly().GetName().Version; } + } + + public static DateTime BuildDateTime + { + get + { + var fileLocation = Assembly.GetCallingAssembly().Location; + return new FileInfo(fileLocation).LastWriteTimeUtc; + } + } + + public static bool IsDebug + { + get + { +#if DEBUG + return true; +#else + return false; +#endif + } + } + } +} \ No newline at end of file diff --git a/NzbDrone.Common/EnvironmentInfo/OsInfo.cs b/NzbDrone.Common/EnvironmentInfo/OsInfo.cs new file mode 100644 index 000000000..0afbac9de --- /dev/null +++ b/NzbDrone.Common/EnvironmentInfo/OsInfo.cs @@ -0,0 +1,36 @@ +using System; + +namespace NzbDrone.Common.EnvironmentInfo +{ + public static class OsInfo + { + + public static Version Version + { + get + { + OperatingSystem os = Environment.OSVersion; + Version version = os.Version; + + return version; + } + } + + public static bool IsMono + { + get + { + return Type.GetType("Mono.Runtime") != null; + } + } + + public static bool IsLinux + { + get + { + int p = (int)Environment.OSVersion.Platform; + return (p == 4) || (p == 6) || (p == 128); + } + } + } +} \ No newline at end of file diff --git a/NzbDrone.Common/EnvironmentInfo/RuntimeInfo.cs b/NzbDrone.Common/EnvironmentInfo/RuntimeInfo.cs new file mode 100644 index 000000000..0f96c0f99 --- /dev/null +++ b/NzbDrone.Common/EnvironmentInfo/RuntimeInfo.cs @@ -0,0 +1,65 @@ +using System; +using System.Diagnostics; +using System.Security.Principal; +using NLog; + +namespace NzbDrone.Common.EnvironmentInfo +{ + + public interface IRuntimeInfo + { + bool IsUserInteractive { get; } + bool IsAdmin { get; } + } + + public class RuntimeInfo : IRuntimeInfo + { + private readonly Logger _logger; + + public RuntimeInfo(Logger logger) + { + _logger = logger; + } + + public bool IsUserInteractive + { + get { return Environment.UserInteractive; } + } + + public bool IsAdmin + { + get + { + try + { + var principal = new WindowsPrincipal(WindowsIdentity.GetCurrent()); + return principal.IsInRole(WindowsBuiltInRole.Administrator); + } + catch (Exception ex) + { + _logger.WarnException("Error checking if the current user is an administrator.", ex); + return false; + } + } + } + + private static readonly string ProcessName = Process.GetCurrentProcess().ProcessName.ToLower(); + + public static bool IsProduction + { + get + { + if (BuildInfo.IsDebug || Debugger.IsAttached) return false; + if (BuildInfo.Version.Revision > 10000) return false; //Official builds will never have such a high revision + + var lowerProcessName = ProcessName.ToLower(); + if (lowerProcessName.Contains("vshost")) return false; + if (lowerProcessName.Contains("nunit")) return false; + if (lowerProcessName.Contains("jetbrain")) return false; + if (lowerProcessName.Contains("resharper")) return false; + + return true; + } + } + } +} \ No newline at end of file diff --git a/NzbDrone.Common/HttpProvider.cs b/NzbDrone.Common/HttpProvider.cs index 50d1b1763..97513f984 100644 --- a/NzbDrone.Common/HttpProvider.cs +++ b/NzbDrone.Common/HttpProvider.cs @@ -5,6 +5,7 @@ using System.Net; using System.Text; using NLog; +using NzbDrone.Common.EnvironmentInfo; namespace NzbDrone.Common { @@ -25,14 +26,12 @@ public class HttpProvider : IHttpProvider public const string ContentLenghtHeader = "Content-Length"; - private readonly IEnvironmentProvider _environmentProvider; private static readonly Logger logger = LogManager.GetCurrentClassLogger(); private readonly string _userAgent; - public HttpProvider(IEnvironmentProvider environmentProvider) + public HttpProvider() { - _environmentProvider = environmentProvider; - _userAgent = String.Format("NzbDrone {0}", _environmentProvider.Version); + _userAgent = String.Format("NzbDrone {0}", BuildInfo.Version); } public string DownloadString(string address) diff --git a/NzbDrone.Common/IDiskProvider.cs b/NzbDrone.Common/IDiskProvider.cs index ae3a2db60..6d0dc327d 100644 --- a/NzbDrone.Common/IDiskProvider.cs +++ b/NzbDrone.Common/IDiskProvider.cs @@ -5,6 +5,7 @@ using System.Runtime.InteropServices; using NLog; using NzbDrone.Common.EnsureThat; +using NzbDrone.Common.EnvironmentInfo; namespace NzbDrone.Common { @@ -285,7 +286,7 @@ public virtual long GetAvilableSpace(string path) if (driveInfo == null) { - if (EnvironmentProvider.IsLinux) + if (OsInfo.IsLinux) { return 0; } diff --git a/NzbDrone.Common/IEnvironmentProvider.cs b/NzbDrone.Common/IEnvironmentProvider.cs deleted file mode 100644 index c7fd9ec47..000000000 --- a/NzbDrone.Common/IEnvironmentProvider.cs +++ /dev/null @@ -1,149 +0,0 @@ -using System; -using System.Diagnostics; -using System.IO; -using System.Reflection; -using System.Security.Principal; -using NLog; - -namespace NzbDrone.Common -{ - public interface IEnvironmentProvider - { - bool IsUserInteractive { get; } - bool IsAdmin { get; } - string WorkingDirectory { get; } - string SystemTemp { get; } - Version Version { get; } - DateTime BuildDateTime { get; } - string StartUpPath { get; } - Version GetOsVersion(); - } - - public class EnvironmentProvider : IEnvironmentProvider - { - private readonly Logger _logger; - private static readonly string ProcessName = Process.GetCurrentProcess().ProcessName.ToLower(); - - private static readonly IEnvironmentProvider Instance = new EnvironmentProvider(); - - - public EnvironmentProvider() - { - _logger = LogManager.GetCurrentClassLogger(); - } - - public static bool IsProduction - { - get - { - if (IsDebug || Debugger.IsAttached) return false; - if (Instance.Version.Revision > 10000) return false; //Official builds will never have such a high revision - - var lowerProcessName = ProcessName.ToLower(); - if (lowerProcessName.Contains("vshost")) return false; - if (lowerProcessName.Contains("nunit")) return false; - if (lowerProcessName.Contains("jetbrain")) return false; - if (lowerProcessName.Contains("resharper")) return false; - - return true; - } - } - - public static bool IsMono - { - get - { - return Type.GetType("Mono.Runtime") != null; - } - } - - public static bool IsLinux - { - get - { - int p = (int)Environment.OSVersion.Platform; - return (p == 4) || (p == 6) || (p == 128); - } - } - - public static bool IsDebug - { - get - { -#if DEBUG - return true; -#else - return false; -#endif - } - } - - public static Guid UGuid { get; set; } - - public virtual bool IsUserInteractive - { - get { return Environment.UserInteractive; } - } - - public bool IsAdmin - { - get - { - try - { - var principal = new WindowsPrincipal(WindowsIdentity.GetCurrent()); - return principal.IsInRole(WindowsBuiltInRole.Administrator); - } - catch (Exception ex) - { - _logger.WarnException("Error checking if the current user is an administrator.", ex); - return false; - } - } - } - - public virtual string WorkingDirectory - { - get { return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "NzbDrone"); } - } - - public virtual string StartUpPath - { - get - { - var path = new FileInfo(Assembly.GetExecutingAssembly().Location).Directory.FullName; - return path; - } - } - - public virtual String SystemTemp - { - get - { - return Path.GetTempPath(); - } - } - - public virtual Version Version - { - get { return Assembly.GetExecutingAssembly().GetName().Version; } - } - - public virtual DateTime BuildDateTime - { - get - { - var fileLocation = Assembly.GetCallingAssembly().Location; - return new FileInfo(fileLocation).LastWriteTimeUtc; - } - } - - public virtual Version GetOsVersion() - { - OperatingSystem os = Environment.OSVersion; - Version version = os.Version; - - return version; - } - } -} \ No newline at end of file diff --git a/NzbDrone.Common/Instrumentation/ApplicationLogLayoutRenderer.cs b/NzbDrone.Common/Instrumentation/ApplicationLogLayoutRenderer.cs index 14004139c..2b7f3bdc5 100644 --- a/NzbDrone.Common/Instrumentation/ApplicationLogLayoutRenderer.cs +++ b/NzbDrone.Common/Instrumentation/ApplicationLogLayoutRenderer.cs @@ -3,6 +3,7 @@ using NLog; using NLog.Config; using NLog.LayoutRenderers; +using NzbDrone.Common.EnvironmentInfo; namespace NzbDrone.Common.Instrumentation { @@ -14,7 +15,7 @@ public class ApplicationLogLayoutRenderer : LayoutRenderer public ApplicationLogLayoutRenderer() { - _appData = Path.Combine(new EnvironmentProvider().GetLogFolder(), "nzbdrone.txt"); + _appData = Path.Combine(new AppDirectoryInfo().GetLogFolder(), "nzbdrone.txt"); } diff --git a/NzbDrone.Common/Instrumentation/ExceptronTarget.cs b/NzbDrone.Common/Instrumentation/ExceptronTarget.cs index 8c7b3db36..59ff5e918 100644 --- a/NzbDrone.Common/Instrumentation/ExceptronTarget.cs +++ b/NzbDrone.Common/Instrumentation/ExceptronTarget.cs @@ -7,6 +7,7 @@ using NLog.Config; using NLog.Layouts; using NLog.Targets; +using NzbDrone.Common.EnvironmentInfo; namespace NzbDrone.Common.Instrumentation { @@ -43,12 +44,12 @@ protected override void InitializeTarget() IncludeMachineName = true, }; - if (EnvironmentProvider.IsProduction) + if (RuntimeInfo.IsProduction) { config.ApiKey = "cc4728a35aa9414f9a0baa8eed56bc67"; } - ExceptronClient = new ExceptronClient(config, new EnvironmentProvider().Version); + ExceptronClient = new ExceptronClient(config, BuildInfo.Version); } diff --git a/NzbDrone.Common/Instrumentation/LogglyTarget.cs b/NzbDrone.Common/Instrumentation/LogglyTarget.cs index 5795f7369..532d8e761 100644 --- a/NzbDrone.Common/Instrumentation/LogglyTarget.cs +++ b/NzbDrone.Common/Instrumentation/LogglyTarget.cs @@ -3,6 +3,7 @@ using NLog.Config; using NLog.Layouts; using NLog.Targets; +using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Common.Serializer; using Logger = Loggly.Logger; @@ -10,7 +11,7 @@ namespace NzbDrone.Common.Instrumentation { public class LogglyTarget : TargetWithLayout { - private readonly IEnvironmentProvider _environmentProvider; + private readonly IAppDirectoryInfo _appDirectoryInfo; private Logger _logger; public void Register(LogLevel minLevel) @@ -25,16 +26,16 @@ public void Register(LogLevel minLevel) LogManager.ReconfigExistingLoggers(); } - public LogglyTarget(IEnvironmentProvider environmentProvider) + public LogglyTarget(IAppDirectoryInfo appDirectoryInfo) { - _environmentProvider = environmentProvider; + _appDirectoryInfo = appDirectoryInfo; } protected override void InitializeTarget() { string apiKey = string.Empty; - if (EnvironmentProvider.IsProduction) + if (RuntimeInfo.IsProduction) { apiKey = "4c4ecb69-d1b9-4e2a-b54b-b0c4cc143a95"; } @@ -67,7 +68,7 @@ protected override void Write(NLog.LogEventInfo logEvent) dictionary.Add("method", Layout.Render(logEvent)); dictionary.Add("level", logEvent.Level.Name); dictionary.Add("message", logEvent.GetFormattedMessage()); - dictionary.Add("ver", _environmentProvider.Version.ToString()); + dictionary.Add("ver", BuildInfo.Version.ToString()); _logger.Log(dictionary.ToJson()); } diff --git a/NzbDrone.Common/Instrumentation/UpdateLogLayoutRenderer.cs b/NzbDrone.Common/Instrumentation/UpdateLogLayoutRenderer.cs index c40e15f40..93293ce1e 100644 --- a/NzbDrone.Common/Instrumentation/UpdateLogLayoutRenderer.cs +++ b/NzbDrone.Common/Instrumentation/UpdateLogLayoutRenderer.cs @@ -4,6 +4,7 @@ using NLog; using NLog.Config; using NLog.LayoutRenderers; +using NzbDrone.Common.EnvironmentInfo; namespace NzbDrone.Common.Instrumentation { @@ -15,7 +16,7 @@ public class UpdateLogLayoutRenderer : LayoutRenderer public UpdateLogLayoutRenderer() { - _appData = Path.Combine(new EnvironmentProvider().GetUpdateLogFolder(), DateTime.Now.ToString("yy.MM.d-HH.mm") + ".txt"); + _appData = Path.Combine(new AppDirectoryInfo().GetUpdateLogFolder(), DateTime.Now.ToString("yy.MM.d-HH.mm") + ".txt"); } diff --git a/NzbDrone.Common/NzbDrone.Common.csproj b/NzbDrone.Common/NzbDrone.Common.csproj index e1bb25519..23723d7f9 100644 --- a/NzbDrone.Common/NzbDrone.Common.csproj +++ b/NzbDrone.Common/NzbDrone.Common.csproj @@ -103,6 +103,9 @@ + + + @@ -146,7 +149,7 @@ - + diff --git a/NzbDrone.Common/PathExtensions.cs b/NzbDrone.Common/PathExtensions.cs index 4c320740b..a0051a80a 100644 --- a/NzbDrone.Common/PathExtensions.cs +++ b/NzbDrone.Common/PathExtensions.cs @@ -1,5 +1,6 @@ using System.IO; using NzbDrone.Common.EnsureThat; +using NzbDrone.Common.EnvironmentInfo; namespace NzbDrone.Common { @@ -55,69 +56,69 @@ public static string GetActualCasing(this string filename) } - public static string GetAppDataPath(this IEnvironmentProvider environmentProvider) + public static string GetAppDataPath(this IAppDirectoryInfo IAppDirectoryInfo) { - return environmentProvider.WorkingDirectory; + return IAppDirectoryInfo.WorkingDirectory; } - public static string GetLogFolder(this IEnvironmentProvider environmentProvider) + public static string GetLogFolder(this IAppDirectoryInfo IAppDirectoryInfo) { - return Path.Combine(environmentProvider.GetAppDataPath(), "logs"); + return Path.Combine(GetAppDataPath(IAppDirectoryInfo), "logs"); } - public static string GetConfigPath(this IEnvironmentProvider environmentProvider) + public static string GetConfigPath(this IAppDirectoryInfo IAppDirectoryInfo) { - return Path.Combine(environmentProvider.GetAppDataPath(), APP_CONFIG_FILE); + return Path.Combine(GetAppDataPath(IAppDirectoryInfo), APP_CONFIG_FILE); } - public static string GetMediaCoverPath(this IEnvironmentProvider environmentProvider) + public static string GetMediaCoverPath(this IAppDirectoryInfo IAppDirectoryInfo) { - return Path.Combine(environmentProvider.GetAppDataPath(), "MediaCover"); + return Path.Combine(GetAppDataPath(IAppDirectoryInfo), "MediaCover"); } - public static string GetUpdateLogFolder(this IEnvironmentProvider environmentProvider) + public static string GetUpdateLogFolder(this IAppDirectoryInfo IAppDirectoryInfo) { - return Path.Combine(environmentProvider.GetAppDataPath(), UPDATE_LOG_FOLDER_NAME); + return Path.Combine(GetAppDataPath(IAppDirectoryInfo), UPDATE_LOG_FOLDER_NAME); } - public static string GetUpdateSandboxFolder(this IEnvironmentProvider environmentProvider) + public static string GetUpdateSandboxFolder(this IAppDirectoryInfo IAppDirectoryInfo) { - return Path.Combine(environmentProvider.SystemTemp, UPDATE_SANDBOX_FOLDER_NAME); + return Path.Combine(IAppDirectoryInfo.SystemTemp, UPDATE_SANDBOX_FOLDER_NAME); } - public static string GetUpdateBackUpFolder(this IEnvironmentProvider environmentProvider) + public static string GetUpdateBackUpFolder(this IAppDirectoryInfo IAppDirectoryInfo) { - return Path.Combine(environmentProvider.GetUpdateSandboxFolder(), UPDATE_BACKUP_FOLDER_NAME); + return Path.Combine(GetUpdateSandboxFolder(IAppDirectoryInfo), UPDATE_BACKUP_FOLDER_NAME); } - public static string GetUpdatePackageFolder(this IEnvironmentProvider environmentProvider) + public static string GetUpdatePackageFolder(this IAppDirectoryInfo IAppDirectoryInfo) { - return Path.Combine(environmentProvider.GetUpdateSandboxFolder(), UPDATE_PACKAGE_FOLDER_NAME); + return Path.Combine(GetUpdateSandboxFolder(IAppDirectoryInfo), UPDATE_PACKAGE_FOLDER_NAME); } - public static string GetUpdateClientFolder(this IEnvironmentProvider environmentProvider) + public static string GetUpdateClientFolder(this IAppDirectoryInfo IAppDirectoryInfo) { - return Path.Combine(environmentProvider.GetUpdatePackageFolder(), UPDATE_CLIENT_FOLDER_NAME); + return Path.Combine(GetUpdatePackageFolder(IAppDirectoryInfo), UPDATE_CLIENT_FOLDER_NAME); } - public static string GetUpdateClientExePath(this IEnvironmentProvider environmentProvider) + public static string GetUpdateClientExePath(this IAppDirectoryInfo IAppDirectoryInfo) { - return Path.Combine(environmentProvider.GetUpdateSandboxFolder(), UPDATE_CLIENT_EXE); + return Path.Combine(GetUpdateSandboxFolder(IAppDirectoryInfo), UPDATE_CLIENT_EXE); } - public static string GetConfigBackupFile(this IEnvironmentProvider environmentProvider) + public static string GetConfigBackupFile(this IAppDirectoryInfo IAppDirectoryInfo) { - return Path.Combine(environmentProvider.GetAppDataPath(), BACKUP_ZIP_FILE); + return Path.Combine(GetAppDataPath(IAppDirectoryInfo), BACKUP_ZIP_FILE); } - public static string GetNzbDroneDatabase(this IEnvironmentProvider environmentProvider) + public static string GetNzbDroneDatabase(this IAppDirectoryInfo IAppDirectoryInfo) { - return Path.Combine(environmentProvider.GetAppDataPath(), NZBDRONE_DB); + return Path.Combine(GetAppDataPath(IAppDirectoryInfo), NZBDRONE_DB); } - public static string GetLogDatabase(this IEnvironmentProvider environmentProvider) + public static string GetLogDatabase(this IAppDirectoryInfo IAppDirectoryInfo) { - return Path.Combine(environmentProvider.GetAppDataPath(), NZBDRONE_LOG_DB); + return Path.Combine(GetAppDataPath(IAppDirectoryInfo), NZBDRONE_LOG_DB); } } } \ No newline at end of file diff --git a/NzbDrone.Common/RestProvider.cs b/NzbDrone.Common/RestProvider.cs index 4cd76eed9..1bd43344a 100644 --- a/NzbDrone.Common/RestProvider.cs +++ b/NzbDrone.Common/RestProvider.cs @@ -6,36 +6,20 @@ using NLog; using Newtonsoft.Json; using NzbDrone.Common.Contract; +using NzbDrone.Common.EnvironmentInfo; namespace NzbDrone.Common { public class RestProvider { - - private static readonly Logger logger = LogManager.GetCurrentClassLogger(); - - private readonly IEnvironmentProvider _environmentProvider; - - - public RestProvider(IEnvironmentProvider environmentProvider) - { - _environmentProvider = environmentProvider; - } - - public RestProvider() - { - - } - private const int TIMEOUT = 15000; private const string METHOD = "POST"; public virtual void PostData(string url, ReportBase reportBase) { - reportBase.UGuid = EnvironmentProvider.UGuid; - reportBase.Version = _environmentProvider.Version.ToString(); - reportBase.IsProduction = EnvironmentProvider.IsProduction; + reportBase.Version = BuildInfo.Version.ToString(); + reportBase.IsProduction = RuntimeInfo.IsProduction; PostData(url, reportBase as object); } diff --git a/NzbDrone.Core.Test/Framework/CoreTest.cs b/NzbDrone.Core.Test/Framework/CoreTest.cs index 86e3f024d..347792e2b 100644 --- a/NzbDrone.Core.Test/Framework/CoreTest.cs +++ b/NzbDrone.Core.Test/Framework/CoreTest.cs @@ -21,7 +21,7 @@ protected string ReadAllText(params string[] path) protected void UseRealHttp() { - Mocker.SetConstant(new HttpProvider(new EnvironmentProvider())); + Mocker.SetConstant(new HttpProvider()); } } diff --git a/NzbDrone.Core.Test/MediaCoverTests/MediaCoverServiceFixture.cs b/NzbDrone.Core.Test/MediaCoverTests/MediaCoverServiceFixture.cs index 220ca98d5..6566ee3c3 100644 --- a/NzbDrone.Core.Test/MediaCoverTests/MediaCoverServiceFixture.cs +++ b/NzbDrone.Core.Test/MediaCoverTests/MediaCoverServiceFixture.cs @@ -4,6 +4,7 @@ using Moq; using NUnit.Framework; using NzbDrone.Common; +using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Core.MediaCover; using NzbDrone.Core.Test.Framework; using System.Linq; @@ -16,9 +17,9 @@ public class MediaCoverServiceFixture : CoreTest [SetUp] public void Setup() { - //Mocker.SetConstant(new HttpProvider(new EnvironmentProvider())); + //Mocker.SetConstant(new HttpProvider(new IAppDirectoryInfo())); //Mocker.SetConstant(new DiskProvider()); - Mocker.SetConstant(new EnvironmentProvider()); + Mocker.SetConstant(new AppDirectoryInfo()); } [Test] diff --git a/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj b/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj index ce20320ca..b313da693 100644 --- a/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj +++ b/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj @@ -216,7 +216,6 @@ - diff --git a/NzbDrone.Core.Test/ParserTests/ParserFixture.cs b/NzbDrone.Core.Test/ParserTests/ParserFixture.cs index 6f8564407..320e68aed 100644 --- a/NzbDrone.Core.Test/ParserTests/ParserFixture.cs +++ b/NzbDrone.Core.Test/ParserTests/ParserFixture.cs @@ -87,7 +87,7 @@ public void ParseTitle_single(string postTitle, string title, int seasonNumber, result.EpisodeNumbers.Should().HaveCount(1); result.SeasonNumber.Should().Be(seasonNumber); result.EpisodeNumbers.First().Should().Be(episodeNumber); - result.SeriesTitle.Should().Be(Parser.Parser.CleanSeriesTitle(title)); + result.SeriesTitle.Should().Be(title.CleanSeriesTitle()); } [TestCase(@"z:\tv shows\battlestar galactica (2003)\Season 3\S03E05 - Collaborators.mkv", 3, 5)] @@ -161,7 +161,7 @@ public void TitleParse_multi(string postTitle, string title, int season, int[] e var result = Parser.Parser.ParseTitle(postTitle); result.SeasonNumber.Should().Be(season); result.EpisodeNumbers.Should().BeEquivalentTo(episodes); - result.SeriesTitle.Should().Be(Parser.Parser.CleanSeriesTitle(title)); + result.SeriesTitle.Should().Be(title.CleanSeriesTitle()); } @@ -180,7 +180,7 @@ public void parse_daily_episodes(string postTitle, string title, int year, int m var result = Parser.Parser.ParseTitle(postTitle); var airDate = new DateTime(year, month, day); result.Should().NotBeNull(); - result.SeriesTitle.Should().Be(Parser.Parser.CleanSeriesTitle(title)); + result.SeriesTitle.Should().Be(title.CleanSeriesTitle()); result.AirDate.Should().Be(airDate); result.EpisodeNumbers.Should().BeNull(); } @@ -233,7 +233,7 @@ public void full_season_release_parse(string postTitle, string title, int season { var result = Parser.Parser.ParseTitle(postTitle); result.SeasonNumber.Should().Be(season); - result.SeriesTitle.Should().Be(Parser.Parser.CleanSeriesTitle(title)); + result.SeriesTitle.Should().Be(title.CleanSeriesTitle()); result.EpisodeNumbers.Length.Should().Be(0); result.FullSeason.Should().BeTrue(); } @@ -245,7 +245,7 @@ public void full_season_release_parse(string postTitle, string title, int season [TestCase("Parenthood.2010", "parenthood2010")] public void series_name_normalize(string parsedSeriesName, string seriesName) { - var result = Parser.Parser.CleanSeriesTitle(parsedSeriesName); + var result = parsedSeriesName.CleanSeriesTitle(); result.Should().Be(seriesName); } @@ -257,7 +257,7 @@ public void series_name_normalize(string parsedSeriesName, string seriesName) [TestCase("24", "24")] public void Normalize_Title(string dirty, string clean) { - var result = Parser.Parser.CleanSeriesTitle(dirty); + var result = dirty.CleanSeriesTitle(); result.Should().Be(clean); } @@ -285,7 +285,7 @@ public void Normalize_removed_common_words(string word) foreach (var s in dirtyFormat) { var dirty = String.Format(s, word); - Parser.Parser.CleanSeriesTitle(dirty).Should().Be("wordword"); + dirty.CleanSeriesTitle().Should().Be("wordword"); } } @@ -311,7 +311,7 @@ public void Normalize_not_removed_common_words_in_the_middle(string word) foreach (var s in dirtyFormat) { var dirty = String.Format(s, word); - Parser.Parser.CleanSeriesTitle(dirty).Should().Be(("word" + word.ToLower() + "word")); + dirty.CleanSeriesTitle().Should().Be(("word" + word.ToLower() + "word")); } } @@ -328,7 +328,7 @@ public void Normalize_not_removed_common_words_in_the_middle(string word) public void parse_series_name(string postTitle, string title) { var result = Parser.Parser.ParseSeriesName(postTitle); - result.Should().Be(Parser.Parser.CleanSeriesTitle(title)); + result.Should().Be(title.CleanSeriesTitle()); } [TestCase("Castle.2009.S01E14.English.HDTV.XviD-LOL", Language.English)] @@ -373,7 +373,7 @@ public void parse_season_info(string postTitle, string seriesName, int seasonNum { var result = Parser.Parser.ParseTitle(postTitle); - result.SeriesTitle.Should().Be(Parser.Parser.CleanSeriesTitle(seriesName)); + result.SeriesTitle.Should().Be(seriesName.CleanSeriesTitle()); result.SeasonNumber.Should().Be(seasonNumber); result.FullSeason.Should().BeTrue(); } diff --git a/NzbDrone.Core.Test/UpdateTests/GetUpdateLogFixture.cs b/NzbDrone.Core.Test/UpdateTests/GetUpdateLogFixture.cs deleted file mode 100644 index 5a1bec76b..000000000 --- a/NzbDrone.Core.Test/UpdateTests/GetUpdateLogFixture.cs +++ /dev/null @@ -1,57 +0,0 @@ -using System; -using System.IO; -using FluentAssertions; -using NUnit.Framework; -using NzbDrone.Common; -using NzbDrone.Core.Test.Framework; -using NzbDrone.Core.Update; - -namespace NzbDrone.Core.Test.UpdateTests -{ - public class GetUpdateLogFixture : CoreTest - { - String _updateLogFolder; - - - [SetUp] - public void Setup() - { - WithTempAsAppPath(); - - _updateLogFolder = Mocker.GetMock().Object.GetUpdateLogFolder(); - - Mocker.GetMock() - .Setup(c => c.GetFiles(_updateLogFolder, SearchOption.TopDirectoryOnly)) - .Returns(new[] - { - "C:\\nzbdrone\\update\\2011.09.20-19-08.txt", - "C:\\nzbdrone\\update\\2011.10.20-20-08.txt", - "C:\\nzbdrone\\update\\2011.12.20-21-08.txt" - }); - - Mocker.GetMock() - .Setup(c => c.FolderExists(_updateLogFolder)) - .Returns(true); - } - - - [Test] - public void get_logs_should_return_empty_list_if_directory_doesnt_exist() - { - Mocker.GetMock() - .Setup(c => c.FolderExists(_updateLogFolder)) - .Returns(false); - - Subject.GetUpdateLogFiles().Should().BeEmpty(); - } - - - [Test] - public void get_logs_should_return_list_of_files_in_log_folder() - { - var logs = Subject.GetUpdateLogFiles(); - logs.Should().HaveCount(3); - } - - } -} diff --git a/NzbDrone.Core.Test/UpdateTests/UpdateServiceFixture.cs b/NzbDrone.Core.Test/UpdateTests/UpdateServiceFixture.cs index e660ad3d5..86a294faf 100644 --- a/NzbDrone.Core.Test/UpdateTests/UpdateServiceFixture.cs +++ b/NzbDrone.Core.Test/UpdateTests/UpdateServiceFixture.cs @@ -5,6 +5,7 @@ using Moq; using NUnit.Framework; using NzbDrone.Common; +using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Common.Model; using NzbDrone.Core.Test.Framework; using NzbDrone.Core.Update; @@ -15,26 +16,26 @@ namespace NzbDrone.Core.Test.UpdateTests { [TestFixture] - public class UpdateServiceFixture : CoreTest + public class UpdateServiceFixture : CoreTest { private string _sandboxFolder; private readonly UpdatePackage _updatePackage = new UpdatePackage { - FileName = "NzbDrone.kay.one.0.6.0.2031.zip", - Url = "http://update.nzbdrone.com/_test/NzbDrone.zip", - Version = new Version("0.6.0.2031") + FileName = "NzbDrone.vnext.0.8.1.226.zip", + Url = "http://update.nzbdrone.com/vnext/NzbDrone.vnext.0.8.1.226.zip", + Version = new Version("0.8.1.226") }; [SetUp] public void Setup() { - Mocker.GetMock().SetupGet(c => c.SystemTemp).Returns(TempFolder); - Mocker.GetMock().Setup(c => c.GetLatestUpdate()).Returns(_updatePackage); + Mocker.GetMock().SetupGet(c => c.SystemTemp).Returns(TempFolder); + Mocker.GetMock().Setup(c => c.AvailableUpdate()).Returns(_updatePackage); Mocker.GetMock().Setup(c => c.GetCurrentProcess()).Returns(new ProcessInfo { Id = 12 }); - _sandboxFolder = Mocker.GetMock().Object.GetUpdateSandboxFolder(); + _sandboxFolder = Mocker.GetMock().Object.GetUpdateSandboxFolder(); } @@ -86,7 +87,7 @@ public void Should_extract_update_package() [Test] public void Should_copy_update_client_to_root_of_sandbox() { - var updateClientFolder = Mocker.GetMock().Object.GetUpdateClientFolder(); + var updateClientFolder = Mocker.GetMock().Object.GetUpdateClientFolder(); Subject.Execute(new ApplicationUpdateCommand()); @@ -109,7 +110,7 @@ public void should_start_update_client() [Test] public void when_no_updates_are_available_should_return_without_error_or_warnings() { - Mocker.GetMock().Setup(c => c.GetLatestUpdate()).Returns(null); + Mocker.GetMock().Setup(c => c.AvailableUpdate()).Returns(null); Subject.Execute(new ApplicationUpdateCommand()); @@ -123,7 +124,7 @@ public void Should_download_and_extract_to_temp_folder() { UseRealHttp(); - var updateSubFolder = new DirectoryInfo(Mocker.GetMock().Object.GetUpdateSandboxFolder()); + var updateSubFolder = new DirectoryInfo(Mocker.GetMock().Object.GetUpdateSandboxFolder()); updateSubFolder.Exists.Should().BeFalse(); diff --git a/NzbDrone.Core/Configuration/ConfigFileProvider.cs b/NzbDrone.Core/Configuration/ConfigFileProvider.cs index 99d6ec882..4bc5ecb9f 100644 --- a/NzbDrone.Core/Configuration/ConfigFileProvider.cs +++ b/NzbDrone.Core/Configuration/ConfigFileProvider.cs @@ -6,6 +6,7 @@ using System.Xml.Linq; using NzbDrone.Common; using NzbDrone.Common.Cache; +using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Common.Model; namespace NzbDrone.Core.Configuration @@ -24,16 +25,16 @@ public interface IConfigFileProvider public class ConfigFileProvider : IConfigFileProvider { - private readonly IEnvironmentProvider _environmentProvider; + private readonly IAppDirectoryInfo _appDirectoryInfo; private readonly ICached _cache; private readonly string _configFile; - public ConfigFileProvider(IEnvironmentProvider environmentProvider, ICacheManger cacheManger) + public ConfigFileProvider(IAppDirectoryInfo appDirectoryInfo, ICacheManger cacheManger) { - _environmentProvider = environmentProvider; + _appDirectoryInfo = appDirectoryInfo; _cache = cacheManger.GetCache(this.GetType()); - _configFile = _environmentProvider.GetConfigPath(); + _configFile = _appDirectoryInfo.GetConfigPath(); } public Dictionary GetConfigDictionary() diff --git a/NzbDrone.Core/MediaCover/MediaCoverService.cs b/NzbDrone.Core/MediaCover/MediaCoverService.cs index 80017f1f6..dc896d9e6 100644 --- a/NzbDrone.Core/MediaCover/MediaCoverService.cs +++ b/NzbDrone.Core/MediaCover/MediaCoverService.cs @@ -3,6 +3,7 @@ using System.IO; using NLog; using NzbDrone.Common; +using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Common.Messaging; using NzbDrone.Core.Tv; using NzbDrone.Core.Tv.Events; @@ -21,7 +22,7 @@ public class MediaCoverService : private readonly string _coverRootFolder; - public MediaCoverService(IHttpProvider httpProvider, IDiskProvider diskProvider, IEnvironmentProvider environmentProvider, + public MediaCoverService(IHttpProvider httpProvider, IDiskProvider diskProvider, IAppDirectoryInfo appDirectoryInfo, ICoverExistsSpecification coverExistsSpecification, Logger logger) { _httpProvider = httpProvider; @@ -29,7 +30,7 @@ public MediaCoverService(IHttpProvider httpProvider, IDiskProvider diskProvider, _coverExistsSpecification = coverExistsSpecification; _logger = logger; - _coverRootFolder = environmentProvider.GetMediaCoverPath(); + _coverRootFolder = appDirectoryInfo.GetMediaCoverPath(); } public void HandleAsync(SeriesUpdatedEvent message) diff --git a/NzbDrone.Core/NzbDrone.Core.csproj b/NzbDrone.Core/NzbDrone.Core.csproj index a736c8aae..5c6df3f01 100644 --- a/NzbDrone.Core/NzbDrone.Core.csproj +++ b/NzbDrone.Core/NzbDrone.Core.csproj @@ -525,9 +525,10 @@ + - + diff --git a/NzbDrone.Core/Providers/BackupProvider.cs b/NzbDrone.Core/Providers/BackupProvider.cs index 9833ffb1d..bb3abe315 100644 --- a/NzbDrone.Core/Providers/BackupProvider.cs +++ b/NzbDrone.Core/Providers/BackupProvider.cs @@ -1,30 +1,28 @@ using System; using System.Linq; using Ionic.Zip; -using NLog; using NzbDrone.Common; +using NzbDrone.Common.EnvironmentInfo; namespace NzbDrone.Core.Providers { public class BackupProvider { - private readonly IEnvironmentProvider _environmentProvider; - private static readonly Logger logger = LogManager.GetCurrentClassLogger(); - - public BackupProvider(IEnvironmentProvider environmentProvider) + private readonly IAppDirectoryInfo _appDirectoryInfo; + + public BackupProvider(IAppDirectoryInfo appDirectoryInfo) { - _environmentProvider = environmentProvider; + _appDirectoryInfo = appDirectoryInfo; } public BackupProvider() { - } public virtual string CreateBackupZip() { - var configFile = _environmentProvider.GetConfigPath(); - var zipFile = _environmentProvider.GetConfigBackupFile(); + var configFile = _appDirectoryInfo.GetConfigPath(); + var zipFile = _appDirectoryInfo.GetConfigBackupFile(); using (var zip = new ZipFile()) { diff --git a/NzbDrone.Core/Update/InstallUpdateService.cs b/NzbDrone.Core/Update/InstallUpdateService.cs new file mode 100644 index 000000000..f65cc0bd1 --- /dev/null +++ b/NzbDrone.Core/Update/InstallUpdateService.cs @@ -0,0 +1,86 @@ +using System.Diagnostics; +using System.IO; +using NLog; +using NzbDrone.Common; +using NzbDrone.Common.EnvironmentInfo; +using NzbDrone.Common.Messaging; +using NzbDrone.Core.Update.Commands; + +namespace NzbDrone.Core.Update +{ + public class InstallUpdateService : IExecute + { + private readonly ICheckUpdateService _checkUpdateService; + private readonly Logger _logger; + private readonly IAppDirectoryInfo _appDirectoryInfo; + + private readonly IDiskProvider _diskProvider; + private readonly IHttpProvider _httpProvider; + private readonly ArchiveProvider _archiveProvider; + private readonly IProcessProvider _processProvider; + + + public InstallUpdateService(ICheckUpdateService checkUpdateService, IAppDirectoryInfo appDirectoryInfo, + IDiskProvider diskProvider, IHttpProvider httpProvider, + ArchiveProvider archiveProvider, IProcessProvider processProvider, Logger logger) + { + _checkUpdateService = checkUpdateService; + _appDirectoryInfo = appDirectoryInfo; + _diskProvider = diskProvider; + _httpProvider = httpProvider; + _archiveProvider = archiveProvider; + _processProvider = processProvider; + _logger = logger; + } + + public void Execute(ApplicationUpdateCommand message) + { + var latestAvailable = _checkUpdateService.AvailableUpdate(); + + if (latestAvailable != null) + { + InstallUpdate(latestAvailable); + } + } + + private void InstallUpdate(UpdatePackage updatePackage) + { + var updateSandboxFolder = _appDirectoryInfo.GetUpdateSandboxFolder(); + + var packageDestination = Path.Combine(updateSandboxFolder, updatePackage.FileName); + + if (_diskProvider.FolderExists(updateSandboxFolder)) + { + _logger.Info("Deleting old update files"); + _diskProvider.DeleteFolder(updateSandboxFolder, true); + } + + _logger.Info("Downloading update package from [{0}] to [{1}]", updatePackage.Url, packageDestination); + _httpProvider.DownloadFile(updatePackage.Url, packageDestination); + _logger.Info("Download completed for update package from [{0}]", updatePackage.FileName); + + _logger.Info("Extracting Update package"); + _archiveProvider.ExtractArchive(packageDestination, updateSandboxFolder); + _logger.Info("Update package extracted successfully"); + + _logger.Info("Preparing client"); + _diskProvider.MoveDirectory(_appDirectoryInfo.GetUpdateClientFolder(), + updateSandboxFolder); + + + _logger.Info("Starting update client"); + var startInfo = new ProcessStartInfo + { + FileName = _appDirectoryInfo.GetUpdateClientExePath(), + Arguments = _processProvider.GetCurrentProcess().Id.ToString() + }; + + var process = _processProvider.Start(startInfo); + + _processProvider.WaitForExit(process); + + _logger.Error("Update process failed"); + } + + } +} \ No newline at end of file diff --git a/NzbDrone.Core/Update/UpdateCheckService.cs b/NzbDrone.Core/Update/UpdateCheckService.cs new file mode 100644 index 000000000..36d43ea44 --- /dev/null +++ b/NzbDrone.Core/Update/UpdateCheckService.cs @@ -0,0 +1,38 @@ +using NLog; +using NzbDrone.Common.EnvironmentInfo; + +namespace NzbDrone.Core.Update +{ + public interface ICheckUpdateService + { + UpdatePackage AvailableUpdate(); + } + + + public class CheckUpdateService : ICheckUpdateService + { + private readonly IUpdatePackageProvider _updatePackageProvider; + + private readonly Logger _logger; + + + public CheckUpdateService(IUpdatePackageProvider updatePackageProvider, Logger logger) + { + _updatePackageProvider = updatePackageProvider; + _logger = logger; + } + + public UpdatePackage AvailableUpdate() + { + var latestAvailable = _updatePackageProvider.GetLatestUpdate(); + + if (latestAvailable == null || latestAvailable.Version <= BuildInfo.Version) + { + _logger.Debug("No update available."); + return null; + } + + return latestAvailable; + } + } +} \ No newline at end of file diff --git a/NzbDrone.Core/Update/UpdateService.cs b/NzbDrone.Core/Update/UpdateService.cs deleted file mode 100644 index 90310c36f..000000000 --- a/NzbDrone.Core/Update/UpdateService.cs +++ /dev/null @@ -1,134 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.Globalization; -using System.IO; -using System.Linq; -using NLog; -using NzbDrone.Common; -using NzbDrone.Common.Messaging; -using NzbDrone.Core.Configuration; -using NzbDrone.Core.Update.Commands; - -namespace NzbDrone.Core.Update -{ - public interface IUpdateService : IExecute - { - Dictionary GetUpdateLogFiles(); - UpdatePackage AvailableUpdate(); - } - - public class UpdateService : IUpdateService - { - private readonly IUpdatePackageProvider _updatePackageProvider; - private readonly IEnvironmentProvider _environmentProvider; - - private readonly IDiskProvider _diskProvider; - private readonly IHttpProvider _httpProvider; - private readonly IConfigFileProvider _configFileProvider; - private readonly ArchiveProvider _archiveProvider; - private readonly IProcessProvider _processProvider; - private readonly Logger _logger; - - - public UpdateService(IUpdatePackageProvider updatePackageProvider, IEnvironmentProvider environmentProvider, - IDiskProvider diskProvider, - IHttpProvider httpProvider, IConfigFileProvider configFileProvider, - ArchiveProvider archiveProvider, IProcessProvider processProvider, Logger logger) - { - _updatePackageProvider = updatePackageProvider; - _environmentProvider = environmentProvider; - _diskProvider = diskProvider; - _httpProvider = httpProvider; - _configFileProvider = configFileProvider; - _archiveProvider = archiveProvider; - _processProvider = processProvider; - _logger = logger; - } - - - private void InstallUpdate(UpdatePackage updatePackage) - { - var updateSandboxFolder = _environmentProvider.GetUpdateSandboxFolder(); - - var packageDestination = Path.Combine(updateSandboxFolder, updatePackage.FileName); - - if (_diskProvider.FolderExists(updateSandboxFolder)) - { - _logger.Info("Deleting old update files"); - _diskProvider.DeleteFolder(updateSandboxFolder, true); - } - - _logger.Info("Downloading update package from [{0}] to [{1}]", updatePackage.Url, packageDestination); - _httpProvider.DownloadFile(updatePackage.Url, packageDestination); - _logger.Info("Download completed for update package from [{0}]", updatePackage.FileName); - - _logger.Info("Extracting Update package"); - _archiveProvider.ExtractArchive(packageDestination, updateSandboxFolder); - _logger.Info("Update package extracted successfully"); - - _logger.Info("Preparing client"); - _diskProvider.MoveDirectory(_environmentProvider.GetUpdateClientFolder(), - updateSandboxFolder); - - - _logger.Info("Starting update client"); - var startInfo = new ProcessStartInfo - { - FileName = _environmentProvider.GetUpdateClientExePath(), - Arguments = _processProvider.GetCurrentProcess().Id.ToString() - }; - - var process = _processProvider.Start(startInfo); - - _processProvider.WaitForExit(process); - - _logger.Error("Update process failed"); - } - - public Dictionary GetUpdateLogFiles() - { - var list = new Dictionary(); - - if (_diskProvider.FolderExists(_environmentProvider.GetUpdateLogFolder())) - { - var provider = CultureInfo.InvariantCulture; - 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; - } - - public UpdatePackage AvailableUpdate() - { - var latestAvailable = _updatePackageProvider.GetLatestUpdate(); - - if (latestAvailable == null || latestAvailable.Version <= _environmentProvider.Version) - { - _logger.Debug("No update available."); - return null; - } - - return latestAvailable; - } - - public void Execute(ApplicationUpdateCommand message) - { - var latestAvailable = AvailableUpdate(); - - if (latestAvailable != null) - { - InstallUpdate(latestAvailable); - } - } - } -} \ No newline at end of file diff --git a/NzbDrone.Integration.Test/IntegrationTest.cs b/NzbDrone.Integration.Test/IntegrationTest.cs index f8881e701..0a38ad55c 100644 --- a/NzbDrone.Integration.Test/IntegrationTest.cs +++ b/NzbDrone.Integration.Test/IntegrationTest.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.IO; -using System.Linq; using Moq; using NLog; using NLog.Config; @@ -12,6 +11,7 @@ using NzbDrone.Api.RootFolders; using NzbDrone.Common; using NzbDrone.Common.Composition; +using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Core.Configuration; using NzbDrone.Core.Datastore; using NzbDrone.Core.Jobs; @@ -61,19 +61,19 @@ private void InitDatabase() Logger.Info("Registering Database..."); //TODO: move this to factory - var environmentProvider = new EnvironmentProvider(); - var appDataPath = environmentProvider.GetAppDataPath(); + var IAppDirectoryInfo = new AppDirectoryInfo(); + var appDataPath = IAppDirectoryInfo.GetAppDataPath(); if (!Directory.Exists(appDataPath)) { Directory.CreateDirectory(appDataPath); } - var dbPath = Path.Combine(environmentProvider.WorkingDirectory, DateTime.Now.Ticks + ".db"); + var dbPath = Path.Combine(IAppDirectoryInfo.WorkingDirectory, DateTime.Now.Ticks + ".db"); - Logger.Info("Working Folder: {0}", environmentProvider.WorkingDirectory); - Logger.Info("Data Folder: {0}", environmentProvider.GetAppDataPath()); + Logger.Info("Working Folder: {0}", IAppDirectoryInfo.WorkingDirectory); + Logger.Info("Data Folder: {0}", IAppDirectoryInfo.GetAppDataPath()); Logger.Info("DB Na: {0}", dbPath); @@ -95,27 +95,31 @@ public void SmokeTestSetup() _bootstrapper = new NancyBootstrapper(Container.TinyContainer); - var _hostConfig = new Mock(); - _hostConfig.SetupGet(c => c.Port).Returns(1313); + var hostConfig = new Mock(); + hostConfig.SetupGet(c => c.Port).Returns(1313); - _hostController = new OwinHostController(_hostConfig.Object, new[] { new NancyMiddleWare(_bootstrapper) }, Logger); + _hostController = new OwinHostController(hostConfig.Object, new[] { new NancyMiddleWare(_bootstrapper) }, Logger); + InitRestClients(); + + _hostController.StartServer(); + } + + private void InitRestClients() + { RestClient = new RestClient(_hostController.AppUrl + "/api/"); Series = new SeriesClient(RestClient); Releases = new ReleaseClient(RestClient); RootFolders = new ClientBase(RestClient); Commands = new ClientBase(RestClient); Indexers = new IndexerClient(RestClient); - - _hostController.StartServer(); } [TearDown] public void SmokeTestTearDown() { _hostController.StopServer(); - _bootstrapper.Shutdown(); } } diff --git a/NzbDrone.Test.Common/TestBase.cs b/NzbDrone.Test.Common/TestBase.cs index 0cbe7a98a..5444fc69f 100644 --- a/NzbDrone.Test.Common/TestBase.cs +++ b/NzbDrone.Test.Common/TestBase.cs @@ -6,8 +6,8 @@ using NUnit.Framework; using NzbDrone.Common; using NzbDrone.Common.Cache; +using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Common.Messaging; -using NzbDrone.Common.Serializer; using NzbDrone.Test.Common.AutoMoq; namespace NzbDrone.Test.Common @@ -74,6 +74,8 @@ private string VirtualPath [SetUp] public void TestBaseSetup() { + WithTempAsAppPath(); + GetType().IsPublic.Should().BeTrue("All Test fixtures should be public to work in mono."); Mocker.SetConstant(new CacheManger()); @@ -110,7 +112,7 @@ public void TestBaseTearDown() { var testName = TestContext.CurrentContext.Test.Name.ToLower(); - if (EnvironmentProvider.IsLinux && testName.Contains("windows")) + if (IAppDirectoryInfo.IsLinux && testName.Contains("windows")) { throw new IgnoreException("windows specific test"); } @@ -123,7 +125,7 @@ public void TestBaseTearDown() protected void WindowsOnly() { - if (EnvironmentProvider.IsLinux) + if (OsInfo.IsLinux) { throw new IgnoreException("windows specific test"); } @@ -132,7 +134,7 @@ protected void WindowsOnly() protected void LinuxOnly() { - if (!EnvironmentProvider.IsLinux) + if (!OsInfo.IsLinux) { throw new IgnoreException("linux specific test"); } @@ -140,7 +142,7 @@ protected void LinuxOnly() protected void WithTempAsAppPath() { - Mocker.GetMock() + Mocker.GetMock() .SetupGet(c => c.WorkingDirectory) .Returns(VirtualPath); } diff --git a/NzbDrone.Update.Test/InstallUpdateServiceFixture.cs b/NzbDrone.Update.Test/InstallUpdateServiceFixture.cs index 1c73285dd..af5b9279d 100644 --- a/NzbDrone.Update.Test/InstallUpdateServiceFixture.cs +++ b/NzbDrone.Update.Test/InstallUpdateServiceFixture.cs @@ -3,6 +3,7 @@ using FluentAssertions; using NUnit.Framework; using NzbDrone.Common; +using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Test.Common; using NzbDrone.Update.UpdateEngine; @@ -14,7 +15,7 @@ public class InstallUpdateServiceFixture : TestBase [SetUp] public void Setup() { - Mocker.GetMock() + Mocker.GetMock() .Setup(c => c.SystemTemp).Returns(@"C:\Temp\"); } diff --git a/NzbDrone.Update.Test/UpdateProviderStartFixture.cs b/NzbDrone.Update.Test/UpdateProviderStartFixture.cs index 7add7ae1e..6e6456b59 100644 --- a/NzbDrone.Update.Test/UpdateProviderStartFixture.cs +++ b/NzbDrone.Update.Test/UpdateProviderStartFixture.cs @@ -19,16 +19,16 @@ public class UpdateProviderStartFixture : TestBase private const string BACKUP_FOLDER = @"C:\Temp\nzbdrone_update\nzbdrone_backup\"; private const string TARGET_FOLDER = @"C:\NzbDrone\"; - Mock _environmentProvider; + Mock _IAppDirectoryInfo; [SetUp] public void Setup() { - _environmentProvider = Mocker.GetMock(); + _IAppDirectoryInfo = Mocker.GetMock(); - _environmentProvider.SetupGet(c => c.SystemTemp).Returns(@"C:\Temp\"); + _IAppDirectoryInfo.SetupGet(c => c.SystemTemp).Returns(@"C:\Temp\"); Mocker.GetMock() .Setup(c => c.FolderExists(UPDATE_FOLDER)) diff --git a/NzbDrone.Update/Program.cs b/NzbDrone.Update/Program.cs index c9e29c9a9..1dcde521a 100644 --- a/NzbDrone.Update/Program.cs +++ b/NzbDrone.Update/Program.cs @@ -3,6 +3,7 @@ using NLog; using NzbDrone.Common; using NzbDrone.Common.Composition; +using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Common.Instrumentation; using NzbDrone.Update.UpdateEngine; @@ -29,10 +30,10 @@ public static void Main(string[] args) Console.WriteLine("Starting NzbDrone Update Client"); GlobalExceptionHandlers.Register(); - new LogglyTarget(new EnvironmentProvider()).Register(LogLevel.Debug); + new LogglyTarget(new AppDirectoryInfo()).Register(LogLevel.Debug); _container = UpdateContainerBuilder.Build(); - logger.Info("Updating NzbDrone to version {0}", _container.Resolve().Version); + logger.Info("Updating NzbDrone to version {0}", BuildInfo.Version); _container.Resolve().Start(args); } catch (Exception e) diff --git a/NzbDrone.Update/UpdateEngine/BackupAndRestore.cs b/NzbDrone.Update/UpdateEngine/BackupAndRestore.cs index 02f604751..fe4886049 100644 --- a/NzbDrone.Update/UpdateEngine/BackupAndRestore.cs +++ b/NzbDrone.Update/UpdateEngine/BackupAndRestore.cs @@ -1,5 +1,6 @@ using NLog; using NzbDrone.Common; +using NzbDrone.Common.EnvironmentInfo; namespace NzbDrone.Update.UpdateEngine { @@ -12,27 +13,27 @@ public interface IBackupAndRestore public class BackupAndRestore : IBackupAndRestore { private readonly IDiskProvider _diskProvider; - private readonly IEnvironmentProvider _environmentProvider; + private readonly IAppDirectoryInfo _appDirectoryInfo; private readonly Logger _logger; - public BackupAndRestore(IDiskProvider diskProvider, IEnvironmentProvider environmentProvider, Logger logger) + public BackupAndRestore(IDiskProvider diskProvider, IAppDirectoryInfo appDirectoryInfo, Logger logger) { _diskProvider = diskProvider; - _environmentProvider = environmentProvider; + _appDirectoryInfo = appDirectoryInfo; _logger = logger; } public void BackUp(string source) { _logger.Info("Creating backup of existing installation"); - _diskProvider.CopyDirectory(source, _environmentProvider.GetUpdateBackUpFolder()); + _diskProvider.CopyDirectory(source, _appDirectoryInfo.GetUpdateBackUpFolder()); } public void Restore(string target) { //TODO:this should ignore single file failures. _logger.Info("Attempting to rollback upgrade"); - _diskProvider.CopyDirectory(_environmentProvider.GetUpdateBackUpFolder(), target); + _diskProvider.CopyDirectory(_appDirectoryInfo.GetUpdateBackUpFolder(), target); } } } \ No newline at end of file diff --git a/NzbDrone.Update/UpdateEngine/InstallUpdateService.cs b/NzbDrone.Update/UpdateEngine/InstallUpdateService.cs index d1102a554..1e2d01abf 100644 --- a/NzbDrone.Update/UpdateEngine/InstallUpdateService.cs +++ b/NzbDrone.Update/UpdateEngine/InstallUpdateService.cs @@ -2,6 +2,7 @@ using System.IO; using NLog; using NzbDrone.Common; +using NzbDrone.Common.EnvironmentInfo; namespace NzbDrone.Update.UpdateEngine { @@ -15,18 +16,18 @@ public class InstallUpdateService : IInstallUpdateService private readonly IDiskProvider _diskProvider; private readonly IDetectApplicationType _detectApplicationType; private readonly ITerminateNzbDrone _terminateNzbDrone; - private readonly IEnvironmentProvider _environmentProvider; + private readonly IAppDirectoryInfo _appDirectoryInfo; private readonly IBackupAndRestore _backupAndRestore; private readonly IStartNzbDrone _startNzbDrone; private readonly Logger _logger; public InstallUpdateService(IDiskProvider diskProvider, IDetectApplicationType detectApplicationType, ITerminateNzbDrone terminateNzbDrone, - IProcessProvider processProvider, IEnvironmentProvider environmentProvider, IBackupAndRestore backupAndRestore, IStartNzbDrone startNzbDrone, Logger logger) + IAppDirectoryInfo appDirectoryInfo, IBackupAndRestore backupAndRestore, IStartNzbDrone startNzbDrone, Logger logger) { _diskProvider = diskProvider; _detectApplicationType = detectApplicationType; _terminateNzbDrone = terminateNzbDrone; - _environmentProvider = environmentProvider; + _appDirectoryInfo = appDirectoryInfo; _backupAndRestore = backupAndRestore; _startNzbDrone = startNzbDrone; _logger = logger; @@ -43,8 +44,8 @@ private void Verify(string targetFolder) throw new DirectoryNotFoundException("Target folder doesn't exist " + targetFolder); _logger.Info("Verifying Update Folder"); - if (!_diskProvider.FolderExists(_environmentProvider.GetUpdatePackageFolder())) - throw new DirectoryNotFoundException("Update folder doesn't exist " + _environmentProvider.GetUpdatePackageFolder()); + if (!_diskProvider.FolderExists(_appDirectoryInfo.GetUpdatePackageFolder())) + throw new DirectoryNotFoundException("Update folder doesn't exist " + _appDirectoryInfo.GetUpdatePackageFolder()); } public void Start(string installationFolder) @@ -63,7 +64,7 @@ public void Start(string installationFolder) try { - _diskProvider.CopyDirectory(_environmentProvider.GetUpdatePackageFolder(), installationFolder); + _diskProvider.CopyDirectory(_appDirectoryInfo.GetUpdatePackageFolder(), installationFolder); } catch (Exception e) { diff --git a/NzbDrone.sln.DotSettings b/NzbDrone.sln.DotSettings index d322e75d3..c66f6d0da 100644 --- a/NzbDrone.sln.DotSettings +++ b/NzbDrone.sln.DotSettings @@ -4,6 +4,7 @@ DO_NOT_SHOW DO_NOT_SHOW ERROR + ERROR ERROR HINT HINT diff --git a/NzbDrone/AppMain.cs b/NzbDrone/AppMain.cs index 8def08d22..b61335bfc 100644 --- a/NzbDrone/AppMain.cs +++ b/NzbDrone/AppMain.cs @@ -3,6 +3,7 @@ using System.Reflection; using NLog; using NzbDrone.Common; +using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Common.Instrumentation; namespace NzbDrone @@ -18,7 +19,7 @@ public static void Main(string[] args) { GlobalExceptionHandlers.Register(); - new LogglyTarget(new EnvironmentProvider()).Register(LogLevel.Warn); + new LogglyTarget(new AppDirectoryInfo()).Register(LogLevel.Warn); logger.Info("Starting NzbDrone Console. Version {0}", Assembly.GetExecutingAssembly().GetName().Version); diff --git a/NzbDrone/ApplicationServer.cs b/NzbDrone/ApplicationServer.cs index fdd34727d..cf1a2c4f3 100644 --- a/NzbDrone/ApplicationServer.cs +++ b/NzbDrone/ApplicationServer.cs @@ -2,6 +2,7 @@ using System.ServiceProcess; using NLog; using NzbDrone.Common; +using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Core.Configuration; using NzbDrone.Host; using NzbDrone.Owin; @@ -17,7 +18,7 @@ public interface INzbDroneServiceFactory public class NzbDroneServiceFactory : ServiceBase, INzbDroneServiceFactory { private readonly IConfigFileProvider _configFileProvider; - private readonly IEnvironmentProvider _environmentProvider; + private readonly IRuntimeInfo _runtimeInfo; private readonly IHostController _hostController; private readonly IProcessProvider _processProvider; private readonly PriorityMonitor _priorityMonitor; @@ -25,14 +26,13 @@ public class NzbDroneServiceFactory : ServiceBase, INzbDroneServiceFactory private readonly IUrlAclAdapter _urlAclAdapter; private readonly Logger _logger; - public NzbDroneServiceFactory(IConfigFileProvider configFileProvider, IHostController hostController, - IEnvironmentProvider environmentProvider, + public NzbDroneServiceFactory(IConfigFileProvider configFileProvider, IHostController hostController, IRuntimeInfo runtimeInfo, IProcessProvider processProvider, PriorityMonitor priorityMonitor, IFirewallAdapter firewallAdapter, IUrlAclAdapter urlAclAdapter, Logger logger) { _configFileProvider = configFileProvider; _hostController = hostController; - _environmentProvider = environmentProvider; + _runtimeInfo = runtimeInfo; _processProvider = processProvider; _priorityMonitor = priorityMonitor; _firewallAdapter = firewallAdapter; @@ -47,7 +47,7 @@ protected override void OnStart(string[] args) public void Start() { - if (_environmentProvider.IsAdmin) + if (_runtimeInfo.IsAdmin) { _urlAclAdapter.RefreshRegistration(); _firewallAdapter.MakeAccessible(); @@ -55,7 +55,7 @@ public void Start() } _hostController.StartServer(); - if (_environmentProvider.IsUserInteractive && _configFileProvider.LaunchBrowser) + if (_runtimeInfo.IsUserInteractive && _configFileProvider.LaunchBrowser) { try { diff --git a/NzbDrone/Host/UrlAclAdapter.cs b/NzbDrone/Host/UrlAclAdapter.cs index 101217355..eab0da32f 100644 --- a/NzbDrone/Host/UrlAclAdapter.cs +++ b/NzbDrone/Host/UrlAclAdapter.cs @@ -2,6 +2,7 @@ using System.Diagnostics; using NLog; using NzbDrone.Common; +using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Core.Configuration; namespace NzbDrone.Host @@ -15,20 +16,18 @@ public class UrlAclAdapter : IUrlAclAdapter { private readonly IProcessProvider _processProvider; private readonly IConfigFileProvider _configFileProvider; - private readonly IEnvironmentProvider _environmentProvider; private readonly Logger _logger; - public UrlAclAdapter(IProcessProvider processProvider, IConfigFileProvider configFileProvider, IEnvironmentProvider environmentProvider, Logger logger) + public UrlAclAdapter(IProcessProvider processProvider, IConfigFileProvider configFileProvider, Logger logger) { _processProvider = processProvider; _configFileProvider = configFileProvider; - _environmentProvider = environmentProvider; _logger = logger; } public void RefreshRegistration() { - if (_environmentProvider.GetOsVersion().Major < 6) + if (OsInfo.Version.Major < 6) return; RegisterUrl(_configFileProvider.Port); diff --git a/NzbDrone/MainAppContainerBuilder.cs b/NzbDrone/MainAppContainerBuilder.cs index 540ad56c6..82eec2093 100644 --- a/NzbDrone/MainAppContainerBuilder.cs +++ b/NzbDrone/MainAppContainerBuilder.cs @@ -5,6 +5,7 @@ using NzbDrone.Api.SignalR; using NzbDrone.Common; using NzbDrone.Common.Composition; +using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Common.Messaging; using NzbDrone.Core.Datastore; using NzbDrone.Core.Instrumentation; @@ -41,19 +42,19 @@ private void InitDatabase() Logger.Info("Registering Database..."); //TODO: move this to factory - var environmentProvider = new EnvironmentProvider(); - var appDataPath = environmentProvider.GetAppDataPath(); + var IAppDirectoryInfo = new AppDirectoryInfo(); + var appDataPath = IAppDirectoryInfo.GetAppDataPath(); if (!Directory.Exists(appDataPath)) { Directory.CreateDirectory(appDataPath); } - Container.Register(c => c.Resolve().Create(environmentProvider.GetNzbDroneDatabase())); + Container.Register(c => c.Resolve().Create(IAppDirectoryInfo.GetNzbDroneDatabase())); Container.Register(c => { - var db = c.Resolve().Create(environmentProvider.GetLogDatabase(), MigrationType.Log); + var db = c.Resolve().Create(IAppDirectoryInfo.GetLogDatabase(), MigrationType.Log); return new LogRepository(db, c.Resolve()); }); } diff --git a/NzbDrone/Router.cs b/NzbDrone/Router.cs index fe56b44dc..dd12c8b33 100644 --- a/NzbDrone/Router.cs +++ b/NzbDrone/Router.cs @@ -4,6 +4,7 @@ using NLog; using NzbDrone.Common; using NzbDrone.Common.Composition; +using NzbDrone.Common.EnvironmentInfo; using NzbDrone.SysTray; using IServiceProvider = NzbDrone.Common.IServiceProvider; @@ -14,17 +15,17 @@ public class Router private readonly INzbDroneServiceFactory _nzbDroneServiceFactory; private readonly IServiceProvider _serviceProvider; private readonly IConsoleService _consoleService; - private readonly IEnvironmentProvider _environmentProvider; + private readonly IRuntimeInfo _runtimeInfo; private readonly ISystemTrayApp _systemTrayProvider; private readonly Logger _logger; public Router(INzbDroneServiceFactory nzbDroneServiceFactory, IServiceProvider serviceProvider, - IConsoleService consoleService, IEnvironmentProvider environmentProvider, ISystemTrayApp systemTrayProvider, Logger logger) + IConsoleService consoleService, IRuntimeInfo runtimeInfo, ISystemTrayApp systemTrayProvider, Logger logger) { _nzbDroneServiceFactory = nzbDroneServiceFactory; _serviceProvider = serviceProvider; _consoleService = consoleService; - _environmentProvider = environmentProvider; + _runtimeInfo = runtimeInfo; _systemTrayProvider = systemTrayProvider; _logger = logger; } @@ -36,7 +37,7 @@ public void Route(IEnumerable args) public void Route(ApplicationModes applicationModes) { - if (!_environmentProvider.IsUserInteractive) + if (!_runtimeInfo.IsUserInteractive) { applicationModes = ApplicationModes.Service; } diff --git a/NzbDrone/SysTray/SysTrayApp.cs b/NzbDrone/SysTray/SysTrayApp.cs index 15ca53d2a..5cffc649c 100644 --- a/NzbDrone/SysTray/SysTrayApp.cs +++ b/NzbDrone/SysTray/SysTrayApp.cs @@ -3,6 +3,7 @@ using System.Reflection; using System.Windows.Forms; using NzbDrone.Common; +using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Owin; namespace NzbDrone.SysTray @@ -16,16 +17,14 @@ public class SystemTrayApp : Form, ISystemTrayApp { private readonly IProcessProvider _processProvider; private readonly IHostController _hostController; - private readonly IEnvironmentProvider _environmentProvider; private readonly NotifyIcon _trayIcon = new NotifyIcon(); private readonly ContextMenu _trayMenu = new ContextMenu(); - public SystemTrayApp(IProcessProvider processProvider, IHostController hostController, IEnvironmentProvider environmentProvider) + public SystemTrayApp(IProcessProvider processProvider, IHostController hostController) { _processProvider = processProvider; _hostController = hostController; - _environmentProvider = environmentProvider; } @@ -35,7 +34,7 @@ public void Start() _trayMenu.MenuItems.Add("-"); _trayMenu.MenuItems.Add("Exit", OnExit); - _trayIcon.Text = String.Format("NzbDrone - {0}", _environmentProvider.Version); + _trayIcon.Text = String.Format("NzbDrone - {0}", BuildInfo.Version); _trayIcon.Icon = new Icon(Assembly.GetEntryAssembly().GetManifestResourceStream("NzbDrone.NzbDrone.ico")); _trayIcon.ContextMenu = _trayMenu;