From ea5046591cc5e485ae9210c2174c2eeb99299d41 Mon Sep 17 00:00:00 2001 From: Dan Moseley Date: Sun, 27 Nov 2022 15:05:27 -0700 Subject: [PATCH] Update to net46 --- .../Abstractions/ValueParserProvider.cs | 2 +- .../Attributes/CommandAttribute.cs | 4 +-- .../VersionOptionFromMemberAttribute.cs | 2 +- .../CommandLineApplication.Execute.cs | 6 ++--- .../CommandLineApplication.cs | 2 +- .../OptionAttributeConventionBase.cs | 2 +- .../HelpText/DefaultHelpTextGenerator.cs | 4 +-- src/CommandLineUtils/IO/Pager.cs | 6 ++--- src/CommandLineUtils/Internal/Util.cs | 25 ------------------- .../Internal/ValueParsers/HashSetParser.cs | 2 +- ...cMaster.Extensions.CommandLineUtils.csproj | 4 +-- .../Properties/NullabilityHelpers.cs | 2 +- src/CommandLineUtils/Utilities/DotNetExe.cs | 17 +++++++++---- .../ExecuteMethodConventionTests.cs | 2 +- .../OptionAttributeTests.cs | 2 +- 15 files changed, 32 insertions(+), 50 deletions(-) delete mode 100644 src/CommandLineUtils/Internal/Util.cs diff --git a/src/CommandLineUtils/Abstractions/ValueParserProvider.cs b/src/CommandLineUtils/Abstractions/ValueParserProvider.cs index 8b231ac5..07ff412a 100644 --- a/src/CommandLineUtils/Abstractions/ValueParserProvider.cs +++ b/src/CommandLineUtils/Abstractions/ValueParserProvider.cs @@ -61,7 +61,7 @@ private static readonly MethodInfo s_getParserGeneric public IValueParser GetParser(Type type) { var method = s_getParserGeneric.MakeGenericMethod(type); - return (IValueParser)method.Invoke(this, Util.EmptyArray()); + return (IValueParser)method.Invoke(this, Array.Empty()); } /// diff --git a/src/CommandLineUtils/Attributes/CommandAttribute.cs b/src/CommandLineUtils/Attributes/CommandAttribute.cs index f4800c2a..be789853 100644 --- a/src/CommandLineUtils/Attributes/CommandAttribute.cs +++ b/src/CommandLineUtils/Attributes/CommandAttribute.cs @@ -14,7 +14,7 @@ namespace McMaster.Extensions.CommandLineUtils [AttributeUsage(AttributeTargets.Class)] public sealed class CommandAttribute : Attribute { - private string[] _names = Util.EmptyArray(); + private string[] _names = Array.Empty(); /// /// Initializes a new . @@ -55,7 +55,7 @@ public string? Name } else { - _names = Util.EmptyArray(); + _names = Array.Empty(); } } } diff --git a/src/CommandLineUtils/Attributes/VersionOptionFromMemberAttribute.cs b/src/CommandLineUtils/Attributes/VersionOptionFromMemberAttribute.cs index 0d90145c..e09e53e3 100644 --- a/src/CommandLineUtils/Attributes/VersionOptionFromMemberAttribute.cs +++ b/src/CommandLineUtils/Attributes/VersionOptionFromMemberAttribute.cs @@ -60,7 +60,7 @@ internal CommandOption Configure(CommandLineApplication app, Type type, Func { - return (string)methods[0].Invoke(targetInstanceFactory.Invoke(), Util.EmptyArray()); + return (string)methods[0].Invoke(targetInstanceFactory.Invoke(), Array.Empty()); }; } diff --git a/src/CommandLineUtils/CommandLineApplication.Execute.cs b/src/CommandLineUtils/CommandLineApplication.Execute.cs index 7c2ea98b..222b7c04 100644 --- a/src/CommandLineUtils/CommandLineApplication.Execute.cs +++ b/src/CommandLineUtils/CommandLineApplication.Execute.cs @@ -129,7 +129,7 @@ public static int Execute(params string[] args) public static int Execute(IConsole console, params string[] args) where TApp : class { - args ??= Util.EmptyArray(); + args ??= Array.Empty(); var context = new DefaultCommandLineContext(console, Directory.GetCurrentDirectory(), args); return Execute(context); } @@ -168,7 +168,7 @@ public static Task ExecuteAsync(string[] args, CancellationToken canc #pragma warning restore RS0026 // Do not add multiple public overloads with optional parameters where TApp : class { - args ??= Util.EmptyArray(); + args ??= Array.Empty(); var context = new DefaultCommandLineContext(PhysicalConsole.Singleton, Directory.GetCurrentDirectory(), args); return ExecuteAsync(context, cancellationToken); } @@ -189,7 +189,7 @@ public static Task ExecuteAsync(string[] args, CancellationToken canc public static Task ExecuteAsync(IConsole console, params string[] args) where TApp : class { - args ??= Util.EmptyArray(); + args ??= Array.Empty(); var context = new DefaultCommandLineContext(console, Directory.GetCurrentDirectory(), args); return ExecuteAsync(context); } diff --git a/src/CommandLineUtils/CommandLineApplication.cs b/src/CommandLineUtils/CommandLineApplication.cs index 629c0165..a7138445 100644 --- a/src/CommandLineUtils/CommandLineApplication.cs +++ b/src/CommandLineUtils/CommandLineApplication.cs @@ -748,7 +748,7 @@ public ParseResult Parse(params string[] args) { Reset(); - args ??= Util.EmptyArray(); + args ??= Array.Empty(); var processor = new CommandLineProcessor(this, args); var result = processor.Process(); diff --git a/src/CommandLineUtils/Conventions/OptionAttributeConventionBase.cs b/src/CommandLineUtils/Conventions/OptionAttributeConventionBase.cs index 74390c7a..36fa7cea 100644 --- a/src/CommandLineUtils/Conventions/OptionAttributeConventionBase.cs +++ b/src/CommandLineUtils/Conventions/OptionAttributeConventionBase.cs @@ -131,7 +131,7 @@ private protected void AddOption(ConventionContext context, CommandOption option { if (!option.HasValue()) { - setter.Invoke(modelAccessor.GetModel(), Util.EmptyArray()); + setter.Invoke(modelAccessor.GetModel(), Array.Empty()); } var count = new bool[option.Values.Count]; diff --git a/src/CommandLineUtils/HelpText/DefaultHelpTextGenerator.cs b/src/CommandLineUtils/HelpText/DefaultHelpTextGenerator.cs index 763a1e35..0e2b7df1 100644 --- a/src/CommandLineUtils/HelpText/DefaultHelpTextGenerator.cs +++ b/src/CommandLineUtils/HelpText/DefaultHelpTextGenerator.cs @@ -413,7 +413,7 @@ private string[] ExtractNamesFromEnum(Type? type) { if (type == null) { - return Util.EmptyArray(); + return Array.Empty(); } if (ReflectionHelper.IsNullableType(type, out var wrappedType)) @@ -431,7 +431,7 @@ private string[] ExtractNamesFromEnum(Type? type) return Enum.GetNames(type); } - return Util.EmptyArray(); + return Array.Empty(); } } } diff --git a/src/CommandLineUtils/IO/Pager.cs b/src/CommandLineUtils/IO/Pager.cs index 9aa7da7e..8384f0a1 100644 --- a/src/CommandLineUtils/IO/Pager.cs +++ b/src/CommandLineUtils/IO/Pager.cs @@ -42,10 +42,10 @@ public Pager(IConsole console) throw new ArgumentNullException(nameof(console)); } -#if NET45 +#if NET46_OR_GREATER // if .NET Framework, assume we're on Windows unless it's running on Mono. _enabled = Type.GetType("Mono.Runtime") != null; -#elif NETSTANDARD2_0 || NETSTANDARD2_1 +#elif NETSTANDARD2_0_OR_GREATER _enabled = !RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && !console.IsOutputRedirected; #else #error Update target frameworks @@ -130,7 +130,7 @@ public void Kill() FileName = "less", Arguments = ArgumentEscaper.EscapeAndConcatenate(args), RedirectStandardInput = true, -#if NET45 +#if NET46_OR_GREATER UseShellExecute = false, #endif } diff --git a/src/CommandLineUtils/Internal/Util.cs b/src/CommandLineUtils/Internal/Util.cs deleted file mode 100644 index 8a11fdfd..00000000 --- a/src/CommandLineUtils/Internal/Util.cs +++ /dev/null @@ -1,25 +0,0 @@ -// Copyright (c) Nate McMaster. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; - -namespace McMaster.Extensions.CommandLineUtils -{ - internal static class Util - { - public static T[] EmptyArray() -#if NET45 - => EmptyArrayCache.Value; - - private static class EmptyArrayCache - { - internal static readonly T[] Value = new T[0]; - } - -#elif NETSTANDARD2_0 || NETSTANDARD2_1 - => Array.Empty(); -#else -#error Update target frameworks -#endif - } -} diff --git a/src/CommandLineUtils/Internal/ValueParsers/HashSetParser.cs b/src/CommandLineUtils/Internal/ValueParsers/HashSetParser.cs index 950a01f9..fb84cd23 100644 --- a/src/CommandLineUtils/Internal/ValueParsers/HashSetParser.cs +++ b/src/CommandLineUtils/Internal/ValueParsers/HashSetParser.cs @@ -25,7 +25,7 @@ public HashSetParser(Type elementType, IValueParser elementParser, CultureInfo p public object Parse(string? argName, IReadOnlyList values) { - var set = Activator.CreateInstance(_listType, Util.EmptyArray()); + var set = Activator.CreateInstance(_listType, Array.Empty()); foreach (var t in values) { _addMethod.Invoke(set, new[] { _elementParser.Parse(argName, t, _parserCulture) }); diff --git a/src/CommandLineUtils/McMaster.Extensions.CommandLineUtils.csproj b/src/CommandLineUtils/McMaster.Extensions.CommandLineUtils.csproj index 0feb4749..6e86abd6 100644 --- a/src/CommandLineUtils/McMaster.Extensions.CommandLineUtils.csproj +++ b/src/CommandLineUtils/McMaster.Extensions.CommandLineUtils.csproj @@ -1,7 +1,7 @@  - netstandard2.1;netstandard2.0;net45 + netstandard2.1;netstandard2.0;net46 annotations true true @@ -26,7 +26,7 @@ McMaster.Extensions.CommandLineUtils.ArgumentEscaper - + diff --git a/src/CommandLineUtils/Properties/NullabilityHelpers.cs b/src/CommandLineUtils/Properties/NullabilityHelpers.cs index 7056bc25..2ba73d2a 100644 --- a/src/CommandLineUtils/Properties/NullabilityHelpers.cs +++ b/src/CommandLineUtils/Properties/NullabilityHelpers.cs @@ -2,7 +2,7 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Files here are for simplify annotations of nullable code and are not functional in .NET Standard 2.0 -#if NETSTANDARD2_0 || NET45 +#if NETSTANDARD2_0 || NET46_OR_GREATER namespace System.Diagnostics.CodeAnalysis { // https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.codeanalysis.notnullwhenattribute? diff --git a/src/CommandLineUtils/Utilities/DotNetExe.cs b/src/CommandLineUtils/Utilities/DotNetExe.cs index 73d20585..2e1f8e8f 100644 --- a/src/CommandLineUtils/Utilities/DotNetExe.cs +++ b/src/CommandLineUtils/Utilities/DotNetExe.cs @@ -43,9 +43,9 @@ public static string FullPathOrDefault() private static string? TryFindDotNetExePath() { var fileName = FileName; -#if NET45 +#if NET46_OR_GREATER fileName += ".exe"; -#elif NETSTANDARD2_0 || NETSTANDARD2_1 +#elif NETSTANDARD2_0_OR_GREATER if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { fileName += ".exe"; @@ -60,10 +60,17 @@ public static string FullPathOrDefault() #else #error Update target frameworks #endif + // DOTNET_ROOT specifies the location of the .NET runtimes, if they are not installed in the default location. var dotnetRoot = Environment.GetEnvironmentVariable("DOTNET_ROOT"); - return !string.IsNullOrEmpty(dotnetRoot) - ? Path.Combine(dotnetRoot, fileName) - : null; + + if (string.IsNullOrEmpty(dotnetRoot)) + { + // fall back to default location + // https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-environment-variables#dotnet_root-dotnet_rootx86 + dotnetRoot = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "C:\\Program Files\\dotnet" : "/usr/local/share/dotnet"; + } + + return Path.Combine(dotnetRoot, fileName); } } } diff --git a/test/CommandLineUtils.Tests/ExecuteMethodConventionTests.cs b/test/CommandLineUtils.Tests/ExecuteMethodConventionTests.cs index 742e9038..a283c818 100644 --- a/test/CommandLineUtils.Tests/ExecuteMethodConventionTests.cs +++ b/test/CommandLineUtils.Tests/ExecuteMethodConventionTests.cs @@ -105,7 +105,7 @@ public void OnExecuteIsExecutedOnSelectedSubcommand(string? args, int expectedRe app.Conventions.UseCommandAttribute(); app.Conventions.UseOnExecuteMethodFromModel(); - var result = app.Execute(args?.Split(' ') ?? Util.EmptyArray()); + var result = app.Execute(args?.Split(' ') ?? Array.Empty()); Assert.Equal(expectedResult, result); } diff --git a/test/CommandLineUtils.Tests/OptionAttributeTests.cs b/test/CommandLineUtils.Tests/OptionAttributeTests.cs index 9fd8e838..8214d0f9 100644 --- a/test/CommandLineUtils.Tests/OptionAttributeTests.cs +++ b/test/CommandLineUtils.Tests/OptionAttributeTests.cs @@ -302,7 +302,7 @@ private CommandOption CreateOption(Type propType, string propName) pb.SetCustomAttribute(ab); var program = tb.CreateType(); var appBuilder = typeof(CommandLineApplication<>).MakeGenericType(program); - var app = (CommandLineApplication)Activator.CreateInstance(appBuilder, Util.EmptyArray()); + var app = (CommandLineApplication)Activator.CreateInstance(appBuilder, Array.Empty()); app.Conventions.UseOptionAttributes(); return app.Options.First(); }