-
Notifications
You must be signed in to change notification settings - Fork 743
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add discovery of dev-server add-ins
- Loading branch information
Showing
8 changed files
with
113 additions
and
22 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
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 |
---|---|---|
@@ -1,26 +1,75 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using System.Linq; | ||
using System.Text.RegularExpressions; | ||
using Microsoft.Extensions.Logging; | ||
using Uno.Extensions; | ||
using Uno.UI.RemoteControl.Helpers; | ||
|
||
namespace Uno.UI.RemoteControl.Host.Extensibility; | ||
|
||
public class AddIns | ||
{ | ||
private static ILogger _log = typeof(AddIns).Log(); | ||
|
||
public static IImmutableList<string> Discover(string solutionFile) | ||
=> ProcessHelper.RunProcess("dotnet", $"build \"{solutionFile}\" /t:GetRemoteControlAddIns /nowarn:MSB4057") switch // Ignore missing target | ||
{ | ||
// 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()) | ||
.Distinct(StringComparer.OrdinalIgnoreCase) | ||
.ToImmutableList(); | ||
|
||
if (targetFrameworks.IsEmpty) | ||
{ | ||
if (_log.IsEnabled(LogLevel.Warning)) | ||
{ | ||
_log.Log(LogLevel.Warning, new Exception(result.error), $"Failed to get target frameworks of solution '{solutionFile}' (cf. inner exception for details)."); | ||
} | ||
|
||
return new ImmutableArray<string>(); | ||
} | ||
|
||
|
||
foreach (var targetFramework in targetFrameworks) | ||
{ | ||
// Note: We ignore the exitCode not being 0: even if flagged as nowarn, we can still get MSB4057 for project that does not have the target GetRemoteControlAddIns | ||
{ error: { Length: > 0 } err } => throw new InvalidOperationException($"Failed to get add-ins for solution '{solutionFile}' (cf. inner exception for details).", new Exception(err)), | ||
var result => GetConfigurationValue(result.output, "RemoteControlAddIns") | ||
?.Split(['\r', '\n', ';', ','], StringSplitOptions.RemoveEmptyEntries) | ||
result = ProcessHelper.RunProcess("dotnet", $"build \"{solutionFile}\" --target:UnoDumpRemoteControlAddIns --verbosity detailed --framework \"{targetFramework}\" -nowarn:MSB4057"); | ||
if (!string.IsNullOrWhiteSpace(result.error)) | ||
{ | ||
if (_log.IsEnabled(LogLevel.Warning)) | ||
{ | ||
_log.Log(LogLevel.Warning, new Exception(result.error), $"Failed to get add-ins for solution '{solutionFile}' for tfm {targetFramework} (cf. inner exception for details)."); | ||
} | ||
|
||
continue; | ||
} | ||
|
||
var addIns = GetConfigurationValue(result.output, "RemoteControlAddIns") | ||
.SelectMany(tfms => tfms.Split(['\r', '\n', ';', ','], StringSplitOptions.RemoveEmptyEntries)) | ||
.Select(tfm => tfm.Trim()) | ||
.Distinct(StringComparer.OrdinalIgnoreCase) | ||
.ToImmutableList() ?? ImmutableList<string>.Empty, | ||
}; | ||
.ToImmutableList(); | ||
|
||
if (!addIns.IsEmpty) | ||
{ | ||
return addIns; | ||
} | ||
} | ||
|
||
if (_log.IsEnabled(LogLevel.Information)) | ||
{ | ||
_log.Log(LogLevel.Information, $"Didn't find any add-ins for solution '{solutionFile}'."); | ||
} | ||
|
||
return ImmutableList<string>.Empty; | ||
} | ||
|
||
private static string? GetConfigurationValue(string msbuildResult, string nodeName) | ||
=> Regex.Match(msbuildResult, $"<{nodeName}>(?<value>.*)</{nodeName}>") is { Success: true } match | ||
? match.Groups["value"].Value | ||
: null; | ||
private static IEnumerable<string> GetConfigurationValue(string msbuildResult, string nodeName) | ||
=> Regex | ||
.Matches(msbuildResult, $"<{nodeName}>(?<value>.*)</{nodeName}>") | ||
.Where(match => match.Success) | ||
.Select(match => match.Groups["value"].Value); | ||
} |
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
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
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
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
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
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