-
Notifications
You must be signed in to change notification settings - Fork 135
/
Copy pathRiderFileOpener.cs
108 lines (96 loc) · 3.31 KB
/
RiderFileOpener.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
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
namespace JetBrains.Rider.PathLocator
{
public class RiderFileOpener
{
private readonly IRiderLocatorEnvironment myRiderLocatorEnvironment;
public RiderFileOpener(IRiderLocatorEnvironment riderLocatorEnvironment)
{
myRiderLocatorEnvironment = riderLocatorEnvironment;
}
public static bool IsFleet(FileInfo appFileInfo)
{
return appFileInfo.Name.StartsWith("fleet", StringComparison.OrdinalIgnoreCase);
}
public bool OpenFile(string appPath, string slnFile, string assetFilePath, int line, int column)
{
// on mac empty string in quotes is causing additional solution to be opened https://github.cds.internal.unity3d.com/unity/com.unity.ide.rider/issues/21
var pathArguments = assetFilePath == string.Empty ? string.Empty : $" --line {line} --column {column} \"{assetFilePath}\"";
var args = $"\"{slnFile}\"{pathArguments}";
if (IsFleet(new FileInfo(appPath)))
{
var pathArgumentsFleet = assetFilePath == string.Empty ? string.Empty : $" -- --goto=\"{assetFilePath}\"";
if (line >= 0) // FL-20548 Fleet doesn't like -1:-1 in the goto
{
pathArgumentsFleet += $":{line}";
if (column >= 0)
{
pathArgumentsFleet += $":{column}";
}
}
var solutionDir = new FileInfo(slnFile).Directory.FullName;
args = $"\"{solutionDir}\"{pathArgumentsFleet}";
}
var proc = new Process();
if (myRiderLocatorEnvironment.CurrentOS == OS.MacOSX)
{
proc.StartInfo.FileName = "open";
proc.StartInfo.Arguments = $"-n \"{appPath}\" --args {args}";
}
else
{
proc.StartInfo.FileName = appPath;
proc.StartInfo.Arguments = args;
}
proc.StartInfo.UseShellExecute = true; // avoid HandleInheritance
var message = $"\"{proc.StartInfo.FileName}\" {proc.StartInfo.Arguments}";
myRiderLocatorEnvironment.Verbose(message);
if (!proc.Start())
{
myRiderLocatorEnvironment.Error($"Process failed to start. {message}");
return false;
}
AllowSetForegroundWindow(proc.Id);
return true;
}
// This is required to be called to help focus itself
public void AllowSetForegroundWindow(int? processId=null)
{
if (myRiderLocatorEnvironment.CurrentOS != OS.Windows)
return;
try
{
var process = processId == null ? GetRiderProcess() : Process.GetProcessById((int)processId);
if (process == null)
return;
if (process.Id > 0)
User32Dll.AllowSetForegroundWindow(process.Id);
}
catch (Exception e)
{
myRiderLocatorEnvironment.Warn("Exception on AllowSetForegroundWindow: " + e);
}
}
private static Process GetRiderProcess()
{
var process = Process.GetProcesses().FirstOrDefault(p =>
{
string processName;
try
{
processName =
p.ProcessName; // some processes like kaspersky antivirus throw exception on attempt to get ProcessName
}
catch (Exception)
{
return false;
}
return !p.HasExited && processName.ToLower().Contains("rider");
});
return process;
}
}
}