Skip to content

Commit bbdb795

Browse files
feat: Implement Package.Current.Id.Version for Skia and WASM platforms
Co-authored-by: MartinZikmund <1075116+MartinZikmund@users.noreply.github.com>
1 parent d7e26a4 commit bbdb795

File tree

2 files changed

+64
-3
lines changed

2 files changed

+64
-3
lines changed

src/Uno.UI.RuntimeTests/Tests/Windows_ApplicationModel/Given_PackageId.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#if __APPLE_UIKIT__ || __ANDROID__
1+
#if __APPLE_UIKIT__ || __ANDROID__ || __SKIA__ || __WASM__
22

33
using System;
44
using Microsoft.VisualStudio.TestTools.UnitTesting;
@@ -9,6 +9,7 @@ namespace Uno.UI.RuntimeTests.Tests.Windows_ApplicationModel
99
[TestClass]
1010
public class Given_PackageId
1111
{
12+
#if __APPLE_UIKIT__ || __ANDROID__
1213
[TestMethod]
1314
public void When_FamilyNameQueried()
1415
{
@@ -29,6 +30,7 @@ public void When_NameQueried()
2930
var SUT = Package.Current.Id;
3031
Assert.IsNotNull(SUT.Name);
3132
}
33+
#endif
3234

3335
[TestMethod]
3436
public void When_VersionQueried()
@@ -43,6 +45,24 @@ public void When_VersionQueried()
4345
Assert.Fail("Expected no exception, but got: " + ex.Message);
4446
}
4547
}
48+
49+
#if __SKIA__ || __WASM__
50+
[TestMethod]
51+
public void When_VersionQueried_ReturnsValidVersion()
52+
{
53+
var SUT = Package.Current.Id;
54+
var version = SUT.Version;
55+
56+
// The version should have been populated from AssemblyInformationalVersionAttribute
57+
// At minimum, we expect the Major version to be set (not just all zeros)
58+
// unless the app explicitly sets version 0.0.0.0
59+
// This test verifies that the Version property can be accessed without exception
60+
Assert.IsTrue(version.Major >= 0);
61+
Assert.IsTrue(version.Minor >= 0);
62+
Assert.IsTrue(version.Build >= 0);
63+
Assert.IsTrue(version.Revision >= 0);
64+
}
65+
#endif
4666
}
4767
}
4868
#endif
Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,51 @@
1-
namespace Windows.ApplicationModel;
1+
using System;
2+
using System.Reflection;
3+
using SystemVersion = global::System.Version;
4+
5+
namespace Windows.ApplicationModel;
26

37
public sealed partial class PackageId
48
{
59
public string Name { get; internal set; } = "";
610

7-
public PackageVersion Version { get; internal set; }
11+
public PackageVersion Version { get; internal set; } = GetEntryAssemblyVersion();
812

913
public string Publisher { get; internal set; } = "";
14+
15+
private static PackageVersion GetEntryAssemblyVersion()
16+
{
17+
var assembly = Assembly.GetEntryAssembly();
18+
if (assembly is not null)
19+
{
20+
// Try to get the AssemblyInformationalVersionAttribute first
21+
// This corresponds to ApplicationDisplayVersion in MSBuild
22+
var informationalVersionAttribute = assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>();
23+
if (informationalVersionAttribute is not null)
24+
{
25+
var versionString = informationalVersionAttribute.InformationalVersion;
26+
// AssemblyInformationalVersion may contain additional metadata (e.g., "+commitHash")
27+
// Extract only the version part
28+
var plusIndex = versionString.IndexOf('+');
29+
if (plusIndex >= 0)
30+
{
31+
versionString = versionString.Substring(0, plusIndex);
32+
}
33+
34+
if (SystemVersion.TryParse(versionString, out var version))
35+
{
36+
return new PackageVersion(version);
37+
}
38+
}
39+
40+
// Fallback to AssemblyVersion
41+
var assemblyVersion = assembly.GetName().Version;
42+
if (assemblyVersion is not null)
43+
{
44+
return new PackageVersion(assemblyVersion);
45+
}
46+
}
47+
48+
// Return default version if nothing is found
49+
return new PackageVersion();
50+
}
1051
}

0 commit comments

Comments
 (0)