Skip to content

Commit

Permalink
Merge pull request #18733 from unoplatform/dev/dr/devSrvAddIns2
Browse files Browse the repository at this point in the history
fix: Use temp file for AddIn discovery instead of output parsing
  • Loading branch information
jeromelaban authored Nov 8, 2024
2 parents 1ee3603 + 4151902 commit da7ca59
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 23 deletions.
6 changes: 6 additions & 0 deletions src/Uno.Sdk/targets/Uno.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,12 @@
It is useful to determine all target frameworks used by all projects of a solution.
-->
<Message Text="&lt;TargetFrameworks&gt;$(TargetFramework);$(TargetFrameworks)&lt;/TargetFrameworks&gt;" Importance="High" />
<WriteLinesToFile
Condition="$(UnoDumpTargetFrameworksTargetFile) != ''"
File="$(UnoDumpTargetFrameworksTargetFile)"
Lines="$(TargetFramework);$(TargetFrameworks)"
Overwrite="false"
Encoding="Unicode"/>
</Target>

<Import Project="Uno.SingleProject.VS.Build.targets"
Expand Down
55 changes: 32 additions & 23 deletions src/Uno.UI.RemoteControl.Host/Extensibility/AddIns.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Text;
using Microsoft.Extensions.Logging;
using Uno.Extensions;
using Uno.UI.RemoteControl.Helpers;
Expand All @@ -15,14 +16,9 @@ public class AddIns

public static IImmutableList<string> Discover(string solutionFile)
{
// Note: With .net 9 we need to specify --verbosity detailed to get messages with High importance.
var result = ProcessHelper.RunProcess("dotnet", $"build \"{solutionFile}\" --target:UnoDumpTargetFrameworks --verbosity detailed");
var targetFrameworks = GetConfigurationValue(result.output ?? "", "TargetFrameworks")
.SelectMany(tfms => tfms.Split(['\r', '\n', ';', ','], StringSplitOptions.RemoveEmptyEntries))
.Select(tfm => tfm.Trim())
.Where(tfm => tfm is { Length: > 0 })
.Distinct(StringComparer.OrdinalIgnoreCase)
.ToImmutableList();
var tmp = Path.GetTempFileName();
var result = ProcessHelper.RunProcess("dotnet", $"build \"{solutionFile}\" --target:UnoDumpTargetFrameworks \"-p:UnoDumpTargetFrameworksTargetFile={tmp}\" --verbosity quiet");
var targetFrameworks = Read(tmp);

if (targetFrameworks.IsEmpty)
{
Expand All @@ -37,7 +33,8 @@ public static IImmutableList<string> Discover(string solutionFile)

foreach (var targetFramework in targetFrameworks)
{
result = ProcessHelper.RunProcess("dotnet", $"build \"{solutionFile}\" --target:UnoDumpRemoteControlAddIns --verbosity detailed --framework \"{targetFramework}\" -nowarn:MSB4057");
tmp = Path.GetTempFileName();
result = ProcessHelper.RunProcess("dotnet", $"build \"{solutionFile}\" --target:UnoDumpRemoteControlAddIns \"-p:UnoDumpRemoteControlAddInsTargetFile={tmp}\" --verbosity quiet --framework \"{targetFramework}\" -nowarn:MSB4057");
if (!string.IsNullOrWhiteSpace(result.error))
{
if (_log.IsEnabled(LogLevel.Warning))
Expand All @@ -48,13 +45,7 @@ public static IImmutableList<string> Discover(string solutionFile)
continue;
}

var addIns = GetConfigurationValue(result.output, "RemoteControlAddIns")
.SelectMany(tfms => tfms.Split(['\r', '\n', ';', ','], StringSplitOptions.RemoveEmptyEntries))
.Select(tfm => tfm.Trim())
.Where(tfm => tfm is { Length: > 0 })
.Distinct(StringComparer.OrdinalIgnoreCase)
.ToImmutableList();

var addIns = Read(tmp);
if (!addIns.IsEmpty)
{
return addIns;
Expand All @@ -69,9 +60,27 @@ public static IImmutableList<string> Discover(string solutionFile)
return ImmutableArray<string>.Empty;
}

private static IEnumerable<string> GetConfigurationValue(string msbuildResult, string nodeName)
=> Regex
.Matches(msbuildResult, $"<{nodeName}>(?<value>[^\\<\\>]*)</{nodeName}>", RegexOptions.Singleline)
.Where(match => match.Success)
.Select(match => match.Groups["value"].Value);
private static ImmutableList<string> Read(string file)
{
var values = ImmutableList<string>.Empty;
try
{
values = File
.ReadAllLines(file, Encoding.Unicode)
.SelectMany(line => line.Split(['\r', '\n', ';', ','], StringSplitOptions.RemoveEmptyEntries))
.Select(value => value.Trim())
.Where(value => value is { Length: > 0 })
.Distinct(StringComparer.OrdinalIgnoreCase)
.ToImmutableList();
}
catch { }

try
{
File.Delete(file);
}
catch { }

return values;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@

<Target Name="UnoDumpRemoteControlAddIns">
<Message Text="&lt;RemoteControlAddIns&gt;@(UnoRemoteControlAddIns)&lt;/RemoteControlAddIns&gt;" Importance="High" />
<WriteLinesToFile
Condition="$(UnoDumpRemoteControlAddInsTargetFile) != ''"
File="$(UnoDumpRemoteControlAddInsTargetFile)"
Lines="@(UnoRemoteControlAddIns)"
Overwrite="false"
Encoding="Unicode"/>
</Target>

<!-- .NET 7 and earlier compatibility for .NET8 msbuild CLI `getProperty` equivalent -->
Expand Down

0 comments on commit da7ca59

Please sign in to comment.