forked from gdevic/GitForce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClassUtils.cs
333 lines (308 loc) · 11.2 KB
/
ClassUtils.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
using System;
using System.Text;
using System.Diagnostics;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
namespace GitForce
{
/// <summary>
/// Contains various utility functions
/// </summary>
public static class ClassUtils
{
/// <summary>
/// Set of environment variables used by the execution environment
/// </summary>
private static readonly Dictionary<string, string> Env = new Dictionary<string, string>();
/// <summary>
/// Adds an environment variable for the Run method
/// </summary>
public static void AddEnvar(string name, string value)
{
if (Env.ContainsKey(name))
Env[name] = value;
else
Env.Add(name, value);
}
/// <summary>
/// Returns a set of environment variables registered for this execution environment
/// </summary>
public static Dictionary<string, string> GetEnvars()
{
return Env;
}
/// <summary>
/// Writes binary resource to a temporary file
/// </summary>
public static string WriteResourceToFile(string pathName, string fileName, byte[] buffer)
{
string path = Path.Combine(pathName, fileName);
try
{
using (var sw = new BinaryWriter(File.Open(path, FileMode.Create)))
{
sw.Write(buffer);
}
}
catch (Exception ex)
{
App.PrintLogMessage(ex.Message);
}
return path;
}
/// <summary>
/// Returns true if the app is running on Mono (Linux), false if it is Windows
/// </summary>
public static bool IsMono()
{
return Type.GetType("Mono.Runtime") != null;
}
/// <summary>
/// Open a command prompt at the specific directory
/// </summary>
public static void CommandPromptHere(string where)
{
Directory.SetCurrentDirectory(where);
try
{
App.PrintStatusMessage("Command prompt at " + where);
Process proc = new Process();
proc.StartInfo.UseShellExecute = false;
// Add all environment variables listed
foreach (var envar in Env)
proc.StartInfo.EnvironmentVariables.Add(envar.Key, envar.Value);
// WAR: Opening a command window/terminal is platform-specific
if (IsMono())
{
// TODO: This may not work on a non-Ubuntu system?
proc.StartInfo.FileName = @"/usr/bin/gnome-terminal";
proc.StartInfo.Arguments = "--working-directory=" + where;
}
else
{
proc.StartInfo.FileName = "cmd.exe";
}
proc.Start();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Command Prompt Here error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
/// <summary>
/// Open a file browser/Explorer at the specific directory, optionally selecting a file
/// </summary>
public static void ExplorerHere(string where, string selFile)
{
try
{
App.PrintStatusMessage("Opening a file browser at " + where);
// WAR: Opening an "Explorer" is platform-specific
if (IsMono())
{
// TODO: Start a Linux (Ubuntu?) file explorer in a more flexible way
Process.Start(@"/usr/bin/nautilus", "--browser " + where);
}
else
{
string path = selFile == string.Empty
? "/e,\"" + where + "\""
: "/e, /select,\"" + selFile + "\"";
App.PrintLogMessage(path);
Process.Start("explorer.exe", path);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Explorer Here error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
/// <summary>
/// Executes a shell command
/// </summary>
public static string ExecuteShellCommand(string cmd, string args)
{
App.PrintStatusMessage("Shell execute: " + cmd + " " + args);
// WAR: Shell execute is platform-specific
if (IsMono())
{
return Exec.Run(cmd, args).ToString();
}
else
{
args = "/c " + cmd + " " + args;
return Exec.Run("cmd.exe", args).ToString();
}
}
/// <summary>
/// Returns CMD/SHELL executable name to execute a command line command
/// </summary>
public static string GetShellExecCmd()
{
return IsMono() ? "sh" : "cmd.exe";
}
/// <summary>
/// Returns a string to be used by CMD/SHELL as argument when executing a command line command
/// </summary>
public static string GetShellExecFlags()
{
return IsMono() ? "-c" : "/K";
}
/// <summary>
/// Identical to NET4.0 IsNullOrWhiteSpace()
/// </summary>
public static bool IsNullOrWhiteSpace(string s)
{
if (s == null)
return true;
return s.Trim().Length == 0;
}
/// <summary>
/// Remove given folder and all files and subfolders under it.
/// If fPreserveGit is true, all folders that are named ".git" will be preserved (not removed)
/// If fPreserveRootFolder is true, the first (root) folder will also be preserved
/// Return false if the function could not remove all folders, true otherwise.
/// </summary>
public static bool DeleteFolder(DirectoryInfo dirInfo, bool fPreserveGit, bool fPreserveRootFolder)
{
bool f = true;
try
{
foreach (var subDir in dirInfo.GetDirectories())
{
if (fPreserveGit == false || !subDir.Name.EndsWith(".git"))
f &= DeleteFolder(subDir, false, false);
}
foreach (var file in dirInfo.GetFiles())
f &= DeleteFile(file.FullName);
if (fPreserveRootFolder == false)
f &= DeleteFolder(dirInfo);
}
catch (Exception ex)
{
App.PrintLogMessage("Error deleting directory " + dirInfo.FullName + ": " + ex.Message);
}
return f;
}
/// <summary>
/// Deletes a folder from the local file system.
/// Returns true if delete succeeded, false otherwise.
/// </summary>
private static bool DeleteFolder(DirectoryInfo dirInfo)
{
try
{
dirInfo.Attributes = FileAttributes.Normal;
dirInfo.Delete();
}
catch (Exception ex)
{
App.PrintLogMessage("Error deleting directory " + dirInfo.FullName + ": " + ex.Message);
return false;
}
return true;
}
/// <summary>
/// Deletes a file from the local file system.
/// Returns true if delete succeeded, false otherwise.
/// </summary>
public static bool DeleteFile(string name)
{
try
{
FileInfo file = new FileInfo(name) {Attributes = FileAttributes.Normal};
file.Delete();
}
catch (Exception ex)
{
App.PrintLogMessage("Error deleting file " + name + ": " + ex.Message);
return false;
}
return true;
}
/// <summary>
/// Handle double-clicking on a file.
/// Depending on the saved options, we either do nothing ("0"), open a file
/// using a default Explorer file association ("1"), or open a file using a
/// specified application ("2")
/// </summary>
public static void FileDoubleClick(string file)
{
// Perform the required action on double-click
string option = Properties.Settings.Default.DoubleClick;
string program = Properties.Settings.Default.DoubleClickProgram;
try
{
if (option == "1")
Process.Start(file);
if (option == "2")
Process.Start(program, file);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
/// <summary>
/// Edit selected file using either the default editor (native OS file association,
/// if the tag is null, or the editor program specified in the tag field.
/// This is a handler for the context menu, edit tool bar button and also
/// revision history view menus.
/// </summary>
public static void FileOpenFromMenu(object sender, string file)
{
try
{
if (sender is ToolStripMenuItem)
{
object opt = (sender as ToolStripMenuItem).Tag;
if (opt != null)
{
Process.Start(opt.ToString(), file);
return;
}
}
Process.Start(file);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
/// <summary>
/// Open an HTML link in an external web browser application.
/// </summary>
public static void OpenWebLink(string html)
{
try
{
Process.Start(html);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "GitForce", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
}
/// <summary>
/// Returns a copy of the input string, but with all non-ASCII characters stripped down.
/// This function also removes ANSI escape codes from the string.
/// </summary>
public static string ToPlainAscii(string s)
{
StringBuilder str = new StringBuilder();
for (int i = 0; i < s.Length; i++)
{
if (!Char.IsControl(s[i]))
str.Append(s[i]);
else
{ // Strip ANSI escape codes from the string
// http://ascii-table.com/ansi-escape-sequences.php
if (s[i] == 27 && i<s.Length-1 && s[i+1]=='[')
while(i<s.Length && !Char.IsLetter(s[i])) i++;
}
}
return str.ToString();
}
}
}