[AOT compatible] Clean up some AOT build issue in FilePreviewCommon and MarkdownPreviewHandler (#36207)

* Use AppContext.BaseDirectory to replace assembly.GetExeAseembly.Location.
Fix json serilizer aot issue.

* clean up some AOT build issue

* Update src/modules/previewpane/MarkdownPreviewHandler/MarkdownPreviewHandlerControl.cs

Co-authored-by: Jeremy Sinclair <4016293+snickler@users.noreply.github.com>

* Update src/common/FilePreviewCommon/Formatters/JsonFormatter.cs

Co-authored-by: Jeremy Sinclair <4016293+snickler@users.noreply.github.com>

---------

Co-authored-by: Yu Leng (from Dev Box) <yuleng@microsoft.com>
Co-authored-by: Jeremy Sinclair <4016293+snickler@users.noreply.github.com>
This commit is contained in:
moooyo 2024-12-19 09:31:45 +08:00 коммит произвёл GitHub
Родитель 86c6b4ae95
Коммит 20a5f67222
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
6 изменённых файлов: 25 добавлений и 11 удалений

Просмотреть файл

@ -2,6 +2,7 @@
<!-- Look at Directory.Build.props in root for common stuff as well -->
<Import Project="..\..\Common.Dotnet.CsWinRT.props" />
<Import Project="..\..\Monaco.props" />
<Import Project="..\..\Common.Dotnet.AotCompatibility.props" />
<PropertyGroup>
<Description>PowerToys FilePreviewCommon</Description>

Просмотреть файл

@ -0,0 +1,14 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Microsoft.PowerToys.FilePreviewCommon.Monaco.Formatters;
[JsonSourceGenerationOptions(WriteIndented = true)]
[JsonSerializable(typeof(JsonDocument))]
internal sealed partial class FilePreviewJsonSerializerContext : JsonSerializerContext
{
}

Просмотреть файл

@ -14,7 +14,6 @@ namespace Microsoft.PowerToys.FilePreviewCommon.Monaco.Formatters
private static readonly JsonSerializerOptions _serializerOptions = new JsonSerializerOptions
{
WriteIndented = true,
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
};
@ -28,7 +27,8 @@ namespace Microsoft.PowerToys.FilePreviewCommon.Monaco.Formatters
using (var jDocument = JsonDocument.Parse(value, new JsonDocumentOptions { CommentHandling = JsonCommentHandling.Skip }))
{
return JsonSerializer.Serialize(jDocument, _serializerOptions);
FilePreviewJsonSerializerContext context = new(_serializerOptions);
return JsonSerializer.Serialize(jDocument, context.JsonDocument);
}
}
}

Просмотреть файл

@ -6,9 +6,7 @@ using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Text.Json;
using Microsoft.PowerToys.FilePreviewCommon.Monaco.Formatters;
namespace Microsoft.PowerToys.FilePreviewCommon
@ -38,15 +36,15 @@ namespace Microsoft.PowerToys.FilePreviewCommon
private static string GetRuntimeMonacoDirectory()
{
string exePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ?? string.Empty;
string baseDirectory = AppContext.BaseDirectory ?? string.Empty;
// If the executable is within "WinUI3Apps", correct the path first.
if (Path.GetFileName(exePath) == "WinUI3Apps")
if (Path.GetFileName(baseDirectory) == "WinUI3Apps")
{
exePath = Path.Combine(exePath, "..");
baseDirectory = Path.Combine(baseDirectory, "..");
}
string monacoPath = Path.Combine(exePath, "Assets", "Monaco");
string monacoPath = Path.Combine(baseDirectory, "Assets", "Monaco");
return Directory.Exists(monacoPath) ?
monacoPath :

Просмотреть файл

@ -1,7 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<!-- Look at Directory.Build.props in root for common stuff as well -->
<Import Project="..\..\..\Common.Dotnet.CsWinRT.props" />
<Import Project="..\..\..\Common.SelfContained.props" />
<Import Project="..\..\..\Common.Dotnet.AotCompatibility.props" />
<PropertyGroup>
<ImplicitUsings>enable</ImplicitUsings>

Просмотреть файл

@ -20,7 +20,7 @@ namespace Microsoft.PowerToys.PreviewHandler.Markdown
/// <summary>
/// Win Form Implementation for Markdown Preview Handler.
/// </summary>
public class MarkdownPreviewHandlerControl : FormHandlerControl
public partial class MarkdownPreviewHandlerControl : FormHandlerControl
{
private static readonly IFileSystem FileSystem = new FileSystem();
private static readonly IPath Path = FileSystem.Path;
@ -66,7 +66,7 @@ namespace Microsoft.PowerToys.PreviewHandler.Markdown
{
get
{
string codeBase = Assembly.GetExecutingAssembly().Location;
string codeBase = AppContext.BaseDirectory;
UriBuilder uri = new UriBuilder(codeBase);
string path = Uri.UnescapeDataString(uri.Path);
return Path.GetDirectoryName(path);