-
-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
1,087 additions
and
153 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
namespace CSharpier.Cli.Tests; | ||
|
||
using System.Text; | ||
using CliWrap; | ||
using CliWrap.Buffered; | ||
|
||
public class CsharpierProcess | ||
{ | ||
private static readonly string testFileDirectory = Path.Combine( | ||
Directory.GetCurrentDirectory(), | ||
"TestFiles" | ||
); | ||
|
||
private readonly StringBuilder output = new(); | ||
private readonly StringBuilder errorOutput = new(); | ||
private Command command; | ||
|
||
private readonly Encoding encoding = Encoding.UTF8; | ||
|
||
public CsharpierProcess() | ||
{ | ||
var path = Path.Combine(Directory.GetCurrentDirectory(), "dotnet-csharpier.dll"); | ||
|
||
this.command = Cli | ||
.Wrap("dotnet") | ||
.WithArguments(path) | ||
.WithWorkingDirectory(testFileDirectory) | ||
.WithValidation(CommandResultValidation.None) | ||
.WithStandardOutputPipe(PipeTarget.ToStringBuilder(this.output, this.encoding)) | ||
.WithStandardErrorPipe(PipeTarget.ToStringBuilder(this.errorOutput, this.encoding)); | ||
} | ||
|
||
public CsharpierProcess WithArguments(string arguments) | ||
{ | ||
this.command = this.command.WithArguments(this.command.Arguments + " " + arguments); | ||
return this; | ||
} | ||
|
||
public CsharpierProcess WithPipedInput(string input) | ||
{ | ||
this.command = this.command.WithStandardInputPipe( | ||
PipeSource.FromString(input, this.encoding) | ||
); | ||
|
||
return this; | ||
} | ||
|
||
public async Task<ProcessResult> ExecuteAsync() | ||
{ | ||
var result = await this.command.ExecuteBufferedAsync(this.encoding); | ||
return new ProcessResult( | ||
this.output.ToString(), | ||
this.errorOutput.ToString(), | ||
result.ExitCode | ||
); | ||
} | ||
|
||
public record ProcessResult(string Output, string ErrorOutput, int ExitCode); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
namespace CSharpier.Cli.Tests; | ||
|
||
using System.Diagnostics; | ||
using System.Text; | ||
using CSharpierService.Generated; | ||
using FluentAssertions; | ||
using Grpc.Core; | ||
using NUnit.Framework; | ||
|
||
[TestFixture] | ||
public class GrpcTests | ||
{ | ||
// can this used named pipes instead of ports? | ||
// may not be happy in VSCode, see this, and try getting it working in js | ||
// https://gist.github.com/badsyntax/9827722afcb33a4b0e03c809f1aede98 | ||
// also make sure it will work in java | ||
[Test] | ||
public async Task Stuff() | ||
{ | ||
var path = Path.Combine(Directory.GetCurrentDirectory(), "dotnet-csharpier.dll"); | ||
|
||
var processStartInfo = new ProcessStartInfo("dotnet", $"{path} --named-pipe") | ||
{ | ||
RedirectStandardInput = true, | ||
RedirectStandardOutput = true, | ||
RedirectStandardError = true, | ||
StandardOutputEncoding = Encoding.UTF8, | ||
StandardErrorEncoding = Encoding.UTF8, | ||
UseShellExecute = false, | ||
CreateNoWindow = true | ||
}; | ||
|
||
processStartInfo.EnvironmentVariables["DOTNET_NOLOGO"] = "1"; | ||
var process = new Process { StartInfo = processStartInfo }; | ||
process.Start(); | ||
|
||
var output = await process.StandardOutput.ReadLineAsync(); | ||
|
||
var channel = new Channel("localhost", 50052, ChannelCredentials.Insecure); | ||
var client = new CSharpierService.CSharpierServiceClient(channel); | ||
|
||
var data = new FormatFileDto { FileName = "test.cs", FileContents = "public class TestClass { }"}; | ||
var result = await client.FormatFileAsync(data); | ||
|
||
result.FormattedFile.Should().Be("public class TestClass { }"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
namespace CSharpier.Cli; | ||
|
||
using System.IO.Abstractions; | ||
using System.Text; | ||
using Microsoft.Extensions.Logging; | ||
|
||
public class PipingFormatter | ||
{ | ||
public static async Task<int> PipeMultipleFiles( | ||
SystemConsole console, | ||
ILogger logger, | ||
string? configPath, | ||
CancellationToken cancellationToken | ||
) | ||
{ | ||
using var streamReader = new StreamReader( | ||
Console.OpenStandardInput(), | ||
console.InputEncoding | ||
); | ||
|
||
var stringBuilder = new StringBuilder(); | ||
string? fileName = null; | ||
|
||
var exitCode = 0; | ||
|
||
while (true) | ||
{ | ||
while (true) | ||
{ | ||
var value = streamReader.Read(); | ||
if (value == -1) | ||
{ | ||
return exitCode; | ||
} | ||
var character = Convert.ToChar(value); | ||
DebugLogger.Log("Got " + character); | ||
if (character == '\u0003') | ||
{ | ||
DebugLogger.Log("Got EOF"); | ||
break; | ||
} | ||
|
||
stringBuilder.Append(character); | ||
} | ||
|
||
if (fileName == null) | ||
{ | ||
fileName = stringBuilder.ToString(); | ||
stringBuilder.Clear(); | ||
} | ||
else | ||
{ | ||
var commandLineOptions = new CommandLineOptions | ||
{ | ||
DirectoryOrFilePaths = new[] | ||
{ | ||
Path.Combine(Directory.GetCurrentDirectory(), fileName) | ||
}, | ||
OriginalDirectoryOrFilePaths = new[] | ||
{ | ||
Path.IsPathRooted(fileName) | ||
? fileName | ||
: fileName.StartsWith(".") | ||
? fileName | ||
: "./" + fileName | ||
}, | ||
StandardInFileContents = stringBuilder.ToString(), | ||
Fast = true, | ||
WriteStdout = true, | ||
ConfigPath = configPath | ||
}; | ||
|
||
try | ||
{ | ||
var result = await CommandLineFormatter.Format( | ||
commandLineOptions, | ||
new FileSystem(), | ||
console, | ||
logger, | ||
cancellationToken | ||
); | ||
|
||
console.Write('\u0003'.ToString()); | ||
|
||
if (result != 0) | ||
{ | ||
exitCode = result; | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
logger.LogError(ex, "Failed!"); | ||
} | ||
|
||
stringBuilder.Clear(); | ||
fileName = null; | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.