-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.cs
700 lines (584 loc) · 26 KB
/
Utils.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// Modified by Eli Aloni (A.K.A. elix22)
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Runtime.Loader;
using System.Security.Cryptography;
using System.Text;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
namespace UrhoCooker
{
public static class Utils
{
private static readonly object s_SyncObj = new object();
const string RedColorText = "\u001b[31m";
const string GreenColorText = "\u001b[32m";
const string YellowColorText = "\u001b[33m";
const string BlueColorText = "\u001b[34m";
const string PurpleColorText = "\u001b[35m";
const string CyanColorText = "\u001b[36m";
static string URHONET_HOME_PATH = string.Empty;
public static Options opts;
static string GetUrhoNetHomePath()
{
if(URHONET_HOME_PATH != string.Empty)return URHONET_HOME_PATH;
string homeFolder = Utils.GetHomeFolder();
string urhoNetConfigFolderPath = Path.Combine(homeFolder, ".urhonet_config");
if (!Directory.Exists(urhoNetConfigFolderPath))
{
Console.WriteLine($".urhonet_config folder not found");
return string.Empty;
}
string urhoNetHomeFilePath = Path.Combine(urhoNetConfigFolderPath, "urhonethome");
if (!File.Exists(urhoNetHomeFilePath))
{
Console.WriteLine($"urhonethome not found");
return string.Empty;
}
string[] lines = urhoNetHomeFilePath.FileReadAllLines();
foreach (var line in lines)
{
if (line == string.Empty) continue;
if (Directory.Exists(line))
{
URHONET_HOME_PATH = line;
break;
}
}
return URHONET_HOME_PATH;
}
public static string GetHomeFolder()
{
return System.Environment.GetFolderPath(System.Environment.SpecialFolder.UserProfile);
}
public static string FixPathString(this string path)
{
path = path.Replace("\\", "/");
path = path.Replace("//", "/");
return path;
}
public static string[] FileReadAllLines(this string path)
{
List<string> lines = new List<string>();
try
{
using (FileStream fs = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read))
{
using var sr = new StreamReader(fs, Encoding.UTF8);
string? line = String.Empty;
while ((line = sr.ReadLine()) != null)
{
lines.Add(line);
}
}
}
catch (Exception ex)
{
// Console.WriteLine(ex.ToString());
Console.WriteLine(ex.ToString());
}
return lines.ToArray();
}
public static (int exitCode, string output) RunShellCommand(
TaskLoggingHelper ? logger,
string command,
IDictionary<string, string>? envVars,
string workingDir,
bool silent = false,
bool logStdErrAsMessage = false,
MessageImportance debugMessageImportance = MessageImportance.Low,
string? label = null)
{
string scriptFileName = CreateTemporaryBatchFile(command);
(string shell, string args) = RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
? ("cmd", $"/c \"{scriptFileName}\"")
: ("/bin/sh", $"\"{scriptFileName}\"");
string msgPrefix = label == null ? string.Empty : $"[{label}] ";
logger?.LogMessage(debugMessageImportance, $"{msgPrefix}Running {command} via script {scriptFileName}:", msgPrefix);
logger?.LogMessage(debugMessageImportance, File.ReadAllText(scriptFileName), msgPrefix);
return TryRunProcess(logger,
shell,
args,
envVars,
workingDir,
silent: silent,
logStdErrAsMessage: logStdErrAsMessage,
label: label,
debugMessageImportance: debugMessageImportance);
static string CreateTemporaryBatchFile(string command)
{
string extn = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? ".cmd" : ".sh";
string file = Path.Combine(Path.GetTempPath(), $"tmp{Guid.NewGuid():N}{extn}");
using StreamWriter sw = new(file);
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
sw.WriteLine("setlocal");
sw.WriteLine("set errorlevel=dummy");
sw.WriteLine("set errorlevel=");
}
else
{
// Use sh rather than bash, as not all 'nix systems necessarily have Bash installed
sw.WriteLine("#!/bin/sh");
}
sw.WriteLine(command);
return file;
}
}
public static string RunProcess(
TaskLoggingHelper logger,
string path,
string args = "",
IDictionary<string, string>? envVars = null,
string? workingDir = null,
bool ignoreErrors = false,
bool silent = true,
MessageImportance debugMessageImportance = MessageImportance.High)
{
(int exitCode, string output) = TryRunProcess(
logger,
path,
args,
envVars,
workingDir,
silent: silent,
debugMessageImportance: debugMessageImportance);
if (exitCode != 0 && !ignoreErrors)
throw new Exception("Error: Process returned non-zero exit code: " + output);
return output;
}
public static (int, string) TryRunProcess(
TaskLoggingHelper ? logger,
string path,
string args = "",
IDictionary<string, string>? envVars = null,
string? workingDir = null,
bool silent = true,
bool logStdErrAsMessage = false,
MessageImportance debugMessageImportance = MessageImportance.High,
string? label = null)
{
string msgPrefix = label == null ? string.Empty : $"[{label}] ";
logger?.LogMessage(debugMessageImportance, $"{msgPrefix}Running: {path} {args}");
var outputBuilder = new StringBuilder();
var processStartInfo = new ProcessStartInfo
{
FileName = path,
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardError = true,
RedirectStandardOutput = true,
Arguments = args,
};
if (workingDir != null)
processStartInfo.WorkingDirectory = workingDir;
logger?.LogMessage(debugMessageImportance, $"{msgPrefix}Using working directory: {workingDir ?? Environment.CurrentDirectory}", msgPrefix);
if (envVars != null)
{
//TBD ELI
// if (envVars.Count > 0)
// logger?.LogMessage(MessageImportance.Low, $"{msgPrefix}Setting environment variables for execution:", msgPrefix);
foreach (KeyValuePair<string, string> envVar in envVars)
{
processStartInfo.EnvironmentVariables[envVar.Key] = envVar.Value;
//TBD ELI logger?.LogMessage(MessageImportance.Low, $"{msgPrefix}\t{envVar.Key} = {envVar.Value}");
}
}
Process? process = Process.Start(processStartInfo);
if (process == null)
throw new ArgumentException($"{msgPrefix}Process.Start({path} {args}) returned null process");
process.ErrorDataReceived += (sender, e) =>
{
lock (s_SyncObj)
{
if (string.IsNullOrEmpty(e.Data))
return;
string msg = $"{msgPrefix}{e.Data}";
if (!silent)
{
if (logStdErrAsMessage)
logger?.LogMessage(debugMessageImportance, e.Data, msgPrefix);
else
logger?.LogWarning(msg);
}
outputBuilder.AppendLine(e.Data);
}
};
process.OutputDataReceived += (sender, e) =>
{
lock (s_SyncObj)
{
if (string.IsNullOrEmpty(e.Data))
return;
if (!silent)
logger?.LogMessage(debugMessageImportance, e.Data, msgPrefix);
outputBuilder.AppendLine(e.Data);
}
};
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
logger?.LogMessage(debugMessageImportance, $"{msgPrefix}Exit code: {process.ExitCode}");
return (process.ExitCode, outputBuilder.ToString().Trim('\r', '\n'));
}
internal static string CreateTemporaryBatchFile(string command)
{
string extn = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? ".cmd" : ".sh";
string file = Path.Combine(Path.GetTempPath(), $"tmp{Guid.NewGuid():N}{extn}");
using StreamWriter sw = new(file);
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
sw.WriteLine("setlocal");
sw.WriteLine("set errorlevel=dummy");
sw.WriteLine("set errorlevel=");
}
else
{
// Use sh rather than bash, as not all 'nix systems necessarily have Bash installed
sw.WriteLine("#!/bin/sh");
}
sw.WriteLine(command);
return file;
}
public static bool CopyIfDifferent(string src, string dst, bool useHash)
{
if (!File.Exists(src))
throw new ArgumentException($"Cannot find {src} file to copy", nameof(src));
bool areDifferent = !File.Exists(dst) ||
(useHash && ComputeHash(src) != ComputeHash(dst)) ||
(File.ReadAllText(src) != File.ReadAllText(dst));
if (areDifferent)
File.Copy(src, dst, true);
return areDifferent;
}
public static string ComputeHash(string filepath)
{
using var stream = File.OpenRead(filepath);
using HashAlgorithm hashAlgorithm = SHA512.Create();
byte[] hash = hashAlgorithm.ComputeHash(stream);
return Convert.ToBase64String(hash);
}
public static void DirectoryCopy(string sourceDir, string destDir, Func<string, bool>? predicate = null)
{
string[] files = Directory.GetFiles(sourceDir, "*", SearchOption.AllDirectories);
foreach (string file in files)
{
if (predicate != null && !predicate(file))
continue;
string relativePath = Path.GetRelativePath(sourceDir, file);
string? relativeDir = Path.GetDirectoryName(relativePath);
if (!string.IsNullOrEmpty(relativeDir))
Directory.CreateDirectory(Path.Combine(destDir, relativeDir));
File.Copy(file, Path.Combine(destDir, relativePath), true);
}
}
public static void CopyDirectory(this string sourceDir, string destinationDir, bool recursive = true)
{
// Get information about the source directory
var dir = new DirectoryInfo(sourceDir);
// Check if the source directory exists
if (!dir.Exists)
throw new DirectoryNotFoundException($"Source directory not found: {dir.FullName}");
// Cache directories before we start copying
DirectoryInfo[] dirs = dir.GetDirectories();
// Create the destination directory
Directory.CreateDirectory(destinationDir);
// Get the files in the source directory and copy to the destination directory
foreach (FileInfo file in dir.GetFiles())
{
string targetFilePath = Path.Combine(destinationDir, file.Name);
File.Delete(targetFilePath);
file.CopyTo(targetFilePath, true);
}
// If recursive and copying subdirectories, recursively call this method
if (recursive)
{
foreach (DirectoryInfo subDir in dirs)
{
string newDestinationDir = Path.Combine(destinationDir, subDir.Name);
CopyDirectory(subDir.FullName, newDestinationDir, true);
}
}
}
public static void CopyDirectoryIfDifferent(this string sourceDir, string destinationDir, bool recursive = true)
{
// Get information about the source directory
var dir = new DirectoryInfo(sourceDir);
// Check if the source directory exists
if (!dir.Exists)
throw new DirectoryNotFoundException($"Source directory not found: {dir.FullName}");
// Cache directories before we start copying
DirectoryInfo[] dirs = dir.GetDirectories();
// Create the destination directory
Directory.CreateDirectory(destinationDir);
// Get the files in the source directory and copy to the destination directory
foreach (FileInfo file in dir.GetFiles())
{
string targetFilePath = Path.Combine(destinationDir, file.Name);
CopyIfDifferent(file.FullName,targetFilePath,true);
}
// If recursive and copying subdirectories, recursively call this method
if (recursive)
{
foreach (DirectoryInfo subDir in dirs)
{
string newDestinationDir = Path.Combine(destinationDir, subDir.Name);
CopyDirectoryIfDifferent(subDir.FullName, newDestinationDir, true);
}
}
}
public static void DeleteDirectory(this string path)
{
if (!Directory.Exists(path)) return;
foreach (string directory in Directory.GetDirectories(path))
{
DeleteDirectory(directory);
}
try
{
Directory.Delete(path, true);
}
catch (ArgumentException ex)
{
Console.WriteLine(RedColorText + ex.ToString());
}
catch (PathTooLongException ex)
{
Console.WriteLine(RedColorText + ex.ToString());
}
catch (DirectoryNotFoundException ex)
{
Console.WriteLine(RedColorText + ex.ToString());
}
catch (IOException ex)
{
Directory.Delete(path, true);
Console.WriteLine(RedColorText + ex.ToString());
}
catch (UnauthorizedAccessException ex)
{
Directory.Delete(path, true);
Console.WriteLine(RedColorText + ex.ToString());
}
}
public static void ReplaceInfile(this string inFileName, string oldstr, string newStr)
{
if (File.Exists(inFileName) == false) return;
try
{
string allText = File.ReadAllText(inFileName);
string replacedText = allText.Replace(oldstr, newStr);
File.Delete(inFileName);
File.WriteAllText(inFileName, replacedText);
}
catch (Exception e)
{
Console.WriteLine(RedColorText + e.ToString());
}
}
public static void DeleteFile(this string path)
{
if (File.Exists(path) == false) return;
try
{
File.Delete(path);
}
catch (Exception e)
{
Console.WriteLine(RedColorText + e.ToString());
}
}
public static void AppendText(this string path, string content)
{
var writer = File.AppendText(path);
writer.Write(content);
writer.Dispose();
}
public static void AppendTextLine(this string path, string content)
{
var writer = File.AppendText(path);
writer.WriteLine(content);
writer.Dispose();
}
public static string GetEmbeddedResource(string file)
{
using Stream stream = typeof(Utils).Assembly
.GetManifestResourceStream($"{typeof(Utils).Assembly.GetName().Name}.Templates.{file}")!;
using var reader = new StreamReader(stream);
return reader.ReadToEnd();
}
public static void ResolveReferenceAssemblies(string assemblyFullPath, string[] assemblySearchPaths, out List<string> assembliesList, out List<string> assembliesFullPathList)
{
assembliesList = new List<string>();
assembliesFullPathList = new List<string>();
try
{
string[] assemblies = assemblyFullPath.GetReferencedAssemblies();
if (assemblies.Length > 0)
{
foreach (var assemblyName in assemblies)
{
if (assemblyName != string.Empty)
{
string refAssemblyFullPath = getAssemblyFullPath(assemblyName, assemblySearchPaths);
if (refAssemblyFullPath != string.Empty)
{
String ?name = assembliesList.FindLast(item => item.Equals(assemblyName));
if (name == null)
{
assembliesList.Add(assemblyName);
assembliesFullPathList.Add(refAssemblyFullPath);
resolveReferenceAssemblies(assemblyName, assemblySearchPaths, ref assembliesList, ref assembliesFullPathList);
}
}
}
}
}
}
catch (Exception e)
{
Console.WriteLine(RedColorText + e.ToString());
}
}
static void resolveReferenceAssemblies( String assemblyName, string[] assemblySearchPaths, ref List<string> assembliesList, ref List<string> assembliesFullPathList)
{
if (assemblyName == "Game") return;
string assemblyFullPath = getAssemblyFullPath(assemblyName,assemblySearchPaths);
if (assemblyFullPath != string.Empty && File.Exists(assemblyFullPath))
{
if (assembliesFullPathList.FindLast(item => item.Equals(assemblyFullPath)) == null)
assembliesFullPathList.Add(assemblyFullPath);
try
{
var assemblies = assemblyFullPath.GetReferencedAssemblies();
if (assemblies.Length > 0)
{
foreach (var refAssembly in assemblies)
{
if (refAssembly != string.Empty)
{
string refAssemblyFullPath = getAssemblyFullPath(refAssembly, assemblySearchPaths);
if (refAssemblyFullPath != string.Empty)
{
String name = assembliesList.FindLast(item => item.Equals(refAssembly));
if (name == null)
{
assembliesList.Add(refAssembly);
resolveReferenceAssemblies(refAssembly, assemblySearchPaths, ref assembliesList, ref assembliesFullPathList);
}
}
}
}
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
}
static bool referenceExist(AssemblyName reference, string[] assemblySearchPaths)
{
bool res = false;
foreach (var path in assemblySearchPaths)
{
string assemblyFullPath = Path.Combine(path, reference.Name) + ".dll";
if (File.Exists(assemblyFullPath))
{
res = true;
break;
}
}
return res;
}
static string getAssemblyFullPath(AssemblyName reference, string[] assemblySearchPaths)
{
string res = string.Empty;
String assemblyName = reference.Name;
bool hasExtention = reference.Name.EndsWith(".dll") || reference.Name.EndsWith(".exe");
if (hasExtention == false) assemblyName += ".dll";
foreach (var path in assemblySearchPaths)
{
string assemblyFullPath = Path.Combine(path, assemblyName);
if (File.Exists(assemblyFullPath))
{
res = assemblyFullPath;
break;
}
}
return res;
}
static string getAssemblyFullPath(string reference, string[] assemblySearchPaths)
{
string res = string.Empty;
String assemblyName = reference;
bool hasExtention = reference.EndsWith(".dll") || reference.EndsWith(".exe");
if (hasExtention == false) assemblyName += ".dll";
foreach (var path in assemblySearchPaths)
{
string assemblyFullPath = Path.Combine(path, assemblyName);
if (File.Exists(assemblyFullPath))
{
res = assemblyFullPath;
break;
}
}
return res;
}
public static string[] GetReferencedAssemblies(this string assembly)
{
bool isWindows = System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
bool isOSX = System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
bool isLinux = System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
string os_folder= (isWindows)?"windows":((isOSX)?"macos":"linux");
string monodis_exe = Path.Combine(GetUrhoNetHomePath(),"tools","monodis",os_folder,"monodis");
if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
monodis_exe += ".exe";
}
monodis_exe = monodis_exe.Replace("\\","/");
// Console.WriteLine($"monodis_exe {monodis_exe}");
var escapedArgs = assembly.Replace("\"", "\\\"");
// (int exitCode, string result) = Utils.RunShellCommand(null,
// $"{monodis_exe} --assemblyref {assembly} ",
// null,
// workingDir: opts.ProjectPath,
// logStdErrAsMessage: true,
// debugMessageImportance: MessageImportance.High,
// label: $"assemblyref {assembly}");
var process = new Process()
{
StartInfo = new ProcessStartInfo
{
FileName = monodis_exe,
Arguments = "--assemblyref " + assembly,
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true,
}
};
process.Start();
string result = process.StandardOutput.ReadToEnd();
process.WaitForExit();
List<string> reference_assemblies = new List<string>();
// if (exitCode == 0 && result != string.Empty)
{
string[] entries = result.Split("\n");
foreach (string entry in entries)
{
if (entry.Contains("Name="))
{
String name = entry.Replace("Name=", "").Replace("\r", "").Replace("\t", "");
name = name.Trim();
reference_assemblies.Add(name);
}
}
}
return reference_assemblies.ToArray();
}
}
}