-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCsScript.cs
585 lines (495 loc) · 24.4 KB
/
CsScript.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
//#define NODEBUGTRACE
using System;
using System.Linq;
using System.Text;
using Microsoft.CSharp;
using System.CodeDom.Compiler;
using System.IO;
using System.Reflection;
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Threading;
/// <summary>
/// class for executing c# script.
/// </summary>
public class CsScript
{
static CSharpCodeProvider provider;
static ICodeCompiler compiler;
static String[] refAssemblies;
/// <summary>
/// Compiles .cs script into dll/pdb, loads as assembly, and executes Main function.
/// Temporary dll/pdb gets deleted. If .cs throws exception - it will be converted to
/// error information, including .cs filename and source code line information.
/// </summary>
/// <param name="_path">Path to script which to execute</param>
/// <param name="bAllowThrow">true if allow to throw exceptions</param>
/// <param name="errors">Errors if any</param>
/// <param name="args">Main argument parameters</param>
/// <param name="bCompileOnly">true if only to compile</param>
/// <returns>true if execution was successful.</returns>
static public bool RunScript( String _path, bool bCompileOnly, bool bAllowThrow, out String errors, params String[] args )
{
errors = "";
// ----------------------------------------------------------------
// Load script
// ----------------------------------------------------------------
String path = Path.GetFullPath( _path );
if( !File.Exists( path ) )
{
errors = "Error: Could not load file '" + Exception2.getPath(path) + "': File does not exists.";
if (bAllowThrow)
throw new Exception2(errors, 1);
return false;
}
// Create syncProj cache folder next to syncProj.exe executable.
String cacheDir = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "syncProjCache");
if (!Directory.Exists(cacheDir))
{
DirectoryInfo di = Directory.CreateDirectory(cacheDir);
di.Attributes = FileAttributes.Directory | FileAttributes.Hidden;
}
String dllBaseName = Path.GetFileNameWithoutExtension(_path);
String tempDll;
String dllInfoFile = "";
DateTime dllInfoRealDate = DateTime.MinValue;
for (int i = 1; ; i++)
{
tempDll = Path.Combine(cacheDir, dllBaseName);
if (i != 1)
tempDll += i;
dllInfoFile = tempDll + "_script.txt"; // We keep here C# script full path just not to get collisions.
if (!File.Exists(dllInfoFile))
{
File.WriteAllText(dllInfoFile, path);
break;
}
String pathFromFile = "";
//
// Another instance of syncProj might be accessing same file at the same time, we try to retry automatically after some delay.
//
for ( int iTry = 0 ; iTry < 20; iTry++)
{
try
{
pathFromFile = File.ReadAllText(dllInfoFile);
break;
}
catch (Exception)
{
Thread.Sleep(10 + iTry * 5); // 5*20+10, overall (5*20+10)*20 / 2 = 1.1 sec
}
}
if (pathFromFile == path)
{
dllInfoRealDate = File.GetLastWriteTime(dllInfoFile);
break;
}
}
String pdb = tempDll + ".pdb";
tempDll += ".dll";
List<String> filesToCompile = new List<string>();
filesToCompile.Add(path);
//---------------------------------------------------------------------------------------------------
// Get referenced .cs script file list, and from referenced files further other referenced files.
//---------------------------------------------------------------------------------------------------
CsScriptInfo csInfo = getCsFileInfo(filesToCompile[0], true);
filesToCompile.AddRange(csInfo.csFiles);
bool bCompileDll = false;
//---------------------------------------------------------------------------------------------------
// Compile .dll only if script.cs and it's dependent .cs are newer than compiled .dll.
//---------------------------------------------------------------------------------------------------
if (!File.Exists(tempDll))
bCompileDll = true;
DateTime dllInfoTargetDate = DateTime.MinValue;
//---------------------------------------------------------------------------------------------------
// Calculate target date anyway, so we can set it to file.
// I have made such logic that scripts will be compiled if date / time of main script or any sub-script is changed.
//---------------------------------------------------------------------------------------------------
List<long> times = filesToCompile.Select(x => File.GetLastWriteTime(x).Ticks).ToList();
//
// If we are referencing any local .dll file, add it's time into calculation scheme.
//
foreach (String refDll in csInfo.refFiles)
{
if (File.Exists(refDll))
times.Add(File.GetLastAccessTime(refDll).Ticks);
}
// If syncProj.exe also changed, requires recompiling all .dll's.
// GetEntryAssembly() returns null during unit testing.
String exeFile = System.Reflection.Assembly.GetExecutingAssembly().Location;
times.Add(File.GetLastWriteTime(exeFile).Ticks);
times.Sort();
//---------------------------------------------------------------------------------------------------
// Basically we have multiple files, each with it's own modification date, we need to detect if any of files
// has changed - either updated forth (svn update) or back (svn revert with set file dates to last commit time)
// We try to calculate date / time from multiple date times
//---------------------------------------------------------------------------------------------------
long time = times[0]; // smallest date/time
for (int i = 1; i < times.Count; i += 2)
{
if (i + 1 == times.Count)
time = (time + times[i]) / 2; // medium between current date/time and highest
else
time += (times[i + 1] - times[i]) / 2; // just take different between dates / times and get medium from there.
}
dllInfoTargetDate = new DateTime(time);
if (times.Count != 1)
dllInfoTargetDate.AddSeconds(-times.Count); // Just some checksum on how many files we actually have.
if (!bCompileDll)
{
if (dllInfoRealDate != dllInfoTargetDate)
bCompileDll = true;
}
if (csInfo.DebugEnabled())
{
if( !bCompileDll )
Console.WriteLine(Exception2.getPath(path) + " dll is up-to-date.");
else
Console.WriteLine(Exception2.getPath(path) + " dll will be compiled.");
//+ ": Date found: " + dllInfoRealDate.ToString("o") + " Date expected: " + dllInfoTargetDate.ToString("o")
}
if (bCompileDll)
{
// ----------------------------------------------------------------
// Compile it into ram
// ----------------------------------------------------------------
if (provider == null)
provider = new CSharpCodeProvider();
#pragma warning disable 618
if (compiler == null)
compiler = provider.CreateCompiler();
#pragma warning restore 618
CompilerParameters compilerparams = new CompilerParameters();
compilerparams.GenerateExecutable = false;
#if NODEBUGTRACE
// Currently it's not possible to generate in ram pdb debugging information.
// Compiler option /debug:full should in theory allow that, but it does not work.
compilerparams.GenerateInMemory = true;
#else
compilerparams.GenerateInMemory = false;
compilerparams.IncludeDebugInformation = true; // Needed to get line / column numbers
compilerparams.OutputAssembly = tempDll;
compilerparams.CompilerOptions = "/d:DEBUG /d:TRACE"; // /debug+ /debug:full /optimize-
#endif
// Add assemblies from my domain - all which are not dynamic.
if (refAssemblies == null)
{
var assemblies = AppDomain.CurrentDomain.GetAssemblies().Where(a => !a.IsDynamic).Select(a => a.Location).ToList();
for (int i = 0; i < assemblies.Count; i++)
{
if (assemblies[i].EndsWith(".exe") && !assemblies[i].EndsWith("\\syncproj.exe"))
{
assemblies.RemoveAt(i);
i--;
}
}
refAssemblies = assemblies.ToArray();
}
compilerparams.ReferencedAssemblies.AddRange(refAssemblies);
foreach( var f in csInfo.refFiles)
compilerparams.ReferencedAssemblies.Add(f);
// ----------------------------------------------------------------
// If compile errors - report and exit.
// ----------------------------------------------------------------
CompilerResults results = compiler.CompileAssemblyFromFileBatch(compilerparams, filesToCompile.ToArray());
if (results.Errors.HasErrors)
{
// Mimic visual studio error handling.
StringBuilder sb = new StringBuilder();
foreach (CompilerError error in results.Errors)
{
// Missing reference file will not give any file or line information, we just use compilation
// script filename, and first line position. (Not exactly right, but something at least).
if (error.FileName == "")
sb.Append(Exception2.getPath(filesToCompile[0]));
else
sb.Append(Exception2.getPath(error.FileName));
if (error.Line == 0)
// error CS0006: Metadata file 'MystiqueDll.dll' could not be found
sb.Append("(1,1)");
else
sb.Append("(" + error.Line + "," + error.Column + ")");
sb.AppendFormat(": error {0}: {1}\r\n", error.ErrorNumber, error.ErrorText );
}
errors = sb.ToString();
if (bAllowThrow)
throw new Exception2(errors);
return false;
}
} //if
try
{
File.SetLastWriteTime(dllInfoFile, dllInfoTargetDate);
}
catch (Exception)
{
// Visual studio can launch multiple instances of syncProj, and then each will try to compile it's own copy.
// Add here just some guard, let's check if this needs to be improved later on.
}
if (bCompileOnly)
return true;
//------------------------------------------------------------------------------------------------------
//
// Let's check that script contains correct css_ref (Might be copied from another project).
// We allow here also multiple copies of syncProj, as long as path to syncProj.exe is valid in .cs header
// (Can be edited by C# script)
//
//------------------------------------------------------------------------------------------------------
Regex reCssRef = new Regex("^ *//css_ref *(.*syncproj\\.exe);?([\r\n]+|$)", RegexOptions.Multiline | RegexOptions.IgnoreCase);
bool bUpdateScriptPath = false;
String targetCsPath = "";
using (StreamReader reader = new StreamReader(path))
{
for (int i = 0; i < 10; i++)
{
String line = reader.ReadLine() ?? "";
var re = reCssRef.Match(line);
if (re.Success)
{
// Current path, referred from C# script
String currentCsPath = re.Groups[1].Value;
String dir = Path.GetDirectoryName(path);
String exePath = SolutionOrProject.getSyncProjExeLocation(dir, currentCsPath);
targetCsPath = Path2.makeRelative(exePath, dir);
String referredExe = currentCsPath;
if( !Path.IsPathRooted(referredExe) ) // Uses relative path, let's make it absolute.
referredExe = Path.Combine(dir, currentCsPath);
if (currentCsPath != targetCsPath && !File.Exists(referredExe))
bUpdateScriptPath = true; // Path is not the same as ours, and .exe referred by C# script does not exists.
} //if
} //for
} //using
if (bUpdateScriptPath)
{
String file = File.ReadAllText(path);
String newFile = reCssRef.Replace(file, new MatchEvaluator( m => { return "//css_ref " + targetCsPath + "\r\n"; } ) );
File.WriteAllText(path, newFile);
}
// ----------------------------------------------------------------
// Preload compiled .dll and it's debug information into ram.
// ----------------------------------------------------------------
MethodInfo entry = null;
String funcName = "";
Assembly asm = Assembly.LoadFrom(tempDll);
//Assembly asm = results.CompiledAssembly;
// ----------------------------------------------------------------
// Locate entry point
// ----------------------------------------------------------------
BindingFlags flags = BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.IgnoreCase;
Type builderClass = null;
foreach (Type type in asm.GetTypes())
{
funcName = "Main";
entry = type.GetMethod(funcName, flags);
if (entry == null)
{
funcName = "ScriptMain";
entry = type.GetMethod(funcName, flags);
}
if (entry != null)
{
builderClass = type;
break;
}
}
if( entry == null )
{
errors = String.Format( "{0}(1,1): error: Code does not have 'Main' function\r\n", Exception2.getPath(path) );
if (bAllowThrow)
throw new Exception2(errors);
return false;
}
if ( entry.GetParameters().Length != 1 )
{
errors = String.Format("{0}(1,1): error: Function '{1}' is not expected to have {2} parameter(s)\r\n", Exception2.getPath(path), funcName,entry.GetParameters().Length);
if (bAllowThrow)
throw new Exception2(errors);
return false;
}
String oldScriptRelativeDir = SolutionProjectBuilder.m_scriptRelativeDir;
String scriptSubPath = Path2.makeRelative(Path.GetDirectoryName(_path), SolutionProjectBuilder.m_workPath);
SolutionProjectBuilder.m_scriptRelativeDir = scriptSubPath;
String oldScriptPath = SolutionProjectBuilder.m_currentlyExecutingScriptPath;
SolutionProjectBuilder.m_currentlyExecutingScriptPath = _path;
String oldDir = Environment.CurrentDirectory;
//
// We set current directory to where script is, just so script can use Directory.GetFiles without specifying directory.
//
Directory.SetCurrentDirectory( Path.GetDirectoryName(_path) );
// ----------------------------------------------------------------
// Run script
// ----------------------------------------------------------------
try
{
entry.Invoke(null, new object[] { args });
Directory.SetCurrentDirectory( oldDir );
SolutionProjectBuilder.m_scriptRelativeDir = oldScriptRelativeDir;
SolutionProjectBuilder.m_currentlyExecutingScriptPath = oldScriptPath;
}
catch ( Exception ex )
{
Directory.SetCurrentDirectory( oldDir );
SolutionProjectBuilder.m_scriptRelativeDir = oldScriptRelativeDir;
SolutionProjectBuilder.m_currentlyExecutingScriptPath = oldScriptPath;
Exception2 ex2 = ex.InnerException as Exception2;
if (ex2 != null && bAllowThrow)
throw ex2;
try
{
StackFrame[] stack = new StackTrace(ex.InnerException, true).GetFrames();
StackFrame lastCall = stack[0];
errors = String.Format("{0}({1},{2}): error: {3}\r\n", path,
lastCall.GetFileLineNumber(), lastCall.GetFileColumnNumber(), ex.InnerException.Message);
} catch (Exception ex3 )
{
errors = String.Format("{0}(1,1): error: Internal error - exception '{3}'\r\n", path, ex3.Message);
}
if (bAllowThrow)
throw new Exception2(errors);
return false;
}
return true;
} //RunScript
static String GetUniqueTempFilename( String path )
{
String baseName = Path.GetFileNameWithoutExtension(path);
string ProcID = Process.GetCurrentProcess().Id.ToString();
string tmpFolder = Path.GetTempPath();
string outFile = tmpFolder + baseName + "_" + ProcID;
return outFile;
}
/// <summary>
/// Scans through C# script and gets additional information about C# script itself,
/// like dependent .cs files, and so on.
/// </summary>
/// <param name="csPath">C# script to load and scan</param>
/// <param name="bUseAbsolutePaths">true if to use absolute paths, false if not</param>
/// <param name="exceptFiles">Don't include path'es specified in here</param>
/// <returns>C# script info</returns>
static public CsScriptInfo getCsFileInfo( String csPath, bool bUseAbsolutePaths, List<String> exceptFiles = null )
{
CsScriptInfo csInfo = new CsScriptInfo();
if (exceptFiles == null)
exceptFiles = new List<string>();
if(!exceptFiles.Contains(csPath) )
exceptFiles.Add(csPath);
// ----------------------------------------------------------------
// Using C# kind of syntax - like this:
// //css_include <file.cs>;
// ----------------------------------------------------------------
var regexOpt = RegexOptions.Multiline | RegexOptions.IgnoreCase;
Regex reIsCommentUsingEmptyLine = new Regex("^ *(//|using|$)", regexOpt);
Regex reCssImport = new Regex("^ *//css_include +(.*?);?$", regexOpt);
Regex reDebug = new Regex("^ *//css_debug", regexOpt);
Regex reCssRef = new Regex("^ *//css_ref +(.*?);?$", regexOpt);
int iLine = 1;
csInfo.bCsDebug = false;
using (StreamReader reader = new StreamReader(csPath))
{
for (; ; iLine++)
{
String line = reader.ReadLine();
if (line == null)
break;
// If we have any comments, or using namespace or empty line, we continue scanning, otherwise aborting (class, etc...)
if (!reIsCommentUsingEmptyLine.Match(line).Success)
break;
// Pick up .dll filename from //css_ref <dll filename> file line.
var rasm = reCssRef.Match(line);
if (rasm.Success)
{
// Not interested in ourselves, we are added automatically as reference .dll
if (line.EndsWith("syncproj.exe", StringComparison.CurrentCultureIgnoreCase))
continue;
// Allow end user to use %SystemRoot%\....dll environment variables.
String file = Environment.ExpandEnvironmentVariables(rasm.Groups[1].Value);
// If assembly file exists near script, use it.
if (bUseAbsolutePaths)
{
String fileFullPath = Path.Combine(Path.GetDirectoryName(csPath), file);
if (File.Exists(fileFullPath))
file = fileFullPath;
} //if
csInfo.refFiles.Add(file);
continue;
}
var rem = reCssImport.Match(line);
if (rem.Success)
{
String file = rem.Groups[1].Value;
String fileFullPath = file;
if (!Path.IsPathRooted(file))
fileFullPath = Path.Combine(Path.GetDirectoryName(csPath), file);
if (!File.Exists(fileFullPath))
throw new FileSpecificException("Include file specified in '" + Exception2.getPath(fileFullPath) +
"' was not found (Included from '" + Exception2.getPath(csPath) + "')", csPath, iLine);
bool bContains = false;
String fPath;
if (bUseAbsolutePaths)
fPath = fileFullPath;
else
fPath = file;
// Prevent cyclic references.
bContains = csInfo.csFiles.Contains(fPath);
if (!bContains) bContains = exceptFiles.Contains(fileFullPath);
if (!bContains)
{
csInfo.csFiles.Add(fPath.Replace("/", "\\"));
exceptFiles.Add(fileFullPath);
}
if (!bContains)
{
CsScriptInfo subCsInfo = getCsFileInfo(fileFullPath, bUseAbsolutePaths, exceptFiles);
if (subCsInfo.bCsDebug) // Flag turned on from any dependent .cs file will enable debug also for main file.
csInfo.bCsDebug = true;
foreach (String subFile in subCsInfo.csFiles)
if (!csInfo.csFiles.Contains(subFile))
csInfo.csFiles.Add(subFile);
foreach (String refFile in subCsInfo.refFiles)
if (!csInfo.refFiles.Contains(refFile))
csInfo.refFiles.Add(refFile);
} //if
} //if
if (reDebug.Match(line).Success)
csInfo.bCsDebug = true;
} //for
} //using
return csInfo;
} //getCsFileInfo
} //class CsScript
/// <summary>
/// Additional info about c# script.
/// </summary>
public class CsScriptInfo
{
/// <summary>
/// Referred .cs files to include into compilation
/// </summary>
public List<String> csFiles = new List<string>();
/// <summary>
/// Referred .dll's and assembly names, which must be included as reference assemblies when compiling.
/// </summary>
public List<String> refFiles = new List<string>();
/// <summary>
/// Just additional //css_debug for compile troubleshooting in this code
/// </summary>
public bool bCsDebug;
/// <summary>
/// checks if debug enabled.
/// </summary>
/// <returns>true - enabled</returns>
public bool DebugEnabled()
{
if (bCsDebug)
return true;
if( g_bCsDebug )
return true;
return false;
}
/// <summary>
/// Global flag to enable C# script compilation debugging
/// </summary>
public static bool g_bCsDebug = false;
}