mirror of
				https://github.com/nsnail/spectre.console.git
				synced 2025-10-31 17:15:28 +08:00 
			
		
		
		
	GH-242: Fix version retrieval for single file applications (#245)
This commit is contained in:
		| @@ -1,6 +1,7 @@ | ||||
| using System; | ||||
| using System.Collections.Generic; | ||||
| using System.Diagnostics; | ||||
| using System.Linq; | ||||
| using System.Threading.Tasks; | ||||
| using Spectre.Console.Rendering; | ||||
|  | ||||
|   | ||||
| @@ -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> | ||||
|   | ||||
| @@ -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> | ||||
|   | ||||
| @@ -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, | ||||
|   | ||||
| @@ -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; } | ||||
|   | ||||
| @@ -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; | ||||
|         } | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -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 ?? "?"; | ||||
|         } | ||||
|     } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Milosz Krajewski
					Milosz Krajewski