forked from wakatime/visualstudio-wakatime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UtilityManager.cs
254 lines (226 loc) · 9.16 KB
/
UtilityManager.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
namespace WakaTime.WakaTime {
/// <summary>
/// Singleton class to check plugin .
/// </summary>
class UtilityManager {
static bool is64BitProcess = (IntPtr.Size == 8);
static bool is64BitOperatingSystem = is64BitProcess || InternalCheckIsWow64();
[DllImport("kernel32.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool IsWow64Process(
[In] IntPtr hProcess,
[Out] out bool wow64Process
);
private const string PLUGIN_NAME = "visualstudio-wakatime";
private const string VERSION = "2.0.2";
private Process _process = new Process();
private string _apiKey = null;
private static UtilityManager _instance;
private UtilityManager() { }
public string ApiKey {
get {
return _apiKey;
}
set {
if (string.IsNullOrWhiteSpace(value) == false) {
_apiKey = value;
ConfigFileHelper.updateApiKey(value);
}
}
}
public static UtilityManager Instance {
get {
if (_instance == null) {
_instance = new UtilityManager();
}
return _instance;
}
}
/// <summary>
/// Check Python installed or not, APIKEY exists or not, Command line utility installed or not
/// </summary>
/// <returns></returns>
public void initialize() {
try {
// Make sure python is installed
if (!isPythonInstalled()) {
Logger.Instance.info("UtilityManager: Python not found.");
string pythonDownloadUrl = "https://www.python.org/ftp/python/3.4.2/python-3.4.2.msi";
if (is64BitOperatingSystem) {
pythonDownloadUrl = "https://www.python.org/ftp/python/3.4.2/python-3.4.2.amd64.msi";
}
Downloader.downloadPython(pythonDownloadUrl, null);
} else {
Logger.Instance.info("UtilityManager: Python found at " + getPython());
}
if (!doesCLIExist()) {
Logger.Instance.info("UtilityManager: wakatime-cli not found.");
Downloader.downloadCLI("https://github.com/wakatime/wakatime/archive/master.zip", getCLIDir());
} else {
Logger.Instance.info("UtilityManager: wakatime-cli found at " + getCLI());
}
_apiKey = ConfigFileHelper.getApiKey();
if (string.IsNullOrWhiteSpace(_apiKey)) {
Logger.Instance.error("API Key could not be found.");
}
} catch(Exception ex) {
Logger.Instance.error("UtilityManager initialize : " + ex.Message);
}
}
public string getPythonDir() {
return getCurrentDirectory() + "\\Python";
}
public string getPython() {
string[] locations = {
"pythonw",
"python",
"\\Python37\\pythonw",
"\\Python36\\pythonw",
"\\Python35\\pythonw",
"\\Python34\\pythonw",
"\\Python33\\pythonw",
"\\Python32\\pythonw",
"\\Python31\\pythonw",
"\\Python30\\pythonw",
"\\Python27\\pythonw",
"\\Python26\\pythonw",
"\\python37\\pythonw",
"\\python36\\pythonw",
"\\python35\\pythonw",
"\\python34\\pythonw",
"\\python33\\pythonw",
"\\python32\\pythonw",
"\\python31\\pythonw",
"\\python30\\pythonw",
"\\python27\\pythonw",
"\\python26\\pythonw",
"\\Python37\\python",
"\\Python36\\python",
"\\Python35\\python",
"\\Python34\\python",
"\\Python33\\python",
"\\Python32\\python",
"\\Python31\\python",
"\\Python30\\python",
"\\Python27\\python",
"\\Python26\\python",
"\\python37\\python",
"\\python36\\python",
"\\python35\\python",
"\\python34\\python",
"\\python33\\python",
"\\python32\\python",
"\\python31\\python",
"\\python30\\python",
"\\python27\\python",
"\\python26\\python",
};
foreach (string location in locations) {
try {
ProcessStartInfo procInfo = new ProcessStartInfo();
procInfo.UseShellExecute = false;
procInfo.RedirectStandardError = true;
procInfo.FileName = location;
procInfo.CreateNoWindow = true;
procInfo.Arguments = "--version";
var proc = Process.Start(procInfo);
string errors = proc.StandardError.ReadToEnd();
if (errors == null || errors == "") {
return location;
}
} catch (Exception ex) { }
}
return null;
}
public string getCLIDir() {
return getCurrentDirectory() + "\\wakatime";
}
public string getCLI() {
return getCLIDir() + "\\wakatime-master\\wakatime-cli.py";
}
public void sendFile(string fileName, string projectName, bool isWrite, string visualStudioVersion) {
string arguments = "\"" + getCLI() + "\" --key=\"" + _apiKey + "\""
+ " --file=\"" + fileName + "\""
+ " --plugin=\"visualstudio/" + visualStudioVersion + " " + PLUGIN_NAME + "/" + VERSION + "\"";
if (!string.IsNullOrWhiteSpace(projectName))
arguments = arguments + " --project=\"" + projectName + "\"";
if (isWrite)
arguments = arguments + " --write";
ProcessStartInfo procInfo = new ProcessStartInfo();
procInfo.UseShellExecute = false;
procInfo.FileName = getPython();
procInfo.CreateNoWindow = true;
procInfo.Arguments = arguments;
try {
var proc = Process.Start(procInfo);
} catch (InvalidOperationException ex) {
Logger.Instance.error("UtilityManager sendFile : " + getPython() + " " + arguments);
Logger.Instance.error("UtilityManager sendFile : " + ex.Message);
} catch (Exception ex) {
Logger.Instance.error("UtilityManager sendFile : " + getPython() + " " + arguments);
Logger.Instance.error("UtilityManager sendFile : " + ex.Message);
}
}
/// <summary>
/// Is it 64 bit Windows?
/// http://stackoverflow.com/questions/336633/how-to-detect-windows-64-bit-platform-with-net
/// </summary>
/// <returns></returns>
public static bool InternalCheckIsWow64() {
if ((Environment.OSVersion.Version.Major == 5 && Environment.OSVersion.Version.Minor >= 1) || Environment.OSVersion.Version.Major >= 6) {
using (Process p = Process.GetCurrentProcess()) {
bool retVal;
if (!IsWow64Process(p.Handle, out retVal)) {
return false;
}
return retVal;
}
} else {
return false;
}
}
/// <summary>
/// Check if wakatime command line exists or not
/// </summary>
/// <returns></returns>
private bool doesCLIExist() {
if (File.Exists(getCLI())) {
return true;
}
return false;
}
/// <summary>
/// Check if bundled python installation exists
/// </summary>
/// <returns></returns>
private bool doesPythonExist() {
if (File.Exists(getPythonDir() + "\\pythonw.exe")) {
return true;
}
return false;
}
/// <summary>
/// Check if python is installed
/// </summary>
/// <returns></returns>
private bool isPythonInstalled() {
if (getPython() != null) {
return true;
}
return false;
}
/// <summary>
/// Returns current working dir
/// </summary>
/// <returns></returns>
static public string getCurrentDirectory() {
var assembly = System.Reflection.Assembly.GetExecutingAssembly().Location;
var path = Path.GetDirectoryName(assembly);
return path;
}
}
}