Skip to content

Commit 28bed1c

Browse files
committed
code_review: PR #2043
- Opening repo as folder in VS should always be available - Use single `_optionsGenerator` instead of `_execArgsGenerator` and `_subOptionsFinder` - Make sure path is always quoted Signed-off-by: leo <longshuang@msn.cn>
1 parent 3dbab1c commit 28bed1c

File tree

5 files changed

+47
-55
lines changed

5 files changed

+47
-55
lines changed

src/Models/ExternalTool.cs

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ public class ExternalTool
1616
public string ExecFile { get; }
1717
public Bitmap IconImage { get; }
1818

19-
public ExternalTool(string name, string icon, string execFile, Func<string, string> execArgsGenerator = null, Func<string, List<string>> subOptionsFinder = null)
19+
public ExternalTool(string name, string icon, string execFile, Func<string, List<string>> optionsGenerator = null)
2020
{
2121
Name = name;
2222
ExecFile = execFile;
23-
_execArgsGenerator = execArgsGenerator ?? (path => path.Quoted());
24-
_subOptionsFinder = subOptionsFinder;
23+
24+
_optionsGenerator = optionsGenerator;
2525

2626
try
2727
{
@@ -35,30 +35,25 @@ public ExternalTool(string name, string icon, string execFile, Func<string, stri
3535
}
3636
}
3737

38-
public void Open(string path)
38+
public List<string> MakeLaunchOptions(string repo)
3939
{
40-
// The executable file may be removed after the tool list is loaded (once time on startup).
41-
if (!File.Exists(ExecFile))
42-
return;
43-
44-
Process.Start(new ProcessStartInfo()
45-
{
46-
FileName = ExecFile,
47-
Arguments = _execArgsGenerator.Invoke(path),
48-
UseShellExecute = false,
49-
});
40+
return _optionsGenerator?.Invoke(repo) ?? [repo.Quoted()];
5041
}
5142

52-
public List<string> FindSubOptions(string path)
43+
public void Launch(string args)
5344
{
54-
if (_subOptionsFinder == null)
55-
return null;
56-
57-
return _subOptionsFinder.Invoke(path);
45+
if (File.Exists(ExecFile))
46+
{
47+
Process.Start(new ProcessStartInfo()
48+
{
49+
FileName = ExecFile,
50+
Arguments = args,
51+
UseShellExecute = false,
52+
});
53+
}
5854
}
5955

60-
private Func<string, string> _execArgsGenerator = null;
61-
private Func<string, List<string>> _subOptionsFinder = null;
56+
private Func<string, List<string>> _optionsGenerator = null;
6257
}
6358

6459
public class VisualStudioInstance
@@ -140,20 +135,20 @@ public ExternalToolsFinder()
140135
_customization ??= new ExternalToolCustomization();
141136
}
142137

143-
public void TryAdd(string name, string icon, Func<string> finder, Func<string, string> execArgsGenerator = null, Func<string, List<string>> subOptionsFinder = null)
138+
public void TryAdd(string name, string icon, Func<string> finder, Func<string, List<string>> optionsGenerator = null)
144139
{
145140
if (_customization.Excludes.Contains(name))
146141
return;
147142

148143
if (_customization.Tools.TryGetValue(name, out var customPath) && File.Exists(customPath))
149144
{
150-
Tools.Add(new ExternalTool(name, icon, customPath, execArgsGenerator, subOptionsFinder));
145+
Tools.Add(new ExternalTool(name, icon, customPath, optionsGenerator));
151146
}
152147
else
153148
{
154149
var path = finder();
155150
if (!string.IsNullOrEmpty(path) && File.Exists(path))
156-
Tools.Add(new ExternalTool(name, icon, path, execArgsGenerator, subOptionsFinder));
151+
Tools.Add(new ExternalTool(name, icon, path, optionsGenerator));
157152
}
158153
}
159154

src/Native/Windows.cs

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ private void FindVisualStudio(Models.ExternalToolsFinder finder)
401401
{
402402
var exec = instance.ProductPath;
403403
var icon = instance.IsPrerelease ? "vs-preview" : "vs";
404-
finder.TryAdd(instance.DisplayName, icon, () => exec, GenerateCommandlineArgsForVisualStudio, FindVisualStudioSolutions);
404+
finder.TryAdd(instance.DisplayName, icon, () => exec, GenerateVSProjectLaunchOptions);
405405
}
406406
}
407407
}
@@ -444,35 +444,33 @@ private void OpenFolderAndSelectFile(string folderPath)
444444
}
445445
}
446446

447-
private string GenerateCommandlineArgsForVisualStudio(string path)
447+
private List<string> GenerateVSProjectLaunchOptions(string path)
448448
{
449-
var solutions = FindVisualStudioSolutions(path);
450-
return solutions.Count > 0 ? solutions[0].Quoted() : path.Quoted();
451-
}
452-
453-
public List<string> FindVisualStudioSolutions(string path)
454-
{
455-
var solutions = new List<string>();
456-
if (!Directory.Exists(path))
457-
return solutions;
449+
var options = new List<string>();
450+
options.Add(path.Replace("/", "\\").Quoted());
458451

459-
void Search(DirectoryInfo dir, int depth)
452+
if (Directory.Exists(path))
460453
{
461-
if (depth < 0)
462-
return;
454+
void Search(List<string> opts, DirectoryInfo dir, int depth)
455+
{
456+
if (depth < 0)
457+
return;
463458

464-
foreach (var file in dir.GetFiles("*.sln"))
465-
solutions.Add(file.FullName);
459+
foreach (var file in dir.GetFiles())
460+
{
461+
if (file.Name.EndsWith(".sln", StringComparison.OrdinalIgnoreCase) ||
462+
file.Name.EndsWith(".slnx", StringComparison.OrdinalIgnoreCase))
463+
opts.Add(file.FullName.Quoted());
464+
}
466465

467-
foreach (var file in dir.GetFiles("*.slnx"))
468-
solutions.Add(file.FullName);
466+
foreach (var subDir in dir.GetDirectories())
467+
Search(opts, subDir, depth - 1);
468+
}
469469

470-
foreach (var subDir in dir.GetDirectories())
471-
Search(subDir, depth - 1);
470+
Search(options, new DirectoryInfo(path), 4);
472471
}
473-
Search(new DirectoryInfo(path), 4);
474472

475-
return solutions;
473+
return options;
476474
}
477475
}
478476
}

src/ViewModels/CommitDetail.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ await Commands.SaveRevisionFile
343343
if (tool == null)
344344
Native.OS.OpenWithDefaultEditor(tmpFile);
345345
else
346-
tool.Open(tmpFile);
346+
tool.Launch(tmpFile.Quoted());
347347
}
348348

349349
public async Task SaveRevisionFileAsync(Models.Object file, string saveTo)

src/Views/RepositoryToolbar.axaml.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Collections.Generic;
2-
using System.IO;
32

43
using Avalonia.Controls;
54
using Avalonia.Input;
@@ -63,16 +62,16 @@ private void OpenWithExternalTools(object sender, RoutedEventArgs ev)
6362
item.Header = App.Text("Repository.OpenIn", dupTool.Name);
6463
item.Icon = new Image { Width = 16, Height = 16, Source = dupTool.IconImage };
6564

66-
var subOptions = dupTool.FindSubOptions(fullpath);
67-
if (subOptions != null && subOptions.Count > 1)
65+
var options = dupTool.MakeLaunchOptions(fullpath);
66+
if (options.Count > 1)
6867
{
69-
foreach (var subOption in subOptions)
68+
foreach (var opt in options)
7069
{
7170
var subItem = new MenuItem();
72-
subItem.Header = Path.GetFileName(subOption);
71+
subItem.Header = opt.Trim('"');
7372
subItem.Click += (_, e) =>
7473
{
75-
dupTool.Open(subOption);
74+
dupTool.Launch(opt);
7675
e.Handled = true;
7776
};
7877

@@ -85,7 +84,7 @@ private void OpenWithExternalTools(object sender, RoutedEventArgs ev)
8584
{
8685
item.Click += (_, e) =>
8786
{
88-
dupTool.Open(fullpath);
87+
dupTool.Launch(options[0]);
8988
e.Handled = true;
9089
};
9190

src/Views/WorkingCopy.axaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1290,7 +1290,7 @@ private void TryAddOpenFileToContextMenu(ContextMenu menu, string fullpath)
12901290
item.Icon = new Image { Width = 16, Height = 16, Source = tool.IconImage };
12911291
item.Click += (_, e) =>
12921292
{
1293-
tool.Open(fullpath);
1293+
tool.Launch(fullpath.Quoted());
12941294
e.Handled = true;
12951295
};
12961296

0 commit comments

Comments
 (0)