-
Notifications
You must be signed in to change notification settings - Fork 204
/
Copy pathOS.cs
229 lines (196 loc) · 6.68 KB
/
OS.cs
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using Avalonia;
namespace SourceGit.Native
{
public static partial class OS
{
public interface IBackend
{
void SetupApp(AppBuilder builder);
string FindGitExecutable();
string FindTerminal(Models.ShellOrTerminal shell);
List<Models.ExternalTool> FindExternalTools();
void OpenTerminal(string workdir);
void OpenInFileManager(string path, bool select);
void OpenBrowser(string url);
void OpenWithDefaultEditor(string file);
}
public static string DataDir
{
get;
private set;
} = string.Empty;
public static string GitExecutable
{
get => _gitExecutable;
set
{
if (_gitExecutable != value)
{
_gitExecutable = value;
UpdateGitVersion();
}
}
}
public static string GitVersionString
{
get;
private set;
} = string.Empty;
public static Version GitVersion
{
get;
private set;
} = new Version(0, 0, 0);
public static string ShellOrTerminal
{
get;
set;
} = string.Empty;
public static List<Models.ExternalTool> ExternalTools
{
get;
set;
} = [];
static OS()
{
if (OperatingSystem.IsWindows())
{
_backend = new Windows();
}
else if (OperatingSystem.IsMacOS())
{
_backend = new MacOS();
}
else if (OperatingSystem.IsLinux())
{
_backend = new Linux();
}
else
{
throw new Exception("Platform unsupported!!!");
}
}
public static void SetupApp(AppBuilder builder)
{
_backend.SetupApp(builder);
}
public static void SetupDataDir()
{
if (OperatingSystem.IsWindows())
{
var execFile = Process.GetCurrentProcess().MainModule!.FileName;
var portableDir = Path.Combine(Path.GetDirectoryName(execFile), "data");
if (Directory.Exists(portableDir))
{
DataDir = portableDir;
return;
}
}
var osAppDataDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
if (string.IsNullOrEmpty(osAppDataDir))
DataDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".sourcegit");
else
DataDir = Path.Combine(osAppDataDir, "SourceGit");
if (!Directory.Exists(DataDir))
Directory.CreateDirectory(DataDir);
}
public static void SetupEnternalTools()
{
ExternalTools = _backend.FindExternalTools();
}
public static string FindGitExecutable()
{
return _backend.FindGitExecutable();
}
public static bool TestShellOrTerminal(Models.ShellOrTerminal shell)
{
return !string.IsNullOrEmpty(_backend.FindTerminal(shell));
}
public static void SetShellOrTerminal(Models.ShellOrTerminal shell)
{
if (shell == null)
ShellOrTerminal = string.Empty;
else
ShellOrTerminal = _backend.FindTerminal(shell);
}
public static void OpenInFileManager(string path, bool select = false)
{
_backend.OpenInFileManager(path, select);
}
public static void OpenBrowser(string url)
{
_backend.OpenBrowser(url);
}
public static void OpenTerminal(string workdir)
{
if (string.IsNullOrEmpty(ShellOrTerminal))
App.RaiseException(workdir, $"Terminal is not specified! Please confirm that the correct shell/terminal has been configured.");
else
_backend.OpenTerminal(workdir);
}
public static void OpenWithDefaultEditor(string file)
{
_backend.OpenWithDefaultEditor(file);
}
public static string GetAbsPath(string root, string sub)
{
var fullpath = Path.Combine(root, sub);
if (OperatingSystem.IsWindows())
return fullpath.Replace('/', '\\');
return fullpath;
}
private static void UpdateGitVersion()
{
if (string.IsNullOrEmpty(_gitExecutable) || !File.Exists(_gitExecutable))
{
GitVersionString = string.Empty;
GitVersion = new Version(0, 0, 0);
return;
}
var start = new ProcessStartInfo();
start.FileName = _gitExecutable;
start.Arguments = "--version";
start.UseShellExecute = false;
start.CreateNoWindow = true;
start.RedirectStandardOutput = true;
start.RedirectStandardError = true;
start.StandardOutputEncoding = Encoding.UTF8;
start.StandardErrorEncoding = Encoding.UTF8;
var proc = new Process() { StartInfo = start };
try
{
proc.Start();
var rs = proc.StandardOutput.ReadToEnd();
proc.WaitForExit();
if (proc.ExitCode == 0 && !string.IsNullOrWhiteSpace(rs))
{
GitVersionString = rs.Trim();
var match = REG_GIT_VERSION().Match(GitVersionString);
if (match.Success)
{
var major = int.Parse(match.Groups[1].Value);
var minor = int.Parse(match.Groups[2].Value);
var build = int.Parse(match.Groups[3].Value);
GitVersion = new Version(major, minor, build);
GitVersionString = GitVersionString.Substring(11).Trim();
}
}
}
catch
{
// Ignore errors
}
proc.Close();
}
[GeneratedRegex(@"^git version[\s\w]*(\d+)\.(\d+)[\.\-](\d+).*$")]
private static partial Regex REG_GIT_VERSION();
private static IBackend _backend = null;
private static string _gitExecutable = string.Empty;
}
}