Repository navigation
Expand file tree
/
Copy pathLinux.cs
More file actions
237 lines (205 loc) · 8.31 KB
/
Copy pathLinux.cs
File metadata and controls
237 lines (205 loc) · 8.31 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
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
230
231
232
233
234
235
236
237
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Platform;
namespace SourceGit.Native
{
[SupportedOSPlatform("linux")]
internal class Linux : OS.IBackend
{
[DllImport("libc", SetLastError = true)]
private static extern int kill(int pid, int sig);
public void SetupApp(AppBuilder builder)
{
builder.With(new X11PlatformOptions() { EnableIme = true });
}
public void SetupWindow(Window window)
{
window.BorderThickness = new Thickness(0);
if (OS.UseSystemWindowFrame)
{
window.ExtendClientAreaChromeHints = ExtendClientAreaChromeHints.Default;
window.ExtendClientAreaToDecorationsHint = false;
}
else
{
window.ExtendClientAreaChromeHints = ExtendClientAreaChromeHints.NoChrome;
window.ExtendClientAreaToDecorationsHint = true;
window.Classes.Add("custom_window_frame");
}
}
public OS.Directories GetOrCreateDirectories()
{
var dirs = new OS.Directories();
var home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
// AppImage supports portable mode
var appImage = Environment.GetEnvironmentVariable("APPIMAGE");
if (!string.IsNullOrEmpty(appImage) && File.Exists(appImage))
{
var portableDir = Path.Combine(Path.GetDirectoryName(appImage)!, "data");
if (Directory.Exists(portableDir))
{
dirs.ConfigDir = portableDir;
dirs.CacheDir = portableDir;
return dirs;
}
}
// XDG Base Directory Specification: https://specifications.freedesktop.org/basedir/latest/
dirs.ConfigDir = GetXdgDirectory("XDG_CONFIG_HOME", Path.Combine(home, ".config"), "SourceGit");
dirs.CacheDir = GetXdgDirectory("XDG_CACHE_HOME", Path.Combine(home, ".cache"), "SourceGit");
// If the app basic dirs already exist, we can skip the migration step
if (Directory.Exists(dirs.ConfigDir) && Directory.Exists(dirs.CacheDir))
return dirs;
// Create the config and cache directories if they don't exist
if (!Directory.Exists(dirs.ConfigDir))
Directory.CreateDirectory(dirs.ConfigDir);
if (!Directory.Exists(dirs.CacheDir))
Directory.CreateDirectory(dirs.CacheDir);
// Migrate legacy data dir: ~/.sourcegit to XDG standard directories
var legacyDir = Path.Combine(home, ".sourcegit");
if (Directory.Exists(legacyDir))
{
try
{
File.Copy(Path.Combine(legacyDir, "preference.json"), Path.Combine(dirs.ConfigDir, "preference.json"), true);
Directory.Move(Path.Combine(legacyDir, "avatars"), Path.Combine(dirs.CacheDir, "avatars"));
Directory.Delete(legacyDir, true);
}
catch
{
// Ignore any errors during migration
}
}
return dirs;
}
public string FindGitExecutable()
{
return FindExecutable("git");
}
public string FindTerminal(Models.ShellOrTerminal shell)
{
if (shell.Type.Equals("custom", StringComparison.Ordinal))
return string.Empty;
return FindExecutable(shell.Exec);
}
public List<Models.ExternalTool> FindExternalTools()
{
var localAppDataDir = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
var finder = new Models.ExternalToolsFinder();
finder.VSCode(() => FindExecutable("code"));
finder.VSCodeInsiders(() => FindExecutable("code-insiders"));
finder.VSCodium(() => FindExecutable("codium"));
finder.Cursor(() => FindExecutable("cursor"));
finder.FindJetBrainsFromToolbox(() => Path.Combine(localAppDataDir, "JetBrains/Toolbox"));
finder.SublimeText(() => FindExecutable("subl"));
finder.Zed(() =>
{
var exec = FindExecutable("zeditor");
return string.IsNullOrEmpty(exec) ? FindExecutable("zed") : exec;
});
return finder.Tools;
}
public void OpenBrowser(string url)
{
var browser = Environment.GetEnvironmentVariable("BROWSER");
if (string.IsNullOrEmpty(browser))
browser = "xdg-open";
Process.Start(browser, url.Quoted());
}
public void OpenInFileManager(string path)
{
if (Directory.Exists(path))
{
Process.Start("xdg-open", path.Quoted());
}
else
{
var dir = Path.GetDirectoryName(path);
if (Directory.Exists(dir))
Process.Start("xdg-open", dir.Quoted());
}
}
public void OpenTerminal(string workdir, string args)
{
var home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
var cwd = string.IsNullOrEmpty(workdir) ? home : workdir;
var startInfo = new ProcessStartInfo();
startInfo.WorkingDirectory = cwd;
startInfo.FileName = OS.ShellOrTerminal;
startInfo.Arguments = args;
try
{
Process.Start(startInfo);
}
catch (Exception e)
{
Models.Notification.Send(workdir, $"Failed to start '{OS.ShellOrTerminal}'. Reason: {e.Message}", true);
}
}
public void OpenWithDefaultEditor(string file)
{
var proc = Process.Start("xdg-open", file.Quoted());
if (proc != null)
{
proc.WaitForExit();
if (proc.ExitCode != 0)
Models.Notification.Send("", $"Failed to open: {file}", true);
proc.Close();
}
}
public bool SupportSetSid()
{
return true;
}
public string GetSetSidExecutable()
{
return "setsid";
}
public void TerminateProcess(Process proc)
{
if (kill(-proc.Id, 15) != 0)
{
// If the process already exited, we can just ignore the error.
if (Marshal.GetLastPInvokeError() == 3 /* ESRCH */)
return;
// Actually, this will not be called since the process is
// spawned by us (EPERM will not happen), and SIGTERM (15)
// is a valid signal (EINVAL will not happen).
// See https://www.man7.org/linux/man-pages/man2/kill.2.html
try
{
proc.Kill(true);
}
catch
{
// Ignore any errors when trying to kill the process
}
}
}
private string FindExecutable(string filename)
{
var pathVariable = Environment.GetEnvironmentVariable("PATH") ?? string.Empty;
var paths = pathVariable.Split(Path.PathSeparator, StringSplitOptions.RemoveEmptyEntries);
foreach (var path in paths)
{
var test = Path.Combine(path, filename);
if (File.Exists(test))
return test;
}
var local = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".local", "bin", filename);
return File.Exists(local) ? local : string.Empty;
}
private string GetXdgDirectory(string envVar, string fallback, string subDirName)
{
var dir = Environment.GetEnvironmentVariable(envVar);
if (!string.IsNullOrEmpty(dir) && Directory.Exists(dir))
return Path.Combine(dir, subDirName);
return Path.Combine(fallback, subDirName);
}
}
}