Skip to content
New issue

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

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

Already on GitHub? # to your account

Fix redirected input #91

Merged
merged 4 commits into from
Nov 23, 2023
Merged
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- [CLI] Fix reading of redirected input ([PR](https://github.com/dotnet/roslynator/pull/91))

## [0.6.0] - 2023-11-07

## [0.6.0-beta] - 2023-10-19
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,12 @@ public bool TryParse(CommonReplaceCommandOptions options, ParseContext context)
pipeMode = pipeMode2;
}

string? redirectedInput = ConsoleHelpers.ReadRedirectedInput();

if (pipeMode == CommandLine.PipeMode.Paths
|| pipeMode == CommandLine.PipeMode.Text)
{
if (!Console.IsInputRedirected)
if (redirectedInput is null)
{
context.WriteError("Redirected/piped input is required "
+ $"when option '{OptionNames.GetHelpText(OptionNames.Pipe)}' is specified.");
Expand Down Expand Up @@ -143,7 +145,7 @@ public bool TryParse(CommonReplaceCommandOptions options, ParseContext context)

if (options.IsDefaultPath()
&& pipeMode != CommandLine.PipeMode.Paths
&& Console.IsInputRedirected)
&& redirectedInput is not null)
{
if (input is not null)
{
Expand All @@ -161,7 +163,7 @@ public bool TryParse(CommonReplaceCommandOptions options, ParseContext context)
return false;
}

input = ConsoleHelpers.ReadRedirectedInput();
input = redirectedInput;
}

var displayParts = DisplayParts.None;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -350,11 +350,13 @@ protected virtual bool TryParsePaths(out ImmutableArray<PathInfo> paths, ParseCo
paths = paths.AddRange(pathsFromFile);
}

if (Console.IsInputRedirected
ImmutableArray<string> redirectedInput = ConsoleHelpers.ReadRedirectedInputAsLines();

if (!redirectedInput.IsDefault
&& PipeMode == CommandLine.PipeMode.Paths)
{
if (!TryEnsureFullPath(
ConsoleHelpers.ReadRedirectedInputAsLines().Where(f => !string.IsNullOrEmpty(f)),
redirectedInput.Where(f => !string.IsNullOrEmpty(f)),
PathOrigin.RedirectedInput,
context,
out ImmutableArray<PathInfo> pathsFromInput))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,12 @@ public bool TryParse(FindCommandOptions options, ParseContext context)
pipeMode = pipeMode2;
}

string? redirectedInput = ConsoleHelpers.ReadRedirectedInput();

if (pipeMode == CommandLine.PipeMode.Paths
|| pipeMode == CommandLine.PipeMode.Text)
{
if (!Console.IsInputRedirected)
if (redirectedInput is null)
{
context.WriteError("Redirected/piped input is required "
+ $"when option '{OptionNames.GetHelpText(OptionNames.Pipe)}' is specified.");
Expand All @@ -86,7 +88,7 @@ public bool TryParse(FindCommandOptions options, ParseContext context)

if (options.IsDefaultPath()
&& pipeMode != CommandLine.PipeMode.Paths
&& Console.IsInputRedirected)
&& redirectedInput is not null)
{
if (options.ContentFilter is null)
{
Expand All @@ -96,7 +98,7 @@ public bool TryParse(FindCommandOptions options, ParseContext context)
return false;
}

input = ConsoleHelpers.ReadRedirectedInput();
input = redirectedInput;

if (options.Paths.Length == 1
&& options.Paths[0].Origin == PathOrigin.CurrentDirectory
Expand Down
30 changes: 27 additions & 3 deletions src/CommandLine/ConsoleHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace Orang.CommandLine;

Expand All @@ -29,26 +30,49 @@ public static void WaitForKeyPress(string? message = null)
using (Stream stream = Console.OpenStandardInput())
using (var streamReader = new StreamReader(stream, Console.InputEncoding))
{
return streamReader.ReadToEnd();
Task<string> readToEndTask = streamReader.ReadToEndAsync();

// https://github.com/dotnet/runtime/issues/95079
if (!readToEndTask.Wait(TimeSpan.FromMilliseconds(500)))
return null;

return readToEndTask.Result;
}
}

return null;
}

public static IEnumerable<string> ReadRedirectedInputAsLines()
public static ImmutableArray<string> ReadRedirectedInputAsLines()
{
if (Console.IsInputRedirected)
{
ImmutableArray<string>.Builder lines = ImmutableArray.CreateBuilder<string>();

using (Stream stream = Console.OpenStandardInput())
using (var streamReader = new StreamReader(stream, Console.InputEncoding))
{
Task<string?> readLineTask = streamReader.ReadLineAsync();

// https://github.com/dotnet/runtime/issues/95079
if (!readLineTask.Wait(TimeSpan.FromMilliseconds(500)))
return default;

if (readLineTask.Result is null)
return ImmutableArray<string>.Empty;

lines.Add(readLineTask.Result);

string? line;

while ((line = streamReader.ReadLine()) is not null)
yield return line;
lines.Add(line);
}

return lines.ToImmutableArray();
}

return ImmutableArray<string>.Empty;
}

public static bool AskToExecute(string text, string? indent = null)
Expand Down