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> /// <summary> File path of the .AppImage which mounted and ran this application. </summary>
public string? AppImagePath => Environment.GetEnvironmentVariable("APPIMAGE"); public string? AppImagePath => Environment.GetEnvironmentVariable("APPIMAGE");
/// <inheritdoc />
public override uint ProcessId { get; }
/// <inheritdoc />
public override string ProcessExePath { get; }
/// <summary> /// <summary>
/// Creates a new <see cref="OsxVelopackLocator"/> and auto-detects the /// 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."); throw new NotSupportedException("Cannot instantiate LinuxVelopackLocator on a non-linux system.");
ProcessId = currentProcessId; ProcessId = currentProcessId;
ProcessExePath = currentProcessPath; var ourPath = ProcessExePath = currentProcessPath;
Log.Info($"Initialising {nameof(LinuxVelopackLocator)}"); Log.Info($"Initialising {nameof(LinuxVelopackLocator)}");
// are we inside a mounted .AppImage? // 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) { 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; return;
} }
var rootDir = ProcessExePath.Substring(0, ix); var rootDir = ourPath.Substring(0, ix);
var contentsDir = Path.Combine(rootDir, "usr", "bin"); var contentsDir = Path.Combine(rootDir, "usr", "bin");
var updateExe = Path.Combine(contentsDir, "UpdateNix"); var updateExe = Path.Combine(contentsDir, "UpdateNix");
var metadataPath = Path.Combine(contentsDir, CoreUtil.SpecVersionFileName); var metadataPath = Path.Combine(contentsDir, CoreUtil.SpecVersionFileName);

View File

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

View File

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

View File

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

View File

@@ -38,6 +38,12 @@ namespace Velopack.Locators
/// <inheritdoc /> /// <inheritdoc />
public override string? Channel { get; } public override string? Channel { get; }
/// <inheritdoc />
public override uint ProcessId { get; }
/// <inheritdoc />
public override string ProcessExePath { get; }
/// <inheritdoc cref="WindowsVelopackLocator" /> /// <inheritdoc cref="WindowsVelopackLocator" />
public WindowsVelopackLocator(string currentProcessPath, uint currentProcessId, ILogger logger) public WindowsVelopackLocator(string currentProcessPath, uint currentProcessId, ILogger logger)
: base(logger) : base(logger)
@@ -46,7 +52,7 @@ namespace Velopack.Locators
throw new NotSupportedException("Cannot instantiate WindowsLocator on a non-Windows system."); throw new NotSupportedException("Cannot instantiate WindowsLocator on a non-Windows system.");
ProcessId = currentProcessId; ProcessId = currentProcessId;
ProcessExePath = currentProcessPath; var ourPath = ProcessExePath = currentProcessPath;
// We try various approaches here. Firstly, if Update.exe is in the parent directory, // 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, // 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}" // 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. // directory which is NOT containing a sq.version, in which case we need to infer a lot of info.
ProcessExePath = Path.GetFullPath(ProcessExePath); ProcessExePath = Path.GetFullPath(ourPath);
string myDirPath = Path.GetDirectoryName(ProcessExePath)!; string myDirPath = Path.GetDirectoryName(ourPath)!;
var myDirName = Path.GetFileName(myDirPath); var myDirName = Path.GetFileName(myDirPath);
var possibleUpdateExe = Path.GetFullPath(Path.Combine(myDirPath, "..", "Update.exe")); 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)}"); Log.Info($"Initializing {nameof(WindowsVelopackLocator)}");
@@ -86,7 +92,7 @@ namespace Velopack.Locators
} }
} else if (ixCurrent > 0) { } else if (ixCurrent > 0) {
// this is an attempt to handle the case where we are running in a nested current directory. // 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 currentDir = Path.Combine(rootDir, "current");
var manifestFile = Path.Combine(currentDir, CoreUtil.SpecVersionFileName); var manifestFile = Path.Combine(currentDir, CoreUtil.SpecVersionFileName);
possibleUpdateExe = Path.GetFullPath(Path.Combine(rootDir, "Update.exe")); possibleUpdateExe = Path.GetFullPath(Path.Combine(rootDir, "Update.exe"));