-
Notifications
You must be signed in to change notification settings - Fork 373
/
WorkloadConstraintFactory.cs
123 lines (104 loc) · 6 KB
/
WorkloadConstraintFactory.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.TemplateEngine.Abstractions;
using Microsoft.TemplateEngine.Abstractions.Components;
using Microsoft.TemplateEngine.Abstractions.Constraints;
using Microsoft.TemplateEngine.Utils;
namespace Microsoft.TemplateEngine.Edge.Constraints
{
public sealed class WorkloadConstraintFactory : ITemplateConstraintFactory
{
Guid IIdentifiedComponent.Id { get; } = Guid.Parse("{F8BA5B13-7BD6-47C8-838C-66626526817B}");
string ITemplateConstraintFactory.Type => "workload";
async Task<ITemplateConstraint> ITemplateConstraintFactory.CreateTemplateConstraintAsync(IEngineEnvironmentSettings environmentSettings, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
// need to await due to lack of covariance on Tasks
return await WorkloadConstraint.CreateAsync(environmentSettings, this, cancellationToken).ConfigureAwait(false);
}
internal class WorkloadConstraint : ConstraintBase
{
private readonly HashSet<string> _installedWorkloads;
private readonly string _installedWorkloadsString;
private readonly Func<IReadOnlyList<string>, string> _remedySuggestionFactory;
private WorkloadConstraint(
IEngineEnvironmentSettings environmentSettings,
ITemplateConstraintFactory factory,
IReadOnlyList<WorkloadInfo> workloads,
Func<IReadOnlyList<string>, string> remedySuggestionFactory)
: base(environmentSettings, factory)
{
_installedWorkloads = new HashSet<string>(workloads.Select(w => w.Id), StringComparer.InvariantCultureIgnoreCase);
_installedWorkloadsString = workloads.Select(w => $"{w.Id} \"{w.Description}\"").ToCsvString();
_remedySuggestionFactory = remedySuggestionFactory;
}
public override string DisplayName => LocalizableStrings.WorkloadConstraint_Name;
internal static async Task<WorkloadConstraint> CreateAsync(IEngineEnvironmentSettings environmentSettings, ITemplateConstraintFactory factory, CancellationToken cancellationToken)
{
(IReadOnlyList<WorkloadInfo> workloads, Func<IReadOnlyList<string>, string> remedySuggestionFactory) =
await ExtractWorkloadInfoAsync(
environmentSettings.Components.OfType<IWorkloadsInfoProvider>(),
environmentSettings.Host.Logger,
cancellationToken).ConfigureAwait(false);
return new WorkloadConstraint(environmentSettings, factory, workloads, remedySuggestionFactory);
}
protected override TemplateConstraintResult EvaluateInternal(string? args)
{
IReadOnlyList<string> supportedWorkloads = ParseArgs(args).ToList();
bool isSupportedWorkload = supportedWorkloads.Any(_installedWorkloads.Contains);
if (isSupportedWorkload)
{
return TemplateConstraintResult.CreateAllowed(this);
}
return TemplateConstraintResult.CreateRestricted(
this,
string.Format(
LocalizableStrings.WorkloadConstraint_Message_Restricted,
string.Join(", ", supportedWorkloads),
string.Join(", ", _installedWorkloadsString)),
_remedySuggestionFactory(supportedWorkloads));
}
//supported configuration:
// "args": "maui-mobile" // workload expected
// "args": [ "maui-mobile", "maui-desktop" ] // any of workloads expected
private static IEnumerable<string> ParseArgs(string? args)
{
return args.ParseArrayOfConstraintStrings();
}
private static async Task<(IReadOnlyList<WorkloadInfo> Workloads, Func<IReadOnlyList<string>, string> RemedySuggestionFactory)> ExtractWorkloadInfoAsync(IEnumerable<IWorkloadsInfoProvider> workloadsInfoProviders, ILogger logger, CancellationToken token)
{
List<IWorkloadsInfoProvider> providers = workloadsInfoProviders.ToList();
List<WorkloadInfo>? workloads = null;
if (providers.Count == 0)
{
throw new ConfigurationException(LocalizableStrings.WorkloadConstraint_Error_MissingProvider);
}
if (providers.Count > 1)
{
throw new ConfigurationException(
string.Format(LocalizableStrings.WorkloadConstraint_Error_MismatchedProviders, providers.Select(p => p.Id).ToCsvString()));
}
token.ThrowIfCancellationRequested();
IEnumerable<WorkloadInfo> currentProviderWorkloads = await providers[0].GetInstalledWorkloadsAsync(token).ConfigureAwait(false);
workloads = currentProviderWorkloads.ToList();
if (workloads.Select(w => w.Id).HasDuplicates(StringComparer.InvariantCultureIgnoreCase))
{
logger.LogWarning(string.Format(
LocalizableStrings.WorkloadConstraint_Warning_DuplicateWorkloads,
workloads.Select(w => w.Id).GetDuplicates(StringComparer.InvariantCultureIgnoreCase).ToCsvString()));
workloads = workloads
.GroupBy(w => w.Id, StringComparer.InvariantCultureIgnoreCase)
.Select(g => g.First())
.ToList();
}
return (workloads, providers[0].ProvideConstraintRemedySuggestion);
}
}
}
}