Skip to content

Commit

Permalink
Merge pull request #39 from hakmart1/riderDiff
Browse files Browse the repository at this point in the history
Added JetBrains Rider diff application as a diff program
  • Loading branch information
droyad committed Feb 4, 2023
2 parents f71c499 + 8aefcf8 commit a720444
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
82 changes: 82 additions & 0 deletions src/Assent/Reporters/DiffPrograms/RiderDiffProgram.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;

namespace Assent.Reporters.DiffPrograms;

public class RiderDiffProgram : DiffProgramBase
{
static readonly IReadOnlyList<string> DefaultSearchPaths;

static RiderDiffProgram()
{
var riderChannelsDirectory = Environment.GetEnvironmentVariable("LocalAppData") + "\\JetBrains\\Toolbox\\apps\\Rider\\";
if (!Directory.Exists(riderChannelsDirectory))
{
DefaultSearchPaths = new List<string>();
return;
}

DefaultSearchPaths = Directory.GetDirectories(riderChannelsDirectory).Select(GetRiderExePathInChannel).ToList();
}

public RiderDiffProgram() : base(DefaultSearchPaths) { }

protected override string CreateProcessStartArgs(string receivedFile, string approvedFile) =>
$"\"diff\" \"{receivedFile}\" \"{approvedFile}\"";

protected static string GetRiderExePathInChannel(string channelDirectory)
{
var versions = Directory.GetDirectories(channelDirectory)
.Where(directory => Regex.IsMatch(directory, @".*[0-9]+\.[0-9]+\.[0-9]+$"))
.Select(path => GetRiderVersion(path.Replace(channelDirectory + "\\", "")));

var newestVersion = versions.MaxBy(x => x, new RiderVersionComparer());

return $"{channelDirectory}\\{newestVersion.Major}.{newestVersion.Minor}.{newestVersion.Patch}\\bin\\rider64.exe";
}

protected static RiderVersion GetRiderVersion(string version)
{
var split = version.Split(".");
if (split.Length != 3)
{
//Fallback to empty version
return new RiderVersion(0, 0, 0);
}

return new RiderVersion(int.Parse(split[0]), int.Parse(split[1]), int.Parse(split[2]));
}
}

public record RiderVersion(int Major, int Minor, int Patch);

public class RiderVersionComparer : System.Collections.Generic.IComparer<RiderVersion>
{
public int Compare(RiderVersion x, RiderVersion y)
{
if (x == null)
{
return -1;
}

if (y == null)
{
return 1;
}

if (x.Major - y.Major != 0)
{
return x.Major - y.Major;
}

if (x.Minor - y.Minor != 0)
{
return x.Minor - y.Minor;
}

return x.Patch - y.Patch;
}
}
1 change: 1 addition & 0 deletions src/Assent/Reporters/DiffReporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ static DiffReporter()
new KDiff3DiffProgram(),
new XdiffDiffProgram(),
new P4MergeDiffProgram(),
new RiderDiffProgram(),
new VsCodeDiffProgram(),
new MeldDiffProgram()
}
Expand Down

0 comments on commit a720444

Please sign in to comment.