From 48dcbf51fd2b65ebd025b4cdf3ebfd855bbd7827 Mon Sep 17 00:00:00 2001 From: Manodasan Wignarajah Date: Sun, 16 Apr 2023 20:33:04 -0700 Subject: [PATCH] Make extension self-contained (#14) * Make self contained * Enable BuiltInComSupport which is disabled by default when trimming. --- .gitignore | 1 - .../Widgets/GithubIssuesWidget.cs | 4 ++-- src/GithubPlugin/Widgets/GithubPullsWidget.cs | 2 +- src/GithubPlugin/Widgets/GithubWidget.cs | 22 ++++++++++++++----- src/GithubPlugin/Widgets/WidgetServer.cs | 5 +++++ .../GithubPluginServer.csproj | 7 ++++++ .../PublishProfiles/win10-arm64.pubxml | 17 ++++++++++++++ .../PublishProfiles/win10-x64.pubxml | 17 ++++++++++++++ .../PublishProfiles/win10-x86.pubxml | 17 ++++++++++++++ src/Telemetry/ILogger.cs | 5 +++-- src/Telemetry/Logger.cs | 17 +++++++++----- 11 files changed, 96 insertions(+), 18 deletions(-) create mode 100644 src/GithubPluginServer/Properties/PublishProfiles/win10-arm64.pubxml create mode 100644 src/GithubPluginServer/Properties/PublishProfiles/win10-x64.pubxml create mode 100644 src/GithubPluginServer/Properties/PublishProfiles/win10-x86.pubxml diff --git a/.gitignore b/.gitignore index 8dd4607a..6e4ea400 100644 --- a/.gitignore +++ b/.gitignore @@ -184,7 +184,6 @@ publish/ *.azurePubxml # Note: Comment the next line if you want to checkin your web deploy settings, # but database connection strings (with potential passwords) will be unencrypted -*.pubxml *.publishproj # Microsoft Azure Web App publish settings. Comment the next line if you want to diff --git a/src/GithubPlugin/Widgets/GithubIssuesWidget.cs b/src/GithubPlugin/Widgets/GithubIssuesWidget.cs index f0726740..a3ff5ac7 100644 --- a/src/GithubPlugin/Widgets/GithubIssuesWidget.cs +++ b/src/GithubPlugin/Widgets/GithubIssuesWidget.cs @@ -117,12 +117,12 @@ public override void LoadContentData() { "color", label.Color }, }; - issueLabels.Add(issueLabel); + ((IList)issueLabels).Add(issueLabel); } issue.Add("labels", issueLabels); - issuesArray.Add(issue); + ((IList)issuesArray).Add(issue); } issuesData.Add("issues", issuesArray); diff --git a/src/GithubPlugin/Widgets/GithubPullsWidget.cs b/src/GithubPlugin/Widgets/GithubPullsWidget.cs index 7738e2c3..cfc3d9fd 100644 --- a/src/GithubPlugin/Widgets/GithubPullsWidget.cs +++ b/src/GithubPlugin/Widgets/GithubPullsWidget.cs @@ -107,7 +107,7 @@ public override void LoadContentData() { "avatar", pullItem.Author.AvatarUrl }, }; - pullsArray.Add(pull); + ((IList)pullsArray).Add(pull); } pullsData.Add("pulls", pullsArray); diff --git a/src/GithubPlugin/Widgets/GithubWidget.cs b/src/GithubPlugin/Widgets/GithubWidget.cs index 6f26300f..f8d6a215 100644 --- a/src/GithubPlugin/Widgets/GithubWidget.cs +++ b/src/GithubPlugin/Widgets/GithubWidget.cs @@ -4,6 +4,7 @@ using System.Text; using System.Text.Json; using System.Text.Json.Nodes; +using System.Text.Json.Serialization; using GitHubPlugin.Client; using GitHubPlugin.DataManager; using GitHubPlugin.DeveloperId; @@ -71,11 +72,6 @@ public GithubWidget() DeveloperIdProvider.GetInstance().LoggedOut -= HandleDeveloperIdChange; } - private class DataPayload - { - public string? Repo { get; set; } - } - public virtual void RequestContentData() { throw new NotImplementedException(); @@ -149,7 +145,7 @@ private void HandleCheckUrl(WidgetActionInvokedArgs args) // the Configure state. Page = WidgetPageState.Configure; var data = args.Data; - var dataObject = JsonSerializer.Deserialize(data); + var dataObject = JsonSerializer.Deserialize(data, SourceGenerationContext.Default.DataPayload); if (dataObject != null && dataObject.Repo != null) { var updateRequestOptions = new WidgetUpdateRequestOptions(Id) @@ -423,3 +419,17 @@ private void HandleDeveloperIdChange(object? sender, IDeveloperId e) UpdateActivityState(); } } + +internal class DataPayload +{ + public string? Repo + { + get; set; + } +} + +[JsonSourceGenerationOptions(WriteIndented = true)] +[JsonSerializable(typeof(DataPayload))] +internal partial class SourceGenerationContext : JsonSerializerContext +{ +} diff --git a/src/GithubPlugin/Widgets/WidgetServer.cs b/src/GithubPlugin/Widgets/WidgetServer.cs index e18a906b..98508a2b 100644 --- a/src/GithubPlugin/Widgets/WidgetServer.cs +++ b/src/GithubPlugin/Widgets/WidgetServer.cs @@ -2,6 +2,7 @@ // Licensed under the MIT license. using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; using System.Runtime.InteropServices; using Microsoft.Windows.Widgets.Providers; @@ -10,6 +11,10 @@ public sealed class WidgetServer : IDisposable { private readonly HashSet registrationCookies = new (); + [UnconditionalSuppressMessage( + "ReflectionAnalysis", + "IL2050:COMCorrectness", + Justification = "WidgetProviderFactory and all the interfaces it implements are defined in an assembly that is not marked trimmable which means the relevant interfaces won't be trimmed.")] public void RegisterWidget(Func createWidget) where T : IWidgetProvider { diff --git a/src/GithubPluginServer/GithubPluginServer.csproj b/src/GithubPluginServer/GithubPluginServer.csproj index 3f86a92f..bdbd75ec 100644 --- a/src/GithubPluginServer/GithubPluginServer.csproj +++ b/src/GithubPluginServer/GithubPluginServer.csproj @@ -18,6 +18,13 @@ false true DevHomeGitHubExtension + win10-x86;win10-x64;win10-arm64 + Properties\PublishProfiles\win10-$(Platform).pubxml + true + partial + true + false + true diff --git a/src/GithubPluginServer/Properties/PublishProfiles/win10-arm64.pubxml b/src/GithubPluginServer/Properties/PublishProfiles/win10-arm64.pubxml new file mode 100644 index 00000000..c5e8be62 --- /dev/null +++ b/src/GithubPluginServer/Properties/PublishProfiles/win10-arm64.pubxml @@ -0,0 +1,17 @@ + + + + + FileSystem + arm64 + win10-arm64 + true + False + False + True + True + True + + \ No newline at end of file diff --git a/src/GithubPluginServer/Properties/PublishProfiles/win10-x64.pubxml b/src/GithubPluginServer/Properties/PublishProfiles/win10-x64.pubxml new file mode 100644 index 00000000..e8e2c0a3 --- /dev/null +++ b/src/GithubPluginServer/Properties/PublishProfiles/win10-x64.pubxml @@ -0,0 +1,17 @@ + + + + + FileSystem + x64 + win10-x64 + true + False + False + True + True + True + + \ No newline at end of file diff --git a/src/GithubPluginServer/Properties/PublishProfiles/win10-x86.pubxml b/src/GithubPluginServer/Properties/PublishProfiles/win10-x86.pubxml new file mode 100644 index 00000000..e601cec4 --- /dev/null +++ b/src/GithubPluginServer/Properties/PublishProfiles/win10-x86.pubxml @@ -0,0 +1,17 @@ + + + + + FileSystem + x86 + win10-x86 + true + False + False + True + True + True + + \ No newline at end of file diff --git a/src/Telemetry/ILogger.cs b/src/Telemetry/ILogger.cs index 69cf150a..2ef95987 100644 --- a/src/Telemetry/ILogger.cs +++ b/src/Telemetry/ILogger.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -55,7 +56,7 @@ public interface ILogger /// Values to send to the telemetry system. /// Optional relatedActivityId which will allow to correlate this telemetry with other telemetry in the same action/activity or thread and corelate them /// Anonymous type. - public void Log(string eventName, LogLevel level, T data, Guid? relatedActivityId = null); + public void Log<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] T>(string eventName, LogLevel level, T data, Guid? relatedActivityId = null); /// /// Log an error event. Typically used for just a single event that's only called one place in the code. @@ -66,5 +67,5 @@ public interface ILogger /// Values to send to the telemetry system. /// Optional Optional relatedActivityId which will allow to correlate this telemetry with other telemetry in the same action/activity or thread and corelate them /// Anonymous type. - public void LogError(string eventName, LogLevel level, T data, Guid? relatedActivityId = null); + public void LogError<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] T>(string eventName, LogLevel level, T data, Guid? relatedActivityId = null); } diff --git a/src/Telemetry/Logger.cs b/src/Telemetry/Logger.cs index f3598923..d46898ed 100644 --- a/src/Telemetry/Logger.cs +++ b/src/Telemetry/Logger.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; using System.Diagnostics.Tracing; using System.IO; using System.Linq; @@ -190,7 +191,7 @@ public void LogTimeTaken(string eventName, uint timeTakenMilliseconds, Guid? rel /// Values to send to the telemetry system. /// Optional relatedActivityId which will allow to correlate this telemetry with other telemetry in the same action/activity or thread and corelate them /// Anonymous type. - public void Log(string eventName, LogLevel level, T data, Guid? relatedActivityId = null) + public void Log<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] T>(string eventName, LogLevel level, T data, Guid? relatedActivityId = null) { WriteTelemetryEvent(eventName, level, relatedActivityId ?? DefaultRelatedActivityId, false, data); } @@ -203,7 +204,7 @@ public void Log(string eventName, LogLevel level, T data, Guid? relatedActivi /// Values to send to the telemetry system. /// Optional Optional relatedActivityId which will allow to correlate this telemetry with other telemetry in the same action/activity or thread and corelate them /// Anonymous type. - public void LogError(string eventName, LogLevel level, T data, Guid? relatedActivityId = null) + public void LogError<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] T>(string eventName, LogLevel level, T data, Guid? relatedActivityId = null) { WriteTelemetryEvent(eventName, level, relatedActivityId ?? DefaultRelatedActivityId, true, data); } @@ -246,8 +247,8 @@ private string ReplaceSensitiveStrings(string message) } return message; - } - + } + /// /// Writes the telemetry event info using the TraceLogging API. /// @@ -255,8 +256,12 @@ private string ReplaceSensitiveStrings(string message) /// Name of the event. /// Determines whether to upload the data to our servers, and the sample set of host machines. /// Set to true if an error condition raised this event. - /// Values to send to the telemetry system. - private void WriteTelemetryEvent(string eventName, LogLevel level, Guid relatedActivityId, bool isError, T data) + /// Values to send to the telemetry system. + [UnconditionalSuppressMessage( + "ReflectionAnalysis", + "IL2026:RequiresUnreferencedCode", + Justification = "The type passed for data is an anonymous type consisting of primitive type properties declared in an assembly that is not marked trimmable.")] + private void WriteTelemetryEvent<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] T>(string eventName, LogLevel level, Guid relatedActivityId, bool isError, T data) { EventSourceOptions telemetryOptions; if (IsTelemetryOn)