mirror of
https://github.com/Tyrrrz/CliFx.git
synced 2025-10-25 15:19:17 +00:00
Make CliApplicationBuilder set defaults through itself to increase reuse
This commit is contained in:
@@ -65,25 +65,39 @@ namespace CliFx
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ICliApplication Build()
|
private void SetFallbackValues()
|
||||||
{
|
{
|
||||||
// Entry assembly is null in tests
|
// Entry assembly is null in tests
|
||||||
var entryAssembly = Assembly.GetEntryAssembly();
|
var entryAssembly = Assembly.GetEntryAssembly();
|
||||||
|
|
||||||
|
if (_title.IsNullOrWhiteSpace())
|
||||||
|
UseTitle(entryAssembly?.GetName().Name ?? "App");
|
||||||
|
|
||||||
|
if (_executableName.IsNullOrWhiteSpace())
|
||||||
|
UseExecutableName(Path.GetFileNameWithoutExtension(entryAssembly?.Location) ?? "app");
|
||||||
|
|
||||||
|
if (_versionText.IsNullOrWhiteSpace())
|
||||||
|
UseVersionText(entryAssembly?.GetName().Version.ToString() ?? "1.0");
|
||||||
|
|
||||||
|
if (_console == null)
|
||||||
|
UseConsole(new SystemConsole());
|
||||||
|
|
||||||
|
if (_commandFactory == null)
|
||||||
|
UseCommandFactory(new CommandFactory());
|
||||||
|
}
|
||||||
|
|
||||||
|
public ICliApplication Build()
|
||||||
|
{
|
||||||
// Use defaults for required parameters that were not configured
|
// Use defaults for required parameters that were not configured
|
||||||
var title = _title ?? entryAssembly?.GetName().Name ?? "App";
|
SetFallbackValues();
|
||||||
var executableName = _executableName ?? Path.GetFileNameWithoutExtension(entryAssembly?.Location) ?? "app";
|
|
||||||
var versionText = _versionText ?? entryAssembly?.GetName().Version.ToString() ?? "1.0";
|
|
||||||
var console = _console ?? new SystemConsole();
|
|
||||||
var commandFactory = _commandFactory ?? new CommandFactory();
|
|
||||||
|
|
||||||
// Project parameters to expected types
|
// Project parameters to expected types
|
||||||
var metadata = new ApplicationMetadata(title, executableName, versionText);
|
var metadata = new ApplicationMetadata(_title, _executableName, _versionText);
|
||||||
var commandTypes = _commandTypes.ToArray();
|
var commandTypes = _commandTypes.ToArray();
|
||||||
|
|
||||||
return new CliApplication(metadata, commandTypes,
|
return new CliApplication(metadata, commandTypes,
|
||||||
console, new CommandInputParser(), new CommandSchemaResolver(),
|
_console, new CommandInputParser(), new CommandSchemaResolver(),
|
||||||
commandFactory, new CommandInitializer(), new CommandHelpTextRenderer());
|
_commandFactory, new CommandInitializer(), new CommandHelpTextRenderer());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user