forked from scriptcs/scriptcs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFilePreProcessor.cs
More file actions
121 lines (104 loc) · 4.48 KB
/
FilePreProcessor.cs
File metadata and controls
121 lines (104 loc) · 4.48 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
using System.Collections.Generic;
using System.Linq;
using Common.Logging;
namespace ScriptCs
{
public class FilePreProcessor : IFilePreProcessor
{
private readonly ILog _logger;
protected readonly IFileSystem _fileSystem;
public FilePreProcessor(IFileSystem fileSystem, ILog logger)
{
_fileSystem = fileSystem;
_logger = logger;
}
public string ProcessFile(string path)
{
_logger.DebugFormat("{0} - Reading lines", path);
var entryFile = _fileSystem.ReadFileLines(path);
var usings = new List<string>();
var rs = new List<string>();
var loads = new List<string>();
_logger.DebugFormat("{0} - Parsing ", path);
var parsed = ParseFile(path, entryFile, ref usings, ref rs, ref loads);
_logger.DebugFormat("{0} - Generating references (#r)", path);
var result = GenerateRs(rs);
if (!string.IsNullOrWhiteSpace(result))
{
result += _fileSystem.NewLine;
}
_logger.DebugFormat("{0} - Generating using statements", path);
result += GenerateUsings(usings);
if (!string.IsNullOrWhiteSpace(result))
{
result += _fileSystem.NewLine;
}
result += parsed;
return result;
}
protected virtual string GenerateUsings(ICollection<string> usingLines)
{
return string.Join(_fileSystem.NewLine, usingLines.Distinct());
}
protected virtual string GenerateRs(ICollection<string> rLines)
{
return string.Join(_fileSystem.NewLine, rLines.Distinct());
}
private string ParseFile(string path, IEnumerable<string> file, ref List<string> usings, ref List<string> rs, ref List<string> loads)
{
var fileList = file.ToList();
var firstCode = fileList.FindIndex(l => PreProcessorUtil.IsNonDirectiveLine(l));
var firstBody = fileList.FindIndex(l => PreProcessorUtil.IsNonDirectiveLine(l) && !PreProcessorUtil.IsUsingLine(l));
// add #line before the actual code begins
// +1 because we are in a zero indexed list, but line numbers are 1 indexed
// we need to keep the original position of the actual line
if (firstBody != -1)
{
_logger.DebugFormat("Added #line statement for file {0} at line {1}", path, firstBody);
fileList.Insert(firstBody, string.Format(@"#line {0} ""{1}""", firstBody + 1, path));
}
for (var i = 0; i < fileList.Count; i++)
{
var line = fileList[i];
if (PreProcessorUtil.IsUsingLine(line))
{
usings.Add(line);
}
else if (PreProcessorUtil.IsRLine(line))
{
if (i < firstCode)
{
rs.Add(line);
}
else
{
fileList[i] = string.Empty;
}
}
else if (PreProcessorUtil.IsLoadLine(line))
{
if ((i < firstCode || firstCode < 0) && !loads.Contains(line))
{
var filepath = PreProcessorUtil.GetPath(PreProcessorUtil.LoadString, line);
var filecontent = _fileSystem.IsPathRooted(filepath)
? _fileSystem.ReadFileLines(filepath)
: _fileSystem.ReadFileLines(_fileSystem.CurrentDirectory + @"\" + filepath);
if (filecontent != null)
{
loads.Add(line);
_logger.DebugFormat("Parsing file {0}", path);
var parsed = ParseFile(filepath, filecontent, ref usings, ref rs, ref loads);
fileList[i] = parsed;
}
}
else
{
fileList[i] = string.Empty;
}
}
}
var result = string.Join(_fileSystem.NewLine, fileList.Where(line => !PreProcessorUtil.IsUsingLine(line) && !PreProcessorUtil.IsRLine(line)));
return result;
}
}
}