Skip to content

Commit 8e1af86

Browse files
committed
Cleaned up the remaining changed src files. Avoided ToolPackageDownloader and ToolPackageDownloaderBase since those will be updated in main. Not interested in cleaning up test files.
1 parent 814746d commit 8e1af86

File tree

9 files changed

+169
-215
lines changed

9 files changed

+169
-215
lines changed
Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,33 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
namespace Microsoft.Extensions.EnvironmentAbstractions
4+
namespace Microsoft.Extensions.EnvironmentAbstractions;
5+
6+
public readonly struct FilePath
57
{
6-
public struct FilePath
7-
{
8-
public string Value { get; }
8+
public string Value { get; }
99

10-
/// <summary>
11-
/// Create FilePath to represent an absolute file path. Note it may not exist.
12-
/// </summary>
13-
/// <param name="value">If the value is not rooted. Path.GetFullPath will be called during the constructor.</param>
14-
public FilePath(string value)
10+
/// <summary>
11+
/// Create FilePath to represent an absolute file path. Note: It may not exist.
12+
/// </summary>
13+
/// <param name="value">If the value is not rooted. Path.GetFullPath will be called during the constructor.</param>
14+
public FilePath(string value)
15+
{
16+
if (!Path.IsPathRooted(value))
1517
{
16-
if (!Path.IsPathRooted(value))
17-
{
18-
value = Path.GetFullPath(value);
19-
}
20-
21-
Value = value;
18+
value = Path.GetFullPath(value);
2219
}
2320

24-
public override string ToString()
25-
{
26-
return Value;
27-
}
21+
Value = value;
22+
}
2823

29-
public DirectoryPath GetDirectoryPath()
30-
{
31-
return new DirectoryPath(Path.GetDirectoryName(Value)!);
32-
}
24+
public override string ToString()
25+
{
26+
return Value;
27+
}
28+
29+
public DirectoryPath GetDirectoryPath()
30+
{
31+
return new DirectoryPath(Path.GetDirectoryName(Value)!);
3332
}
3433
}

src/Cli/dotnet/CommandDirectoryContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public static void PerformActionWithBasePath(string basePath, Action action)
3535
$"Calls to {nameof(CommandDirectoryContext)}.{nameof(PerformActionWithBasePath)} cannot be nested.");
3636
}
3737
_basePath = basePath;
38-
Telemetry.Telemetry.CurrentSessionId = null;
38+
Telemetry.Telemetry.s_currentSessionId = null;
3939
try
4040
{
4141
action();

src/Cli/dotnet/Commands/MSBuild/MSBuildForwardingApp.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class MSBuildForwardingApp : CommandBase
1818

1919
private static IEnumerable<string> ConcatTelemetryLogger(IEnumerable<string> argsToForward)
2020
{
21-
if (Telemetry.Telemetry.CurrentSessionId != null)
21+
if (Telemetry.Telemetry.s_currentSessionId != null)
2222
{
2323
try
2424
{
@@ -66,7 +66,7 @@ public ProcessStartInfo GetProcessStartInfo()
6666

6767
private void InitializeRequiredEnvironmentVariables()
6868
{
69-
EnvironmentVariable(TelemetrySessionIdEnvironmentVariableName, Telemetry.Telemetry.CurrentSessionId);
69+
EnvironmentVariable(TelemetrySessionIdEnvironmentVariableName, Telemetry.Telemetry.s_currentSessionId);
7070
}
7171

7272
/// <summary>

src/Cli/dotnet/CommonOptionsFactory.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
#nullable disable
5-
64
using System.CommandLine;
5+
using System.CommandLine.Invocation;
76
using System.CommandLine.Parsing;
87
using Microsoft.DotNet.Cli.Extensions;
98
using Microsoft.DotNet.Cli.Utils;
@@ -25,7 +24,7 @@ internal static class CommonOptionsFactory
2524
Arity = ArgumentArity.Zero
2625
};
2726

28-
internal class SetDiagnosticModeAction(Option<bool> diagnosticOption) : System.CommandLine.Invocation.SynchronousCommandLineAction
27+
internal class SetDiagnosticModeAction(Option<bool> diagnosticOption) : SynchronousCommandLineAction
2928
{
3029
public override int Invoke(ParseResult parseResult)
3130
{
@@ -42,6 +41,7 @@ public override int Invoke(ParseResult parseResult)
4241
Reporter.Reset();
4342
}
4443
}
44+
4545
return 0;
4646
}
4747
}

src/Cli/dotnet/Extensions/ParseResultExtensions.cs

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
#nullable disable
5-
64
using System.CommandLine;
75
using System.CommandLine.Parsing;
86
using System.Diagnostics;
@@ -17,21 +15,21 @@ namespace Microsoft.DotNet.Cli.Extensions;
1715

1816
public static class ParseResultExtensions
1917
{
20-
///<summary>
18+
/// <summary>
2119
/// Finds the command of the parse result and invokes help for that command.
2220
/// If no command is specified, invokes help for the application.
23-
///<summary>
24-
///<remarks>
21+
/// <summary>
22+
/// <remarks>
2523
/// This is accomplished by finding a set of tokens that should be valid and appending a help token
2624
/// to that list, then re-parsing the list of tokens. This is not ideal - either we should have a direct way
2725
/// of invoking help for a ParseResult, or we should eliminate this custom, ad-hoc help invocation by moving
2826
/// more situations that want to show help into Parsing Errors (which trigger help in the default System.CommandLine pipeline)
2927
/// or custom Invocation Middleware, so we can more easily create our version of a HelpResult type.
30-
///</remarks>
28+
/// </remarks>
3129
public static void ShowHelp(this ParseResult parseResult)
3230
{
33-
// take from the start of the list until we hit an option/--/unparsed token
34-
// since commands can have arguments, we must take those as well in order to get accurate help
31+
// Take from the start of the list until we hit an option/--/unparsed token.
32+
// Since commands can have arguments, we must take those as well in order to get accurate help.
3533
var tokenList = parseResult.Tokens.TakeWhile(token => token.Type == TokenType.Argument || token.Type == TokenType.Command || token.Type == TokenType.Directive).Select(t => t.Value).ToList();
3634
tokenList.Add("-h");
3735
Instance.Parse(tokenList).Invoke();
@@ -57,14 +55,17 @@ public static void ShowHelpOrErrorIfAppropriate(this ParseResult parseResult)
5755
}
5856
}
5957

60-
///<summary>Splits a .NET format string by the format placeholders (the {N} parts) to get an array of the literal parts, to be used in message-checking</summary>
58+
/// <summary>
59+
/// Splits a .NET format string by the format placeholders (the {N} parts) to get an array of the literal parts, to be used in message-checking.
60+
/// </summary>
6161
static string[] DistinctFormatStringParts(string formatString)
6262
{
6363
return Regex.Split(formatString, @"{[0-9]+}"); // match the literal '{', followed by any of 0-9 one or more times, followed by the literal '}'
6464
}
6565

66-
67-
/// <summary>given a string and a series of parts, ensures that all parts are present in the string in sequential order</summary>
66+
/// <summary>
67+
/// Given a string and a series of parts, ensures that all parts are present in the string in sequential order.
68+
/// </summary>
6869
static bool ErrorContainsAllParts(ReadOnlySpan<char> error, string[] parts)
6970
{
7071
foreach (var part in parts)
@@ -162,19 +163,12 @@ public static bool DiagOptionPrecedesSubcommand(this string[] args, string subCo
162163
return false;
163164
}
164165

165-
private static string GetSymbolResultValue(ParseResult parseResult, SymbolResult symbolResult) => symbolResult switch
166-
{
167-
CommandResult commandResult => commandResult.Command.Name,
168-
ArgumentResult argResult => argResult.Tokens.FirstOrDefault()?.Value ?? string.Empty,
169-
_ => parseResult.GetResult(DotnetSubCommand)?.GetValueOrDefault<string>()
170-
};
171-
172166
public static bool BothArchAndOsOptionsSpecified(this ParseResult parseResult) =>
173167
(parseResult.HasOption(CommonOptions.ArchitectureOption) ||
174168
parseResult.HasOption(CommonOptions.LongFormArchitectureOption)) &&
175169
parseResult.HasOption(CommonOptions.OperatingSystemOption);
176170

177-
internal static string GetCommandLineRuntimeIdentifier(this ParseResult parseResult)
171+
internal static string? GetCommandLineRuntimeIdentifier(this ParseResult parseResult)
178172
{
179173
return parseResult.HasOption(CommonOptions.RuntimeOptionName) ?
180174
parseResult.GetValue<string>(CommonOptions.RuntimeOptionName) :
@@ -189,7 +183,7 @@ internal static string GetCommandLineRuntimeIdentifier(this ParseResult parseRes
189183

190184
public static bool UsingRunCommandShorthandProjectOption(this ParseResult parseResult)
191185
{
192-
if (parseResult.HasOption(RunCommandParser.PropertyOption) && parseResult.GetValue(RunCommandParser.PropertyOption).Any())
186+
if (parseResult.HasOption(RunCommandParser.PropertyOption) && (parseResult.GetValue(RunCommandParser.PropertyOption)?.Any() ?? false))
193187
{
194188
var projVals = parseResult.GetRunCommandShorthandProjectValues();
195189
if (projVals.Any())
@@ -225,7 +219,7 @@ private static IEnumerable<string> GetRunPropertyOptions(ParseResult parseResult
225219
var propertyValues = propertyOptions.SelectMany(o => o.Tokens.Select(t => t.Value)).ToArray();
226220
return propertyValues;
227221

228-
static Token GetOptionTokenOrDefault(SymbolResult symbolResult)
222+
static Token? GetOptionTokenOrDefault(SymbolResult symbolResult)
229223
{
230224
if (symbolResult is not OptionResult optionResult)
231225
{
@@ -251,7 +245,7 @@ public static void HandleDebugSwitch(this ParseResult parseResult)
251245
/// If you are inside a command handler or 'normal' System.CommandLine code then you don't need this - the parse error handling
252246
/// will have covered these cases.
253247
/// </summary>
254-
public static T SafelyGetValueForOption<T>(this ParseResult parseResult, Option<T> optionToGet)
248+
public static T? SafelyGetValueForOption<T>(this ParseResult parseResult, Option<T> optionToGet)
255249
{
256250
if (parseResult.GetResult(optionToGet) is OptionResult optionResult &&
257251
!parseResult.Errors.Any(e => e.SymbolResult == optionResult))

src/Cli/dotnet/Parser.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
#nullable disable
5-
64
using System.CommandLine;
75
using System.CommandLine.Completions;
86
using System.CommandLine.Invocation;
@@ -105,12 +103,13 @@ public static class Parser
105103
Action = new PrintVersionAction()
106104
};
107105

108-
internal class PrintVersionAction : System.CommandLine.Invocation.SynchronousCommandLineAction
106+
internal class PrintVersionAction : SynchronousCommandLineAction
109107
{
110108
public PrintVersionAction()
111109
{
112110
Terminating = true;
113111
}
112+
114113
public override int Invoke(ParseResult parseResult)
115114
{
116115
CommandLineInfo.PrintVersion();
@@ -124,7 +123,7 @@ public override int Invoke(ParseResult parseResult)
124123
Action = new PrintInfoAction()
125124
};
126125

127-
internal class PrintInfoAction : System.CommandLine.Invocation.SynchronousCommandLineAction
126+
internal class PrintInfoAction : SynchronousCommandLineAction
128127
{
129128
public PrintInfoAction()
130129
{
@@ -220,14 +219,14 @@ private static Command ConfigureCommandLine(RootCommand rootCommand)
220219
return rootCommand;
221220
}
222221

223-
public static Command GetBuiltInCommand(string commandName) =>
222+
public static Command? GetBuiltInCommand(string commandName) =>
224223
Subcommands.FirstOrDefault(c => c.Name.Equals(commandName, StringComparison.OrdinalIgnoreCase));
225224

226225
/// <summary>
227226
/// Implements token-per-line response file handling for the CLI. We use this instead of the built-in S.CL handling
228227
/// to ensure backwards-compatibility with MSBuild.
229228
/// </summary>
230-
public static bool TokenPerLine(string tokenToReplace, out IReadOnlyList<string> replacementTokens, out string errorMessage)
229+
public static bool TokenPerLine(string tokenToReplace, out IReadOnlyList<string>? replacementTokens, out string? errorMessage)
231230
{
232231
var filePath = Path.GetFullPath(tokenToReplace);
233232
if (File.Exists(filePath))
@@ -260,7 +259,7 @@ public static bool TokenPerLine(string tokenToReplace, out IReadOnlyList<string>
260259
ResponseFileTokenReplacer = TokenPerLine
261260
};
262261

263-
internal static int ExceptionHandler(Exception exception, ParseResult parseResult)
262+
internal static int ExceptionHandler(Exception? exception, ParseResult parseResult)
264263
{
265264
if (exception is TargetInvocationException)
266265
{
@@ -280,13 +279,13 @@ internal static int ExceptionHandler(Exception exception, ParseResult parseResul
280279
exception.Message.Red().Bold());
281280
parseResult.ShowHelp();
282281
}
283-
else if (exception.GetType().Name.Equals("WorkloadManifestCompositionException"))
282+
else if (exception is not null && exception.GetType().Name.Equals("WorkloadManifestCompositionException"))
284283
{
285284
Reporter.Error.WriteLine(CommandLoggingContext.IsVerbose ?
286285
exception.ToString().Red().Bold() :
287286
exception.Message.Red().Bold());
288287
}
289-
else
288+
else if (exception is not null)
290289
{
291290
Reporter.Error.Write("Unhandled exception: ".Red().Bold());
292291
Reporter.Error.WriteLine(CommandLoggingContext.IsVerbose ?
@@ -374,7 +373,7 @@ public override void Write(HelpContext context)
374373
}
375374
else if (command.Name.Equals(FormatCommandParser.GetCommand().Name))
376375
{
377-
var arguments = context.ParseResult.GetValue(FormatCommandParser.Arguments);
376+
var arguments = context.ParseResult.GetValue(FormatCommandParser.Arguments) ?? [];
378377
new FormatForwardingApp([.. arguments, .. helpArgs]).Execute();
379378
}
380379
else if (command.Name.Equals(FsiCommandParser.GetCommand().Name))
@@ -401,9 +400,9 @@ public override void Write(HelpContext context)
401400
{
402401
if (command.Name.Equals(ListReferenceCommandParser.GetCommand().Name))
403402
{
404-
Command listCommand = command.Parents.Single() as Command;
403+
Command? listCommand = command.Parents.Single() as Command;
405404

406-
for (int i = 0; i < listCommand.Arguments.Count; i++)
405+
for (int i = 0; i < listCommand?.Arguments.Count; i++)
407406
{
408407
if (listCommand.Arguments[i].Name == CliStrings.SolutionOrProjectArgumentName)
409408
{
@@ -434,6 +433,7 @@ internal PrintCliSchemaAction()
434433
{
435434
Terminating = true;
436435
}
436+
437437
public override int Invoke(ParseResult parseResult)
438438
{
439439
CliSchema.PrintCliSchema(parseResult.CommandResult, parseResult.Configuration.Output, Program.TelemetryClient);

0 commit comments

Comments
 (0)