|
| 1 | +using System; |
| 2 | +using System.Reflection; |
| 3 | + |
| 4 | +namespace OpenClaw.Shared; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// Single source of truth for the OpenClaw Companion app version that is |
| 8 | +/// surfaced to users. Reads <see cref="AssemblyInformationalVersionAttribute"/> |
| 9 | +/// (or falls back to <c>AssemblyVersion</c>) from the tray executable so every |
| 10 | +/// UI/diagnostic/handshake site reports the same number driven by the csproj |
| 11 | +/// <c><Version></c> property. |
| 12 | +/// </summary> |
| 13 | +/// <remarks> |
| 14 | +/// Under <c>dotnet test</c> and inside CLI siblings, <see cref="Assembly.GetEntryAssembly"/> |
| 15 | +/// is the host process (testhost / dotnet), not the tray exe — so we first |
| 16 | +/// search the current <see cref="AppDomain"/> for the tray assembly by name. |
| 17 | +/// Tests that need a deterministic value can set <see cref="TestOverride"/>. |
| 18 | +/// </remarks> |
| 19 | +public static class AppVersionInfo |
| 20 | +{ |
| 21 | + private const string TrayAssemblyName = "OpenClaw.Tray.WinUI"; |
| 22 | + |
| 23 | + private static readonly string _version = ResolveVersion(); |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// Test-only override. When non-null, <see cref="Version"/> returns this |
| 27 | + /// value instead of the reflected one, giving tests a deterministic |
| 28 | + /// version string regardless of the host process. |
| 29 | + /// </summary> |
| 30 | + internal static string? TestOverride { get; set; } |
| 31 | + |
| 32 | + /// <summary>Bare version string, e.g. <c>"0.4.7"</c>.</summary> |
| 33 | + public static string Version => TestOverride ?? _version; |
| 34 | + |
| 35 | + /// <summary>Version prefixed with <c>v</c>, e.g. <c>"v0.4.7"</c>.</summary> |
| 36 | + public static string DisplayVersion => "v" + Version; |
| 37 | + |
| 38 | + private static string ResolveVersion() |
| 39 | + { |
| 40 | + try |
| 41 | + { |
| 42 | + var assembly = FindTrayAssembly() |
| 43 | + ?? Assembly.GetEntryAssembly() |
| 44 | + ?? typeof(AppVersionInfo).Assembly; |
| 45 | + |
| 46 | + var informational = assembly |
| 47 | + .GetCustomAttribute<AssemblyInformationalVersionAttribute>() |
| 48 | + ?.InformationalVersion; |
| 49 | + if (!string.IsNullOrWhiteSpace(informational)) |
| 50 | + { |
| 51 | + return NormalizeSemVer(informational); |
| 52 | + } |
| 53 | + |
| 54 | + var name = assembly.GetName().Version; |
| 55 | + if (name != null) |
| 56 | + { |
| 57 | + // System.Version uses -1 for unspecified components; coerce to 0. |
| 58 | + var build = Math.Max(0, name.Build); |
| 59 | + var revision = Math.Max(0, name.Revision); |
| 60 | + return revision == 0 |
| 61 | + ? $"{name.Major}.{name.Minor}.{build}" |
| 62 | + : $"{name.Major}.{name.Minor}.{build}.{revision}"; |
| 63 | + } |
| 64 | + |
| 65 | + return "0.0.0"; |
| 66 | + } |
| 67 | + catch |
| 68 | + { |
| 69 | + // Never let the type initializer fail — a TypeInitializationException |
| 70 | + // would poison every future access from every caller. |
| 71 | + return "0.0.0"; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + private static Assembly? FindTrayAssembly() |
| 76 | + { |
| 77 | + foreach (var asm in AppDomain.CurrentDomain.GetAssemblies()) |
| 78 | + { |
| 79 | + if (string.Equals(asm.GetName().Name, TrayAssemblyName, StringComparison.Ordinal)) |
| 80 | + return asm; |
| 81 | + } |
| 82 | + return null; |
| 83 | + } |
| 84 | + |
| 85 | + private static string NormalizeSemVer(string s) |
| 86 | + { |
| 87 | + // Strip SourceLink build metadata, e.g. "0.4.7+abc123" -> "0.4.7". |
| 88 | + var plus = s.IndexOf('+'); |
| 89 | + if (plus >= 0) s = s.Substring(0, plus); |
| 90 | + |
| 91 | + // Strip the SemVer pre-release suffix, e.g. "0.4.7-beta.1" -> "0.4.7". |
| 92 | + // This keeps the UI string aligned with Updatum, which compares the |
| 93 | + // numeric AssemblyVersion only. Revisit if pre-release labels should |
| 94 | + // ever be surfaced to users. |
| 95 | + var dash = s.IndexOf('-'); |
| 96 | + if (dash >= 0) s = s.Substring(0, dash); |
| 97 | + |
| 98 | + return s; |
| 99 | + } |
| 100 | +} |
| 101 | + |
0 commit comments