Skip to content

Commit 5f341b2

Browse files
committed
code_style: add extension methods WalkFiles and GetRelativePath for DirectoryInfo
Signed-off-by: leo <longshuang@msn.cn>
1 parent 45a3936 commit 5f341b2

File tree

3 files changed

+39
-43
lines changed

3 files changed

+39
-43
lines changed

src/App.Extensions.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.IO;
23

34
namespace SourceGit
45
{
@@ -23,4 +24,24 @@ public static T Use<T>(this T cmd, Models.ICommandLog log) where T : Commands.Co
2324
return cmd;
2425
}
2526
}
27+
28+
public static class DirectoryInfoExtension
29+
{
30+
public static void WalkFiles(this DirectoryInfo dir, Action<string> onFile, int maxDepth = 4)
31+
{
32+
foreach (var file in dir.GetFiles())
33+
onFile(file.FullName);
34+
35+
if (maxDepth > 0)
36+
{
37+
foreach (var subDir in dir.GetDirectories())
38+
WalkFiles(subDir, onFile, maxDepth - 1);
39+
}
40+
}
41+
42+
public static string GetRelativePath(this DirectoryInfo dir, string fullpath)
43+
{
44+
return fullpath.Substring(dir.FullName.Length).TrimStart(Path.DirectorySeparatorChar);
45+
}
46+
}
2647
}

src/Models/ExternalTool.cs

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -225,27 +225,16 @@ public void FindJetBrainsFromToolbox(Func<string> platformFinder)
225225

226226
private List<ExternalTool.LaunchOption> GenerateVSCodeLaunchOptions(string path)
227227
{
228-
if (!Directory.Exists(path))
228+
var root = new DirectoryInfo(path);
229+
if (!root.Exists)
229230
return null;
230231

231-
void Search(List<ExternalTool.LaunchOption> opts, DirectoryInfo dir, string root, int depth)
232-
{
233-
if (depth < 0)
234-
return;
235-
236-
foreach (var file in dir.GetFiles())
237-
{
238-
if (file.Name.EndsWith(".code-workspace", StringComparison.OrdinalIgnoreCase))
239-
opts.Add(new(Path.GetRelativePath(root, file.FullName), file.FullName.Quoted()));
240-
}
241-
242-
foreach (var subDir in dir.GetDirectories())
243-
Search(opts, subDir, root, depth - 1);
244-
}
245-
246-
var rootDir = new DirectoryInfo(path);
247232
var options = new List<ExternalTool.LaunchOption>();
248-
Search(options, rootDir, rootDir.FullName, 4);
233+
root.WalkFiles(f =>
234+
{
235+
if (f.EndsWith(".code-workspace", StringComparison.OrdinalIgnoreCase))
236+
options.Add(new(root.GetRelativePath(f), f.Quoted()));
237+
});
249238
return options;
250239
}
251240

src/Native/Windows.cs

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -458,32 +458,18 @@ private void OpenFolderAndSelectFile(string folderPath)
458458

459459
private List<Models.ExternalTool.LaunchOption> GenerateVSProjectLaunchOptions(string path)
460460
{
461-
if (Directory.Exists(path))
462-
{
463-
void Search(List<Models.ExternalTool.LaunchOption> opts, DirectoryInfo dir, string root, int depth)
464-
{
465-
if (depth < 0)
466-
return;
467-
468-
foreach (var file in dir.GetFiles())
469-
{
470-
if (file.Name.EndsWith(".sln", StringComparison.OrdinalIgnoreCase) ||
471-
file.Name.EndsWith(".slnx", StringComparison.OrdinalIgnoreCase))
472-
opts.Add(new(Path.GetRelativePath(root, file.FullName), file.FullName.Quoted()));
473-
}
474-
475-
foreach (var subDir in dir.GetDirectories())
476-
Search(opts, subDir, root, depth - 1);
477-
}
478-
479-
var rootDir = new DirectoryInfo(path);
480-
var options = new List<Models.ExternalTool.LaunchOption>();
481-
Search(options, rootDir, rootDir.FullName, 4);
482-
if (options.Count > 0)
483-
return options;
484-
}
461+
var root = new DirectoryInfo(path);
462+
if (!root.Exists)
463+
return null;
485464

486-
return null;
465+
var options = new List<Models.ExternalTool.LaunchOption>();
466+
root.WalkFiles(f =>
467+
{
468+
if (f.EndsWith(".sln", StringComparison.OrdinalIgnoreCase) ||
469+
f.EndsWith(".slnx", StringComparison.OrdinalIgnoreCase))
470+
options.Add(new(root.GetRelativePath(f), f.Quoted()));
471+
});
472+
return options;
487473
}
488474
}
489475
}

0 commit comments

Comments
 (0)