-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(diff): add 'rpa diff' command to compare 2 files
Signed-off-by: JobaDiniz <[email protected]>
- Loading branch information
Showing
9 changed files
with
146 additions
and
85 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
namespace Joba.IBM.RPA.Cli | ||
{ | ||
internal class TempFile : IDisposable | ||
{ | ||
private readonly FileInfo file; | ||
|
||
private TempFile(FileInfo file) | ||
{ | ||
this.file = file; | ||
} | ||
|
||
internal FileInfo Info => file; | ||
|
||
internal static async Task<TempFile> CreateAsync(WalFile wal, string prefix, CancellationToken cancellation) | ||
{ | ||
var tempDir = new DirectoryInfo(Path.GetTempPath()); | ||
if (!tempDir.Exists) | ||
tempDir.Create(); | ||
|
||
var file = new FileInfo(Path.Combine(tempDir.FullName, $"[{prefix}] {wal.Info.Name}")); | ||
await File.WriteAllTextAsync(file.FullName, wal.ToString(), cancellation); | ||
return new TempFile(file); | ||
} | ||
|
||
internal async Task<string> ReadAsync(CancellationToken cancellation) => await File.ReadAllTextAsync(file.FullName, cancellation); | ||
public static implicit operator FileInfo(TempFile temp) => temp.Info; | ||
|
||
void IDisposable.Dispose() | ||
{ | ||
if (file.Exists) | ||
file.Delete(); | ||
} | ||
} | ||
} |
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,47 @@ | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Joba.IBM.RPA.Cli | ||
{ | ||
internal partial class DiffCommand : Command | ||
{ | ||
internal const string CommandName = "diff"; | ||
|
||
internal DiffCommand() : base(CommandName, "Allows comparing two wal files") | ||
{ | ||
var leftFile = new Argument<FileInfo>("leftFilePath", "The left file to compare. It accepts the full path or relative to the working directory."); | ||
var rightFile = new Argument<FileInfo>("rightFilePath", "The right file to compare. It accepts the full path or relative to the working directory."); | ||
|
||
AddArgument(leftFile); | ||
AddArgument(rightFile); | ||
|
||
this.SetHandler(HandleAsync, leftFile, rightFile, Bind.FromLogger<DiffCommand>(), Bind.FromServiceProvider<InvocationContext>()); | ||
} | ||
|
||
private async Task HandleAsync(FileInfo leftFile, FileInfo rightFile, ILogger<DiffCommand> logger, InvocationContext context) | ||
{ | ||
var cancellation = context.GetCancellationToken(); | ||
var workingDirectory = System.Environment.CurrentDirectory; | ||
var leftPath = leftFile.FullName; | ||
var rightPath = rightFile.FullName; | ||
if (Path.IsPathFullyQualified(leftPath) is false) | ||
leftPath = Path.Combine(workingDirectory, leftPath); | ||
if (Path.IsPathFullyQualified(rightPath) is false) | ||
rightPath = Path.Combine(workingDirectory, rightPath); | ||
|
||
leftFile = new FileInfo(leftPath); | ||
rightFile = new FileInfo(rightPath); | ||
|
||
var leftWal = WalFile.Read(leftFile); | ||
using var leftTxt = await TempFile.CreateAsync(leftWal, "left", cancellation); | ||
logger.LogDebug("Temp created for left {File}", leftTxt.Info); | ||
|
||
var rightWal = WalFile.Read(rightFile); | ||
using var rightTxt = await TempFile.CreateAsync(rightWal, "right", cancellation); | ||
logger.LogDebug("Temp created for right {File}", rightTxt.Info); | ||
|
||
var vsCode = new VsCode(); | ||
logger.LogDebug("Launching Vs Code..."); | ||
await vsCode.DiffAsync(leftTxt, rightTxt, cancellation); | ||
} | ||
} | ||
} |
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
File renamed without changes.
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
File renamed without changes.
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,37 @@ | ||
using System.Diagnostics; | ||
|
||
namespace Joba.IBM.RPA.Cli | ||
{ | ||
internal class VsCode | ||
{ | ||
private const string ExeName = "code"; | ||
|
||
/// <summary> | ||
/// Launches a new session of VSCode to 3-way merge files, according to the <a href="https://code.visualstudio.com/docs/editor/command-line#_core-cli-options">documentation</a>. | ||
/// </summary> | ||
internal async Task MergeAsync(FileInfo leftFile, FileInfo rightFile, FileInfo baseFile, FileInfo resultFile, CancellationToken cancellation) | ||
{ | ||
var arguments = $"-n -m \"{leftFile.FullName}\" \"{rightFile.FullName}\" \"{baseFile.FullName}\" \"{resultFile.FullName}\" --wait"; | ||
var info = new ProcessStartInfo(ExeName, arguments) { UseShellExecute = true, CreateNoWindow = true }; | ||
using var process = Process.Start(info); | ||
if (process == null) | ||
throw new Exception($"Could not start '{ExeName}' tool."); | ||
|
||
await process.WaitForExitAsync(cancellation); | ||
} | ||
|
||
/// <summary> | ||
/// Launches a new session of VSCode to show differences between two files, according to the <a href="https://code.visualstudio.com/docs/editor/command-line#_core-cli-options">documentation</a>. | ||
/// </summary> | ||
internal async Task DiffAsync(FileInfo leftFile, FileInfo rightFile, CancellationToken cancellation) | ||
{ | ||
var arguments = $"-n -d \"{leftFile.FullName}\" \"{rightFile.FullName}\" --wait"; | ||
var info = new ProcessStartInfo(ExeName, arguments) { UseShellExecute = true, CreateNoWindow = true }; | ||
using var process = Process.Start(info); | ||
if (process == null) | ||
throw new Exception($"Could not start '{ExeName}' tool."); | ||
|
||
await process.WaitForExitAsync(cancellation); | ||
} | ||
} | ||
} |
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