GH-242: Fix version retrieval for single file applications (#245)

This commit is contained in:
Milosz Krajewski 2021-01-25 23:45:38 +00:00 committed by GitHub
parent 31a5e17a45
commit ad49b6aa67
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 53 additions and 17 deletions

View File

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Spectre.Console.Rendering;

View File

@ -25,6 +25,23 @@ namespace Spectre.Console.Cli
return configurator;
}
/// <summary>
/// Overrides the auto-detected version of the application.
/// </summary>
/// <param name="configurator">The configurator.</param>
/// <param name="version">The version of application.</param>
/// <returns>A configurator that can be used to configure the application further.</returns>
public static IConfigurator SetApplicationVersion(this IConfigurator configurator, string version)
{
if (configurator == null)
{
throw new ArgumentNullException(nameof(configurator));
}
configurator.Settings.ApplicationVersion = version;
return configurator;
}
/// <summary>
/// Configures the console.
/// </summary>

View File

@ -10,6 +10,11 @@ namespace Spectre.Console.Cli
/// </summary>
string? ApplicationName { get; set; }
/// <summary>
/// Gets or sets the application version (use it to override auto-detected value).
/// </summary>
string? ApplicationVersion { get; set; }
/// <summary>
/// Gets or sets the <see cref="IAnsiConsole"/>.
/// </summary>

View File

@ -44,7 +44,7 @@ namespace Spectre.Console.Cli
firstArgument.Equals("-v", StringComparison.OrdinalIgnoreCase))
{
var console = configuration.Settings.Console.GetConsole();
console.WriteLine(VersionHelper.GetVersion(Assembly.GetEntryAssembly()));
console.WriteLine(ResolveApplicationVersion(configuration));
return Task.FromResult(0);
}
}
@ -83,6 +83,13 @@ namespace Spectre.Console.Cli
return Execute(leaf, parsedResult.Tree, context, resolver, configuration);
}
private static string ResolveApplicationVersion(IConfiguration configuration)
{
return
configuration.Settings.ApplicationVersion ?? // potential override
VersionHelper.GetVersion(Assembly.GetEntryAssembly());
}
private static Task<int> Execute(
CommandTree leaf,
CommandTree tree,

View File

@ -5,6 +5,7 @@ namespace Spectre.Console.Cli
internal sealed class CommandAppSettings : ICommandAppSettings
{
public string? ApplicationName { get; set; }
public string? ApplicationVersion { get; set; }
public IAnsiConsole? Console { get; set; }
public ICommandInterceptor? Interceptor { get; set; }
public ITypeRegistrarFrontend Registrar { get; set; }

View File

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
@ -28,7 +29,23 @@ namespace Spectre.Console.Cli
public string GetApplicationName()
{
return ApplicationName ?? Path.GetFileName(Assembly.GetEntryAssembly()?.Location) ?? "?";
return
ApplicationName ??
Path.GetFileName(GetApplicationFile()) ?? // null is propagated by GetFileName
"?";
}
private static string? GetApplicationFile()
{
var location = Assembly.GetEntryAssembly()?.Location;
if (string.IsNullOrWhiteSpace(location))
{
// this is special case for single file executable
// (Assembly.Location returns empty string)
return Process.GetCurrentProcess().MainModule?.FileName;
}
return location;
}
}
}

View File

@ -1,4 +1,3 @@
using System.Diagnostics;
using System.Reflection;
namespace Spectre.Console.Cli
@ -7,20 +6,9 @@ namespace Spectre.Console.Cli
{
public static string GetVersion(Assembly? assembly)
{
if (assembly == null)
{
return "?";
}
try
{
var info = FileVersionInfo.GetVersionInfo(assembly.Location);
return info.ProductVersion ?? assembly?.GetName()?.Version?.ToString() ?? "?";
}
catch
{
return "?";
}
return assembly?
.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?
.InformationalVersion ?? "?";
}
}
}