Skip to content

Commit

Permalink
Small DesktopApplier fix and VersionHelper
Browse files Browse the repository at this point in the history
Also added CanApplyUpdates tests and moving around some classes into their own files
  • Loading branch information
Azyyyyyy committed Feb 10, 2024
1 parent 87cff79 commit 951061c
Show file tree
Hide file tree
Showing 22 changed files with 172 additions and 56 deletions.
7 changes: 6 additions & 1 deletion examples/ExampleApplication/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,9 @@

using TinyUpdate.Core;

Console.WriteLine("Hello, World! In Test Runner?: " + Testing.InTestRunner);
var versionDetails = VersionHelper.GetVersionDetails();

Console.WriteLine("Hello, Updates!");
Console.WriteLine("In Test Runner?: " + Testing.InTestRunner);
Console.WriteLine("Version: " + versionDetails.Version.ToString());
Console.WriteLine("SourceRevisionId: " + versionDetails.SourceRevisionId.ToString());
20 changes: 14 additions & 6 deletions src/Appliers/TinyUpdate.Desktop/DesktopApplier.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics;
using System.Collections.Immutable;
using System.Diagnostics;
using System.IO.Abstractions;
using Microsoft.Extensions.Logging;
using TinyUpdate.Core.Abstract.Delta;
Expand Down Expand Up @@ -28,17 +29,24 @@ public DesktopApplier(ILogger<DesktopApplier> logger, IHasher hasher, INative? n
public async Task<bool> ApplyUpdates(ICollection<IUpdatePackage> updatePackages, string applicationLocation,
IProgress<double>? progress = null)
{
var previousVersion = updatePackages.OrderBy(x => x.ReleaseEntry.PreviousVersion).First().ReleaseEntry.PreviousVersion;
var newVersion = updatePackages.OrderBy(x => x.ReleaseEntry.PreviousVersion).Last().ReleaseEntry.PreviousVersion;
if (updatePackages.Count == 0)
{
//TODO: Add logging
return false;
}

var orderedUpdatePackages = updatePackages.OrderBy(x => x.ReleaseEntry.PreviousVersion).ToImmutableArray();

var previousVersionLocation = Path.Combine(applicationLocation, previousVersion.ToString());
var newVersionLocation = Path.Combine(applicationLocation, newVersion.ToString());
var multiProgress = progress != null
? new MultiProgress(progress, updatePackages.Count)
: null;

foreach (var updatePackage in updatePackages)
foreach (var updatePackage in orderedUpdatePackages)
{
var releaseEntry = updatePackage.ReleaseEntry;
var previousVersionLocation = Path.Combine(applicationLocation, releaseEntry.PreviousVersion.ToString());

Check warning on line 47 in src/Appliers/TinyUpdate.Desktop/DesktopApplier.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Dereference of a possibly null reference.

Check warning on line 47 in src/Appliers/TinyUpdate.Desktop/DesktopApplier.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Dereference of a possibly null reference.

Check warning on line 47 in src/Appliers/TinyUpdate.Desktop/DesktopApplier.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Dereference of a possibly null reference.

Check warning on line 47 in src/Appliers/TinyUpdate.Desktop/DesktopApplier.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest)

Dereference of a possibly null reference.
var newVersionLocation = Path.Combine(applicationLocation, releaseEntry.NewVersion.ToString());

var successful =
await ApplyUpdate(updatePackage, previousVersionLocation, newVersionLocation, multiProgress);
if (!successful)
Expand Down
1 change: 1 addition & 0 deletions src/Deltas/TinyUpdate.Delta.MSDelta/Struct/DeltaHash.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Runtime.InteropServices;
using TinyUpdate.Core;
using TinyUpdate.Core.Services;

namespace TinyUpdate.Delta.MSDelta.Struct;

Expand Down
1 change: 1 addition & 0 deletions src/TinyUpdate.Core/Abstract/ReleaseEntry.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Diagnostics.CodeAnalysis;
using System.Text.Json.Serialization;
using SemVersion;
using TinyUpdate.Core.Converters;

namespace TinyUpdate.Core.Abstract;

Expand Down
15 changes: 1 addition & 14 deletions src/TinyUpdate.Core/Abstract/UpdatePackage/IUpdatePackage.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,7 @@
using System.Diagnostics.CodeAnalysis;
using TinyUpdate.Core.Model;
using TinyUpdate.Core.Model;

namespace TinyUpdate.Core.Abstract.UpdatePackage;

public record LoadResult
{
public static LoadResult Failed(string message) => new() { Successful = false, Message = message };

public static readonly LoadResult Success = new() { Successful = true };

[MemberNotNullWhen(false, nameof(Message))]
public bool Successful { get; protected init; }

public string? Message { get; protected init; }
}

/// <summary>
/// Provides base functionality for handling update packages
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace TinyUpdate.Core.Abstract.UpdatePackage;
using TinyUpdate.Core.Model;

namespace TinyUpdate.Core.Abstract.UpdatePackage;

public interface IUpdatePackageFactory
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Text.Json.Serialization;
using SemVersion;

namespace TinyUpdate.Core;
namespace TinyUpdate.Core.Converters;

public class SemanticVersionConverter : JsonConverter<SemanticVersion>
{
Expand Down
8 changes: 4 additions & 4 deletions src/TinyUpdate.Core/Model/DeltaCreationResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace TinyUpdate.Core.Model;
/// <summary>
/// Details of creating a new delta update
/// </summary>
public record DeltaCreationResult(IDeltaCreation? Creator, Stream? DeltaStream, bool Successful)
public class DeltaCreationResult(IDeltaCreation? creator, Stream? deltaStream, bool successful)
{
/// <summary>
/// Failed to create a delta update
Expand All @@ -16,16 +16,16 @@ public record DeltaCreationResult(IDeltaCreation? Creator, Stream? DeltaStream,
/// <summary>
/// The <see cref="IDeltaCreation"/> that created the update
/// </summary>
public IDeltaCreation? Creator { get; } = Creator;
public IDeltaCreation? Creator { get; } = creator;

/// <summary>
/// The contents of the delta update
/// </summary>
public Stream? DeltaStream { get; } = DeltaStream;
public Stream? DeltaStream { get; } = deltaStream;

/// <summary>
/// If we was successful in creating a delta update
/// </summary>
[MemberNotNullWhen(true, nameof(Creator), nameof(DeltaStream))]
public bool Successful { get; } = Successful;
public bool Successful { get; } = successful;
}
15 changes: 15 additions & 0 deletions src/TinyUpdate.Core/Model/LoadResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Diagnostics.CodeAnalysis;

namespace TinyUpdate.Core.Abstract.UpdatePackage;

public record LoadResult
{
public static LoadResult Failed(string message) => new() { Successful = false, Message = message };

public static readonly LoadResult Success = new() { Successful = true };

[MemberNotNullWhen(false, nameof(Message))]
public bool Successful { get; protected init; }

public string? Message { get; protected init; }
}
26 changes: 26 additions & 0 deletions src/TinyUpdate.Core/Model/LoadUpdatePackageResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Diagnostics.CodeAnalysis;
using TinyUpdate.Core.Abstract.UpdatePackage;

namespace TinyUpdate.Core.Model;

public record LoadUpdatePackageResult
{
public LoadUpdatePackageResult(bool successful, string? message, IUpdatePackage? updatePackage)
{
Successful = successful;
Message = message;
UpdatePackage = updatePackage;
}

public static LoadUpdatePackageResult Failed(string message) => new(false, message, null);

public static LoadUpdatePackageResult Success(IUpdatePackage updatePackage) => new(true, null, updatePackage);

[MemberNotNullWhen(true, nameof(UpdatePackage))]
[MemberNotNullWhen(false, nameof(Message))]
public bool Successful { get; }

public string? Message { get; }

public IUpdatePackage? UpdatePackage { get; }
}
12 changes: 12 additions & 0 deletions src/TinyUpdate.Core/Model/VersionDetails.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace TinyUpdate.Core.Model;

public ref struct VersionDetails
{
public VersionDetails(ReadOnlySpan<char> version)
{
Version = version;
}

public ReadOnlySpan<char> Version { get; internal set; } = ReadOnlySpan<char>.Empty;
public ReadOnlySpan<char> SourceRevisionId { get; internal set; } = ReadOnlySpan<char>.Empty;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using TinyUpdate.Core.Abstract.Delta;
using TinyUpdate.Core.Model;

namespace TinyUpdate.Core;
namespace TinyUpdate.Core.Services;

/// <summary>
/// Default <see cref="IDeltaManager"/> implementation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Text.RegularExpressions;
using TinyUpdate.Core.Abstract;

namespace TinyUpdate.Core;
namespace TinyUpdate.Core.Services;

/// <summary>
/// Easy access to processing Streams into a SHA256 hash and comparing SHA256 hashes
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System.Diagnostics.CodeAnalysis;
using TinyUpdate.Core.Abstract;
using TinyUpdate.Core.Abstract;
using TinyUpdate.Core.Abstract.UpdatePackage;
using TinyUpdate.Core.Model;

namespace TinyUpdate.Core;
namespace TinyUpdate.Core.Services;

public class UpdatePackageFactory : IUpdatePackageFactory
{
Expand Down Expand Up @@ -39,26 +39,4 @@ public async Task<LoadUpdatePackageResult> CreateUpdatePackage(Stream? stream, s
? LoadUpdatePackageResult.Success(updatePackage)
: LoadUpdatePackageResult.Failed(loadResult.Message);
}
}

public record LoadUpdatePackageResult
{
public LoadUpdatePackageResult(bool successful, string? message, IUpdatePackage? updatePackage)
{
Successful = successful;
Message = message;
UpdatePackage = updatePackage;
}

public static LoadUpdatePackageResult Failed(string message) => new(false, message, null);

public static LoadUpdatePackageResult Success(IUpdatePackage updatePackage) => new(true, null, updatePackage);

[MemberNotNullWhen(true, nameof(UpdatePackage))]
[MemberNotNullWhen(false, nameof(Message))]
public bool Successful { get; }

public string? Message { get; }

public IUpdatePackage? UpdatePackage { get; }
}
47 changes: 47 additions & 0 deletions src/TinyUpdate.Core/VersionHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System.Reflection;
using SemVersion;
using TinyUpdate.Core.Model;

namespace TinyUpdate.Core;

public static class VersionHelper
{
public static SemanticVersion GetSemanticVersion(this ReadOnlySpan<char> version) => SemanticVersion.Parse(GetVersionDetails(version).Version.ToString());
public static ReadOnlySpan<char> GetVersion(this ReadOnlySpan<char> version) => GetVersionDetails(version).Version;
public static ReadOnlySpan<char> GetSourceRevisionId(this ReadOnlySpan<char> version) => GetVersionDetails(version).SourceRevisionId;

public static VersionDetails GetVersionDetails() => GetVersionDetails(Assembly.GetEntryAssembly() ?? Assembly.GetCallingAssembly());
public static VersionDetails GetVersionDetails(this Assembly assembly)
{
var versionAttribute = assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>();

var version = versionAttribute?.InformationalVersion;
return string.IsNullOrWhiteSpace(version) ? new VersionDetails() : GetVersionDetails(version);
}

public static VersionDetails GetVersionDetails(this ReadOnlySpan<char> version)
{
var versionDetails = new VersionDetails(version);

var plusIndex = version.IndexOf('+');
if (plusIndex != -1)
{
var tmpSpan = version[++plusIndex..];
var dotIndex = tmpSpan.IndexOf('.');
var hasDot = dotIndex != -1;

if (hasDot)
{
versionDetails.SourceRevisionId = tmpSpan[(dotIndex + 1)..];
versionDetails.Version = version[..^(versionDetails.SourceRevisionId.Length + 1)];
}
else if (tmpSpan.Length == 40)
{
versionDetails.SourceRevisionId = tmpSpan;
versionDetails.Version = version[..^41];
}
}

return versionDetails;
}
}
28 changes: 28 additions & 0 deletions tests/TinyUpdate.Appliers.Tests/DesktopTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using TinyUpdate.Core.Abstract.Delta;
using TinyUpdate.Core.Abstract.UpdatePackage;
using TinyUpdate.Core.Model;
using TinyUpdate.Core.Services;
using TinyUpdate.Core.Tests;
using TinyUpdate.Desktop;
using TinyUpdate.Desktop.Abstract;
Expand All @@ -31,6 +32,33 @@ public void Setup()
_updateApplier = new DesktopApplier(NUnitLogger<DesktopApplier>.Instance, _hasher, _mockNative, _deltaManager, _fileSystem);
}

[Test]
public async Task CanApplyUpdates() {
var applicationPath = Path.Combine("Assets", nameof(DesktopApplier));
var appV0Path = Path.Combine(applicationPath, "1.0.0");
var appV1Path = Path.Combine(applicationPath, "1.0.1");
var appV2Path = Path.Combine(applicationPath, "1.0.2");

var appV1 = new MockUpdatePackage(applicationPath, _fileSystem)
{
ReleaseEntry = new MockReleaseEntry("1.0.0", "1.0.1", true),
NewFiles = [DesktopApplierTestSource.MakeFileEntry("testing.exe", appV0Path)]
};
var appV2 = new MockUpdatePackage(applicationPath, _fileSystem)
{
ReleaseEntry = new MockReleaseEntry("1.0.1", "1.0.2", true),
NewFiles = [DesktopApplierTestSource.MakeFileEntry(Path.Combine("testsub", "testApplication.exe"), appV1Path)]
};
var appV3 = new MockUpdatePackage(applicationPath, _fileSystem)
{
ReleaseEntry = new MockReleaseEntry("1.0.2", "1.0.3", true),
NewFiles = [DesktopApplierTestSource.MakeFileEntry(Path.Combine("newsub", "testApplication.exe"), appV2Path)]
};

var result = await _updateApplier.ApplyUpdates([appV1, appV3, appV2], applicationPath);
Assert.That(result, Is.True);
}

[Test]
[TestCaseSource(typeof(DesktopApplierTestSource), nameof(DesktopApplierTestSource.TestS))]
public async Task CanApplyUpdate(ApplierTestData applierTestData)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using TinyUpdate.Core;
using TinyUpdate.Core.Abstract;
using TinyUpdate.Core.Model;
using TinyUpdate.Core.Services;
using TinyUpdate.Core.Tests;
using TinyUpdate.Desktop;

Expand Down Expand Up @@ -91,7 +92,7 @@ public static IEnumerable<ApplierTestData> TestS()
yield return new ApplierTestData("MovedFileSubDirToRoot", movedSubDirToRootUpdatePackage, applicationPath);
}

private static FileEntry MakeFileEntry(string location, string sourceVersionPath, bool createInitialFile = true, bool unchanged = false, string? lastLocation = null)
internal static FileEntry MakeFileEntry(string location, string sourceVersionPath, bool createInitialFile = true, bool unchanged = false, string? lastLocation = null)
{
var dir = Path.GetDirectoryName(location) ?? "";
var targetFileStream = new MemoryStream();
Expand Down
1 change: 1 addition & 0 deletions tests/TinyUpdate.Core.Tests/DeltaMocker.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Moq;
using TinyUpdate.Core.Abstract.Delta;
using TinyUpdate.Core.Services;

namespace TinyUpdate.Core.Tests;

Expand Down
3 changes: 2 additions & 1 deletion tests/TinyUpdate.Core.Tests/HasherTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using TinyUpdate.Core.Tests.Abstract;
using TinyUpdate.Core.Services;
using TinyUpdate.Core.Tests.Abstract;
using TinyUpdate.Core.Tests.Attributes;
using TinyUpdate.Core.Tests.TestSources;

Expand Down
1 change: 1 addition & 0 deletions tests/TinyUpdate.Delta.Tests/Abstract/DeltaCan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using TinyUpdate.Core;
using TinyUpdate.Core.Abstract;
using TinyUpdate.Core.Abstract.Delta;
using TinyUpdate.Core.Services;
using TinyUpdate.Core.Tests;
using TinyUpdate.Core.Tests.Attributes;

Expand Down
1 change: 1 addition & 0 deletions tests/TinyUpdate.Delta.Tests/DeltaManagerTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using TinyUpdate.Core;
using TinyUpdate.Core.Abstract.Delta;
using TinyUpdate.Core.Services;
using TinyUpdate.Core.Tests;
using TinyUpdate.Delta.Tests.Abstract;

Expand Down
1 change: 1 addition & 0 deletions tests/TinyUpdate.Packages.Tests/UpdatePackageTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.IO.Compression;
using TinyUpdate.Core;
using TinyUpdate.Core.Services;
using TinyUpdate.Core.Tests;
using TinyUpdate.Core.Tests.Attributes;
using TinyUpdate.Packages.Tests.Abstract;
Expand Down

0 comments on commit 951061c

Please sign in to comment.