mirror of
https://github.com/wwiinnddyy/LanMountainDesktop.git
synced 2026-06-20 23:54:26 +08:00
Refactor launcher startup, logging & host resolution
Improve launcher startup flow, logging, and host resolution. Key changes: add detailed startup logging and standardized preview messages; unify CLI vs GUI handling and error/result reporting (write result file when requested); refactor DeploymentLocator to a more robust host resolution (new HostResolutionResult, explicit/portable/published/debug resolution paths, legacy fallback); overhaul LauncherFlowCoordinator to better handle IPC stages, activation retries, window lifecycle, plugin/update flows and error reporting; add CommandContext helpers (IsGui/IsPreview/ExplicitAppRoot) and JSON context options; tighten async usage and ConfigureAwait calls; add better UI error handling and consistent exit codes. Several UX/debug conveniences and robustness fixes included.
This commit is contained in:
@@ -33,7 +33,6 @@ internal sealed class DeploymentLocator
|
||||
var candidates = Directory.GetDirectories(_appRoot, "app-*", SearchOption.TopDirectoryOnly);
|
||||
Console.WriteLine($"[DeploymentLocator] Found {candidates.Length} app-* directories");
|
||||
|
||||
// ClassIsland 风格的查询:先筛选,后排序
|
||||
var validInstallations = candidates
|
||||
.Where(path =>
|
||||
{
|
||||
@@ -79,38 +78,199 @@ internal sealed class DeploymentLocator
|
||||
}
|
||||
}
|
||||
|
||||
public string? ResolveHostExecutablePath()
|
||||
public HostResolutionResult ResolveHostExecutable(CommandContext context)
|
||||
{
|
||||
// 使用新的灵活定位器
|
||||
var options = new HostDiscoveryOptions
|
||||
{
|
||||
ExecutableName = "LanMountainDesktop",
|
||||
PreferDevModeConfig = true,
|
||||
RecursiveSearch = false, // 默认不启用递归搜索以提高性能
|
||||
AdditionalSearchPaths = new List<string>
|
||||
{
|
||||
// 可以通过配置文件或环境变量添加更多路径
|
||||
"${AppRoot}",
|
||||
"${AppRoot}/..",
|
||||
"${BaseDirectory}/../..",
|
||||
}
|
||||
};
|
||||
ArgumentNullException.ThrowIfNull(context);
|
||||
|
||||
var locator = new FlexibleHostLocator(_appRoot, options);
|
||||
var result = locator.ResolveHostExecutablePath();
|
||||
|
||||
if (result != null)
|
||||
var executable = OperatingSystem.IsWindows() ? "LanMountainDesktop.exe" : "LanMountainDesktop";
|
||||
var searchedPaths = new List<string>();
|
||||
var explicitAppRoot = context.ExplicitAppRoot;
|
||||
var devModeConfigIgnored = !context.IsDebugMode && Views.ErrorWindow.CheckDevModeEnabled();
|
||||
|
||||
string? resolvedPath;
|
||||
string? source;
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(explicitAppRoot))
|
||||
{
|
||||
return result;
|
||||
var explicitRoot = Path.GetFullPath(explicitAppRoot);
|
||||
resolvedPath = TryResolveExplicitAppRoot(explicitRoot, executable, searchedPaths, out source);
|
||||
}
|
||||
else
|
||||
{
|
||||
resolvedPath = TryResolvePublishedOrPortableHost(executable, searchedPaths, out source);
|
||||
}
|
||||
|
||||
// 回退到旧逻辑(作为备选)
|
||||
if (resolvedPath is null && context.IsDebugMode)
|
||||
{
|
||||
resolvedPath = TryResolveDebugHost(executable, searchedPaths, out source);
|
||||
}
|
||||
|
||||
if (resolvedPath is null)
|
||||
{
|
||||
resolvedPath = ResolveHostExecutablePathLegacy();
|
||||
if (!string.IsNullOrWhiteSpace(resolvedPath))
|
||||
{
|
||||
searchedPaths.Add(Path.GetFullPath(resolvedPath));
|
||||
source = "legacy_fallback";
|
||||
}
|
||||
}
|
||||
|
||||
return new HostResolutionResult
|
||||
{
|
||||
Success = !string.IsNullOrWhiteSpace(resolvedPath),
|
||||
ResolvedHostPath = resolvedPath,
|
||||
ResolutionSource = source,
|
||||
AppRoot = _appRoot,
|
||||
ExplicitAppRoot = explicitAppRoot,
|
||||
DevModeConfigIgnored = devModeConfigIgnored,
|
||||
SearchedPaths = searchedPaths
|
||||
.Where(path => !string.IsNullOrWhiteSpace(path))
|
||||
.Distinct(StringComparer.OrdinalIgnoreCase)
|
||||
.ToList()
|
||||
};
|
||||
}
|
||||
|
||||
public string? ResolveHostExecutablePath()
|
||||
{
|
||||
return ResolveHostExecutablePathLegacy();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 传统的主程序路径解析(作为备选)
|
||||
/// </summary>
|
||||
private string? TryResolveExplicitAppRoot(
|
||||
string explicitRoot,
|
||||
string executable,
|
||||
List<string> searchedPaths,
|
||||
out string? source)
|
||||
{
|
||||
var directPath = Path.Combine(explicitRoot, executable);
|
||||
searchedPaths.Add(directPath);
|
||||
if (File.Exists(directPath))
|
||||
{
|
||||
source = "explicit_app_root_direct";
|
||||
return directPath;
|
||||
}
|
||||
|
||||
var deployment = FindBestDeploymentHost(explicitRoot, executable, searchedPaths);
|
||||
if (deployment is not null)
|
||||
{
|
||||
source = "explicit_app_root_deployment";
|
||||
return deployment;
|
||||
}
|
||||
|
||||
source = null;
|
||||
return null;
|
||||
}
|
||||
|
||||
private string? TryResolvePublishedOrPortableHost(
|
||||
string executable,
|
||||
List<string> searchedPaths,
|
||||
out string? source)
|
||||
{
|
||||
var deployment = FindBestDeploymentHost(_appRoot, executable, searchedPaths);
|
||||
if (deployment is not null)
|
||||
{
|
||||
source = "published_deployment";
|
||||
return deployment;
|
||||
}
|
||||
|
||||
var portableCandidates = new[]
|
||||
{
|
||||
Path.Combine(_appRoot, executable),
|
||||
Path.Combine(AppContext.BaseDirectory, executable)
|
||||
};
|
||||
|
||||
foreach (var candidate in portableCandidates
|
||||
.Select(Path.GetFullPath)
|
||||
.Distinct(StringComparer.OrdinalIgnoreCase))
|
||||
{
|
||||
searchedPaths.Add(candidate);
|
||||
if (File.Exists(candidate))
|
||||
{
|
||||
source = "portable_host";
|
||||
return candidate;
|
||||
}
|
||||
}
|
||||
|
||||
source = null;
|
||||
return null;
|
||||
}
|
||||
|
||||
private string? TryResolveDebugHost(
|
||||
string executable,
|
||||
List<string> searchedPaths,
|
||||
out string? source)
|
||||
{
|
||||
if (Views.ErrorWindow.CheckDevModeEnabled())
|
||||
{
|
||||
var savedCustomPath = Views.ErrorWindow.GetSavedCustomHostPath();
|
||||
if (!string.IsNullOrWhiteSpace(savedCustomPath))
|
||||
{
|
||||
var fullSavedPath = Path.GetFullPath(savedCustomPath);
|
||||
searchedPaths.Add(fullSavedPath);
|
||||
if (File.Exists(fullSavedPath))
|
||||
{
|
||||
source = "debug_saved_custom_path";
|
||||
return fullSavedPath;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var devPath in GetDevelopmentPaths(executable))
|
||||
{
|
||||
var fullPath = Path.GetFullPath(devPath);
|
||||
searchedPaths.Add(fullPath);
|
||||
if (File.Exists(fullPath))
|
||||
{
|
||||
source = "debug_build_output";
|
||||
return fullPath;
|
||||
}
|
||||
}
|
||||
|
||||
source = null;
|
||||
return null;
|
||||
}
|
||||
|
||||
private static string? FindBestDeploymentHost(
|
||||
string root,
|
||||
string executable,
|
||||
List<string> searchedPaths)
|
||||
{
|
||||
if (!Directory.Exists(root))
|
||||
{
|
||||
searchedPaths.Add(Path.Combine(root, "app-*", executable));
|
||||
return null;
|
||||
}
|
||||
|
||||
var appDirs = Directory.GetDirectories(root, "app-*", SearchOption.TopDirectoryOnly)
|
||||
.Where(path => !File.Exists(Path.Combine(path, ".destroy")))
|
||||
.Where(path => !File.Exists(Path.Combine(path, ".partial")))
|
||||
.Select(path => new
|
||||
{
|
||||
Path = path,
|
||||
HostPath = Path.Combine(path, executable),
|
||||
HasCurrent = File.Exists(Path.Combine(path, ".current")),
|
||||
Version = ParseVersionFromDirectory(path)
|
||||
})
|
||||
.OrderByDescending(item => item.HasCurrent)
|
||||
.ThenByDescending(item => item.Version)
|
||||
.ToList();
|
||||
|
||||
foreach (var candidate in appDirs)
|
||||
{
|
||||
searchedPaths.Add(candidate.HostPath);
|
||||
if (File.Exists(candidate.HostPath))
|
||||
{
|
||||
return candidate.HostPath;
|
||||
}
|
||||
}
|
||||
|
||||
if (appDirs.Count == 0)
|
||||
{
|
||||
searchedPaths.Add(Path.Combine(root, "app-*", executable));
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private string? ResolveHostExecutablePathLegacy()
|
||||
{
|
||||
var executable = OperatingSystem.IsWindows() ? "LanMountainDesktop.exe" : "LanMountainDesktop";
|
||||
@@ -126,14 +286,12 @@ internal sealed class DeploymentLocator
|
||||
}
|
||||
}
|
||||
|
||||
// 2. 查找 Launcher 所在目录(开发环境 - 直接运行)
|
||||
var inRoot = Path.Combine(_appRoot, executable);
|
||||
if (File.Exists(inRoot))
|
||||
{
|
||||
return inRoot;
|
||||
}
|
||||
|
||||
// 3. 查找父目录(开发环境 - 从 Launcher 项目运行)
|
||||
var parent = Path.GetFullPath(Path.Combine(_appRoot, ".."));
|
||||
var inParent = Path.Combine(parent, executable);
|
||||
if (File.Exists(inParent))
|
||||
@@ -144,14 +302,12 @@ internal sealed class DeploymentLocator
|
||||
// 4. å¼€å<E282AC>‘模å¼<C3A5>:如果å<C593>¯ç”¨äº†å¼€å<E282AC>‘模å¼<C3A5>,优先使用ä¿<C3A4>å˜çš„自定义路径
|
||||
if (Views.ErrorWindow.CheckDevModeEnabled())
|
||||
{
|
||||
// 4.1 首先检查保存的自定义路径
|
||||
var savedCustomPath = Views.ErrorWindow.GetSavedCustomHostPath();
|
||||
if (!string.IsNullOrWhiteSpace(savedCustomPath) && File.Exists(savedCustomPath))
|
||||
{
|
||||
return savedCustomPath;
|
||||
}
|
||||
|
||||
// 4.2 扫描开发路径
|
||||
var devPath = ScanDevelopmentPaths(executable);
|
||||
if (!string.IsNullOrWhiteSpace(devPath))
|
||||
{
|
||||
@@ -179,7 +335,7 @@ internal sealed class DeploymentLocator
|
||||
{
|
||||
var possiblePaths = new[]
|
||||
{
|
||||
// 从 Launcher 项目运行
|
||||
// ä»?Launcher 项目è¿<EFBFBD>行
|
||||
Path.Combine(AppContext.BaseDirectory, "..", "..", "LanMountainDesktop", "bin", "Debug", "net10.0", executable),
|
||||
Path.Combine(AppContext.BaseDirectory, "..", "..", "LanMountainDesktop", "bin", "Release", "net10.0", executable),
|
||||
|
||||
@@ -203,17 +359,14 @@ internal sealed class DeploymentLocator
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取开发环境可能的主程序路径
|
||||
/// </summary>
|
||||
/// 获å<EFBFBD>–å¼€å<EFBFBD>‘环境å<EFBFBD>¯èƒ½çš„主程åº<EFBFBD>è·¯å¾? /// </summary>
|
||||
private static IEnumerable<string> GetDevelopmentPaths(string executable)
|
||||
{
|
||||
// 获取 Launcher 所在目录
|
||||
var launcherDir = AppContext.BaseDirectory;
|
||||
|
||||
// 可能的开发目录结构
|
||||
var possiblePaths = new[]
|
||||
{
|
||||
// 从 Launcher 项目运行:..\LanMountainDesktop\bin\Debug\net10.0\LanMountainDesktop.exe
|
||||
// ä»?Launcher 项目è¿<EFBFBD>行ï¼?.\LanMountainDesktop\bin\Debug\net10.0\LanMountainDesktop.exe
|
||||
Path.Combine(launcherDir, "..", "..", "LanMountainDesktop", "bin", "Debug", "net10.0", executable),
|
||||
Path.Combine(launcherDir, "..", "..", "LanMountainDesktop", "bin", "Release", "net10.0", executable),
|
||||
|
||||
@@ -221,7 +374,7 @@ internal sealed class DeploymentLocator
|
||||
Path.Combine(launcherDir, "..", "LanMountainDesktop", "bin", "Debug", "net10.0", executable),
|
||||
Path.Combine(launcherDir, "..", "LanMountainDesktop", "bin", "Release", "net10.0", executable),
|
||||
|
||||
// 从 dev-test 目录运行
|
||||
// ä»?dev-test 目录è¿<EFBFBD>行
|
||||
Path.Combine(launcherDir, "..", "dev-test", "app-1.0.0-dev", executable),
|
||||
};
|
||||
|
||||
@@ -256,9 +409,8 @@ internal sealed class DeploymentLocator
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 清理旧版本部署,保留最近的N个版本
|
||||
/// </summary>
|
||||
/// <param name="minVersionsToKeep">最少保留版本数,默认3个</param>
|
||||
/// 清ç<EFBFBD>†æ—§ç‰ˆæœ¬éƒ¨ç½²ï¼Œä¿<EFBFBD>留最近的N个版æœ? /// </summary>
|
||||
/// <param name="minVersionsToKeep">最少ä¿<C3A4>留版本数,默è®?ä¸?/param>
|
||||
public void CleanupOldDeployments(int minVersionsToKeep = 3)
|
||||
{
|
||||
try
|
||||
@@ -272,7 +424,6 @@ internal sealed class DeploymentLocator
|
||||
|
||||
var candidates = Directory.GetDirectories(_appRoot, "app-*", SearchOption.TopDirectoryOnly);
|
||||
|
||||
// 过滤掉无效部署目录(排除partial),按版本排序
|
||||
var validDeployments = candidates
|
||||
.Where(path => !File.Exists(Path.Combine(path, ".partial")))
|
||||
.Select(path => new
|
||||
@@ -349,7 +500,6 @@ internal sealed class DeploymentLocator
|
||||
{
|
||||
if (versionsToKeep.Contains(deployment.Path))
|
||||
{
|
||||
// 保留此版本,如果之前标记了destroy则取消标记
|
||||
if (deployment.IsDestroyed)
|
||||
{
|
||||
try
|
||||
@@ -365,7 +515,6 @@ internal sealed class DeploymentLocator
|
||||
continue;
|
||||
}
|
||||
|
||||
// 如果还没标记destroy的,先标记
|
||||
if (!deployment.IsDestroyed)
|
||||
{
|
||||
try
|
||||
@@ -387,7 +536,7 @@ internal sealed class DeploymentLocator
|
||||
}
|
||||
catch
|
||||
{
|
||||
// 忽略删除失败(可能文件被占用),下次启动再试
|
||||
// å¿½ç•¥åˆ é™¤å¤±è´¥(å<>¯èƒ½æ–‡ä»¶è¢«å<C2AB> ç”?,下次å<C2A1>¯åЍå†<C3A5>试
|
||||
Console.WriteLine($"[DeploymentLocator] Failed to delete (will retry later): {deployment.Path}");
|
||||
}
|
||||
}
|
||||
@@ -400,7 +549,7 @@ internal sealed class DeploymentLocator
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 仅清理已标记为.destroy的部署(兼容旧方法)
|
||||
/// 仅清ç<EFBFBD>†å·²æ ‡è®°ä¸?destroy的部署(兼容旧方法)
|
||||
/// </summary>
|
||||
[Obsolete("Use CleanupOldDeployments instead")]
|
||||
public void CleanupDestroyedDeployments()
|
||||
@@ -432,8 +581,7 @@ internal sealed class DeploymentLocator
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从部署目录读取版本信息
|
||||
/// </summary>
|
||||
/// 从部署目录读å<EFBFBD>–版本信æ<EFBFBD>? /// </summary>
|
||||
public AppVersionInfo GetVersionInfo()
|
||||
{
|
||||
var deploymentDir = FindCurrentDeploymentDirectory();
|
||||
@@ -453,16 +601,16 @@ internal sealed class DeploymentLocator
|
||||
}
|
||||
catch
|
||||
{
|
||||
// 忽略读取失败,回退到默认值
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 回退:从目录名解析版本,使用默认开发代号
|
||||
return new AppVersionInfo
|
||||
{
|
||||
Version = GetCurrentVersion(),
|
||||
Codename = "Administrate" // 默认开发代号
|
||||
Codename = "Administrate"
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
18
LanMountainDesktop.Launcher/Services/HostResolutionResult.cs
Normal file
18
LanMountainDesktop.Launcher/Services/HostResolutionResult.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
namespace LanMountainDesktop.Launcher.Services;
|
||||
|
||||
internal sealed class HostResolutionResult
|
||||
{
|
||||
public bool Success { get; init; }
|
||||
|
||||
public string? ResolvedHostPath { get; init; }
|
||||
|
||||
public string? ResolutionSource { get; init; }
|
||||
|
||||
public string AppRoot { get; init; } = string.Empty;
|
||||
|
||||
public string? ExplicitAppRoot { get; init; }
|
||||
|
||||
public bool DevModeConfigIgnored { get; init; }
|
||||
|
||||
public List<string> SearchedPaths { get; init; } = [];
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
42
LanMountainDesktop.Launcher/Services/WelcomeOobeStep.cs
Normal file
42
LanMountainDesktop.Launcher/Services/WelcomeOobeStep.cs
Normal file
@@ -0,0 +1,42 @@
|
||||
using Avalonia.Threading;
|
||||
using LanMountainDesktop.Launcher.Views;
|
||||
|
||||
namespace LanMountainDesktop.Launcher.Services;
|
||||
|
||||
internal sealed class WelcomeOobeStep : IOobeStep
|
||||
{
|
||||
private readonly OobeStateService _oobeStateService;
|
||||
|
||||
public WelcomeOobeStep(OobeStateService oobeStateService)
|
||||
{
|
||||
_oobeStateService = oobeStateService;
|
||||
}
|
||||
|
||||
public async Task RunAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
OobeWindow? window = null;
|
||||
await Dispatcher.UIThread.InvokeAsync(() =>
|
||||
{
|
||||
window = new OobeWindow();
|
||||
window.Show();
|
||||
});
|
||||
|
||||
if (window is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
await window.WaitForEnterAsync().ConfigureAwait(false);
|
||||
_oobeStateService.MarkCompleted();
|
||||
|
||||
await Dispatcher.UIThread.InvokeAsync(() =>
|
||||
{
|
||||
if (window.IsVisible)
|
||||
{
|
||||
window.Close();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user