-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommand.cs
More file actions
104 lines (87 loc) · 3.44 KB
/
Command.cs
File metadata and controls
104 lines (87 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
using LibGit2Sharp;
using System.Diagnostics;
namespace EyePatch
{
public abstract class Command
{
public abstract void Execute(Settings settings, string? arg = null);
internal virtual string EnsurePatchesDirectoryExists(Settings settings)
{
var patchesPath = settings.PatchDirectory;
if (string.IsNullOrEmpty(patchesPath))
{
// Fallback to OneDrive path from the environment variable
var oneDrivePath = Environment.GetEnvironmentVariable("OneDrive");
if (string.IsNullOrEmpty(oneDrivePath))
{
throw new InvalidOperationException("OneDrive is not configured on this system. Either install it or configure a patch directory.");
}
// Construct the full path to the "patches" directory
patchesPath = Path.Combine(oneDrivePath, "patches");
}
// Ensure the "patches" directory exists
if (!Directory.Exists(patchesPath))
{
Directory.CreateDirectory(patchesPath);
}
// Return the full path
return patchesPath;
}
internal virtual void LaunchDiffTool(Settings settings, string tempFolder, List<string> diffFilePairs)
{
var diffFileListPath = Path.Combine(tempFolder, "diff_file_list.txt");
File.WriteAllLines(diffFileListPath, diffFilePairs);
var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
Arguments = $"/c {settings.DiffApp} -I \"{diffFileListPath}\"",
UseShellExecute = false,
CreateNoWindow = true,
}
};
ConsoleWriter.WriteSuccess($"\nAll done. Waiting on diff window to close...");
process.Start();
process.WaitForExit();
}
internal virtual string CreateTempFolder()
{
var tempFolder = Path.Combine(Path.GetTempPath(), $"EyePatch-{Guid.NewGuid()}");
// Clear the directory if it already exists
if (Directory.Exists(tempFolder))
{
Directory.Delete(tempFolder, true);
}
Directory.CreateDirectory(tempFolder);
return tempFolder;
}
internal virtual void DeleteTempFolder(string tempFolder)
{
// Clean up the temporary folder
if (Directory.Exists(tempFolder))
{
Directory.Delete(tempFolder, true);
}
}
internal virtual IRepository FindRepository()
{
Repository repo;
try
{
// Traverse up the directory tree to find the Git repository
var repoPath = Repository.Discover(Environment.CurrentDirectory);
repo = repoPath switch
{
null => throw new EyePatchException("Not in a Git repository."),
_ => new Repository(repoPath)
};
}
catch (RepositoryNotFoundException e)
{
throw new EyePatchException("Not in a Git repository.", e);
}
return repo;
}
}
}