Skip to content

Commit

Permalink
[Peek] Fix Monaco assets folder discovery (#35925)
Browse files Browse the repository at this point in the history
  • Loading branch information
daverayment authored Nov 22, 2024
1 parent b81478e commit 863f7aa
Showing 1 changed file with 16 additions and 22 deletions.
38 changes: 16 additions & 22 deletions src/common/FilePreviewCommon/MonacoHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,34 +29,28 @@ public static class MonacoHelper
new XmlFormatter(),
}.AsReadOnly();

private static string? _monacoDirectory;
private static readonly Lazy<string> _monacoDirectory = new(GetRuntimeMonacoDirectory);

public static string GetRuntimeMonacoDirectory()
/// <summary>
/// Gets the path of the Monaco assets folder.
/// </summary>
public static string MonacoDirectory => _monacoDirectory.Value;

private static string GetRuntimeMonacoDirectory()
{
string codeBase = Assembly.GetExecutingAssembly().Location;
string path = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(codeBase) ?? string.Empty, "Assets", "Monaco"));
if (Path.Exists(path))
{
return path;
}
else
string exePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ?? string.Empty;

// If the executable is within "WinUI3Apps", correct the path first.
if (Path.GetFileName(exePath) == "WinUI3Apps")
{
// We're likely in WinUI3Apps directory and need to go back to the base directory.
return Path.GetFullPath(Path.Combine(Path.GetDirectoryName(codeBase) ?? string.Empty, "..", "Assets", "Monaco"));
exePath = Path.Combine(exePath, "..");
}
}

public static string MonacoDirectory
{
get
{
if (string.IsNullOrEmpty(_monacoDirectory))
{
_monacoDirectory = GetRuntimeMonacoDirectory();
}
string monacoPath = Path.Combine(exePath, "Assets", "Monaco");

return _monacoDirectory;
}
return Directory.Exists(monacoPath) ?
monacoPath :
throw new DirectoryNotFoundException($"Monaco assets directory not found at {monacoPath}");
}

public static JsonDocument GetLanguages()
Expand Down

0 comments on commit 863f7aa

Please sign in to comment.