-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #54 from kysect/feat/run-dotnet-format
Implement dotnet format execution in separated process
- Loading branch information
Showing
6 changed files
with
178 additions
and
0 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
Sources/Kysect.Configuin.Core/CliExecution/CmdExecutionResult.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,32 @@ | ||
namespace Kysect.Configuin.Core.CliExecution; | ||
|
||
public class CmdExecutionResult | ||
{ | ||
public int ExitCode { get; init; } | ||
public IReadOnlyCollection<string> Errors { get; init; } | ||
|
||
public CmdExecutionResult(int exitCode, IReadOnlyCollection<string> errors) | ||
{ | ||
ExitCode = exitCode; | ||
Errors = errors; | ||
} | ||
|
||
public void ThrowIfAnyError() | ||
{ | ||
if (Errors.Count == 1) | ||
throw new CmdProcessException(Errors.Single()); | ||
|
||
if (Errors.Count > 0) | ||
{ | ||
var exceptions = Errors | ||
.Select(m => new CmdProcessException(m)) | ||
.ToList(); | ||
throw new AggregateException(exceptions); | ||
} | ||
|
||
if (ExitCode != 0) | ||
{ | ||
throw new CmdProcessException($"Return {ExitCode} exit code."); | ||
} | ||
} | ||
} |
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,67 @@ | ||
using System.Diagnostics; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace Kysect.Configuin.Core.CliExecution; | ||
|
||
public class CmdProcess | ||
{ | ||
public CmdExecutionResult ExecuteCommand(string command) | ||
{ | ||
using var process = new Process(); | ||
|
||
ProcessStartInfo startInfo = CreateProcessStartInfo(command); | ||
|
||
process.StartInfo = startInfo; | ||
process.Start(); | ||
process.WaitForExit(); | ||
|
||
int exitCode = process.ExitCode; | ||
IReadOnlyCollection<string> errors = GetErrors(process); | ||
process.Close(); | ||
|
||
return new CmdExecutionResult(exitCode, errors); | ||
} | ||
|
||
private ProcessStartInfo CreateProcessStartInfo(string command) | ||
{ | ||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) | ||
{ | ||
return new ProcessStartInfo | ||
{ | ||
WindowStyle = ProcessWindowStyle.Hidden, | ||
RedirectStandardError = true, | ||
FileName = "cmd.exe", | ||
Arguments = $"/C {command}" | ||
}; | ||
} | ||
|
||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) | ||
{ | ||
return new ProcessStartInfo | ||
{ | ||
WindowStyle = ProcessWindowStyle.Hidden, | ||
RedirectStandardError = true, | ||
FileName = "sh", | ||
Arguments = $"-c {command}" | ||
}; | ||
} | ||
|
||
throw new NotSupportedException(RuntimeInformation.OSDescription); | ||
} | ||
|
||
private IReadOnlyCollection<string> GetErrors(Process process) | ||
{ | ||
var errors = new List<string>(); | ||
|
||
// TODO: fixed error stream reading | ||
// Line splitting triggered by char limit =_= | ||
while (!process.StandardError.EndOfStream) | ||
{ | ||
string? line = process.StandardError.ReadLine(); | ||
if (line is not null) | ||
errors.Add(line); | ||
} | ||
|
||
return errors; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
Sources/Kysect.Configuin.Core/CliExecution/CmdProcessException.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,12 @@ | ||
namespace Kysect.Configuin.Core.CliExecution; | ||
|
||
public class CmdProcessException : Exception | ||
{ | ||
public CmdProcessException(string message) : base(message) | ||
{ | ||
} | ||
|
||
public CmdProcessException() : base("Failed to execute cmd command") | ||
{ | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
Sources/Kysect.Configuin.Core/DotnetFormat/DotnetFormatCli.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,19 @@ | ||
using Kysect.Configuin.Core.CliExecution; | ||
|
||
namespace Kysect.Configuin.Core.DotnetFormat; | ||
|
||
public class DotnetFormatCli | ||
{ | ||
private readonly CmdProcess _cmdProcess = new CmdProcess(); | ||
|
||
public void Validate() | ||
{ | ||
_cmdProcess.ExecuteCommand("dotnet format -h").ThrowIfAnyError(); | ||
} | ||
|
||
public void GenerateWarnings(string pathToSolution, string pathToJson) | ||
{ | ||
|
||
_cmdProcess.ExecuteCommand($"dotnet format \"{pathToSolution}\" --verify-no-changes --report \"{pathToJson}\"").ThrowIfAnyError(); | ||
} | ||
} |
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 @@ | ||
using FluentAssertions; | ||
using Kysect.Configuin.Core.CliExecution; | ||
using NUnit.Framework; | ||
|
||
namespace Kysect.Configuin.Tests; | ||
|
||
public class CmdProcessTests | ||
{ | ||
[Test] | ||
public void Execute_ForInvalidCommand_ThrowError() | ||
{ | ||
var cmdProcess = new CmdProcess(); | ||
|
||
CmdExecutionResult cmdExecutionResult = cmdProcess.ExecuteCommand("qwerasdf1234"); | ||
|
||
cmdExecutionResult.ExitCode.Should().NotBe(0); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
Sources/Kysect.Configuin.Tests/DotnetFormat/DotnetFormatCliTests.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,30 @@ | ||
using Kysect.Configuin.Core.DotnetFormat; | ||
using NUnit.Framework; | ||
|
||
namespace Kysect.Configuin.Tests.DotnetFormat; | ||
|
||
public class DotnetFormatCliTests | ||
{ | ||
private readonly DotnetFormatCli _dotnetFormatCli; | ||
|
||
public DotnetFormatCliTests() | ||
{ | ||
_dotnetFormatCli = new DotnetFormatCli(); | ||
} | ||
|
||
[Test] | ||
[Ignore("This test require infrastructure")] | ||
public void Validate_FinishedWithoutErrors() | ||
{ | ||
_dotnetFormatCli.Validate(); | ||
} | ||
|
||
[Test] | ||
[Ignore("This test require infrastructure")] | ||
public void GenerateWarnings_CreateReportFile() | ||
{ | ||
const string pathToSln = "./../../../../"; | ||
|
||
_dotnetFormatCli.GenerateWarnings(pathToSln, "sample.json"); | ||
} | ||
} |