-
Notifications
You must be signed in to change notification settings - Fork 414
Expand file tree
/
Copy pathLaunchProfile.cs
More file actions
187 lines (164 loc) · 8.03 KB
/
LaunchProfile.cs
File metadata and controls
187 lines (164 loc) · 8.03 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE.md file in the project root for more information.
using Microsoft.VisualStudio.Threading;
namespace Microsoft.VisualStudio.ProjectSystem.Debug
{
/// <summary>
/// Represents one launch profile read from the launchSettings file.
/// </summary>
internal class LaunchProfile : ILaunchProfile2, IPersistOption
{
public static LaunchProfile Clone(ILaunchProfile profile)
{
// LaunchProfile is immutable and doesn't need to be cloned.
if (profile is LaunchProfile lp)
{
return lp;
}
// Unknown implementation. Make a defensive copy to a new immutable instance.
return new LaunchProfile(
name: profile.Name,
executablePath: profile.ExecutablePath,
commandName: profile.CommandName,
commandLineArgs: profile.CommandLineArgs,
workingDirectory: profile.WorkingDirectory,
launchBrowser: profile.LaunchBrowser,
launchUrl: profile.LaunchUrl,
environmentVariables: profile.FlattenEnvironmentVariables(),
otherSettings: profile.FlattenOtherSettings(),
doNotPersist: profile.IsInMemoryObject());
}
/// <summary>
/// Creates a copy of <paramref name="profile"/> in which tokens are replaced via <paramref name="replaceAsync"/>.
/// </summary>
/// <remarks>
/// Intended to replace tokens such as environment variables and MSBuild properties.
/// </remarks>
/// <param name="profile">The source profile to copy from.</param>
/// <param name="replaceAsync">A function that performs token substitution.</param>
/// <returns>A profile with tokens substituted.</returns>
internal static async Task<LaunchProfile> ReplaceTokensAsync(ILaunchProfile profile, Func<string, Task<string>> replaceAsync)
{
return new(
name: profile.Name,
commandName: profile.CommandName,
executablePath: await ReplaceOrNullAsync(profile.ExecutablePath),
commandLineArgs: await ReplaceOrNullAsync(profile.CommandLineArgs),
workingDirectory: await ReplaceOrNullAsync(profile.WorkingDirectory),
launchBrowser: profile.LaunchBrowser,
launchUrl: await ReplaceOrNullAsync(profile.LaunchUrl),
doNotPersist: profile.IsInMemoryObject(),
environmentVariables: await GetEnvironmentVariablesAsync(),
otherSettings: await GetOtherSettingsAsync());
Task<string?> ReplaceOrNullAsync(string? s)
{
if (Strings.IsNullOrWhiteSpace(s))
{
return TaskResult.Null<string>();
}
return replaceAsync(s)!;
}
Task<ImmutableArray<(string Key, string Value)>> GetEnvironmentVariablesAsync()
{
return profile switch
{
ILaunchProfile2 profile2 => ReplaceValuesAsync(profile2.EnvironmentVariables, replaceAsync),
_ => ReplaceValuesAsync(profile.FlattenEnvironmentVariables(), replaceAsync)
};
}
Task<ImmutableArray<(string Key, object Value)>> GetOtherSettingsAsync()
{
return profile switch
{
ILaunchProfile2 profile2 => ReplaceValuesAsync(profile2.OtherSettings, ReplaceIfStringAsync),
_ => ReplaceValuesAsync(profile.FlattenOtherSettings(), ReplaceIfStringAsync)
};
async Task<object> ReplaceIfStringAsync(object o)
{
return o switch
{
string s => await replaceAsync(s),
_ => o
};
}
}
static async Task<ImmutableArray<(string Key, T Value)>> ReplaceValuesAsync<T>(ImmutableArray<(string Key, T Value)> source, Func<T, Task<T>> replaceAsync)
where T : class
{
// We will only allocate a new array if a substituion is made
ImmutableArray<(string, T)>.Builder? builder = null;
for (int index = 0; index < source.Length; index++)
{
(string key, T value) = source[index];
T replaced = await replaceAsync(value);
if (!ReferenceEquals(value, replaced))
{
// The value had at least one token substitution.
if (builder is null)
{
// Init the builder.
builder = ImmutableArray.CreateBuilder<(string, T)>(source.Length);
if (index != 0)
{
// Copy any unsubstituted values up until this point.
builder.AddRange(source, index);
}
}
}
builder?.Add((key, (T)replaced));
}
// Return the source unchanged if there were no substitutions made.
return builder?.MoveToImmutable() ?? source;
}
}
public LaunchProfile(
string? name,
string? commandName,
string? executablePath = null,
string? commandLineArgs = null,
string? workingDirectory = null,
bool launchBrowser = false,
string? launchUrl = null,
bool doNotPersist = false,
ImmutableArray<(string Key, string Value)> environmentVariables = default,
ImmutableArray<(string Key, object Value)> otherSettings = default)
{
Name = name;
CommandName = commandName;
ExecutablePath = executablePath;
CommandLineArgs = commandLineArgs;
WorkingDirectory = workingDirectory;
LaunchBrowser = launchBrowser;
LaunchUrl = launchUrl;
DoNotPersist = doNotPersist;
EnvironmentVariables = environmentVariables.IsDefault
? ImmutableArray<(string Key, string Value)>.Empty
: environmentVariables;
OtherSettings = otherSettings.IsDefault
? ImmutableArray<(string Key, object Value)>.Empty
: otherSettings;
}
public string? Name { get; }
public string? CommandName { get; }
public string? ExecutablePath { get; }
public string? CommandLineArgs { get; }
public string? WorkingDirectory { get; }
public bool LaunchBrowser { get; }
public string? LaunchUrl { get; }
public bool DoNotPersist { get; }
public ImmutableArray<(string Key, string Value)> EnvironmentVariables { get; }
public ImmutableArray<(string Key, object Value)> OtherSettings { get; }
ImmutableDictionary<string, string>? ILaunchProfile.EnvironmentVariables => EnvironmentVariables.ToImmutableDictionary(pairs => pairs.Key, pairs => pairs.Value, StringComparers.EnvironmentVariableNames);
ImmutableDictionary<string, object>? ILaunchProfile.OtherSettings => OtherSettings.ToImmutableDictionary(pairs => pairs.Key, pairs => pairs.Value, StringComparers.LaunchProfileProperties);
/// <summary>
/// Compares two profile names. Using this function ensures case comparison consistency
/// </summary>
public static bool IsSameProfileName(string? name1, string? name2)
{
return string.Equals(name1, name2, StringComparisons.LaunchProfileNames);
}
public override string ToString()
{
return $"Name={Name ?? "<null>"}, Command={CommandName ?? "<null>"}";
}
}
}