Don't use virtual member in constructor

This commit is contained in:
Caelan Sayler
2025-02-02 21:03:41 +00:00
committed by Caelan
parent e38b01eef2
commit 7381195d38
5 changed files with 43 additions and 27 deletions

View File

@@ -47,6 +47,12 @@ namespace Velopack.Locators
/// <summary> File path of the .AppImage which mounted and ran this application. </summary>
public string? AppImagePath => Environment.GetEnvironmentVariable("APPIMAGE");
/// <inheritdoc />
public override uint ProcessId { get; }
/// <inheritdoc />
public override string ProcessExePath { get; }
/// <summary>
/// Creates a new <see cref="OsxVelopackLocator"/> and auto-detects the
@@ -59,18 +65,18 @@ namespace Velopack.Locators
throw new NotSupportedException("Cannot instantiate LinuxVelopackLocator on a non-linux system.");
ProcessId = currentProcessId;
ProcessExePath = currentProcessPath;
var ourPath = ProcessExePath = currentProcessPath;
Log.Info($"Initialising {nameof(LinuxVelopackLocator)}");
// are we inside a mounted .AppImage?
var ix = ProcessExePath.IndexOf("/usr/bin/", StringComparison.InvariantCultureIgnoreCase);
var ix = ourPath.IndexOf("/usr/bin/", StringComparison.InvariantCultureIgnoreCase);
if (ix <= 0) {
Log.Warn($"Unable to locate .AppImage root from '{ProcessExePath}'. This warning indicates that the application is not running from a mounted .AppImage, for example during development.");
Log.Warn($"Unable to locate .AppImage root from '{ourPath}'. This warning indicates that the application is not running from a mounted .AppImage, for example during development.");
return;
}
var rootDir = ProcessExePath.Substring(0, ix);
var rootDir = ourPath.Substring(0, ix);
var contentsDir = Path.Combine(rootDir, "usr", "bin");
var updateExe = Path.Combine(contentsDir, "UpdateNix");
var metadataPath = Path.Combine(contentsDir, CoreUtil.SpecVersionFileName);

View File

@@ -44,6 +44,12 @@ namespace Velopack.Locators
/// <inheritdoc />
public override string? Channel { get; }
/// <inheritdoc />
public override uint ProcessId { get; }
/// <inheritdoc />
public override string ProcessExePath { get; }
/// <summary>
/// Creates a new <see cref="OsxVelopackLocator"/> and auto-detects the
@@ -56,18 +62,18 @@ namespace Velopack.Locators
throw new NotSupportedException("Cannot instantiate OsxLocator on a non-osx system.");
ProcessId = currentProcessId;
ProcessExePath = currentProcessPath;
var ourPath = ProcessExePath = currentProcessPath;
Log.Info($"Initialising {nameof(OsxVelopackLocator)}");
// are we inside a .app?
var ix = ProcessExePath.IndexOf(".app/", StringComparison.InvariantCultureIgnoreCase);
var ix = ourPath.IndexOf(".app/", StringComparison.InvariantCultureIgnoreCase);
if (ix <= 0) {
Log.Warn($"Unable to locate .app root from '{ProcessExePath}'");
Log.Warn($"Unable to locate .app root from '{ourPath}'");
return;
}
var appPath = ProcessExePath.Substring(0, ix + 4);
var appPath = ourPath.Substring(0, ix + 4);
var contentsDir = Path.Combine(appPath, "Contents");
var macosDir = Path.Combine(contentsDir, "MacOS");
var updateExe = Path.Combine(macosDir, "UpdateMac");

View File

@@ -2,6 +2,7 @@
using System.Diagnostics.CodeAnalysis;
using Microsoft.Extensions.Logging;
using NuGet.Versioning;
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
namespace Velopack.Locators
{
@@ -13,7 +14,6 @@ namespace Velopack.Locators
[ExcludeFromCodeCoverage]
public class TestVelopackLocator : VelopackLocator
{
/// <inheritdoc />
public override string? AppId {
get {
if (_id == null) {
@@ -23,7 +23,6 @@ namespace Velopack.Locators
}
}
/// <inheritdoc />
public override string? RootAppDir {
get {
if (_root == null) {
@@ -33,7 +32,6 @@ namespace Velopack.Locators
}
}
/// <inheritdoc />
public override string? PackagesDir {
get {
if (_packages == null) {
@@ -43,7 +41,6 @@ namespace Velopack.Locators
}
}
/// <inheritdoc />
public override string? UpdateExePath {
get {
if (_updatePath == null) {
@@ -53,7 +50,6 @@ namespace Velopack.Locators
}
}
/// <inheritdoc />
public override SemanticVersion? CurrentlyInstalledVersion {
get {
if (_version == null) {
@@ -63,7 +59,6 @@ namespace Velopack.Locators
}
}
/// <inheritdoc />
public override string? AppContentDir {
get {
if (_appContent == null) {
@@ -73,14 +68,12 @@ namespace Velopack.Locators
}
}
/// <inheritdoc />
public override string? Channel {
get {
return _channel;
}
}
/// <inheritdoc />
public override VelopackAsset? GetLatestLocalFullPackage()
{
if (_asset != null) {
@@ -89,6 +82,10 @@ namespace Velopack.Locators
return base.GetLatestLocalFullPackage();
}
public override uint ProcessId => 0;
public override string ProcessExePath { get; }
private readonly string? _updatePath;
private readonly SemanticVersion? _version;
private readonly string? _packages;
@@ -106,7 +103,7 @@ namespace Velopack.Locators
/// <inheritdoc cref="TestVelopackLocator" />
public TestVelopackLocator(string appId, string version, string packagesDir, string? appDir,
string? rootDir, string? updateExe, string? channel = null, ILogger? logger = null, VelopackAsset? localPackage = null)
string? rootDir, string? updateExe, string? channel = null, ILogger? logger = null, VelopackAsset? localPackage = null, string processPath = null!)
: base(logger)
{
_id = appId;
@@ -117,6 +114,7 @@ namespace Velopack.Locators
_appContent = appDir;
_channel = channel;
_asset = localPackage;
ProcessExePath = processPath;
}
}
}

View File

@@ -66,13 +66,13 @@ namespace Velopack.Locators
public abstract string? Channel { get; }
/// <inheritdoc/>
public virtual bool IsPortable => false;
/// <inheritdoc/>
public uint ProcessId { get; protected set; }
public abstract uint ProcessId { get; }
/// <inheritdoc/>
public string ProcessExePath { get; protected set; }
public abstract string ProcessExePath { get; }
/// <inheritdoc/>
public virtual bool IsPortable => false;
/// <inheritdoc/>
public virtual string? ThisExeRelativePath {

View File

@@ -38,6 +38,12 @@ namespace Velopack.Locators
/// <inheritdoc />
public override string? Channel { get; }
/// <inheritdoc />
public override uint ProcessId { get; }
/// <inheritdoc />
public override string ProcessExePath { get; }
/// <inheritdoc cref="WindowsVelopackLocator" />
public WindowsVelopackLocator(string currentProcessPath, uint currentProcessId, ILogger logger)
: base(logger)
@@ -46,7 +52,7 @@ namespace Velopack.Locators
throw new NotSupportedException("Cannot instantiate WindowsLocator on a non-Windows system.");
ProcessId = currentProcessId;
ProcessExePath = currentProcessPath;
var ourPath = ProcessExePath = currentProcessPath;
// We try various approaches here. Firstly, if Update.exe is in the parent directory,
// we use that. If it's not present, we search for a parent "current" or "app-{ver}" directory,
@@ -54,11 +60,11 @@ namespace Velopack.Locators
// There is some legacy code here, because it's possible that we're running in an "app-{ver}"
// directory which is NOT containing a sq.version, in which case we need to infer a lot of info.
ProcessExePath = Path.GetFullPath(ProcessExePath);
string myDirPath = Path.GetDirectoryName(ProcessExePath)!;
ProcessExePath = Path.GetFullPath(ourPath);
string myDirPath = Path.GetDirectoryName(ourPath)!;
var myDirName = Path.GetFileName(myDirPath);
var possibleUpdateExe = Path.GetFullPath(Path.Combine(myDirPath, "..", "Update.exe"));
var ixCurrent = ProcessExePath.LastIndexOf("/current/", StringComparison.InvariantCultureIgnoreCase);
var ixCurrent = ourPath.LastIndexOf("/current/", StringComparison.InvariantCultureIgnoreCase);
Log.Info($"Initializing {nameof(WindowsVelopackLocator)}");
@@ -86,7 +92,7 @@ namespace Velopack.Locators
}
} else if (ixCurrent > 0) {
// this is an attempt to handle the case where we are running in a nested current directory.
var rootDir = ProcessExePath.Substring(0, ixCurrent);
var rootDir = ourPath.Substring(0, ixCurrent);
var currentDir = Path.Combine(rootDir, "current");
var manifestFile = Path.Combine(currentDir, CoreUtil.SpecVersionFileName);
possibleUpdateExe = Path.GetFullPath(Path.Combine(rootDir, "Update.exe"));