Skip to content

Commit

Permalink
Make extension self-contained (#14)
Browse files Browse the repository at this point in the history
* Make self contained

* Enable BuiltInComSupport which is disabled by default when trimming.
  • Loading branch information
manodasanW authored Apr 17, 2023
1 parent d93dd81 commit 48dcbf5
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 18 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/GithubPlugin/Widgets/GithubIssuesWidget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,12 @@ public override void LoadContentData()
{ "color", label.Color },
};

issueLabels.Add(issueLabel);
((IList<JsonNode?>)issueLabels).Add(issueLabel);
}

issue.Add("labels", issueLabels);

issuesArray.Add(issue);
((IList<JsonNode?>)issuesArray).Add(issue);
}

issuesData.Add("issues", issuesArray);
Expand Down
2 changes: 1 addition & 1 deletion src/GithubPlugin/Widgets/GithubPullsWidget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public override void LoadContentData()
{ "avatar", pullItem.Author.AvatarUrl },
};

pullsArray.Add(pull);
((IList<JsonNode?>)pullsArray).Add(pull);
}

pullsData.Add("pulls", pullsArray);
Expand Down
22 changes: 16 additions & 6 deletions src/GithubPlugin/Widgets/GithubWidget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -149,7 +145,7 @@ private void HandleCheckUrl(WidgetActionInvokedArgs args)
// the Configure state.
Page = WidgetPageState.Configure;
var data = args.Data;
var dataObject = JsonSerializer.Deserialize<DataPayload>(data);
var dataObject = JsonSerializer.Deserialize(data, SourceGenerationContext.Default.DataPayload);
if (dataObject != null && dataObject.Repo != null)
{
var updateRequestOptions = new WidgetUpdateRequestOptions(Id)
Expand Down Expand Up @@ -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
{
}
5 changes: 5 additions & 0 deletions src/GithubPlugin/Widgets/WidgetServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -10,6 +11,10 @@ public sealed class WidgetServer : IDisposable
{
private readonly HashSet<int> 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<T>(Func<T> createWidget)
where T : IWidgetProvider
{
Expand Down
7 changes: 7 additions & 0 deletions src/GithubPluginServer/GithubPluginServer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@
<CsWinRTEnabled>false</CsWinRTEnabled>
<EnableMsixTooling>true</EnableMsixTooling>
<AssemblyName>DevHomeGitHubExtension</AssemblyName>
<RuntimeIdentifiers>win10-x86;win10-x64;win10-arm64</RuntimeIdentifiers>
<PublishProfile>Properties\PublishProfiles\win10-$(Platform).pubxml</PublishProfile>
<PublishReadyToRunEmitSymbols>true</PublishReadyToRunEmitSymbols>
<TrimMode>partial</TrimMode>
<EnableTrimAnalyzer>true</EnableTrimAnalyzer>
<ILLinkTreatWarningsAsErrors>false</ILLinkTreatWarningsAsErrors>
<BuiltInComInteropSupport>true</BuiltInComInteropSupport>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<PublishProtocol>FileSystem</PublishProtocol>
<Platform>arm64</Platform>
<RuntimeIdentifier>win10-arm64</RuntimeIdentifier>
<SelfContained>true</SelfContained>
<PublishSingleFile>False</PublishSingleFile>
<PublishReadyToRun Condition="'$(Configuration)' == 'Debug'">False</PublishReadyToRun>
<PublishReadyToRun Condition="'$(Configuration)' != 'Debug'">True</PublishReadyToRun>
<PublishReadyToRunComposite Condition="'$(Configuration)' != 'Debug'">True</PublishReadyToRunComposite>
<PublishTrimmed Condition="'$(Configuration)' != 'Debug'">True</PublishTrimmed>
</PropertyGroup>
</Project>
17 changes: 17 additions & 0 deletions src/GithubPluginServer/Properties/PublishProfiles/win10-x64.pubxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<PublishProtocol>FileSystem</PublishProtocol>
<Platform>x64</Platform>
<RuntimeIdentifier>win10-x64</RuntimeIdentifier>
<SelfContained>true</SelfContained>
<PublishSingleFile>False</PublishSingleFile>
<PublishReadyToRun Condition="'$(Configuration)' == 'Debug'">False</PublishReadyToRun>
<PublishReadyToRun Condition="'$(Configuration)' != 'Debug'">True</PublishReadyToRun>
<PublishReadyToRunComposite Condition="'$(Configuration)' != 'Debug'">True</PublishReadyToRunComposite>
<PublishTrimmed Condition="'$(Configuration)' != 'Debug'">True</PublishTrimmed>
</PropertyGroup>
</Project>
17 changes: 17 additions & 0 deletions src/GithubPluginServer/Properties/PublishProfiles/win10-x86.pubxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<PublishProtocol>FileSystem</PublishProtocol>
<Platform>x86</Platform>
<RuntimeIdentifier>win10-x86</RuntimeIdentifier>
<SelfContained>true</SelfContained>
<PublishSingleFile>False</PublishSingleFile>
<PublishReadyToRun Condition="'$(Configuration)' == 'Debug'">False</PublishReadyToRun>
<PublishReadyToRun Condition="'$(Configuration)' != 'Debug'">True</PublishReadyToRun>
<PublishReadyToRunComposite Condition="'$(Configuration)' != 'Debug'">True</PublishReadyToRunComposite>
<PublishTrimmed Condition="'$(Configuration)' != 'Debug'">True</PublishTrimmed>
</PropertyGroup>
</Project>
5 changes: 3 additions & 2 deletions src/Telemetry/ILogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
Expand Down Expand Up @@ -55,7 +56,7 @@ public interface ILogger
/// <param name="data">Values to send to the telemetry system.</param>
/// <param name="relatedActivityId">Optional relatedActivityId which will allow to correlate this telemetry with other telemetry in the same action/activity or thread and corelate them</param>
/// <typeparam name="T">Anonymous type.</typeparam>
public void Log<T>(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);

/// <summary>
/// Log an error event. Typically used for just a single event that's only called one place in the code.
Expand All @@ -66,5 +67,5 @@ public interface ILogger
/// <param name="data">Values to send to the telemetry system.</param>
/// <param name="relatedActivityId">Optional Optional relatedActivityId which will allow to correlate this telemetry with other telemetry in the same action/activity or thread and corelate them</param>
/// <typeparam name="T">Anonymous type.</typeparam>
public void LogError<T>(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);
}
17 changes: 11 additions & 6 deletions src/Telemetry/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -190,7 +191,7 @@ public void LogTimeTaken(string eventName, uint timeTakenMilliseconds, Guid? rel
/// <param name="data">Values to send to the telemetry system.</param>
/// <param name="relatedActivityId">Optional relatedActivityId which will allow to correlate this telemetry with other telemetry in the same action/activity or thread and corelate them</param>
/// <typeparam name="T">Anonymous type.</typeparam>
public void Log<T>(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);
}
Expand All @@ -203,7 +204,7 @@ public void Log<T>(string eventName, LogLevel level, T data, Guid? relatedActivi
/// <param name="data">Values to send to the telemetry system.</param>
/// <param name="relatedActivityId">Optional Optional relatedActivityId which will allow to correlate this telemetry with other telemetry in the same action/activity or thread and corelate them</param>
/// <typeparam name="T">Anonymous type.</typeparam>
public void LogError<T>(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);
}
Expand Down Expand Up @@ -246,17 +247,21 @@ private string ReplaceSensitiveStrings(string message)
}

return message;
}

}

/// <summary>
/// Writes the telemetry event info using the TraceLogging API.
/// </summary>
/// <typeparam name="T">Anonymous type.</typeparam>
/// <param name="eventName">Name of the event.</param>
/// <param name="level">Determines whether to upload the data to our servers, and the sample set of host machines.</param>
/// <param name="isError">Set to true if an error condition raised this event.</param>
/// <param name="data">Values to send to the telemetry system.</param>
private void WriteTelemetryEvent<T>(string eventName, LogLevel level, Guid relatedActivityId, bool isError, T data)
/// <param name="data">Values to send to the telemetry system.</param>
[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)
Expand Down

0 comments on commit 48dcbf5

Please sign in to comment.