Skip to content

Commit

Permalink
Add explicit support for trimming and native AOT compilation and prop…
Browse files Browse the repository at this point in the history
…er supported platform annotations (#61)
  • Loading branch information
Tyrrrz authored May 23, 2024
1 parent f07dce6 commit 8d52049
Show file tree
Hide file tree
Showing 21 changed files with 194 additions and 291 deletions.
1 change: 1 addition & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ jobs:
main:
uses: Tyrrrz/.github/.github/workflows/nuget.yml@master
with:
windows-only: true
deploy: ${{ inputs.deploy || github.ref_type == 'tag' }}
package-version: ${{ inputs.package-version || (github.ref_type == 'tag' && github.ref_name) || format('0.0.0-ci-{0}', github.sha) }}
dotnet-version: 8.0.x
Expand Down
4 changes: 2 additions & 2 deletions Onova.Tests.Dummy/Onova.Tests.Dummy.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net8.0-windows</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CSharpier.MsBuild" Version="0.26.5" PrivateAssets="all" />
<PackageReference Include="CSharpier.MsBuild" Version="0.28.2" PrivateAssets="all" />
</ItemGroup>

<ItemGroup>
Expand Down
22 changes: 11 additions & 11 deletions Onova.Tests/Onova.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net8.0-windows</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0" PrivateAssets="all" />
<PackageReference Include="CliWrap" Version="3.6.4" />
<PackageReference Include="CSharpier.MsBuild" Version="0.26.5" PrivateAssets="all" />
<Content Include="xunit.runner.json" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.2" PrivateAssets="all" />
<PackageReference Include="CliWrap" Version="3.6.6" />
<PackageReference Include="CSharpier.MsBuild" Version="0.28.2" PrivateAssets="all" />
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="GitHubActionsTestLogger" Version="2.3.3" PrivateAssets="all" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="Mono.Cecil" Version="0.11.5" />
<PackageReference Include="xunit" Version="2.6.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.5" PrivateAssets="all" />
<PackageReference Include="xunit" Version="2.8.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.0" PrivateAssets="all" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Onova\Onova.csproj" />
<ProjectReference Include="..\Onova.Tests.Dummy\Onova.Tests.Dummy.csproj" />
</ItemGroup>

<ItemGroup>
<Content Include="xunit.runner.json" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>

</Project>
31 changes: 26 additions & 5 deletions Onova.Tests/UpdateSpecs.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using FluentAssertions;
Expand Down Expand Up @@ -45,7 +46,11 @@ public void Dispose()
public async Task I_can_check_for_updates_and_get_a_higher_version_if_it_is_available()
{
// Arrange
var updatee = new AssemblyMetadata("TestUpdatee", Version.Parse("1.0"), "");
var updatee = new AssemblyMetadata(
"TestUpdatee",
Version.Parse("1.0"),
Assembly.GetExecutingAssembly().Location
);

// Cleanup storage directory (TODO: move this to API)
DirectoryEx.DeleteIfExists(
Expand Down Expand Up @@ -82,7 +87,11 @@ public async Task I_can_check_for_updates_and_get_a_higher_version_if_it_is_avai
public async Task I_can_check_for_updates_and_get_nothing_if_there_is_no_higher_version_available()
{
// Arrange
var updatee = new AssemblyMetadata("TestUpdatee", Version.Parse("3.0"), "");
var updatee = new AssemblyMetadata(
"TestUpdatee",
Version.Parse("3.0"),
Assembly.GetExecutingAssembly().Location
);

// Cleanup storage directory (TODO: move this to API)
DirectoryEx.DeleteIfExists(
Expand Down Expand Up @@ -119,7 +128,11 @@ public async Task I_can_check_for_updates_and_get_nothing_if_there_is_no_higher_
public async Task I_can_check_for_updates_and_get_nothing_if_the_package_source_contains_no_packages()
{
// Arrange
var updatee = new AssemblyMetadata("TestUpdatee", Version.Parse("1.0"), "");
var updatee = new AssemblyMetadata(
"TestUpdatee",
Version.Parse("1.0"),
Assembly.GetExecutingAssembly().Location
);

// Cleanup storage directory (TODO: move this to API)
DirectoryEx.DeleteIfExists(
Expand Down Expand Up @@ -151,7 +164,11 @@ public async Task I_can_check_for_updates_and_get_nothing_if_the_package_source_
public async Task I_can_prepare_an_update_so_that_it_can_be_installed()
{
// Arrange
var updatee = new AssemblyMetadata("TestUpdatee", Version.Parse("1.0"), "");
var updatee = new AssemblyMetadata(
"TestUpdatee",
Version.Parse("1.0"),
Assembly.GetExecutingAssembly().Location
);

// Cleanup storage directory (TODO: move this to API)
DirectoryEx.DeleteIfExists(
Expand Down Expand Up @@ -188,7 +205,11 @@ public async Task I_can_prepare_an_update_so_that_it_can_be_installed()
public async Task I_can_get_a_list_of_updates_which_are_already_prepared_to_install()
{
// Arrange
var updatee = new AssemblyMetadata("TestUpdatee", Version.Parse("1.0"), "");
var updatee = new AssemblyMetadata(
"TestUpdatee",
Version.Parse("1.0"),
Assembly.GetExecutingAssembly().Location
);

// Cleanup storage directory (TODO: move this to API)
DirectoryEx.DeleteIfExists(
Expand Down
26 changes: 8 additions & 18 deletions Onova.Tests/Utils/DummyEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,17 @@

namespace Onova.Tests.Utils;

internal class DummyEnvironment : IDisposable
internal class DummyEnvironment(string rootDirPath) : IDisposable
{
private static readonly Assembly DummyAssembly = typeof(Dummy.Program).Assembly;
private static readonly string DummyAssemblyFileName = Path.GetFileName(DummyAssembly.Location);
private static readonly string DummyAssemblyDirPath = Path.GetDirectoryName(
DummyAssembly.Location
)!;

private readonly string _rootDirPath;
private string DummyFilePath { get; } = Path.Combine(rootDirPath, DummyAssemblyFileName);

private string DummyFilePath { get; }

private string DummyPackagesDirPath { get; }

public DummyEnvironment(string rootDirPath)
{
_rootDirPath = rootDirPath;

DummyFilePath = Path.Combine(_rootDirPath, DummyAssemblyFileName);
DummyPackagesDirPath = Path.Combine(_rootDirPath, "Packages");
}
private string DummyPackagesDirPath { get; } = Path.Combine(rootDirPath, "Packages");

private void SetAssemblyVersion(string filePath, Version version)
{
Expand All @@ -45,13 +35,13 @@ private void SetAssemblyVersion(string filePath, Version version)
private void CreateBase(Version version)
{
// Create dummy directory
Directory.CreateDirectory(_rootDirPath);
Directory.CreateDirectory(rootDirPath);

// Copy files
foreach (var filePath in Directory.EnumerateFiles(DummyAssemblyDirPath))
{
var fileName = Path.GetFileName(filePath);
File.Copy(filePath, Path.Combine(_rootDirPath, fileName));
File.Copy(filePath, Path.Combine(rootDirPath, fileName));
}

// Change base dummy version
Expand All @@ -64,7 +54,7 @@ private void CreatePackage(Version version)
Directory.CreateDirectory(DummyPackagesDirPath);

// Temporarily copy the dummy
var dummyTempFilePath = Path.Combine(_rootDirPath, $"{DummyAssemblyFileName}.{version}");
var dummyTempFilePath = Path.Combine(rootDirPath, $"{DummyAssemblyFileName}.{version}");
File.Copy(DummyFilePath, dummyTempFilePath);

// Change dummy version
Expand All @@ -91,7 +81,7 @@ private void Cleanup()
{
try
{
DirectoryEx.DeleteIfExists(_rootDirPath);
DirectoryEx.DeleteIfExists(rootDirPath);
break;
}
catch (UnauthorizedAccessException) when (retriesRemaining > 0)
Expand All @@ -113,7 +103,7 @@ public void Setup(Version baseVersion, IReadOnlyList<Version> availableVersions)

public string[] GetLastRunArguments(Version version)
{
var filePath = Path.Combine(_rootDirPath, $"lastrun-{version}.txt");
var filePath = Path.Combine(rootDirPath, $"lastrun-{version}.txt");
return File.Exists(filePath) ? File.ReadAllLines(filePath) : Array.Empty<string>();
}

Expand Down
4 changes: 2 additions & 2 deletions Onova.Updater/Onova.Updater.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CSharpier.MsBuild" Version="0.26.5" PrivateAssets="all" />
<PackageReference Include="PolyShim" Version="1.8.0" PrivateAssets="all" />
<PackageReference Include="CSharpier.MsBuild" Version="0.28.2" PrivateAssets="all" />
<PackageReference Include="PolyShim" Version="1.12.0" PrivateAssets="all" />
</ItemGroup>

</Project>
72 changes: 29 additions & 43 deletions Onova.Updater/Updater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,23 @@

namespace Onova.Updater;

public class Updater : IDisposable
public class Updater(
string updateeFilePath,
string packageContentDirPath,
bool restartUpdatee,
string routedArgs
) : IDisposable
{
private readonly string _updateeFilePath;
private readonly string _packageContentDirPath;
private readonly bool _restartUpdatee;
private readonly string _routedArgs;

private readonly TextWriter _log = File.CreateText(
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Log.txt")
);

public Updater(
string updateeFilePath,
string packageContentDirPath,
bool restartUpdatee,
string routedArgs
)
{
_updateeFilePath = updateeFilePath;
_packageContentDirPath = packageContentDirPath;
_restartUpdatee = restartUpdatee;
_routedArgs = routedArgs;
}

private void WriteLog(string content)
{
var date = DateTimeOffset
.Now
.ToString("dd-MMM-yyyy HH:mm:ss.fff", CultureInfo.InvariantCulture);
var date = DateTimeOffset.Now.ToString(
"dd-MMM-yyyy HH:mm:ss.fff",
CultureInfo.InvariantCulture
);

var entry = $"{date}> {content}";

Expand All @@ -51,7 +39,7 @@ private void WaitForUpdateeExit()

for (var retriesRemaining = 15; retriesRemaining > 0; retriesRemaining--)
{
if (FileEx.CheckWriteAccess(_updateeFilePath))
if (FileEx.CheckWriteAccess(updateeFilePath))
return;

Thread.Sleep(1000);
Expand All @@ -63,12 +51,12 @@ private void WaitForUpdateeExit()
private void ApplyUpdate()
{
WriteLog("Copying package contents from storage to the updatee directory...");
DirectoryEx.Copy(_packageContentDirPath, Path.GetDirectoryName(_updateeFilePath)!);
DirectoryEx.Copy(packageContentDirPath, Path.GetDirectoryName(updateeFilePath)!);

try
{
WriteLog("Deleting package contents from storage...");
Directory.Delete(_packageContentDirPath, true);
Directory.Delete(packageContentDirPath, true);
}
catch (Exception ex)
{
Expand All @@ -79,40 +67,38 @@ private void ApplyUpdate()

private void StartUpdatee()
{
using var process = new Process
using var process = new Process();
process.StartInfo = new ProcessStartInfo
{
StartInfo = new ProcessStartInfo
{
WorkingDirectory = Path.GetDirectoryName(_updateeFilePath),
Arguments = _routedArgs,
// Don't let the child process inherit the current console window
UseShellExecute = true
}
WorkingDirectory = Path.GetDirectoryName(updateeFilePath),
Arguments = routedArgs,
// Don't let the child process inherit the current console window
UseShellExecute = true
};

// If the updatee is an .exe file, start it directly.
// This covers self-contained .NET Core apps and legacy .NET Framework apps.
if (
string.Equals(
Path.GetExtension(_updateeFilePath),
Path.GetExtension(updateeFilePath),
".exe",
StringComparison.OrdinalIgnoreCase
)
)
{
process.StartInfo.FileName = _updateeFilePath;
process.StartInfo.FileName = updateeFilePath;
}
// Otherwise, locate the apphost by looking for the .exe file with the same name.
// This covers framework-dependent .NET Core apps.
else if (File.Exists(Path.ChangeExtension(_updateeFilePath, ".exe")))
else if (File.Exists(Path.ChangeExtension(updateeFilePath, ".exe")))
{
process.StartInfo.FileName = Path.ChangeExtension(_updateeFilePath, ".exe");
process.StartInfo.FileName = Path.ChangeExtension(updateeFilePath, ".exe");
}
// As a fallback, try to run the updatee through the .NET CLI
else
{
process.StartInfo.FileName = "dotnet";
process.StartInfo.Arguments = $"{_updateeFilePath} {_routedArgs}";
process.StartInfo.Arguments = $"{updateeFilePath} {routedArgs}";
}

WriteLog(
Expand All @@ -131,17 +117,17 @@ public void Run()
WriteLog(
$"""
Onova Updater v{updaterVersion} started with the following arguments:
- UpdateeFilePath = {_updateeFilePath}
- PackageContentDirPath = {_packageContentDirPath}
- RestartUpdatee = {_restartUpdatee}
- RoutedArgs = {_routedArgs}
- UpdateeFilePath = {updateeFilePath}
- PackageContentDirPath = {packageContentDirPath}
- RestartUpdatee = {restartUpdatee}
- RoutedArgs = {routedArgs}
"""
);

WaitForUpdateeExit();
ApplyUpdate();

if (_restartUpdatee)
if (restartUpdatee)
StartUpdatee();

WriteLog("Update completed successfully.");
Expand Down
14 changes: 4 additions & 10 deletions Onova/Exceptions/LockFileNotAcquiredException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,7 @@ namespace Onova.Exceptions;
/// <summary>
/// Thrown when an attempt to acquire a lock file failed.
/// </summary>
public class LockFileNotAcquiredException : Exception
{
/// <summary>
/// Initializes an instance of <see cref="LockFileNotAcquiredException" />.
/// </summary>
public LockFileNotAcquiredException()
: base(
"Could not acquire a lock file. Most likely, another instance of this application currently owns the lock file."
) { }
}
public class LockFileNotAcquiredException()
: Exception(
"Could not acquire a lock file. Most likely, another instance of this application currently owns the lock file."
);
Loading

0 comments on commit 8d52049

Please sign in to comment.