-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfileparser.cs
More file actions
911 lines (802 loc) · 31.3 KB
/
fileparser.cs
File metadata and controls
911 lines (802 loc) · 31.3 KB
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
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
#region Licence...
//-----------------------------------------------------------------------------
// Date: 31/01/05 Time: 17:15p
// Module: fileparser.cs
// Classes: ParsingParams
// ScriptInfo
// ScriptParser
// FileParser
// FileParserComparer
//
// This module contains the definition of the classes which implement
// parsing script code. The result of such processing is a collections of the names
// of the namespacs and assemblies used by the script code.
//
// Written by Oleg Shilo (oshilo@gmail.com)
//----------------------------------------------
// The MIT License (MIT)
// Copyright (c) 2014 Oleg Shilo
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software
// and associated documentation files (the "Software"), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial
// portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//----------------------------------------------
#endregion Licence...
using System;
using System.IO;
using System.Text;
#if net1
using System.Collections;
#else
using System.Collections.Generic;
#endif
using csscript;
namespace CSScriptLibrary
{
/// <summary>
/// ParsingParams is an class that holds parsing parameters (parameters that controls how file is to be parsed).
/// At this moment they are namespace renaming rules only.
/// </summary>
class ParsingParams
{
#region Public interface...
public ParsingParams()
{
#if net1
renameNamespaceMap = new ArrayList();
#else
renameNamespaceMap = new List<string[]>();
#endif
}
public string[][] RenameNamespaceMap
{
#if net1
get { return (string[][])renameNamespaceMap.ToArray(typeof(string[])); }
#else
get { return renameNamespaceMap.ToArray(); }
#endif
}
public void AddRenameNamespaceMap(string[][] names)
{
renameNamespaceMap.AddRange(names);
}
/// <summary>
/// Compare() is to be used to help with implementation of IComparer for sorting operations.
/// </summary>
public static int Compare(ParsingParams xPrams, ParsingParams yPrams)
{
if (xPrams == null && yPrams == null)
return 0;
int retval = xPrams == null ? -1 : (yPrams == null ? 1 : 0);
if (retval == 0)
{
string[][] xNames = xPrams.RenameNamespaceMap;
string[][] yNames = yPrams.RenameNamespaceMap;
retval = System.Collections.Comparer.Default.Compare(xNames.Length, yNames.Length);
if (retval == 0)
{
for (int i = 0; i < xNames.Length && retval == 0; i++)
{
retval = System.Collections.Comparer.Default.Compare(xNames[i].Length, yNames[i].Length);
if (retval == 0)
{
for (int j = 0; j < xNames[i].Length; j++)
{
retval = System.Collections.Comparer.Default.Compare(xNames[i][j], yNames[i][j]);
}
}
}
}
}
return retval;
}
public bool preserveMain = false;
#endregion Public interface...
#if net1
private ArrayList renameNamespaceMap;
#else
List<string[]> renameNamespaceMap;
#endif
}
/// <summary>
/// Class which is a placeholder for general information of the script file
/// </summary>
class ScriptInfo
{
/// <summary>
/// Constructor
/// </summary>
/// <param name="info">ImportInfo object containing the information how the script file should be parsed.</param>
public ScriptInfo(CSharpParser.ImportInfo info)
{
this.fileName = info.file;
parseParams.AddRenameNamespaceMap(info.renaming);
parseParams.preserveMain = info.preserveMain;
}
public ParsingParams parseParams = new ParsingParams();
public string fileName;
}
/// <summary>
/// Class that implements parsing the single C# script file
/// </summary>
class FileParser
{
static bool _throwOnError = true;
public System.Threading.ApartmentState ThreadingModel
{
get
{
if (this.parser == null)
return System.Threading.ApartmentState.Unknown;
else
return this.parser.ThreadingModel;
}
}
public FileParser()
{
}
public FileParser(string fileName, ParsingParams prams, bool process, bool imported, string[] searchDirs, bool throwOnError)
{
FileParser._throwOnError = throwOnError;
this.imported = imported;
this.prams = prams;
this.searchDirs = searchDirs;
this.fileName = ResolveFile(fileName, searchDirs);
if (process)
ProcessFile();
}
public string fileNameImported = "";
public ParsingParams prams = null;
public string FileToCompile
{
get { return imported ? fileNameImported : fileName; }
}
public string[] SearchDirs
{
get { return searchDirs; }
}
public bool Imported
{
get { return imported; }
}
public string[] ReferencedNamespaces
{
get { return Utils.Except(parser.RefNamespaces, parser.IgnoreNamespaces); }
}
public string[] IgnoreNamespaces
{
get { return parser.IgnoreNamespaces; }
}
public string[] Precompilers
{
get { return parser.Precompilers; }
}
public string[] ReferencedAssemblies
{
get { return parser.RefAssemblies; }
}
public string[] ExtraSearchDirs
{
get { return parser.ExtraSearchDirs; }
}
public string[] ReferencedResources
{
get { return parser.ResFiles; }
}
public string[] CompilerOptions
{
get { return parser.CompilerOptions; }
}
public ScriptInfo[] ReferencedScripts
{
#if net1
get { return (ScriptInfo[])referencedScripts.ToArray(typeof(ScriptInfo)); }
#else
get { return referencedScripts.ToArray(); }
#endif
}
public void ProcessFile()
{
referencedAssemblies.Clear();
referencedScripts.Clear();
referencedNamespaces.Clear();
referencedResources.Clear();
this.parser = new CSharpParser(fileName, true);
foreach (CSharpParser.ImportInfo info in parser.Imports)
{
referencedScripts.Add(new ScriptInfo(info));
}
referencedAssemblies.AddRange(parser.RefAssemblies);
referencedNamespaces.AddRange(Utils.Except(parser.RefNamespaces, parser.IgnoreNamespaces));
referencedResources.AddRange(parser.ResFiles);
if (imported)
{
if (prams != null)
{
parser.DoRenaming(prams.RenameNamespaceMap, prams.preserveMain);
}
if (parser.ModifiedCode == "")
{
fileNameImported = fileName; //importing does not require any modification of the original code
}
else
{
fileNameImported = Path.Combine(CSExecutor.ScriptCacheDir, string.Format("i_{0}_{1}{2}", Path.GetFileNameWithoutExtension(fileName), CSSUtils.GetHashCodeEx(Path.GetDirectoryName(fileName)), Path.GetExtension(fileName)));
if (!Directory.Exists(Path.GetDirectoryName(fileNameImported)))
Directory.CreateDirectory(Path.GetDirectoryName(fileNameImported));
if (File.Exists(fileNameImported))
{
File.SetAttributes(fileNameImported, FileAttributes.Normal);
Utils.FileDelete(fileNameImported, true);
}
using (StreamWriter scriptWriter = new StreamWriter(fileNameImported, false, Encoding.UTF8))
{
//scriptWriter.Write(ComposeHeader(fileNameImported)); //using a big header at start is overkill (it also shifts line numbers so they do not match with the original script file)
//but might be required in future
scriptWriter.WriteLine(parser.ModifiedCode);
scriptWriter.WriteLine("///////////////////////////////////////////");
scriptWriter.WriteLine("// Compiler-generated file - DO NOT EDIT!");
scriptWriter.WriteLine("///////////////////////////////////////////");
}
File.SetAttributes(fileNameImported, FileAttributes.ReadOnly);
}
}
}
#if net1
private ArrayList referencedScripts = new ArrayList();
private ArrayList referencedNamespaces = new ArrayList();
private ArrayList referencedAssemblies = new ArrayList();
private ArrayList referencedResources = new ArrayList();
#else
private List<ScriptInfo> referencedScripts = new List<ScriptInfo>();
private List<string> referencedNamespaces = new List<string>();
private List<string> referencedAssemblies = new List<string>();
private List<string> referencedResources = new List<string>();
#endif
private string[] searchDirs;
private bool imported = false;
/// <summary>
/// Searches for script file by given script name. Calls ResolveFile(string fileName, string[] extraDirs, bool throwOnError)
/// with throwOnError flag set to true.
/// </summary>
public static string ResolveFile(string fileName, string[] extraDirs)
{
return ResolveFile(fileName, extraDirs, _throwOnError);
}
/// <summary>
/// Searches for script file by given script name. Search order:
/// 1. Current directory
/// 2. extraDirs (usually %CSSCRIPT_DIR%\Lib and ExtraLibDirectory)
/// 3. PATH
/// Also fixes file name if user did not provide extension for script file (assuming .cs extension)
/// </summary>
public static string ResolveFile(string file, string[] extraDirs, bool throwOnError)
{
string retval = ResolveFile(file, extraDirs, "");
if (retval == "")
retval = ResolveFile(file, extraDirs, ".cs");
if (retval == "")
retval = ResolveFile(file, extraDirs, ".csl"); //script link file
if (retval == "")
{
if (throwOnError)
throw new FileNotFoundException(string.Format("Could not find file \"{0}\"", file));
retval = file;
if (!retval.EndsWith(".cs"))
retval += ".cs";
}
return retval;
}
private static string ResolveFile(string file, string[] extraDirs, string extension)
{
string fileName = file;
//current directory
if (Path.GetExtension(fileName) == "")
fileName += extension;
if (File.Exists(fileName))
{
return Path.GetFullPath(fileName);
}
//arbitrary directories
if (extraDirs != null)
{
foreach (string extraDir in extraDirs)
{
string dir = extraDir;
if (File.Exists(Path.Combine(dir, fileName)))
{
return Path.GetFullPath(Path.Combine(dir, fileName));
}
}
}
//PATH
string[] pathDirs = Environment.GetEnvironmentVariable("PATH").Replace("\"", "").Split(';');
foreach (string pathDir in pathDirs)
{
string dir = pathDir;
if (File.Exists(Path.Combine(dir, fileName)))
{
return Path.GetFullPath(Path.Combine(dir, fileName));
}
}
return "";
}
static public string headerTemplate =
@"/*" + Environment.NewLine +
@" Created by {0}" +
@" Original location: {1}" + Environment.NewLine +
@" C# source equivalent of {2}" + Environment.NewLine +
@" compiler-generated file created {3} - DO NOT EDIT!" + Environment.NewLine +
@"*/" + Environment.NewLine;
public string ComposeHeader(string path)
{
return string.Format(headerTemplate, csscript.AppInfo.appLogoShort, path, fileName, DateTime.Now);
}
public string fileName = "";
public CSharpParser parser;
}
/// <summary>
/// Class that implements parsing the single C# Script file
/// </summary>
/// <summary>
/// Implementation of the IComparer for sorting operations of collections of FileParser instances
/// </summary>
///
#if net1
class FileParserComparer : IComparer
{
public int Compare(object x, object y)
{
if (x == null && y == null)
return 0;
int retval = x == null ? -1 : (y == null ? 1 : 0);
if (retval == 0)
{
FileParser xParser = (FileParser)x;
FileParser yParser = (FileParser)y;
retval = string.Compare(xParser.fileName, yParser.fileName, true);
if (retval == 0)
{
retval = ParsingParams.Compare(xParser.prams, yParser.prams);
}
}
return retval;
}
}
#else
class FileParserComparer : IComparer<FileParser>
{
public int Compare(FileParser x, FileParser y)
{
if (x == null && y == null)
return 0;
int retval = x == null ? -1 : (y == null ? 1 : 0);
if (retval == 0)
{
retval = string.Compare(x.fileName, y.fileName, true);
if (retval == 0)
{
retval = ParsingParams.Compare(x.prams, y.prams);
}
}
return retval;
}
}
#endif
/// <summary>
/// Class that manages parsing the main and all imported (if any) C# Script files
/// </summary>
public class ScriptParser
{
bool throwOnError = true;
/// <summary>
/// ApartmentState of a script during the execution (default: ApartmentState.Unknown)
/// </summary>
public System.Threading.ApartmentState apartmentState = System.Threading.ApartmentState.Unknown;
/// <summary>
/// Collection of the files to be compiled (including dependant scripts)
/// </summary>
public string[] FilesToCompile
{
get
{
#if net1
ArrayList retval = new ArrayList();
foreach (FileParser file in fileParsers)
retval.Add(file.FileToCompile);
return (string[])retval.ToArray(typeof(string));
#else
List<string> retval = new List<string>();
foreach (FileParser file in fileParsers)
retval.Add(file.FileToCompile);
return retval.ToArray();
#endif
}
}
/// <summary>
/// Collection of the imported files (dependant scripts)
/// </summary>
public string[] ImportedFiles
{
get
{
#if net1
ArrayList retval = new ArrayList();
foreach (FileParser file in fileParsers)
{
if (file.Imported)
retval.Add(file.fileName);
}
return (string[])retval.ToArray(typeof(string));
#else
List<string> retval = new List<string>();
foreach (FileParser file in fileParsers)
{
if (file.Imported)
retval.Add(file.fileName);
}
return retval.ToArray();
#endif
}
}
/// <summary>
/// Collection of resource files referenced from code
/// </summary>
public string[] ReferencedResources
{
#if net1
get { return (string[])referencedResources.ToArray(typeof(string)); }
#else
get { return referencedResources.ToArray(); }
#endif
}
/// <summary>
/// Collection of compiler options
/// </summary>
public string[] CompilerOptions
{
#if net1
get { return (string[])compilerOptions.ToArray(typeof(string)); }
#else
get { return compilerOptions.ToArray(); }
#endif
}
/// <summary>
/// Precompilers specified in the primary script file.
/// </summary>
public string[] Precompilers
{
#if net1
get { return (string[])precompilers.ToArray(typeof(string)); }
#else
get { return precompilers.ToArray(); }
#endif
}
/// <summary>
/// Collection of namespaces referenced from code (including those referenced in dependand scripts)
/// </summary>
public string[] ReferencedNamespaces
{
#if net1
get { return (string[])referencedNamespaces.ToArray(typeof(string)); }
#else
get { return referencedNamespaces.ToArray(); }
#endif
}
/// <summary>
/// Collection of namespaces, which if found in code, should not be resolved into referenced assembly.
/// </summary>
public string[] IgnoreNamespaces
{
#if net1
get { return (string[])ignoreNamespaces.ToArray(typeof(string)); }
#else
get { return ignoreNamespaces.ToArray(); }
#endif
}
/// <summary>
/// Collection of referenced asesemblies. All assemblies are referenced either from command-line, code or resolved from referenced namespaces.
/// </summary>
public string[] ReferencedAssemblies
{
#if net1
get { return (string[])referencedAssemblies.ToArray(typeof(string)); }
#else
get { return referencedAssemblies.ToArray(); }
#endif
}
/// <summary>
/// Constructor.
/// </summary>
/// <param name="fileName">Script file name</param>
public ScriptParser(string fileName)
{
Init(fileName, null);
}
/// <summary>
/// Constructor.
/// </summary>
/// <param name="fileName">Script file name</param>
/// <param name="searchDirs">Extra ScriptLibrary directory </param>
public ScriptParser(string fileName, string[] searchDirs)
{
//if ((CSExecutor.ExecuteOptions.options != null && CSExecutor.options.useSmartCaching) && CSExecutor.ScriptCacheDir == "") //in case if ScriptParser is used outside of the script engine
if (CSExecutor.ScriptCacheDir == "") //in case if ScriptParser is used outside of the script engine
CSExecutor.SetScriptCacheDir(fileName);
Init(fileName, searchDirs);
}
/// <summary>
/// Constructor.
/// </summary>
/// <param name="fileName">Script file name</param>
/// <param name="searchDirs">Extra ScriptLibrary directory(ies) </param>
/// <param name="throwOnError">flag to indicate if the file parsing/processing error should raise an exception</param>
public ScriptParser(string fileName, string[] searchDirs, bool throwOnError)
{
this.throwOnError = throwOnError;
//if ((CSExecutor.ExecuteOptions.options != null && CSExecutor.ExecuteOptions.options.useSmartCaching) && CSExecutor.ScriptCacheDir == "") //in case if ScriptParser is used outside of the script engine
if (CSExecutor.ScriptCacheDir == "") //in case if ScriptParser is used outside of the script engine
CSExecutor.SetScriptCacheDir(fileName);
Init(fileName, searchDirs);
}
/// <summary>
/// Initialization of ScriptParser instance
/// </summary>
/// <param name="fileName">Script file name</param>
/// <param name="searchDirs">Extra ScriptLibrary directory(ies) </param>
private void Init(string fileName, string[] searchDirs)
{
#if net1
referencedNamespaces = new ArrayList();
referencedAssemblies = new ArrayList();
referencedResources = new ArrayList();
ignoreNamespaces = new ArrayList();
compilerOptions = new ArrayList();
precompilers = new ArrayList();
#else
referencedNamespaces = new List<string>();
referencedAssemblies = new List<string>();
referencedResources = new List<string>();
ignoreNamespaces = new List<string>();
precompilers = new List<string>();
compilerOptions = new List<string>();
#endif
//process main file
FileParser mainFile = new FileParser(fileName, null, true, false, searchDirs, throwOnError);
this.apartmentState = mainFile.ThreadingModel;
foreach (string file in mainFile.Precompilers)
PushPrecompiler(file);
foreach (string namespaceName in mainFile.IgnoreNamespaces)
PushIgnoreNamespace(namespaceName);
foreach (string namespaceName in mainFile.ReferencedNamespaces)
PushNamespace(namespaceName);
foreach (string asmName in mainFile.ReferencedAssemblies)
PushAssembly(asmName);
foreach (string resFile in mainFile.ReferencedResources)
PushResource(resFile);
foreach (string opt in mainFile.CompilerOptions)
PushCompilerOptions(opt);
#if net1
ArrayList dirs = new ArrayList();
dirs.Add(Path.GetDirectoryName(mainFile.fileName));//note: mainFile.fileName is warrantied to be a full name but fileName is not
if (searchDirs != null)
dirs.AddRange(searchDirs);
foreach (string dir in mainFile.ExtraSearchDirs)
if (Path.IsPathRooted(dir))
dirs.Add(Path.GetFullPath(dir));
else
dirs.Add(Path.Combine(Path.GetDirectoryName(mainFile.fileName), dir));
this.SearchDirs = (string[])dirs.ToArray(typeof(string));
#else
List<string> dirs = new List<string>();
dirs.Add(Path.GetDirectoryName(mainFile.fileName));//note: mainFile.fileName is warrantied to be a full name but fileName is not
if (searchDirs != null)
dirs.AddRange(searchDirs);
foreach (string dir in mainFile.ExtraSearchDirs)
{
if (Path.IsPathRooted(dir))
dirs.Add(Path.GetFullPath(dir));
else
dirs.Add(Path.Combine(Path.GetDirectoryName(mainFile.fileName), dir));
}
this.SearchDirs = dirs.ToArray();
#endif
//process imported files if any
foreach (ScriptInfo fileInfo in mainFile.ReferencedScripts)
ProcessFile(fileInfo);
//Main script file shall always be the first. Add it now as previously array was sorted a few times
this.fileParsers.Insert(0, mainFile);
}
private void ProcessFile(ScriptInfo fileInfo)
{
FileParserComparer fileComparer = new FileParserComparer();
FileParser importedFile = new FileParser(fileInfo.fileName, fileInfo.parseParams, false, true, this.SearchDirs, throwOnError); //do not parse it yet (the third param is false)
if (fileParsers.BinarySearch(importedFile, fileComparer) < 0)
{
if (File.Exists(importedFile.fileName))
{
importedFile.ProcessFile(); //parse now namespaces, ref. assemblies and scripts; also it will do namespace renaming
this.fileParsers.Add(importedFile);
this.fileParsers.Sort(fileComparer);
foreach (string namespaceName in importedFile.ReferencedNamespaces)
PushNamespace(namespaceName);
foreach (string asmName in importedFile.ReferencedAssemblies)
PushAssembly(asmName);
foreach (string file in importedFile.Precompilers)
PushPrecompiler(file);
foreach (ScriptInfo scriptFile in importedFile.ReferencedScripts)
ProcessFile(scriptFile);
foreach (string resFile in importedFile.ReferencedResources)
PushResource(resFile);
foreach (string file in importedFile.IgnoreNamespaces)
PushIgnoreNamespace(file);
#if net1
ArrayList dirs = new ArrayList(this.SearchDirs);
foreach (string dir in importedFile.ExtraSearchDirs)
if (Path.IsPathRooted(dir))
dirs.Add(Path.GetFullPath(dir));
else
dirs.Add(Path.Combine(Path.GetDirectoryName(importedFile.fileName), dir));
this.SearchDirs = (string[])dirs.ToArray(typeof(string));
#else
List<string> dirs = new List<string>(this.SearchDirs);
foreach (string dir in importedFile.ExtraSearchDirs)
if (Path.IsPathRooted(dir))
dirs.Add(Path.GetFullPath(dir));
else
dirs.Add(Path.Combine(Path.GetDirectoryName(importedFile.fileName), dir));
this.SearchDirs = dirs.ToArray();
#endif
}
else
{
importedFile.fileNameImported = importedFile.fileName;
this.fileParsers.Add(importedFile);
this.fileParsers.Sort(fileComparer);
}
}
}
#if net1
private ArrayList fileParsers = new ArrayList();
#else
private List<FileParser> fileParsers = new List<FileParser>();
#endif
/// <summary>
/// Saves all imported scripts int temporary location.
/// </summary>
/// <returns>Collection of the saved imported scrips file names</returns>
public string[] SaveImportedScripts()
{
string workingDir = Path.GetDirectoryName(((FileParser)fileParsers[0]).fileName);
#if net1
ArrayList retval = new ArrayList();
#else
List<string> retval = new List<string>();
#endif
foreach (FileParser file in fileParsers)
{
if (file.Imported)
{
if (file.fileNameImported != file.fileName) //script file was copied
retval.Add(file.fileNameImported);
else
retval.Add(file.fileName);
}
}
#if net1
return (string[])retval.ToArray(typeof(string));
#else
return retval.ToArray();
#endif
}
/// <summary>
/// Deletes imported scripts as a cleanup operation
/// </summary>
public void DeleteImportedFiles()
{
foreach (FileParser file in fileParsers)
{
if (file.Imported && file.fileNameImported != file.fileName) //the file was copied
{
try
{
File.SetAttributes(file.FileToCompile, FileAttributes.Normal);
Utils.FileDelete(file.FileToCompile);
}
catch { }
}
}
}
#if net1
private ArrayList referencedNamespaces;
private ArrayList ignoreNamespaces;
private ArrayList compilerOptions;
private ArrayList referencedResources;
private ArrayList precompilers;
private ArrayList referencedAssemblies;
#else
private List<string> referencedNamespaces;
private List<string> ignoreNamespaces;
private List<string> referencedResources;
private List<string> compilerOptions;
private List<string> precompilers;
private List<string> referencedAssemblies;
#endif
/// <summary>
/// CS-Script SearchDirectories specified in the parsed script or its dependent scripts.
/// </summary>
public string[] SearchDirs;
#if net1
private void PushItem(ArrayList collection, string item)
#else
private void PushItem(List<string> collection, string item)
#endif
{
if (collection.Count > 1)
collection.Sort();
AddIfNotThere(collection, item);
}
private void PushNamespace(string nameSpace)
{
PushItem(referencedNamespaces, nameSpace);
}
private void PushPrecompiler(string file)
{
PushItem(precompilers, file);
}
private void PushIgnoreNamespace(string nameSpace)
{
PushItem(ignoreNamespaces, nameSpace);
}
private void PushAssembly(string asmName)
{
PushItem(referencedAssemblies, asmName);
}
private void PushResource(string resName)
{
PushItem(referencedResources, resName);
}
private void PushCompilerOptions(string option)
{
AddIfNotThere(compilerOptions, option);
}
#if net1
class StringComparer : System.Collections.IComparer
{
public int Compare(object x, object y)
{
return string.Compare(x as string, y as string, true);
}
}
void AddIfNotThere(System.Collections.ArrayList arr, string item)
{
if (arr.BinarySearch(item, new StringComparer()) < 0)
arr.Add(item);
}
#else
class StringComparer : IComparer<string>
{
public int Compare(string x, string y)
{
return string.Compare(x, y, true);
}
}
void AddIfNotThere(List<string> list, string item)
{
if (list.BinarySearch(item, new StringComparer()) < 0)
list.Add(item);
}
#endif
}
}