-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add ability to use local file system to update file
- Loading branch information
Showing
9 changed files
with
368 additions
and
191 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
src/Uno.UI.RuntimeTests.Engine.Library/Library/Helpers/HotReloadHelper.DevServer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
#if !UNO_RUNTIMETESTS_DISABLE_LIBRARY && HAS_UNO_DEVSERVER // Set as soon as the DevServer package is referenced, cf. Uno.UI.RuntimeTests.Engine.targets | ||
#nullable enable | ||
|
||
#if !IS_UNO_RUNTIMETEST_PROJECT | ||
#pragma warning disable | ||
#endif | ||
#pragma warning disable CA1848 // Log perf | ||
|
||
using System; | ||
using System.Collections; | ||
using System.IO; | ||
using System.Net.WebSockets; | ||
using System.Reflection; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Logging; | ||
using Uno.UI.RuntimeTests.Internal.Helpers; | ||
using Uno.UI.RemoteControl; // DevServer | ||
using Uno.UI.RemoteControl.HotReload; // DevServer | ||
using Uno.UI.RemoteControl.HotReload.Messages; // DevServer | ||
using Uno.UI.RemoteControl.HotReload.MetadataUpdater; // DevServer | ||
|
||
namespace Uno.UI.RuntimeTests; | ||
|
||
partial class HotReloadHelper | ||
{ | ||
static partial void TryUseDevServerFileUpdater() | ||
=> Use(new DevServerUpdater()); | ||
|
||
partial record FileEdit | ||
{ | ||
public UpdateFile ToMessage() | ||
=> new UpdateFile | ||
{ | ||
FilePath = FilePath, | ||
OldText = OldText, | ||
NewText = NewText | ||
}; | ||
} | ||
|
||
private class DevServerUpdater : IFileUpdater | ||
{ | ||
/// <inheritdoc /> | ||
public async ValueTask EnsureReady(CancellationToken ct) | ||
{ | ||
_log.LogTrace("Getting dev-server ready ..."); | ||
|
||
if (RemoteControlClient.Instance is null) | ||
{ | ||
throw new InvalidOperationException("Dev server is not available."); | ||
} | ||
|
||
var timeout = Task.Delay(ConnectionTimeout, ct); | ||
if (await Task.WhenAny(timeout, RemoteControlClient.Instance.WaitForConnection(ct)) == timeout) | ||
{ | ||
throw new TimeoutException( | ||
"Timeout while waiting for the app to connect to the dev-server. " | ||
+ "This usually indicates that the dev-server is not running on the expected combination of hots and port" | ||
+ "(For runtime-tests run in secondary app instance, the server should be listening for connection on " | ||
+ $"{Environment.GetEnvironmentVariable("UNO_DEV_SERVER_HOST")}:{Environment.GetEnvironmentVariable("UNO_DEV_SERVER_PORT")})."); | ||
} | ||
|
||
_log.LogTrace("Client connected, waiting for dev-server to load the workspace (i.e. initializing roslyn with the solution) ..."); | ||
|
||
var processors = typeof(RemoteControlClient).GetField("_processors", BindingFlags.Instance | BindingFlags.NonPublic)?.GetValue(RemoteControlClient.Instance) as IDictionary ?? throw new InvalidOperationException("Processors is null"); | ||
var processor = processors["hotreload"] as ClientHotReloadProcessor ?? throw new InvalidOperationException("HotReloadProcessor is null"); | ||
var hotReloadReady = typeof(ClientHotReloadProcessor).GetProperty("HotReloadWorkspaceLoaded", BindingFlags.Instance | BindingFlags.NonPublic)?.GetValue(processor) as Task ?? throw new IOException("HotReloadWorkspaceLoaded is null"); | ||
|
||
timeout = Task.Delay(WorkspaceTimeout, ct); | ||
if (await Task.WhenAny(timeout, hotReloadReady) == timeout) | ||
{ | ||
throw new TimeoutException( | ||
"Timeout while waiting for hot reload workspace to be loaded. " | ||
+ "This usually indicates that the dev-server failed to load the solution, you will find more info in dev-server logs " | ||
+ "(output in main app logs with [DEV_SERVER] prefix for runtime-tests run in secondary app instance)."); | ||
} | ||
|
||
_log.LogTrace("Workspace is ready on dev-server, sending file update request ..."); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public async ValueTask<ReConnect?> Apply(FileEdit edition, CancellationToken ct) | ||
{ | ||
try | ||
{ | ||
await RemoteControlClient.Instance!.SendMessage(edition.ToMessage()); | ||
|
||
return null; | ||
} | ||
catch (WebSocketException) | ||
{ | ||
return ct => | ||
{ | ||
_log.LogWarning($"WAITING {RemoteControlClient.Instance!.ConnectionRetryInterval:g} to let client reconnect before retry**."); | ||
return Task.Delay(RemoteControlClient.Instance.ConnectionRetryInterval + TimeSpan.FromMilliseconds(100), ct); | ||
}; | ||
} | ||
} | ||
} | ||
} | ||
#endif |
48 changes: 48 additions & 0 deletions
48
src/Uno.UI.RuntimeTests.Engine.Library/Library/Helpers/HotReloadHelper.Local.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#if !UNO_RUNTIMETESTS_DISABLE_LIBRARY | ||
|
||
#if !IS_UNO_RUNTIMETEST_PROJECT | ||
#pragma warning disable | ||
#endif | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel; | ||
using System.IO; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Uno.UI.RuntimeTests; | ||
|
||
partial class HotReloadHelper | ||
{ | ||
static partial void TryUseLocalFileUpdater() | ||
=> Use(new LocalFileUpdater()); | ||
|
||
[EditorBrowsable(EditorBrowsableState.Advanced)] | ||
public static void UseLocalFileUpdater() | ||
=> Use(new LocalFileUpdater()); | ||
|
||
private class LocalFileUpdater : IFileUpdater | ||
{ | ||
/// <inheritdoc /> | ||
public async ValueTask EnsureReady(CancellationToken ct) { } | ||
|
||
/// <inheritdoc /> | ||
public async ValueTask<ReConnect?> Apply(FileEdit edition, CancellationToken ct) | ||
{ | ||
if (!File.Exists(edition.FilePath)) | ||
{ | ||
throw new InvalidOperationException($"Source file {edition.FilePath} does not exist!"); | ||
} | ||
|
||
var originalContent = await File.ReadAllTextAsync(edition.FilePath, ct); | ||
var updatedContent = originalContent.Replace(edition.OldText, edition.NewText); | ||
|
||
await File.WriteAllTextAsync(edition.FilePath, updatedContent, ct); | ||
|
||
return null; | ||
} | ||
} | ||
} | ||
#endif |
28 changes: 28 additions & 0 deletions
28
...o.UI.RuntimeTests.Engine.Library/Library/Helpers/HotReloadHelper.MetadataUpdateHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#if !UNO_RUNTIMETESTS_DISABLE_LIBRARY | ||
|
||
#if !IS_UNO_RUNTIMETEST_PROJECT | ||
#pragma warning disable | ||
#endif | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using Uno.UI.RuntimeTests; | ||
|
||
[assembly:System.Reflection.Metadata.MetadataUpdateHandlerAttribute(typeof(HotReloadHelper.MetadataUpdateHandler))] | ||
Check failure on line 12 in src/Uno.UI.RuntimeTests.Engine.Library/Library/Helpers/HotReloadHelper.MetadataUpdateHandler.cs GitHub Actions / build
Check failure on line 12 in src/Uno.UI.RuntimeTests.Engine.Library/Library/Helpers/HotReloadHelper.MetadataUpdateHandler.cs GitHub Actions / build
|
||
|
||
namespace Uno.UI.RuntimeTests; | ||
|
||
partial class HotReloadHelper | ||
{ | ||
internal static class MetadataUpdateHandler | ||
{ | ||
public static event EventHandler? MetadataUpdated; | ||
|
||
internal static void UpdateApplication(Type[]? types) | ||
{ | ||
MetadataUpdated?.Invoke(null, EventArgs.Empty); | ||
} | ||
} | ||
} | ||
#endif |
31 changes: 0 additions & 31 deletions
31
src/Uno.UI.RuntimeTests.Engine.Library/Library/Helpers/HotReloadHelper.Mocks.cs
This file was deleted.
Oops, something went wrong.
27 changes: 27 additions & 0 deletions
27
src/Uno.UI.RuntimeTests.Engine.Library/Library/Helpers/HotReloadHelper.NotSupported.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#if !UNO_RUNTIMETESTS_DISABLE_LIBRARY | ||
|
||
#if !IS_UNO_RUNTIMETEST_PROJECT | ||
#pragma warning disable | ||
#endif | ||
|
||
using System; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Uno.UI.RuntimeTests; | ||
|
||
public static partial class HotReloadHelper | ||
{ | ||
private class NotSupported : IFileUpdater | ||
{ | ||
/// <inheritdoc /> | ||
public ValueTask EnsureReady(CancellationToken ct) | ||
=> throw new NotSupportedException("Source code file edition is not supported on this platform."); | ||
|
||
/// <inheritdoc /> | ||
public ValueTask<ReConnect?> Apply(FileEdit edition, CancellationToken ct) | ||
=> throw new NotSupportedException("Source code file edition is not supported on this platform."); | ||
} | ||
} | ||
#endif |
Oops, something went wrong.