-
-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Returning errors from csharpier cli. Make sure we don't hang the old …
…stdout version when cli fails (#1191)
- Loading branch information
Showing
19 changed files
with
207 additions
and
162 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
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
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 @@ | ||
#nullable enable |
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,18 @@ | ||
CSharpier.Cli.Server.FormatFileParameter | ||
CSharpier.Cli.Server.FormatFileParameter.fileContents.get -> string! | ||
CSharpier.Cli.Server.FormatFileParameter.fileContents.set -> void | ||
CSharpier.Cli.Server.FormatFileParameter.fileName.get -> string! | ||
CSharpier.Cli.Server.FormatFileParameter.fileName.set -> void | ||
CSharpier.Cli.Server.FormatFileParameter.FormatFileParameter() -> void | ||
CSharpier.Cli.Server.FormatFileResult | ||
CSharpier.Cli.Server.FormatFileResult.errorMessage.get -> string? | ||
CSharpier.Cli.Server.FormatFileResult.errorMessage.set -> void | ||
CSharpier.Cli.Server.FormatFileResult.FormatFileResult(CSharpier.Cli.Server.Status status) -> void | ||
CSharpier.Cli.Server.FormatFileResult.formattedFile.get -> string? | ||
CSharpier.Cli.Server.FormatFileResult.formattedFile.set -> void | ||
CSharpier.Cli.Server.FormatFileResult.status.get -> CSharpier.Cli.Server.Status | ||
CSharpier.Cli.Server.FormatFileResult.status.set -> void | ||
CSharpier.Cli.Server.Status | ||
CSharpier.Cli.Server.Status.Failed = 2 -> CSharpier.Cli.Server.Status | ||
CSharpier.Cli.Server.Status.Formatted = 0 -> CSharpier.Cli.Server.Status | ||
CSharpier.Cli.Server.Status.Ignored = 1 -> CSharpier.Cli.Server.Status |
68 changes: 68 additions & 0 deletions
68
Src/CSharpier.Cli/Server/CSharpierServiceImplementation.cs
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,68 @@ | ||
namespace CSharpier.Cli.Server; | ||
|
||
using System.IO.Abstractions; | ||
using CSharpier.Cli.Options; | ||
|
||
internal class CSharpierServiceImplementation | ||
{ | ||
private readonly string? configPath; | ||
private readonly IFileSystem fileSystem; | ||
private readonly ConsoleLogger logger; | ||
|
||
public CSharpierServiceImplementation(string? configPath, ConsoleLogger logger) | ||
{ | ||
this.configPath = configPath; | ||
this.logger = logger; | ||
this.fileSystem = new FileSystem(); | ||
} | ||
|
||
public async Task<FormatFileResult> FormatFile( | ||
FormatFileParameter formatFileParameter, | ||
CancellationToken cancellationToken | ||
) | ||
{ | ||
try | ||
{ | ||
var directoryName = this.fileSystem.Path.GetDirectoryName(formatFileParameter.fileName); | ||
DebugLogger.Log(directoryName ?? string.Empty); | ||
if (directoryName == null) | ||
{ | ||
throw new Exception( | ||
$"There was no directory found for file {formatFileParameter.fileName}" | ||
); | ||
} | ||
|
||
var optionsProvider = await OptionsProvider.Create( | ||
directoryName, | ||
this.configPath, | ||
this.fileSystem, | ||
this.logger, | ||
cancellationToken | ||
); | ||
|
||
if ( | ||
GeneratedCodeUtilities.IsGeneratedCodeFile(formatFileParameter.fileName) | ||
|| optionsProvider.IsIgnored(formatFileParameter.fileName) | ||
) | ||
{ | ||
return new FormatFileResult(Status.Ignored); | ||
} | ||
|
||
var result = await CSharpFormatter.FormatAsync( | ||
formatFileParameter.fileContents, | ||
optionsProvider.GetPrinterOptionsFor(formatFileParameter.fileName), | ||
cancellationToken | ||
); | ||
|
||
return new FormatFileResult(Status.Formatted) { formattedFile = result.Code }; | ||
} | ||
catch (Exception ex) | ||
{ | ||
DebugLogger.Log(ex.ToString()); | ||
return new FormatFileResult(Status.Failed) | ||
{ | ||
errorMessage = "An exception was thrown\n" + ex | ||
}; | ||
} | ||
} | ||
} |
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,7 @@ | ||
namespace CSharpier.Cli.Server; | ||
|
||
public class FormatFileParameter | ||
{ | ||
public required string fileContents { get; set; } | ||
public required string fileName { get; set; } | ||
} |
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,12 @@ | ||
namespace CSharpier.Cli.Server; | ||
|
||
using System.Text.Json.Serialization; | ||
|
||
public class FormatFileResult(Status status) | ||
{ | ||
public string? formattedFile { get; set; } | ||
|
||
[JsonConverter(typeof(JsonStringEnumConverter))] | ||
public Status status { get; set; } = status; | ||
public string? errorMessage { get; set; } | ||
} |
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,68 @@ | ||
namespace CSharpier.Cli.Server; | ||
|
||
using System.Net; | ||
using System.Net.NetworkInformation; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.Extensions.Logging; | ||
|
||
internal static class ServerFormatter | ||
{ | ||
public static async Task<int> StartServer( | ||
int? port, | ||
ConsoleLogger logger, | ||
string? actualConfigPath, | ||
CancellationToken cancellationToken | ||
) | ||
{ | ||
var thePort = port ?? FindFreePort(); | ||
var builder = WebApplication.CreateBuilder(); | ||
builder.WebHost.ConfigureKestrel( | ||
(_, serverOptions) => | ||
{ | ||
serverOptions.Listen(IPAddress.Loopback, thePort); | ||
} | ||
); | ||
|
||
var app = builder.Build(); | ||
var service = new CSharpierServiceImplementation(actualConfigPath, logger); | ||
app.MapPost( | ||
"/format", | ||
(FormatFileParameter formatFileDto, CancellationToken cancellationToken) => | ||
service.FormatFile(formatFileDto, cancellationToken) | ||
); | ||
logger.LogInformation("Started on " + thePort); | ||
|
||
await app.RunAsync(); | ||
|
||
Console.ReadKey(); | ||
|
||
return 0; | ||
} | ||
|
||
public static int FindFreePort() | ||
{ | ||
const int startPort = 49152; | ||
const int endPort = 65535; | ||
var ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties(); | ||
var tcpConnInfoArray = ipGlobalProperties.GetActiveTcpConnections(); | ||
var ipEndPoint = ipGlobalProperties.GetActiveTcpListeners(); | ||
|
||
var usedPorts = ipEndPoint | ||
.Select(o => o.Port) | ||
.Concat(tcpConnInfoArray.Select(o => o.LocalEndPoint.Port)) | ||
.ToList(); | ||
|
||
for (var i = startPort; i < endPort; i++) | ||
{ | ||
if (!usedPorts.Contains(i)) | ||
{ | ||
return i; | ||
} | ||
} | ||
|
||
throw new InvalidOperationException( | ||
$"Could not find any free TCP port between ports {startPort}-{endPort}" | ||
); | ||
} | ||
} |
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,8 @@ | ||
namespace CSharpier.Cli.Server; | ||
|
||
public enum Status | ||
{ | ||
Formatted, | ||
Ignored, | ||
Failed | ||
} |
Oops, something went wrong.