Ignore VelopackApp check if the binary architecture is not the same as current system

This commit is contained in:
Caelan Sayler
2024-01-23 20:16:25 +00:00
parent d994404009
commit 5978e6d91f
7 changed files with 104 additions and 26 deletions

View File

@@ -4,6 +4,7 @@ using System.Linq;
using System.Runtime.Versioning;
using System.Text;
using System.Threading.Tasks;
using ELFSharp.ELF;
using Microsoft.Extensions.Logging;
namespace Velopack.Packaging.Unix.Commands
@@ -18,6 +19,25 @@ namespace Velopack.Packaging.Unix.Commands
{
}
protected override void VerifyMainBinary(string mainExePath)
{
var elf = ELFReader.Load(mainExePath);
var machine = elf.Machine switch {
Machine.AArch64 => RuntimeCpu.arm64,
Machine.AMD64 => RuntimeCpu.x64,
Machine.Intel386 => RuntimeCpu.x86,
_ => throw new Exception($"Unsupported ELF machine type '{elf.Machine}'.")
};
if (machine != VelopackRuntimeInfo.SystemArch) {
Log.Warn($"Skipping VelopackApp verification, because system architecture ({VelopackRuntimeInfo.SystemArch}) does not match main binary architecture ({machine}).");
return;
}
base.VerifyMainBinary(mainExePath);
}
protected override Task<string> PreprocessPackDir(Action<int> progress, string packDir)
{
var dir = TempDir.CreateSubdirectory("PreprocessPackDir.AppDir");

View File

@@ -1,6 +1,7 @@
using System.Text.RegularExpressions;
using Microsoft.Extensions.Logging;
using NuGet.Versioning;
using Velopack.Packaging.Exceptions;
namespace Velopack.Packaging.Unix.Commands;
@@ -27,8 +28,13 @@ public class OsxBundleCommandRunner
_logger.Info("Generating new '.app' bundle from a directory of application files.");
var mainExePath = Path.Combine(packDirectory, exeName);
if (!File.Exists(mainExePath) || !MachO.IsMachOImage(mainExePath))
throw new ArgumentException($"--exeName '{mainExePath}' does not exist or is not a mach-o executable.");
if (!File.Exists(mainExePath)) {
throw new UserInfoException($"--exeName '{mainExePath}' does not exist.");
}
if (!MachO.IsMachOImage(mainExePath)) {
throw new UserInfoException($"--exeName '{mainExePath}' is not a mach-o executable.");
}
var appleId = $"com.{packAuthors ?? packId}.{packId}";
var escapedAppleId = Regex.Replace(appleId, @"[^\w\.]", "_");

View File

@@ -1,5 +1,6 @@
using System.Runtime.Versioning;
using System.Threading.Channels;
using ELFSharp.MachO;
using Microsoft.Extensions.Logging;
using NuGet.Versioning;
@@ -13,6 +14,25 @@ public class OsxPackCommandRunner : PackageBuilder<OsxPackOptions>
{
}
protected override void VerifyMainBinary(string mainExePath)
{
var binary = MachOReader.Load(mainExePath);
var machine = binary.Machine switch {
Machine.Arm64 => RuntimeCpu.arm64,
Machine.X86_64 => RuntimeCpu.x64,
Machine.X86 => RuntimeCpu.x86,
_ => throw new Exception($"Unsupported Mach-O machine type '{binary.Machine}'.")
};
if (machine != VelopackRuntimeInfo.SystemArch) {
Log.Warn($"Skipping VelopackApp verification, because system architecture ({VelopackRuntimeInfo.SystemArch}) does not match main binary architecture ({machine}).");
return;
}
base.VerifyMainBinary(mainExePath);
}
protected override Task<string> PreprocessPackDir(Action<int> progress, string packDir)
{
var dir = TempDir.CreateSubdirectory(Options.PackId + ".app");

View File

@@ -7,6 +7,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="ELFSharp" Version="2.17.3" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>

View File

@@ -1,8 +1,7 @@
using System.Diagnostics;
using System.Runtime.Versioning;
using System.Threading.Channels;
using System.Runtime.Versioning;
using AsmResolver.PE;
using AsmResolver.PE.File.Headers;
using Microsoft.Extensions.Logging;
using NuGet.Versioning;
using Spectre.Console;
using Velopack.Compression;
using Velopack.NuGet;
@@ -59,6 +58,25 @@ public class WindowsPackCommandRunner : PackageBuilder<WindowsPackOptions>
return Task.FromResult(packDir);
}
protected override void VerifyMainBinary(string mainExePath)
{
var binary = PEImage.FromFile(mainExePath);
var machine = binary.MachineType switch {
MachineType.Arm64 => RuntimeCpu.arm64,
MachineType.Amd64 => RuntimeCpu.x64,
MachineType.I386 => RuntimeCpu.x86,
_ => throw new Exception($"Unsupported PE machine type '{binary.MachineType}'.")
};
if (machine != VelopackRuntimeInfo.SystemArch) {
Log.Warn($"Skipping VelopackApp verification, because system architecture ({VelopackRuntimeInfo.SystemArch}) does not match main binary architecture ({machine}).");
return;
}
base.VerifyMainBinary(mainExePath);
}
protected override string GetRuntimeDependencies()
{
if (string.IsNullOrWhiteSpace(Options.Runtimes))

View File

@@ -11,4 +11,8 @@
<ProjectReference Include="..\Velopack.Packaging\Velopack.Packaging.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="AsmResolver.PE" Version="5.5.0" />
</ItemGroup>
</Project>

View File

@@ -2,7 +2,6 @@
using System.Diagnostics;
using System.Security;
using System.Text.RegularExpressions;
using System.Xml;
using Microsoft.Extensions.Logging;
using NuGet.Versioning;
using Spectre.Console;
@@ -77,25 +76,7 @@ namespace Velopack.Packaging
MainExePath = mainExePath;
// verify that the main executable is a valid velopack app
try {
var psi = new ProcessStartInfo(mainExePath);
psi.AppendArgumentListSafe(new[] { "--veloapp-version" }, out var _);
var output = psi.Output(5000);
if (String.IsNullOrWhiteSpace(output)) {
throw new VelopackAppVerificationException("Exited with no output");
}
if (SemanticVersion.TryParse(output.Trim(), out var version)) {
if (version != VelopackRuntimeInfo.VelopackNugetVersion) {
Log.Warn($"VelopackApp version '{version}' does not match CLI version '{VelopackRuntimeInfo.VelopackNugetVersion}'.");
} else {
Log.Info($"VelopackApp version verified ({version}).");
}
} else {
throw new VelopackAppVerificationException($"Failed to parse version: {output.Trim()}");
}
} catch (TimeoutException) {
throw new VelopackAppVerificationException("Timed out");
}
VerifyMainBinary(mainExePath);
using var _1 = Utility.GetTempDirectory(out var pkgTempDir);
TempDir = new DirectoryInfo(pkgTempDir);
@@ -184,6 +165,34 @@ namespace Velopack.Packaging
return null;
}
protected virtual void VerifyMainBinary(string mainExePath)
{
try {
var psi = new ProcessStartInfo(mainExePath);
psi.AppendArgumentListSafe(new[] { "--veloapp-version" }, out var _);
var output = psi.Output(5000);
if (String.IsNullOrWhiteSpace(output)) {
throw new VelopackAppVerificationException("Exited with no output");
}
AssertVelopackVersionCompatible(output);
} catch (TimeoutException) {
throw new VelopackAppVerificationException("Timed out");
}
}
protected virtual void AssertVelopackVersionCompatible(string velopackVersion)
{
if (SemanticVersion.TryParse(velopackVersion.Trim(), out var version)) {
if (version != VelopackRuntimeInfo.VelopackNugetVersion) {
Log.Warn($"VelopackApp version '{version}' does not match CLI version '{VelopackRuntimeInfo.VelopackNugetVersion}'.");
} else {
Log.Info($"VelopackApp version verified ({version}).");
}
} else {
throw new VelopackAppVerificationException($"Failed to parse version: {velopackVersion.Trim()}");
}
}
protected virtual string GenerateNuspecContent()
{
var packId = Options.PackId;