Skip to content

Commit f5ea9ec

Browse files
authored
Merge pull request #21955 from unoplatform/dev/dr/hrInfo2
Restore add of HR info to avoid roslyn bug
2 parents 63d4321 + ee81d33 commit f5ea9ec

26 files changed

+598
-162
lines changed

src/SourceGenerators/Uno.UI.Tasks/Content/Uno.UI.Tasks.targets

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@
2020
<UsingTask Condition="'$(_IsUnoUISolution)'==''" AssemblyFile="$(UnoUIMSBuildTasksPath)\Uno.UI.Tasks.v0.dll" TaskName="Uno.UI.Tasks.Assets.RetargetAssets_v0" />
2121
<UsingTask Condition="'$(_IsUnoUISolution)'==''" AssemblyFile="$(UnoUIMSBuildTasksPath)\Uno.UI.Tasks.v0.dll" TaskName="Uno.UI.Tasks.RuntimeAssetsSelector.RuntimeAssetsSelectorTask_v0" />
2222
<UsingTask Condition="'$(_IsUnoUISolution)'==''" AssemblyFile="$(UnoUIMSBuildTasksPath)\Uno.UI.Tasks.v0.dll" TaskName="Uno.UI.Tasks.RuntimeAssetsValidator.RuntimeAssetsValidatorTask_v0" />
23+
<UsingTask Condition="'$(_IsUnoUISolution)'==''" AssemblyFile="$(UnoUIMSBuildTasksPath)\Uno.UI.Tasks.v0.dll" TaskName="Uno.UI.Tasks.HotReloadInfo.HotReloadInfoTask_v0" />
2324

2425
<UsingTask Condition="'$(_IsUnoUISolution)'!=''" AssemblyFile="$(UnoUIMSBuildTasksPath)\Uno.UI.Tasks.v0.dll" TaskName="Uno.UI.Tasks.ResourcesGenerator.ResourcesGenerationTask_v0" TaskFactory="TaskHostFactory"/>
2526
<UsingTask Condition="'$(_IsUnoUISolution)'!=''" AssemblyFile="$(UnoUIMSBuildTasksPath)\Uno.UI.Tasks.v0.dll" TaskName="Uno.UI.Tasks.ResourcesGenerator.UpriFeaturesGeneratorTask_v0" TaskFactory="TaskHostFactory"/>
2627
<UsingTask Condition="'$(_IsUnoUISolution)'!=''" AssemblyFile="$(UnoUIMSBuildTasksPath)\Uno.UI.Tasks.v0.dll" TaskName="Uno.UI.Tasks.ResourcesGenerator.UpriSubstitutionsGeneratorTask_v0" TaskFactory="TaskHostFactory" />
2728
<UsingTask Condition="'$(_IsUnoUISolution)'!=''" AssemblyFile="$(UnoUIMSBuildTasksPath)\Uno.UI.Tasks.v0.dll" TaskName="Uno.UI.Tasks.Assets.RetargetAssets_v0" TaskFactory="TaskHostFactory" />
2829
<UsingTask Condition="'$(_IsUnoUISolution)'!=''" AssemblyFile="$(UnoUIMSBuildTasksPath)\Uno.UI.Tasks.v0.dll" TaskName="Uno.UI.Tasks.RuntimeAssetsSelector.RuntimeAssetsSelectorTask_v0" TaskFactory="TaskHostFactory" />
2930
<UsingTask Condition="'$(_IsUnoUISolution)'!=''" AssemblyFile="$(UnoUIMSBuildTasksPath)\Uno.UI.Tasks.v0.dll" TaskName="Uno.UI.Tasks.RuntimeAssetsValidator.RuntimeAssetsValidatorTask_v0" TaskFactory="TaskHostFactory" />
31+
<UsingTask Condition="'$(_IsUnoUISolution)'!=''" AssemblyFile="$(UnoUIMSBuildTasksPath)\Uno.UI.Tasks.v0.dll" TaskName="Uno.UI.Tasks.HotReloadInfo.HotReloadInfoTask_v0" TaskFactory="TaskHostFactory" />
3032

3133
<Target Name="_UnoLangSetup">
3234
<!-- String resources -->
@@ -405,14 +407,11 @@
405407
Condition="'$(_UnoWasmBootstrapVersionString)'!='' and '$(_UnoWasmBootstrapVersionString.Split(`-`)[0])'&lt;'3.0'"/>
406408
</Target>
407409

408-
<!--
409-
Warn when the task is executed in devenv.exe: https://github.com/dotnet/project-system/issues/4494
410-
-->
411-
410+
<!-- Warn when the task is executed in devenv.exe: https://github.com/dotnet/project-system/issues/4494 -->
412411
<UsingTask
413-
TaskName="CheckForDevenv"
414-
TaskFactory="CodeTaskFactory"
415-
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll" >
412+
TaskName="CheckForDevenv"
413+
TaskFactory="CodeTaskFactory"
414+
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll" >
416415
<ParameterGroup />
417416
<Task>
418417
<Reference Include="System.Xml"/>
@@ -429,4 +428,21 @@
429428
</Task>
430429
</UsingTask>
431430

431+
<Target
432+
Name="_GenerateHotReloadInfo"
433+
BeforeTargets="BeforeCompile"
434+
Condition="'$(UnoGenerateHotReloadInfo)' == 'True'">
435+
436+
<HotReloadInfoTask_v0
437+
IntermediateOutputPath="$(IntermediateOutputPath)">
438+
<Output
439+
TaskParameter="GeneratedFiles"
440+
ItemName="_HotReloadInfoTaskGeneratedFiles" />
441+
</HotReloadInfoTask_v0>
442+
443+
<ItemGroup>
444+
<Compile Include="@(_HotReloadInfoTaskGeneratedFiles)" />
445+
</ItemGroup>
446+
</Target>
447+
432448
</Project>
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Runtime.CompilerServices;
5+
using System.Text;
6+
using Microsoft.Build.Framework;
7+
using Microsoft.Build.Utilities;
8+
9+
namespace Uno.UI.Tasks.HotReloadInfo;
10+
11+
public class HotReloadInfoTask_v0 : Microsoft.Build.Utilities.Task
12+
{
13+
[Required]
14+
public string IntermediateOutputPath { get; set; }
15+
16+
[Output]
17+
public ITaskItem[] GeneratedFiles { get; set; }
18+
19+
/// <inheritdoc />
20+
public override bool Execute()
21+
{
22+
var attributePath = Path.Combine(IntermediateOutputPath, "uno.hot-reload.info", "HotReloadInfo.Attribute.g.cs");
23+
var infoPath = Path.Combine(IntermediateOutputPath, "uno.hot-reload.info", "HotReloadInfo.g.cs");
24+
25+
Log.LogMessage($"Generating hot-reload info to : {infoPath}");
26+
27+
try
28+
{
29+
Directory.CreateDirectory(Path.Combine(IntermediateOutputPath, "uno.hot-reload.info"));
30+
31+
Write(attributePath, HotReloadInfoHelper.GenerateAttribute(infoPath));
32+
Write(infoPath, HotReloadInfoHelper.GenerateInfo());
33+
34+
GeneratedFiles =
35+
[
36+
new TaskItem(attributePath, new Dictionary<string, string>
37+
{
38+
{ "Generator", "Uno.UI.Tasks.HotReloadInfo" },
39+
{ "GeneratorVersion", typeof(HotReloadInfoTask_v0).Assembly.GetName().Version.ToString() }
40+
}),
41+
new TaskItem(infoPath, new Dictionary<string, string>
42+
{
43+
{ "Generator", "Uno.UI.Tasks.HotReloadInfo" },
44+
{ "GeneratorVersion", typeof(HotReloadInfoTask_v0).Assembly.GetName().Version.ToString() }
45+
})
46+
];
47+
48+
return true;
49+
}
50+
catch (Exception ex)
51+
{
52+
Log.LogError($"Failed to generate hot-reload info. Details: {ex.Message}");
53+
}
54+
55+
return false;
56+
}
57+
58+
private void Write(string path, string content)
59+
{
60+
using var stream = File.Open(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read);
61+
62+
// First we validate if the content is the same, if so, we don't even touch the file (to avoid unnecessary rebuilds)
63+
using (var reader = new StreamReader(stream, Encoding.UTF8, true, 4096, leaveOpen: true))
64+
{
65+
if (content.Equals(reader.ReadToEnd()))
66+
{
67+
return;
68+
}
69+
}
70+
71+
// Rewrite the content ... from the beginning!
72+
stream.SetLength(0);
73+
stream.Position = 0;
74+
75+
using var writer = new StreamWriter(stream, Encoding.UTF8, 4096, leaveOpen: true);
76+
writer.Write(content);
77+
writer.Flush();
78+
}
79+
}

src/SourceGenerators/Uno.UI.Tasks/Uno.UI.Tasks.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
<Link>Resources\AndroidResourceNameEncoder.cs</Link>
3434
</Compile>
3535
<Compile Include="..\Uno.UI.SourceGenerators\BindableTypeProviders\LinkerHintsHelpers.cs" Link="Helpers\LinkerHintsHelpers.cs" />
36+
<Compile Include="..\..\Uno.UI.RemoteControl\HotReload\HotReloadInfoHelper.cs" Link="HotReloadInfo/%(Filename)%(Extension)" />
3637
</ItemGroup>
3738

3839
<Target Name="_copyUnoTasksBuildTime" AfterTargets="Build">

src/Uno.UI.RemoteControl.Host/IDEChannel/IdeChannelServer.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,11 @@ public IdeChannelServer(ILogger<IdeChannelServer> logger, IOptionsMonitor<IdeCha
5151
/// <inheritdoc />
5252
public event EventHandler<IdeMessage>? MessageFromIde;
5353

54-
/// <inheritdoc />
5554
async Task IIdeChannel.SendToIdeAsync(IdeMessage message, CancellationToken ct)
55+
=> await ((IIdeChannel)this).TrySendToIdeAsync(message, ct);
56+
57+
/// <inheritdoc />
58+
async Task<bool> IIdeChannel.TrySendToIdeAsync(IdeMessage message, CancellationToken ct)
5659
{
5760
await WaitForReady(ct);
5861

@@ -61,16 +64,17 @@ async Task IIdeChannel.SendToIdeAsync(IdeMessage message, CancellationToken ct)
6164
this.Log().LogInformation(
6265
"Received a message {MessageType} to send to the IDE, but there is no connection available for that.",
6366
message.Scope);
67+
68+
return false;
6469
}
6570
else
6671
{
6772
_proxy.SendToIde(message);
6873
ScheduleKeepAlive();
69-
}
7074

71-
await Task.Yield();
75+
return true;
76+
}
7277
}
73-
7478
#endregion
7579

7680
/// <inheritdoc />

src/Uno.UI.RemoteControl.Host/RemoteControlServer.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -647,8 +647,11 @@ await WebSocketHelper.SendFrame(
647647
}
648648
}
649649

650-
public Task SendMessageToIDEAsync(IdeMessage message)
651-
=> _ideChannel.SendToIdeAsync(message, default);
650+
async Task IRemoteControlServer.SendMessageToIDEAsync(IdeMessage message)
651+
=> await TrySendMessageToIDEAsync(message, CancellationToken.None);
652+
653+
public Task<bool> TrySendMessageToIDEAsync(IdeMessage message, CancellationToken ct)
654+
=> _ideChannel.TrySendToIdeAsync(message, ct);
652655

653656
public void Dispose()
654657
{

src/Uno.UI.RemoteControl.Messaging/IRemoteControlServer.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using System.Threading.Tasks;
1+
using System;
2+
using System.Threading;
3+
using System.Threading.Tasks;
24
using Uno.UI.RemoteControl.Messaging.IdeChannel;
35

46
namespace Uno.UI.RemoteControl.Host
@@ -9,6 +11,19 @@ public interface IRemoteControlServer
911

1012
Task SendFrame(IMessage message);
1113

14+
/// <summary>
15+
/// DO NOT USE, prefer the TrySendMessageToIDEAsync
16+
/// </summary>
1217
Task SendMessageToIDEAsync(IdeMessage message);
18+
19+
/// <summary>
20+
/// Attempt to send a message to the IDE, if any connection is established.
21+
/// </summary>
22+
/// <param name="message">The message to send to the IDE.</param>
23+
/// <returns>
24+
/// An asynchronous boolean indicating if the message has been sent to the IDE or not.
25+
/// WARNING: This does NOT indicate that the IDE has processed the message, only that an IDE is listening for messages.
26+
/// </returns>
27+
Task<bool> TrySendMessageToIDEAsync(IdeMessage message, CancellationToken ct);
1328
}
1429
}

src/Uno.UI.RemoteControl.Messaging/IdeChannel/IIdeChannelServer.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,20 @@ public interface IIdeChannel
4949
event EventHandler<IdeMessage>? MessageFromIde;
5050

5151
/// <summary>
52+
/// DO NOT USE - Prefer the TrySendToIdeAsync instead
5253
/// Sends a message to the IDE.
5354
/// </summary>
5455
/// <param name="message">The message to send to the IDE.</param>
5556
/// <param name="ct">CancellationToken to cancel the async operation.</param>
5657
/// <returns>An async operation representing the fact that the message has been sent to the IDE (does not include any form of processing by IDE).</returns>
5758
Task SendToIdeAsync(IdeMessage message, CancellationToken ct);
59+
60+
/// <summary>
61+
/// Sends a message to the IDE.
62+
/// </summary>
63+
/// <param name="message">The message to send to the IDE.</param>
64+
/// <param name="ct">CancellationToken to cancel the async operation.</param>
65+
/// <returns>An async bool representing the fact that the message has been sent to the IDE or not (does not include any form of processing by IDE).</returns>
66+
Task<bool> TrySendToIdeAsync(IdeMessage message, CancellationToken ct);
5867
}
5968
}

0 commit comments

Comments
 (0)