diff --git a/CliFx/CliApplicationBuilder.cs b/CliFx/CliApplicationBuilder.cs index 74b0145..06eaf63 100644 --- a/CliFx/CliApplicationBuilder.cs +++ b/CliFx/CliApplicationBuilder.cs @@ -12,7 +12,7 @@ namespace CliFx /// /// Default implementation of . /// - public class CliApplicationBuilder : ICliApplicationBuilder + public partial class CliApplicationBuilder : ICliApplicationBuilder { private readonly HashSet _commandTypes = new HashSet(); @@ -90,23 +90,44 @@ namespace CliFx private void SetFallbackValues() { - // Entry assembly is null in tests - var entryAssembly = Assembly.GetEntryAssembly(); - if (_title.IsNullOrWhiteSpace()) - UseTitle(entryAssembly?.GetName().Name ?? "App"); + { + // Entry assembly is null in tests + var title = EntryAssembly?.GetName().Name ?? "App"; + + UseTitle(title); + } if (_executableName.IsNullOrWhiteSpace()) - UseExecutableName(Path.GetFileNameWithoutExtension(entryAssembly?.Location) ?? "app"); + { + // Entry assembly is null in tests + var entryAssemblyLocation = EntryAssembly?.Location; + var executableName = Path.GetFileNameWithoutExtension(entryAssemblyLocation) ?? "app"; + + // Set proper executable name for apps launched with dotnet SDK + if (string.Equals(Path.GetExtension(entryAssemblyLocation), ".dll", StringComparison.OrdinalIgnoreCase)) + executableName = "dotnet " + executableName; + + UseExecutableName(executableName); + } if (_versionText.IsNullOrWhiteSpace()) - UseVersionText(entryAssembly?.GetName().Version.ToString() ?? "1.0"); + { + // Entry assembly is null in tests + var versionText = EntryAssembly?.GetName().Version.ToString() ?? "1.0"; + + UseVersionText(versionText); + } if (_console == null) + { UseConsole(new SystemConsole()); + } if (_commandFactory == null) + { UseCommandFactory(new CommandFactory()); + } } /// @@ -124,4 +145,11 @@ namespace CliFx _commandFactory, new CommandInitializer(), new HelpTextRenderer()); } } + + public partial class CliApplicationBuilder + { + private static readonly Lazy LazyEntryAssembly = new Lazy(Assembly.GetEntryAssembly); + + private static Assembly EntryAssembly => LazyEntryAssembly.Value; + } } \ No newline at end of file