Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Split out DiffEngine nuget #36

Merged
merged 15 commits into from Feb 2, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/diff-tool.md
Expand Up @@ -62,7 +62,7 @@ This behavior is currently supported on Windows. On Linux and OSX, diff tool ins

## Supported Diff tools:

<!-- include: diffTools. path: /src/Verify.Tests/DiffTool/diffTools.include.md -->
<!-- include: diffTools. path: /src/DiffEngine.Tests/diffTools.include.md -->
## [VisualStudio](https://docs.microsoft.com/en-us/visualstudio/ide/reference/diff)

* IsMdi: True
Expand Down Expand Up @@ -248,7 +248,7 @@ This behavior is currently supported on Windows. On Linux and OSX, diff tool ins
* jpg
* png
* tif
* rtf <!-- end include: diffTools. path: /src/Verify.Tests/DiffTool/diffTools.include.md -->
* rtf <!-- end include: diffTools. path: /src/DiffEngine.Tests/diffTools.include.md -->


## Disable Diff
Expand Down
40 changes: 40 additions & 0 deletions src/DiffEngine.Tests/DiffEngine.Tests.csproj
@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netcoreapp3.1;net48</TargetFrameworks>
<GenerateAssemblyInfo>true</GenerateAssemblyInfo>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />
<PackageReference Include="MarkdownSnippets.MsBuild" Version="18.0.0" PrivateAssets="all" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="System.Drawing.Common" Version="4.7.0" />
<PackageReference Include="Xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
<PackageReference Include="ProjectDefaults" Version="1.0.41" PrivateAssets="All" />
<ProjectReference Include="..\TargetLibrary\TargetLibrary.csproj" />
<ProjectReference Include="..\Verify.Xunit\Verify.Xunit.csproj" />
<ProjectReference Include="..\Verify\Verify.csproj" />
</ItemGroup>
<ItemGroup>
<None Update="DiffToolsTest.AfterJson.verified.txt">
<DependentUpon>$(
[System.String]::Copy('%(FileName)')
.SubString(
0,
$([System.String]::Copy('%(FileName)').IndexOf('.'))
)
)$(ProjectExt.Replace('proj', ''))</DependentUpon>
</None>
<None Update="DiffToolsTest.Simple.verified.txt">
<DependentUpon>$(
[System.String]::Copy('%(FileName)')
.SubString(
0,
$([System.String]::Copy('%(FileName)').IndexOf('.'))
)
)$(ProjectExt.Replace('proj', ''))</DependentUpon>
</None>
</ItemGroup>
</Project>
@@ -1,4 +1,5 @@
using System.IO;
using DiffEngine;
using VerifyXunit;
using Xunit;
using Xunit.Abstractions;
Expand All @@ -9,12 +10,9 @@ public class DiffRunnerTests :
[Fact(Skip = "Explicit")]
public void Launch()
{
if (!DiffTools.TryFind("txt", out var tool))
{
return;
}
var file = new FilePair("txt", Path.Combine(SourceDirectory, "DiffRunner"));
DiffRunner.Launch(tool, file);
DiffRunner.TryLaunch("txt",
Path.Combine(SourceDirectory, "DiffRunner.file1.txt"),
Path.Combine(SourceDirectory, "DiffRunner.file2.txt"));
}

public DiffRunnerTests(ITestOutputHelper output) :
Expand Down
Expand Up @@ -2,6 +2,7 @@
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using DiffEngine;
using VerifyXunit;
using Xunit;
using Xunit.Abstractions;
Expand Down Expand Up @@ -88,13 +89,14 @@ public async Task WriteFoundTools()
// }
//}

[Fact(Skip="reason")]
[Fact(Skip = "reason")]
public void LaunchTextDiff()
{
foreach (var tool in DiffTools.ResolvedDiffTools)
{
var file = new FilePair("txt", Path.Combine(SourceDirectory, "input"));
DiffRunner.Launch(tool,file);
DiffRunner.Launch(tool,
Path.Combine(SourceDirectory, "input.file1.txt"),
Path.Combine(SourceDirectory, "input.file2.txt"));
}
}

Expand Down
@@ -1,4 +1,5 @@
using VerifyXunit;
using DiffEngine;
using VerifyXunit;
using Xunit;
using Xunit.Abstractions;

Expand Down
@@ -1,5 +1,6 @@
using System.Diagnostics;
using System.Linq;
using DiffEngine;
using VerifyXunit;
using Xunit;
using Xunit.Abstractions;
Expand Down
15 changes: 15 additions & 0 deletions src/DiffEngine/DiffEngine.csproj
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;netstandard2.1;net472</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<Compile Include="..\Verify\Helpers\Guard.cs" Link="Guard.cs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="EmptyFiles" Version="0.7.0" PrivateAssets="None" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" Condition="$(Configuration) == 'Release'" />
<PackageReference Include="ProjectDefaults" Version="1.0.41" PrivateAssets="All" />
<PackageReference Include="Nullable" Version="1.2.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.Windows.Compatibility" Version="3.1.0" />
</ItemGroup>
</Project>
86 changes: 86 additions & 0 deletions src/DiffEngine/DiffRunner.cs
@@ -0,0 +1,86 @@
using System.Diagnostics;
using System.IO;

namespace DiffEngine
{
public static class DiffRunner
{
public static void TryKillProcessIfSupported(string extension, string path1, string path2)
{
Guard.AgainstNullOrEmpty(extension, nameof(extension));
Guard.AgainstNullOrEmpty(path1, nameof(path1));
Guard.AgainstNullOrEmpty(path2, nameof(path2));
if (!DiffTools.TryFind(extension, out var diffTool))
{
return;
}
var command = diffTool.BuildCommand(path1, path2);

if (diffTool.IsMdi)
{
return;
}

ProcessCleanup.Kill(command);
}

public static void TryLaunch(string extension, string path1, string path2)
{
Guard.AgainstNullOrEmpty(path1, nameof(path1));
Guard.AgainstNullOrEmpty(path2, nameof(path2));
if (!DiffTools.TryFind(extension, out var diffTool))
{
return;
}
//TODO: throw if both dont exist
if (!File.Exists(path1))
{
if (!EmptyFilesWrapper.TryWriteEmptyFile(extension, path1))
{
return;
}
}

if (!File.Exists(path2))
{
if (!EmptyFilesWrapper.TryWriteEmptyFile(extension, path2))
{
return;
}
}

Launch(diffTool, path1, path2);
}

internal static void Launch(ResolvedDiffTool tool, string path1, string path2)
{
Guard.AgainstNull(tool, nameof(tool));
var command = tool.BuildCommand(path1, path2);
var isDiffToolRunning = ProcessCleanup.IsRunning(command);
if (isDiffToolRunning)
{
if (tool.SupportsAutoRefresh)
{
return;
}

if (!tool.IsMdi)
{
ProcessCleanup.Kill(command);
}
}

using var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = tool.ExePath,
Arguments = tool.BuildArguments(path1, path2),
UseShellExecute = false,
CreateNoWindow = false,
}
};
process.StartWithCatch();
}
}
}
Expand Up @@ -8,7 +8,7 @@ class DiffTool
public string Url { get; }
public bool SupportsAutoRefresh { get; }
public bool IsMdi { get; }
public Func<FilePair, string> BuildArguments { get; }
public Func<string, string, string> BuildArguments { get; }
public string[] BinaryExtensions { get; }
public string? ExePath { get; private set; }
public bool Exists { get; private set; }
Expand All @@ -21,7 +21,7 @@ class DiffTool
string url,
bool supportsAutoRefresh,
bool isMdi,
Func<FilePair,string> buildArguments,
Func<string, string, string> buildArguments,
string[] windowsExePaths,
string[] binaryExtensions,
string[] linuxExePaths,
Expand Down Expand Up @@ -72,6 +72,7 @@ void FindExe(string[] exePaths)
{
continue;
}

ExePath = expanded;
Exists = true;
return;
Expand Down
28 changes: 12 additions & 16 deletions src/Verify/DiffTool/DiffTools.cs → src/DiffEngine/DiffTools.cs
@@ -1,35 +1,31 @@
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using DiffEngine;

static partial class DiffTools
static class DiffTools
{
public static Dictionary<string, ResolvedDiffTool> ExtensionLookup = new Dictionary<string, ResolvedDiffTool>();
public static List<ResolvedDiffTool> ResolvedDiffTools = new List<ResolvedDiffTool>();

public static List<DiffTool> Tools()
internal static List<DiffTool> Tools()
{
return new List<DiffTool>
{
VisualStudio(),
VsCode(),
WinMerge(),
CodeCompare(),
SublimeMerge(),
Meld(),
AraxisMerge(),
P4Merge(),
BeyondCompare(),
Implementation.VisualStudio(),
Implementation.VsCode(),
Implementation.WinMerge(),
Implementation.CodeCompare(),
Implementation.SublimeMerge(),
Implementation.Meld(),
Implementation.AraxisMerge(),
Implementation.P4Merge(),
Implementation.BeyondCompare()
};
}

static DiffTools()
{
if (BuildServerDetector.Detected)
{
return;
}

foreach (var tool in Tools().Where(x => x.Exists))
{
var diffTool = new ResolvedDiffTool(
Expand Down
@@ -1,4 +1,5 @@
using System.IO;
using DiffEngine;

static class EmptyFilesWrapper
{
Expand Down