Skip to content

Commit

Permalink
feat: Enable VS non-debugger session updates
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromelaban committed Oct 21, 2023
1 parent 553cbd9 commit 1264913
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ private void InitializeMetadataUpdater(ConfigureServer configureServer)
{
_ = bool.TryParse(_remoteControlServer.GetServerConfiguration("metadata-updates"), out _useRoslynHotReload);

if (_useRoslynHotReload)
if (_useRoslynHotReload || configureServer.EnableMetadataUpdates)
{
CompilationWorkspaceProvider.InitializeRoslyn(Path.GetDirectoryName(configureServer.ProjectPath));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -77,6 +78,24 @@ partial void InitializeMetadataUpdater()
});
}

private bool MetadataUpdatesEnabled
{
get
{
var unoRuntimeIdentifier = GetMSBuildProperty("UnoRuntimeIdentifier");

Check notice on line 85 in src/Uno.UI.RemoteControl/HotReload/ClientHotReloadProcessor.MetadataUpdate.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/Uno.UI.RemoteControl/HotReload/ClientHotReloadProcessor.MetadataUpdate.cs#L85

Remove the unused local variable 'unoRuntimeIdentifier'.
var targetFramework = GetMSBuildProperty("TargetFramework");
var buildingInsideVisualStudio = GetMSBuildProperty("BuildingInsideVisualStudio");

return
buildingInsideVisualStudio.Equals("true", StringComparison.OrdinalIgnoreCase)
&& (

Check warning on line 91 in src/Uno.UI.RemoteControl/HotReload/ClientHotReloadProcessor.MetadataUpdate.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/Uno.UI.RemoteControl/HotReload/ClientHotReloadProcessor.MetadataUpdate.cs#L91

Remove these redundant parentheses.
// As of VS 17.8, when the debugger is not attached, mobile targets can use
// DevServer's hotreload workspace, as visual studio does not enable it on its own.
(!Debugger.IsAttached
&& (targetFramework.Contains("-android") || targetFramework.Contains("-ios"))));
}
}

private string[] GetMetadataUpdateCapabilities()
{
if (Type.GetType(HotReloadAgent.MetadataUpdaterType) is { } type)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,22 @@ private void InitializePartialReload()
_supportsLightweightHotReload =
buildingInsideVisualStudio.Equals("true", StringComparison.OrdinalIgnoreCase)
&& (
// As of VS 17.8, Mobile targets don't invoke MetadataUpdateHandlers
// As of VS 17.8, when the debugger is attached, mobile targets don't invoke MetadataUpdateHandlers
// and both targets are not providing updated types. We simulate parts of this process
// to determine which types have been updated, particularly those with "CreateNewOnMetadataUpdate".
(Debugger.IsAttached
&& (targetFramework.Contains("-android") || targetFramework.Contains("-ios")))

targetFramework.Contains("-android")
|| targetFramework.Contains("-ios")
// WebAssembly does not support sending updated types, and does not support debugger based hot reload.
|| (unoRuntimeIdentifier?.Equals("WebAssembly", StringComparison.OrdinalIgnoreCase) ?? false));

if (this.Log().IsEnabled(LogLevel.Trace))
{
this.Log().Trace($"Partial Hot Reload: Enabled:{_supportsLightweightHotReload} unoRuntimeIdentifier:{unoRuntimeIdentifier} targetFramework:{targetFramework} buildingInsideVisualStudio:{targetFramework}");
this.Log().Trace($"Partial Hot Reload Enabled:{_supportsLightweightHotReload} " +
$"unoRuntimeIdentifier:{unoRuntimeIdentifier} " +
$"targetFramework:{targetFramework} " +
$"buildingInsideVisualStudio:{targetFramework}" +
$"debuggerAttached: {Debugger.IsAttached}");
}

_mappedTypes = _supportsLightweightHotReload
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ private async Task ConfigureServer()
}
}

ConfigureServer message = new(_projectPath, _xamlPaths, GetMetadataUpdateCapabilities(), config.MSBuildProperties);
_msbuildProperties = Messages.ConfigureServer.BuildMSBuildProperties(config.MSBuildProperties);

await _rcClient.SendMessage(message);
ConfigureServer message = new(_projectPath, _xamlPaths, GetMetadataUpdateCapabilities(), MetadataUpdatesEnabled, config.MSBuildProperties);

_msbuildProperties = message.MSBuildProperties;
await _rcClient.SendMessage(message);

InitializePartialReload();
}
Expand Down
32 changes: 16 additions & 16 deletions src/Uno.UI.RemoteControl/HotReload/Messages/ConfigureServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ class ConfigureServer : IMessage
public const string Name = nameof(ConfigureServer);
private Dictionary<string, string>? _msbuildPropertiesCache;

public ConfigureServer(string projectPath, string[] xamlPaths, string[] metadataUpdateCapabilities, string[] msbuildPropertiesRaw)
public ConfigureServer(string projectPath, string[] xamlPaths, string[] metadataUpdateCapabilities, bool enableMetadataUpdates, string[] msbuildPropertiesRaw)
{
ProjectPath = projectPath;
XamlPaths = xamlPaths;
MetadataUpdateCapabilities = metadataUpdateCapabilities;
MSBuildPropertiesRaw = msbuildPropertiesRaw;
EnableMetadataUpdates = enableMetadataUpdates;
}

public string ProjectPath { get; set; }
Expand All @@ -25,28 +26,27 @@ public ConfigureServer(string projectPath, string[] xamlPaths, string[] metadata

public string[] MetadataUpdateCapabilities { get; set; }

public bool EnableMetadataUpdates { get; set; }

public string Scope => HotReloadConstants.HotReload;

string IMessage.Name => Name;

public Dictionary<string, string> MSBuildProperties
public Dictionary<string, string> MSBuildProperties
=> _msbuildPropertiesCache ??= BuildMSBuildProperties(MSBuildPropertiesRaw);

public static Dictionary<string, string> BuildMSBuildProperties(string[] rawMSBuildProperties)
{
get
var msbuildPropertiesCache = new Dictionary<string, string>();

foreach (var property in rawMSBuildProperties)
{
if (_msbuildPropertiesCache is null)
{
_msbuildPropertiesCache = new Dictionary<string, string>();

foreach (var property in MSBuildPropertiesRaw)
{
var firstEqual = property.IndexOf('=');
var split = new[] { property.Substring(0, firstEqual), property.Substring(firstEqual + 1) };
_msbuildPropertiesCache.Add(split[0], Encoding.UTF8.GetString(Convert.FromBase64String(split[1])));
}
}

return _msbuildPropertiesCache;
var firstEqual = property.IndexOf('=');
var split = new[] { property.Substring(0, firstEqual), property.Substring(firstEqual + 1) };
msbuildPropertiesCache.Add(split[0], Encoding.UTF8.GetString(Convert.FromBase64String(split[1])));
}

return msbuildPropertiesCache;
}
}
}

0 comments on commit 1264913

Please sign in to comment.