Skip to content

Commit 70374bb

Browse files
authored
add tool detection for re-launching (#253)
1 parent faf15f1 commit 70374bb

4 files changed

Lines changed: 63 additions & 14 deletions

File tree

src/DiffEngine/DiffTools.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace DiffEngine
99
public static class DiffTools
1010
{
1111
static Dictionary<string, ResolvedTool> ExtensionLookup = new();
12+
static Dictionary<string, ResolvedTool> PathLookup = new();
1213
static List<ResolvedTool> resolved = new();
1314

1415
public static IEnumerable<ResolvedTool> Resolved
@@ -178,6 +179,7 @@ static void AddResolvedToolAtStart(ResolvedTool resolvedTool)
178179
var cleanedExtension = Extensions.GetExtension(extension);
179180
ExtensionLookup[cleanedExtension] = resolvedTool;
180181
}
182+
PathLookup[resolvedTool.ExePath] = resolvedTool;
181183
}
182184

183185
static DiffTools()
@@ -198,6 +200,7 @@ static void InitTools(bool resultFoundInEnvVar, IEnumerable<DiffTool> tools)
198200
{
199201
var custom = resolved.Where(x => x.Tool == null).ToList();
200202
ExtensionLookup.Clear();
203+
PathLookup.Clear();
201204
resolved.Clear();
202205

203206
foreach (var tool in ToolsOrder.Sort(resultFoundInEnvVar, tools).Reverse())
@@ -234,6 +237,13 @@ public static void UseOrder(in bool throwForNoTool, params DiffTool[] order)
234237
InitTools(throwForNoTool, order);
235238
}
236239

240+
public static bool TryFindByPath(
241+
string path,
242+
[NotNullWhen(true)] out ResolvedTool? tool)
243+
{
244+
return PathLookup.TryGetValue(path, out tool);
245+
}
246+
237247
public static bool TryFind(
238248
string extension,
239249
[NotNullWhen(true)] out ResolvedTool? tool)

src/DiffEngine/ResolvedTool.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public string BuildCommand(string tempFile, string targetFile)
2828
return $"\"{ExePath}\" {GetArguments(tempFile, targetFile)}";
2929
}
3030

31-
string GetArguments(string tempFile, string targetFile)
31+
public string GetArguments(string tempFile, string targetFile)
3232
{
3333
if (TargetPosition.TargetOnLeft)
3434
{
@@ -38,7 +38,6 @@ string GetArguments(string tempFile, string targetFile)
3838
return TargetRightArguments(tempFile, targetFile);
3939
}
4040

41-
4241
internal ResolvedTool(string name,
4342
DiffTool? tool,
4443
string exePath,

src/DiffEngineTray/TrackedMove.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,21 @@
33

44
class TrackedMove
55
{
6-
public TrackedMove(
7-
string temp,
6+
public TrackedMove(string temp,
87
string target,
98
string? exe,
109
string? arguments,
1110
bool canKill,
1211
Process? process,
13-
string? group)
12+
string? group,
13+
string extension)
1414
{
1515
Temp = temp;
1616
Target = target;
1717
Exe = exe;
1818
Arguments = arguments;
1919
Name = Path.GetFileNameWithoutExtension(Path.GetFileNameWithoutExtension(target));
20-
Extension = Path.GetExtension(target).TrimStart('.');
20+
Extension = extension;
2121
CanKill = canKill;
2222
Process = process;
2323
Group = group;

src/DiffEngineTray/Tracker.cs

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Linq;
77
using System.Threading;
88
using System.Threading.Tasks;
9+
using DiffEngine;
910
using Serilog;
1011

1112
class Tracker :
@@ -116,10 +117,18 @@ public TrackedMove AddMove(
116117
ProcessEx.TryGet(processId.Value, out process);
117118
}
118119

119-
Log.Information("MoveAdded. Target:{target}, CanKill:{canKill}, Process:{process}, Command:{command}", targetFile, canKill, processId, $"{exeFile} {arguments}");
120+
var move = BuildTrackedMove(temp, exe, arguments, canKill, target, process);
120121

121-
var solution = SolutionDirectoryFinder.Find(target);
122-
return BuildTrackedMove(temp, exe, arguments, canKill, target, process, solution);
122+
if (exeFile == null)
123+
{
124+
Log.Information("MoveAdded. Target:{target}, CanKill:{canKill}, Process:{process}", targetFile, move.CanKill, processId);
125+
}
126+
else
127+
{
128+
Log.Information("MoveAdded. Target:{target}, CanKill:{canKill}, Process:{process}, Command:{command}", targetFile, move.CanKill, processId, $"{exeFile} {arguments}");
129+
}
130+
131+
return move;
123132
},
124133
updateValueFactory: (target, existing) =>
125134
{
@@ -134,16 +143,47 @@ public TrackedMove AddMove(
134143
ProcessEx.TryGet(processId.Value, out process);
135144
}
136145

137-
Log.Information("MoveUpdated. Target:{target}, CanKill:{canKill}, Process:{process}, Command:{command}", targetFile, canKill, processId, $"{exeFile} {arguments}");
146+
var move = BuildTrackedMove(temp, exe, arguments, canKill, target, process);
147+
148+
if (exeFile == null)
149+
{
150+
Log.Information("MoveUpdated. Target:{target}, CanKill:{canKill}, Process:{process}", targetFile, move.CanKill, processId);
151+
}
152+
else
153+
{
154+
Log.Information("MoveUpdated. Target:{target}, CanKill:{canKill}, Process:{process}, Command:{command}", targetFile, move.CanKill, processId, $"{exeFile} {arguments}");
155+
}
138156

139-
var solution = SolutionDirectoryFinder.Find(target);
140-
return BuildTrackedMove(temp, exe, arguments, canKill, target, process, solution);
157+
return move;
141158
});
142159
}
143160

144-
static TrackedMove BuildTrackedMove(string temp, string? exe, string? arguments, bool canKill, string target, Process? process, string? solution)
161+
static TrackedMove BuildTrackedMove(string temp, string? exe, string? arguments, bool? canKill, string target, Process? process)
145162
{
146-
return new(temp, target, exe, arguments, canKill, process, solution);
163+
var solution = SolutionDirectoryFinder.Find(target);
164+
var extension = Path.GetExtension(target).TrimStart('.');
165+
if (exe == null)
166+
{
167+
if(DiffTools.TryFind(extension, out var tool))
168+
{
169+
arguments = tool.GetArguments(temp, target);
170+
exe = tool.ExePath;
171+
canKill = !tool.IsMdi;
172+
}
173+
}
174+
else if (canKill == null)
175+
{
176+
if (DiffTools.TryFindByPath(exe, out var tool))
177+
{
178+
canKill = !tool.IsMdi;
179+
}
180+
else
181+
{
182+
canKill = false;
183+
}
184+
}
185+
186+
return new(temp, target, exe, arguments, canKill.GetValueOrDefault(false), process, solution, extension);
147187
}
148188

149189
public TrackedDelete AddDelete(string file)

0 commit comments

Comments
 (0)