Skip to content

Bump the system-commandline group with 2 updates #385

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -399,3 +399,5 @@ FodyWeavers.xsd

# Verify snapshots
*.received.*

launchSettings.json
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

### Added

- Build and test on each platform version available
- macOS 13
- macOS 14
Expand All @@ -15,6 +17,11 @@
- Ubuntu 24.04
- Windows 11

### Updated

- Bumped `System.CommandLine` from 2.0.0-beta4.22272.1 to 2.0.0-beta5.25306.1
- Bumped `System.CommandLine.Rendering` from 0.4.0-alpha.22272.1 to 0.4.0-alpha.25306.1

## [0.6.0](https://github.com/xt0rted/dotnet-run-script/compare/v0.5.0...v0.6.0) - 2024-04-10

- Dropped support for .NET Core 3.1
Expand Down
31 changes: 17 additions & 14 deletions src/ConsoleWriter.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
namespace RunScript;

using System;
using System.CommandLine.Rendering;
using System.Globalization;

internal class ConsoleWriter : IConsoleWriter
{
private readonly IConsole _console;
private readonly IFormatProvider? _consoleFormatProvider;

private readonly TextWriter _output;
private readonly IFormatProvider _consoleFormatProvider;
private readonly bool _verbose;

public ConsoleWriter(IConsole console, IFormatProvider consoleFormatProvider, bool verbose)
public ConsoleWriter(TextWriter output, IFormatProvider consoleFormatProvider, bool verbose)
{
_console = console ?? throw new ArgumentNullException(nameof(console));
_consoleFormatProvider = consoleFormatProvider ?? throw new ArgumentNullException(nameof(consoleFormatProvider));
ArgumentNullException.ThrowIfNull(output);
ArgumentNullException.ThrowIfNull(consoleFormatProvider);

_output = output;
_consoleFormatProvider = consoleFormatProvider;
_verbose = verbose;
}

Expand All @@ -23,36 +26,36 @@ private void WriteLine(AnsiControlCode modifierOn, AnsiControlCode modifierOff,
{
if (message is not null)
{
_console.Out.Write(modifierOn.ToString(null, _consoleFormatProvider));
_output.Write(modifierOn.ToString(null, _consoleFormatProvider));

if (args?.Length > 0)
{
_console.Out.Write(string.Format(CultureInfo.CurrentCulture, message, args));
_output.Write(string.Format(CultureInfo.CurrentCulture, message, args));
}
else
{
_console.Out.Write(message);
_output.Write(message);
}

_console.Out.Write(modifierOff.ToString(null, _consoleFormatProvider));
_console.Out.Write(Environment.NewLine);
_output.Write(modifierOff.ToString(null, _consoleFormatProvider));
_output.Write(Environment.NewLine);
}
}

public void Raw(string? message)
=> _console.Out.Write(message);
=> _output.Write(message);

public void VerboseBanner()
=> LineVerbose("Verbose mode is on. This will print more information.");

public void BlankLine()
=> _console.Out.Write(Environment.NewLine);
=> _output.Write(Environment.NewLine);

public void BlankLineVerbose()
{
if (_verbose)
{
_console.Out.Write(Environment.NewLine);
_output.Write(Environment.NewLine);
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/GlobalArguments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ namespace RunScript;

public static class GlobalArguments
{
public static readonly Argument<string[]> Scripts = new("scripts", "One or more scripts to run")
public static readonly Argument<string[]> Scripts = new("scripts")
{
Arity = ArgumentArity.ZeroOrMore,
Description = "One or more scripts to run",
DefaultValueFactory = _ => [],
};
}
15 changes: 11 additions & 4 deletions src/GlobalOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@ namespace RunScript;

public static class GlobalOptions
{
public static readonly Option<bool> IfPresent = new("--if-present", "Don't exit with an error code if the script isn't found");
public static readonly Option<bool> IfPresent = new("--if-present")
{
Description = "Don't exit with an error code if the script isn't found",
};

public static readonly Option<string> ScriptShell = new("--script-shell", "The shell to use when running scripts (cmd, pwsh, sh, etc.)")
public static readonly Option<string> ScriptShell = new("--script-shell")
{
ArgumentHelpName = "shell",
Description = "The shell to use when running scripts (cmd, pwsh, sh, etc.)",
HelpName = "shell",
};

public static readonly Option<bool> Verbose = new(new[] { "-v", "--verbose" }, "Enable verbose output");
public static readonly Option<bool> Verbose = new("--verbose", "-v")
{
Description = "Enable verbose output",
};
}
37 changes: 6 additions & 31 deletions src/Program.cs
Original file line number Diff line number Diff line change
@@ -1,41 +1,16 @@
using System.CommandLine.Builder;
using System.CommandLine.Parsing;

using RunScript;

var environment = new EnvironmentWrapper();
var consoleFormatProvider = ConsoleHelpers.FormatInfo(environment);
var rootCommand = new RunScriptCommand(
environment,
consoleFormatProvider,
environment.CurrentDirectory);

var parser = new CommandLineBuilder(rootCommand)
.UseVersionOption()
.UseHelp()
.UseEnvironmentVariableDirective()
.UseParseDirective()
.UseSuggestDirective()
.RegisterWithDotnetSuggest()
.UseParseErrorReporting()
.UseExceptionHandler((ex, ctx) =>
{
var verbose = ctx.ParseResult.HasOption(GlobalOptions.Verbose);
var writer = new ConsoleWriter(ctx.Console, consoleFormatProvider, verbose);

if (verbose)
{
writer.Error(ex.ToString());
}
else
{
writer.Error(ex.Message);
}
})
.CancelOnProcessTermination()
.EnableLegacyDoubleDashBehavior()
.Build();
environment.CurrentDirectory)
{
new DiagramDirective(),
new EnvironmentVariablesDirective()
};

var parseResult = parser.Parse(args);
var parseResult = rootCommand.Parse(args);

return await parseResult.InvokeAsync();
35 changes: 15 additions & 20 deletions src/RunScriptCommand.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
namespace RunScript;

using System.CommandLine.Invocation;

using DotNet.Globbing;

using RunScript.Logging;

internal class RunScriptCommand : RootCommand, ICommandHandler
internal class RunScriptCommand : RootCommand
{
private readonly IEnvironment _environment;
private readonly IFormatProvider _consoleFormatProvider;
Expand All @@ -25,28 +23,25 @@ internal RunScriptCommand(

_workingDirectory = workingDirectory;

AddArgument(GlobalArguments.Scripts);
Arguments.Add(GlobalArguments.Scripts);

AddOption(GlobalOptions.IfPresent);
AddOption(GlobalOptions.ScriptShell);
AddOption(GlobalOptions.Verbose);
Options.Add(GlobalOptions.IfPresent);
Options.Add(GlobalOptions.ScriptShell);
Options.Add(GlobalOptions.Verbose);

Handler = this;
SetAction(InvokeAsync);
}

public int Invoke(InvocationContext context)
=> throw new NotImplementedException();

public async Task<int> InvokeAsync(InvocationContext context)
internal async Task<int> InvokeAsync(ParseResult parseResult, CancellationToken cancellationToken)
{
ArgumentNullException.ThrowIfNull(context);
ArgumentNullException.ThrowIfNull(parseResult);

var ifPresent = context.ParseResult.GetValueForOption(GlobalOptions.IfPresent);
var scriptShell = context.ParseResult.GetValueForOption(GlobalOptions.ScriptShell);
var verbose = context.ParseResult.GetValueForOption(GlobalOptions.Verbose);
var scripts = context.ParseResult.GetValueForArgument(GlobalArguments.Scripts);
var ifPresent = parseResult.GetValue(GlobalOptions.IfPresent);
var scriptShell = parseResult.GetValue(GlobalOptions.ScriptShell);
var verbose = parseResult.GetValue(GlobalOptions.Verbose);
var scripts = parseResult.GetRequiredValue(GlobalArguments.Scripts);

var writer = new ConsoleWriter(context.Console, _consoleFormatProvider, verbose);
var writer = new ConsoleWriter(parseResult.Configuration.Output, _consoleFormatProvider, verbose);

writer.VerboseBanner();

Expand Down Expand Up @@ -113,9 +108,9 @@ public async Task<int> InvokeAsync(InvocationContext context)
// UnparsedTokens is backed by string[] so if we cast
// back to that we get a lot better perf down the line.
// Hopefully this doesn't break in the future 🤞
var scriptArgs = (string[])context.ParseResult.UnparsedTokens;
var scriptArgs = (string[])parseResult.UnmatchedTokens;

var scriptRunner = builder.CreateGroupRunner(context.GetCancellationToken());
var scriptRunner = builder.CreateGroupRunner(cancellationToken);

var result = await scriptRunner.RunAsync(
script.Name,
Expand Down
4 changes: 2 additions & 2 deletions src/run-script.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@

<ItemGroup>
<PackageReference Include="DotNet.Glob" Version="3.1.3" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
<PackageReference Include="System.CommandLine.Rendering" Version="0.4.0-alpha.22272.1" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta5.25306.1" />
<PackageReference Include="System.CommandLine.Rendering" Version="0.4.0-alpha.25306.1" />
</ItemGroup>

</Project>
5 changes: 2 additions & 3 deletions test/CommandBuilderTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
namespace RunScript;

using System.Collections.Generic;
using System.CommandLine.IO;
using System.CommandLine.Rendering;

[Trait("category", "unit")]
Expand Down Expand Up @@ -91,12 +90,12 @@ public void CreateGroupRunner_should_create_a_runner(bool isWindows)

private static CommandBuilder SetUpTest(bool isWindows, string? comSpec = DefaultComSpec)
{
var console = new TestConsole();
var output = new StringWriter();
var consoleFormatProvider = new ConsoleFormatInfo
{
SupportsAnsiCodes = false,
};
var consoleWriter = new ConsoleWriter(console, consoleFormatProvider, verbose: true);
var consoleWriter = new ConsoleWriter(output, consoleFormatProvider, verbose: true);

var project = new Project
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
{
console: {
IsErrorRedirected: false,
IsInputRedirected: false,
IsOutputRedirected: false,
Out:
output:
> env

value1=value 1
value2=value 2
value3=value 3

},
,
commandRunners: [
{
Name: preenv,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
{
console: {
IsErrorRedirected: false,
IsInputRedirected: false,
IsOutputRedirected: false,
Out:
output:
> env

value1=value 1
value2=value 2
value3=value 3

},
,
commandRunners: [
{
Name: preenv,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
{
IsErrorRedirected: false,
IsInputRedirected: false,
IsOutputRedirected: false,
Out:

> env

value1=value 1
value2=value 2
value3=value 3

}
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
{
IsErrorRedirected: false,
IsInputRedirected: false,
IsOutputRedirected: false,
Out:

> env

value1=value 1
value2=value 2
value3=value 3

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
{
console: {
IsErrorRedirected: false,
IsInputRedirected: false,
IsOutputRedirected: false
},
output: ,
commandRunners: [
{
Name: env,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
{
console: {
IsErrorRedirected: false,
IsInputRedirected: false,
IsOutputRedirected: false
},
output: ,
commandRunners: [
{
Name: env,
Expand Down
Loading
Loading