Skip to content

Commit

Permalink
add version to settings in front end app, rename 'ComputerDrivesComma…
Browse files Browse the repository at this point in the history
…nd' to 'GetComputerDrivesCommand'

fixes GH-34
  • Loading branch information
tmathura committed Aug 27, 2020
1 parent 1ce9813 commit eced6a8
Show file tree
Hide file tree
Showing 34 changed files with 433 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
namespace Spacearr.Common.Tests.Command.Commands
{
[TestClass]
public class ComputerDrivesCommandTests
public class GetComputerDrivesCommandTests
{
private Mock<IComputerDrives> _mockIComputerDrives;
private ComputerDrivesCommand _computerDrivesCommand;
private GetComputerDrivesCommand _getComputerDrivesCommand;

[TestInitialize]
public void SetUp()
Expand All @@ -29,10 +29,10 @@ public async Task Execute()

// Arrange
_mockIComputerDrives.Setup(x => x.GetComputerDrives()).Returns(ComputerDriveInfoFactory.CreateComputerDriveInfos(noOfComputerDrives));
_computerDrivesCommand = new ComputerDrivesCommand(_mockIComputerDrives.Object);
_getComputerDrivesCommand = new GetComputerDrivesCommand(_mockIComputerDrives.Object);

// Act
var commandData = await _computerDrivesCommand.Execute();
var commandData = await _getComputerDrivesCommand.Execute();
var computerDrives = JsonConvert.DeserializeObject<List<ComputerDriveModel>>(commandData);

// Assert
Expand All @@ -44,10 +44,10 @@ public async Task Execute_ZeroComputerDrives()
{
// Arrange
_mockIComputerDrives.Setup(x => x.GetComputerDrives()).Returns(ComputerDriveInfoFactory.CreateComputerDriveInfos(0));
_computerDrivesCommand = new ComputerDrivesCommand(_mockIComputerDrives.Object);
_getComputerDrivesCommand = new GetComputerDrivesCommand(_mockIComputerDrives.Object);

// Act
var commandData = await _computerDrivesCommand.Execute();
var commandData = await _getComputerDrivesCommand.Execute();
var computerDrives = JsonConvert.DeserializeObject<List<ComputerDriveModel>>(commandData);

// Assert
Expand Down
2 changes: 0 additions & 2 deletions Spacearr.Common.Tests/Factories/OctokitReleaseFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ public static List<Release> CreateOctokitReleases(int total)
{
new ReleaseAsset("url", 1, "1", $"apk_{i}", "label", "state", "contentType",
1, 1, DateTimeOffset.Now, DateTimeOffset.Now, $"browserDownloadUrl_apk_{i}", new Author()),
new ReleaseAsset("url", 1, "1", $"windowsservice_{i}", "label", "state", "contentType",
1, 1, DateTimeOffset.Now, DateTimeOffset.Now, $"browserDownloadUrl_windowsservice_{i}", new Author()),
new ReleaseAsset("url", 1, "1", $"workerservice_{i}", "label", "state", "contentType",
1, 1, DateTimeOffset.Now, DateTimeOffset.Now, $"browserDownloadUrl_workerservice_{i}", new Author()),
new ReleaseAsset("url", 1, "1", $"uwp_{i}", "label", "state", "contentType",
Expand Down
36 changes: 0 additions & 36 deletions Spacearr.Common.Tests/Services/UpdateServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,24 +79,6 @@ public async Task UpdateUrlOfLastUpdateCheck_Apk()
Assert.AreEqual($"browserDownloadUrl_apk_{noOfReleases-1}", url);
}

[TestMethod]
public async Task UpdateUrlOfLastUpdateCheck_WindowsService()
{
// Arrange
const int noOfReleases = 9;
var releases = OctokitRelease.CreateOctokitReleases(noOfReleases);
_mockIGitHubClient.Setup(x => x.Repository.Release.GetAll("tmathura", "Spacearr")).ReturnsAsync(releases);
_updateService = new UpdateService(_mockILogger.Object, _mockIGitHubClient.Object, _mockIFileService.Object);

// Act
var update = await _updateService.CheckForUpdateAsync(_currentVersion.ToString());
var url = await _updateService.UpdateUrlOfLastUpdateCheck(UpdateType.WindowsService);

// Assert
Assert.AreEqual(true, update);
Assert.AreEqual($"browserDownloadUrl_windowsservice_{noOfReleases-1}", url);
}

[TestMethod]
public async Task UpdateUrlOfLastUpdateCheck_WorkerService()
{
Expand Down Expand Up @@ -164,24 +146,6 @@ public async Task FileNameOfLastUpdateCheck_Apk()
Assert.AreEqual($"apk_{noOfReleases - 1}", fileName);
}

[TestMethod]
public async Task FileNameOfLastUpdateCheck_WindowsService()
{
// Arrange
const int noOfReleases = 3;
var releases = OctokitRelease.CreateOctokitReleases(noOfReleases);
_mockIGitHubClient.Setup(x => x.Repository.Release.GetAll("tmathura", "Spacearr")).ReturnsAsync(releases);
_updateService = new UpdateService(_mockILogger.Object, _mockIGitHubClient.Object, _mockIFileService.Object);

// Act
var update = await _updateService.CheckForUpdateAsync(_currentVersion.ToString());
var fileName = await _updateService.FileNameOfLastUpdateCheck(UpdateType.WindowsService);

// Assert
Assert.AreEqual(true, update);
Assert.AreEqual($"windowsservice_{noOfReleases - 1}", fileName);
}

[TestMethod]
public async Task FileNameOfLastUpdateCheck_WorkerService()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@

namespace Spacearr.Common.Command.Implementations.Commands
{
public class ComputerDrivesCommand : ICommand
public class GetComputerDrivesCommand : ICommand
{
private readonly IComputerDrives _computerDrives;

public ComputerDrivesCommand(IComputerDrives computerDrives)
public GetComputerDrivesCommand(IComputerDrives computerDrives)
{
_computerDrives = computerDrives;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Newtonsoft.Json;
using Spacearr.Common.Command.Interfaces;
using Spacearr.Common.Models;
using Spacearr.Common.Services.Interfaces;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;

namespace Spacearr.Common.Command.Implementations.Commands
{
public class GetWorkerServiceVersionCommand : ICommand
{
private const string AppName = "Spacearr.WorkerService.Windows";
private readonly IFileService _fileService;

public GetWorkerServiceVersionCommand(IFileService fileService)
{
_fileService = fileService;
}

/// <summary>
/// Returns the Worker Service version.
/// </summary>
/// <returns>Returns a WorkerServiceVersionModel</returns>
public async Task<string> Execute()
{
var parentDirectory = Directory.GetParent(_fileService.GetUpdateAppStorageFolderPath());
var currentAppPath = Path.GetFullPath(parentDirectory?.FullName ?? string.Empty);
var currentApp = Path.Combine(currentAppPath, $"{AppName}.dll");
var assemblyName = AssemblyName.GetAssemblyName(currentApp);
var workerServiceVersion = new WorkerServiceVersionModel { Version = assemblyName.Version};

return await Task.FromResult(JsonConvert.SerializeObject(workerServiceVersion));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@ namespace Spacearr.Common.Command.Implementations.Commands
{
public class UpdateCommand : ICommand
{
private readonly UpdateType _updateType;
private const string AppName = "Spacearr.WorkerService.Windows";
private readonly ILogger _logger;
private readonly IUpdateService _updateService;
private readonly IDownloadService _downloadService;
private readonly IFileService _fileService;

public UpdateCommand(UpdateType updateType, ILogger logger, IUpdateService updateService, IDownloadService downloadService, IFileService fileService)
public UpdateCommand(ILogger logger, IUpdateService updateService, IDownloadService downloadService, IFileService fileService)
{
_updateType = updateType;
_logger = logger;
_updateService = updateService;
_downloadService = downloadService;
Expand All @@ -34,16 +33,15 @@ public UpdateCommand(UpdateType updateType, ILogger logger, IUpdateService updat
/// <returns>string.Empty</returns>
public async Task<string> Execute()
{
var appName = _updateType == UpdateType.WorkerService ? "Spacearr.WorkerService.Windows" : "Spacearr.Windows.Windows";
var parentDirectory = Directory.GetParent(_fileService.GetUpdateAppStorageFolderPath()).Parent;
var currentAppPath = Path.GetFullPath(parentDirectory?.FullName ?? string.Empty);
var currentApp = Path.Combine(currentAppPath, $"{appName}.dll");
var currentApp = Path.Combine(currentAppPath, $"{AppName}.dll");

var assemblyName = AssemblyName.GetAssemblyName(currentApp);

if (await _updateService.CheckForUpdateAsync(assemblyName.Version.ToString()))
{
var url = await _updateService.UpdateUrlOfLastUpdateCheck(_updateType);
var url = await _updateService.UpdateUrlOfLastUpdateCheck(UpdateType.WorkerService);
var progressIndicator = new Progress<double>();
var cts = new CancellationTokenSource();
try
Expand All @@ -52,15 +50,15 @@ public async Task<string> Execute()
Directory.CreateDirectory(_fileService.GetUpdateAppStorageFolderPath());
await _downloadService.DownloadFileAsync(url, progressIndicator, cts.Token);

var controller = new ServiceController(appName);
var controller = new ServiceController(AppName);

if (controller.Status != ServiceControllerStatus.StopPending && controller.Status != ServiceControllerStatus.Stopped)
{
controller.Stop();
controller.WaitForStatus(ServiceControllerStatus.Stopped);
}

await _updateService.UpdateApp(_updateType);
await _updateService.UpdateApp(UpdateType.WorkerService);

controller.Start();
controller.WaitForStatus(ServiceControllerStatus.Running);
Expand Down
5 changes: 3 additions & 2 deletions Spacearr.Common/Enums/CommandType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
/// </summary>
public enum CommandType
{
ComputerDrivesCommand,
GetComputerDrivesCommand,
ComputerDrivesLowCommand,
SaveFirebasePushNotificationTokenCommand
SaveFirebasePushNotificationTokenCommand,
GetWorkerServiceVersionCommand
}
}
1 change: 0 additions & 1 deletion Spacearr.Common/Enums/DeviceType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
public enum UpdateType
{
Android,
WindowsService,
WorkerService,
Uwp
}
Expand Down
5 changes: 4 additions & 1 deletion Spacearr.Common/Models/SettingModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public class SettingModel

[Column("UpdatedDate")]
public DateTime UpdatedDate { get; set; }
}

[Ignore]
public string Version { get; set; }
}
}

9 changes: 9 additions & 0 deletions Spacearr.Common/Models/WorkerServiceVersionModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace Spacearr.Common.Models
{
public class WorkerServiceVersionModel
{
public Version Version { get; set; }
}
}
8 changes: 1 addition & 7 deletions Spacearr.Common/Services/Implementations/UpdateService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,6 @@ public async Task<string> UpdateUrlOfLastUpdateCheck(UpdateType updateType)
case UpdateType.Android:
url = _latestRelease.Assets.FirstOrDefault(x => x.Name.ToLower().Contains("apk"))?.BrowserDownloadUrl;
break;
case UpdateType.WindowsService:
url = _latestRelease.Assets.FirstOrDefault(x => x.Name.ToLower().Contains("windowsservice"))?.BrowserDownloadUrl;
break;
case UpdateType.WorkerService:
url = _latestRelease.Assets.FirstOrDefault(x => x.Name.ToLower().Contains("workerservice"))?.BrowserDownloadUrl;
break;
Expand Down Expand Up @@ -123,9 +120,6 @@ public async Task<string> FileNameOfLastUpdateCheck(UpdateType updateType)
case UpdateType.Android:
fileName = _latestRelease.Assets.FirstOrDefault(x => x.Name.ToLower().Contains("apk"))?.Name;
break;
case UpdateType.WindowsService:
fileName = _latestRelease.Assets.FirstOrDefault(x => x.Name.ToLower().Contains("windowsservice"))?.Name;
break;
case UpdateType.WorkerService:
fileName = _latestRelease.Assets.FirstOrDefault(x => x.Name.ToLower().Contains("workerservice"))?.Name;
break;
Expand Down Expand Up @@ -164,7 +158,7 @@ public async Task UpdateApp(UpdateType updateType)
/// <returns></returns>
private async Task UpdateFiles(UpdateType updateType)
{
var appName = updateType == UpdateType.WorkerService ? "Spacearr.WorkerService.Windows" : "Spacearr.Windows.Windows";
var appName = "Spacearr.WorkerService.Windows";
var updateFilesPath = Path.Combine(_fileService.GetUpdateAppStorageFolderPath(), $@"Spacearr\{appName}");
var parentDirectory = Directory.GetParent(_fileService.GetUpdateAppStorageFolderPath()).Parent;
var currentAppPath = Path.GetFullPath(parentDirectory?.FullName ?? string.Empty);
Expand Down
7 changes: 2 additions & 5 deletions Spacearr.Common/Timers/Implementations/UpdateAppTimer.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using Microsoft.Extensions.Configuration;
using Spacearr.Common.Command.Implementations.Commands;
using Spacearr.Common.Command.Interfaces;
using Spacearr.Common.Enums;
using Spacearr.Common.Logger.Interfaces;
using Spacearr.Common.Services.Interfaces;
using Spacearr.Common.Timers.Interfaces;
Expand All @@ -13,7 +12,6 @@ namespace Spacearr.Common.Timers.Implementations
{
public class UpdateAppTimer : IUpdateAppTimer
{
private UpdateType _updateType;
private readonly IConfiguration _configuration;
private readonly IInvoker _invoker;
private readonly ILogger _logger;
Expand Down Expand Up @@ -43,9 +41,8 @@ public UpdateAppTimer(IConfiguration configuration, IInvoker invoker, ILogger lo
/// <summary>
/// Start the timer.
/// </summary>
public void Instantiate(UpdateType updateType)
public void Instantiate()
{
_updateType = updateType;
if (Convert.ToBoolean(_configuration.GetSection("AutoUpdateApp").Value))
{
_timer.Start();
Expand Down Expand Up @@ -74,7 +71,7 @@ private async void ElapsedEventHandler(object sender, ElapsedEventArgs e)
{
if (!_downloadService.IsDownloading)
{
var command = new UpdateCommand(_updateType, _logger, _updateService, _downloadService, _fileService);
var command = new UpdateCommand(_logger, _updateService, _downloadService, _fileService);
await _invoker.Invoke(command);
}
}
Expand Down
6 changes: 2 additions & 4 deletions Spacearr.Common/Timers/Interfaces/IUpdateAppTimer.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
using Spacearr.Common.Enums;

namespace Spacearr.Common.Timers.Interfaces
namespace Spacearr.Common.Timers.Interfaces
{
public interface IUpdateAppTimer
{
/// <summary>
/// Start the timer.
/// </summary>
void Instantiate(UpdateType updateType);
void Instantiate();

/// <summary>
/// Stop the timer.
Expand Down
Loading

0 comments on commit eced6a8

Please sign in to comment.