Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 7 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/common/FilePreviewCommon/FilePreviewCommon.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
@@ -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
moooyo marked this conversation as resolved.
Show resolved Hide resolved
{
}
5 changes: 3 additions & 2 deletions src/common/FilePreviewCommon/Formatters/JsonFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public class JsonFormatter : IFormatter

private static readonly JsonSerializerOptions _serializerOptions = new JsonSerializerOptions
{
WriteIndented = true,
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
};

Expand All @@ -28,7 +27,9 @@ public string Format(string value)

using (var jDocument = JsonDocument.Parse(value, new JsonDocumentOptions { CommentHandling = JsonCommentHandling.Skip }))
{
return JsonSerializer.Serialize(jDocument, _serializerOptions);
FilePreviewJsonSerializerContext context = new(_serializerOptions);

moooyo marked this conversation as resolved.
Show resolved Hide resolved
return JsonSerializer.Serialize(jDocument, context.JsonDocument);
}
}
}
Expand Down
10 changes: 4 additions & 6 deletions src/common/FilePreviewCommon/MonacoHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
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
Expand Down Expand Up @@ -38,15 +36,15 @@ public static class MonacoHelper

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 :
Expand Down
Original file line number Diff line number Diff line change
@@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -66,7 +66,8 @@ public static string AssemblyDirectory
{
get
{
string codeBase = Assembly.GetExecutingAssembly().Location;
string codeBase = AppContext.BaseDirectory;

moooyo marked this conversation as resolved.
Show resolved Hide resolved
UriBuilder uri = new UriBuilder(codeBase);
string path = Uri.UnescapeDataString(uri.Path);
return Path.GetDirectoryName(path);
Expand Down
Loading