Skip to content

Commit

Permalink
Fix formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyrrrz committed Aug 22, 2023
1 parent 21b601d commit aed53eb
Show file tree
Hide file tree
Showing 53 changed files with 436 additions and 392 deletions.
5 changes: 3 additions & 2 deletions CliFx/ApplicationConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ public class ApplicationConfiguration
public ApplicationConfiguration(
IReadOnlyList<Type> commandTypes,
bool isDebugModeAllowed,
bool isPreviewModeAllowed)
bool isPreviewModeAllowed
)
{
CommandTypes = commandTypes;
IsDebugModeAllowed = isDebugModeAllowed;
IsPreviewModeAllowed = isPreviewModeAllowed;
}
}
}
5 changes: 3 additions & 2 deletions CliFx/ApplicationMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ public ApplicationMetadata(
string title,
string executableName,
string version,
string? description)
string? description
)
{
Title = title;
ExecutableName = executableName;
Version = version;
Description = description;
}
}
}
6 changes: 2 additions & 4 deletions CliFx/Attributes/CommandAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,5 @@ public CommandAttribute(string name)
/// <summary>
/// Initializes an instance of <see cref="CommandAttribute" />.
/// </summary>
public CommandAttribute()
{
}
}
public CommandAttribute() { }
}
14 changes: 4 additions & 10 deletions CliFx/Attributes/CommandOptionAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,23 +81,17 @@ private CommandOptionAttribute(string? name, char? shortName)
/// Initializes an instance of <see cref="CommandOptionAttribute" />.
/// </summary>
public CommandOptionAttribute(string name, char shortName)
: this(name, (char?)shortName)
{
}
: this(name, (char?)shortName) { }

/// <summary>
/// Initializes an instance of <see cref="CommandOptionAttribute" />.
/// </summary>
public CommandOptionAttribute(string name)
: this(name, null)
{
}
: this(name, null) { }

/// <summary>
/// Initializes an instance of <see cref="CommandOptionAttribute" />.
/// </summary>
public CommandOptionAttribute(char shortName)
: this(null, (char?)shortName)
{
}
}
: this(null, (char?)shortName) { }
}
2 changes: 1 addition & 1 deletion CliFx/Attributes/CommandParameterAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,4 @@ public CommandParameterAttribute(int order)
{
Order = order;
}
}
}
76 changes: 45 additions & 31 deletions CliFx/CliApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ public CliApplication(
ApplicationMetadata metadata,
ApplicationConfiguration configuration,
IConsole console,
ITypeActivator typeActivator)
ITypeActivator typeActivator
)
{
Metadata = metadata;
Configuration = configuration;
Expand All @@ -58,9 +59,11 @@ private bool IsPreviewModeEnabled(CommandInput commandInput) =>
Configuration.IsPreviewModeAllowed && commandInput.IsPreviewDirectiveSpecified;

private bool ShouldShowHelpText(CommandSchema commandSchema, CommandInput commandInput) =>
commandSchema.IsHelpOptionAvailable && commandInput.IsHelpOptionSpecified ||
commandSchema.IsHelpOptionAvailable && commandInput.IsHelpOptionSpecified
||
// Show help text also if the fallback default command is executed without any arguments
commandSchema == FallbackDefaultCommand.Schema && !commandInput.HasArguments;
commandSchema == FallbackDefaultCommand.Schema
&& !commandInput.HasArguments;

private bool ShouldShowVersionText(CommandSchema commandSchema, CommandInput commandInput) =>
commandSchema.IsVersionOptionAvailable && commandInput.IsVersionOptionSpecified;
Expand All @@ -83,7 +86,10 @@ private async ValueTask PromptDebuggerAsync()
await Task.Delay(100);
}

private async ValueTask<int> RunAsync(ApplicationSchema applicationSchema, CommandInput commandInput)
private async ValueTask<int> RunAsync(
ApplicationSchema applicationSchema,
CommandInput commandInput
)
{
// Console colors may have already been overridden by the parent process,
// so we need to reset it to make sure that everything we write looks properly.
Expand All @@ -104,21 +110,25 @@ private async ValueTask<int> RunAsync(ApplicationSchema applicationSchema, Comma

// Try to get the command schema that matches the input
var commandSchema =
(!string.IsNullOrWhiteSpace(commandInput.CommandName)
// If the command name is specified, try to find the command by name.
// This should always succeed, because the input parsing relies on
// the list of available command names.
? applicationSchema.TryFindCommand(commandInput.CommandName)
// Otherwise, try to find the default command
: applicationSchema.TryFindDefaultCommand()) ??
(
!string.IsNullOrWhiteSpace(commandInput.CommandName)
// If the command name is specified, try to find the command by name.
// This should always succeed, because the input parsing relies on
// the list of available command names.
? applicationSchema.TryFindCommand(commandInput.CommandName)
// Otherwise, try to find the default command
: applicationSchema.TryFindDefaultCommand()
)
??
// If a valid command was not found, use the fallback default command.
// This is only used as a stub to show the help text.
FallbackDefaultCommand.Schema;

// Initialize an instance of the command type
var commandInstance = commandSchema == FallbackDefaultCommand.Schema
? new FallbackDefaultCommand() // bypass the activator
: _typeActivator.CreateInstance<ICommand>(commandSchema.Type);
var commandInstance =
commandSchema == FallbackDefaultCommand.Schema
? new FallbackDefaultCommand() // bypass the activator
: _typeActivator.CreateInstance<ICommand>(commandSchema.Type);

// Assemble the help context
var helpContext = new HelpContext(
Expand Down Expand Up @@ -178,7 +188,8 @@ private async ValueTask<int> RunAsync(ApplicationSchema applicationSchema, Comma
/// </remarks>
public async ValueTask<int> RunAsync(
IReadOnlyList<string> commandLineArguments,
IReadOnlyDictionary<string, string> environmentVariables)
IReadOnlyDictionary<string, string> environmentVariables
)
{
try
{
Expand Down Expand Up @@ -213,16 +224,17 @@ public async ValueTask<int> RunAsync(
/// When running WITHOUT the debugger attached (i.e. in production), this method swallows
/// all exceptions and reports them to the console.
/// </remarks>
public async ValueTask<int> RunAsync(IReadOnlyList<string> commandLineArguments) => await RunAsync(
commandLineArguments,
Environment
.GetEnvironmentVariables()
.ToDictionary<string, string>(
RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
? StringComparer.OrdinalIgnoreCase
: StringComparer.Ordinal
)
);
public async ValueTask<int> RunAsync(IReadOnlyList<string> commandLineArguments) =>
await RunAsync(
commandLineArguments,
Environment
.GetEnvironmentVariables()
.ToDictionary<string, string>(
RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
? StringComparer.OrdinalIgnoreCase
: StringComparer.Ordinal
)
);

/// <summary>
/// Runs the application.
Expand All @@ -233,9 +245,11 @@ public async ValueTask<int> RunAsync(IReadOnlyList<string> commandLineArguments)
/// When running WITHOUT the debugger attached (i.e. in production), this method swallows
/// all exceptions and reports them to the console.
/// </remarks>
public async ValueTask<int> RunAsync() => await RunAsync(
Environment.GetCommandLineArgs()
.Skip(1) // first element is the file path
.ToArray()
);
}
public async ValueTask<int> RunAsync() =>
await RunAsync(
Environment
.GetCommandLineArgs()
.Skip(1) // first element is the file path
.ToArray()
);
}
44 changes: 28 additions & 16 deletions CliFx/CliApplicationBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public CliApplicationBuilder AddCommand(Type commandType)
/// <summary>
/// Adds a command to the application.
/// </summary>
public CliApplicationBuilder AddCommand<TCommand>() where TCommand : ICommand =>
AddCommand(typeof(TCommand));
public CliApplicationBuilder AddCommand<TCommand>()
where TCommand : ICommand => AddCommand(typeof(TCommand));

/// <summary>
/// Adds multiple commands to the application.
Expand All @@ -62,7 +62,9 @@ public CliApplicationBuilder AddCommands(IEnumerable<Type> commandTypes)
/// </remarks>
public CliApplicationBuilder AddCommandsFrom(Assembly commandAssembly)
{
foreach (var commandType in commandAssembly.ExportedTypes.Where(CommandSchema.IsCommandType))
foreach (
var commandType in commandAssembly.ExportedTypes.Where(CommandSchema.IsCommandType)
)
AddCommand(commandType);

return this;
Expand Down Expand Up @@ -90,7 +92,8 @@ public CliApplicationBuilder AddCommandsFrom(IEnumerable<Assembly> commandAssemb
/// This method looks for public non-abstract classes that implement <see cref="ICommand" />
/// and are annotated by <see cref="CommandAttribute" />.
/// </remarks>
public CliApplicationBuilder AddCommandsFromThisAssembly() => AddCommandsFrom(Assembly.GetCallingAssembly());
public CliApplicationBuilder AddCommandsFromThisAssembly() =>
AddCommandsFrom(Assembly.GetCallingAssembly());

/// <summary>
/// Specifies whether debug mode (enabled with the [debug] directive) is allowed in the application.
Expand Down Expand Up @@ -190,8 +193,9 @@ public CliApplicationBuilder UseTypeActivator(IServiceProvider serviceProvider)
/// This method takes a delegate that receives the list of all added command types, so that you can
/// easily register them with the service provider.
/// </summary>
public CliApplicationBuilder UseTypeActivator(Func<IReadOnlyList<Type>, IServiceProvider> getServiceProvider) =>
UseTypeActivator(getServiceProvider(_commandTypes.ToArray()));
public CliApplicationBuilder UseTypeActivator(
Func<IReadOnlyList<Type>, IServiceProvider> getServiceProvider
) => UseTypeActivator(getServiceProvider(_commandTypes.ToArray()));

/// <summary>
/// Creates a configured instance of <see cref="CliApplication" />.
Expand Down Expand Up @@ -228,8 +232,8 @@ private static string GetDefaultTitle()
if (string.IsNullOrWhiteSpace(entryAssemblyName))
{
throw new InvalidOperationException(
"Failed to infer the default application title. " +
$"Please specify it explicitly using `{nameof(SetTitle)}()`."
"Failed to infer the default application title. "
+ $"Please specify it explicitly using `{nameof(SetTitle)}()`."
);
}

Expand All @@ -241,11 +245,14 @@ private static string GetDefaultExecutableName()
var entryAssemblyFilePath = EnvironmentEx.EntryAssembly?.Location;
var processFilePath = EnvironmentEx.ProcessPath;

if (string.IsNullOrWhiteSpace(entryAssemblyFilePath) || string.IsNullOrWhiteSpace(processFilePath))
if (
string.IsNullOrWhiteSpace(entryAssemblyFilePath)
|| string.IsNullOrWhiteSpace(processFilePath)
)
{
throw new InvalidOperationException(
"Failed to infer the default application executable name. " +
$"Please specify it explicitly using `{nameof(SetExecutableName)}()`."
"Failed to infer the default application executable name. "
+ $"Please specify it explicitly using `{nameof(SetExecutableName)}()`."
);
}

Expand All @@ -258,8 +265,13 @@ private static string GetDefaultExecutableName()

// If the process path has the same name and parent directory as the entry assembly path,
// but different extension, it's a framework-dependent .NET Core app launched through the apphost.
if (PathEx.AreEqual(Path.ChangeExtension(entryAssemblyFilePath, "exe"), processFilePath) ||
PathEx.AreEqual(Path.GetFileNameWithoutExtension(entryAssemblyFilePath), processFilePath))
if (
PathEx.AreEqual(Path.ChangeExtension(entryAssemblyFilePath, "exe"), processFilePath)
|| PathEx.AreEqual(
Path.GetFileNameWithoutExtension(entryAssemblyFilePath),
processFilePath
)
)
{
return Path.GetFileNameWithoutExtension(entryAssemblyFilePath);
}
Expand All @@ -274,11 +286,11 @@ private static string GetDefaultVersionText()
if (entryAssemblyVersion is null)
{
throw new InvalidOperationException(
"Failed to infer the default application version. " +
$"Please specify it explicitly using `{nameof(SetVersion)}()`."
"Failed to infer the default application version. "
+ $"Please specify it explicitly using `{nameof(SetVersion)}()`."
);
}

return "v" + entryAssemblyVersion.ToSemanticString();
}
}
}
Loading

0 comments on commit aed53eb

Please sign in to comment.