-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFileReader.cs
148 lines (139 loc) · 5.59 KB
/
FileReader.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace Hunt
{
public class FileReader
{
private const string StartNamespace = "{";
private const string EndNamespace = "}";
private readonly StringBuilder MainClass;
private readonly List<string> ExcludedFiles;
private List<string> ExcludedDirs;
private readonly List<string> References;
private readonly Dictionary<string, StringBuilder> NamespaceMergedContent;
private readonly string BasePath;
private readonly List<string> OutputPath;
private readonly string Extension;
private readonly string OutputFileName;
public FileReader(string basePath, List<string> outputPath, string outputFileName)
{
BasePath = basePath;
Extension = "*.cs";
NamespaceMergedContent = new Dictionary<string, StringBuilder>();
MainClass = new StringBuilder();
References = new List<string>();
ExcludedFiles = new List<string> {"AssemblyInfo.cs"};
ExcludedDirs = new List<string>() {"obj"};
OutputPath = outputPath;
OutputFileName = outputFileName;
}
public void AddExcludedFiles(List<string> excludedFilesToAdd)
{
ExcludedFiles.AddRange(excludedFilesToAdd);
}
private void GetAllFilesOfTypeInDirectoryRecursivly(string directoryPath, List<string> fileList)
{
try
{
foreach (string file in Directory.GetFiles(directoryPath, Extension))
{
var fileName = Path.GetFileName(file);
if (ExcludedFiles.Contains(fileName))
continue;
fileList.Add(file);
}
foreach (string directory in Directory.GetDirectories(directoryPath))
{
var directoryName = new DirectoryInfo(directory).Name;
if(ExcludedDirs.Contains(directoryName))
continue;
GetAllFilesOfTypeInDirectoryRecursivly(directory, fileList);
}
}
catch (Exception excpt)
{
Console.WriteLine(excpt.Message);
}
}
public void MergeDirectory()
{
var fileList = new List<string>();
GetAllFilesOfTypeInDirectoryRecursivly(BasePath, fileList);
foreach (var file in fileList)
{
ReadFile(file);
}
foreach (var outpath in OutputPath)
{
using (var outfile = new StreamWriter(Path.Combine(outpath, OutputFileName)))
{
outfile.WriteLine("// Reference: Oxide.Ext.Rust");
outfile.WriteLine("// Reference: Newtonsoft.Json");
foreach (var reference in References)
outfile.WriteLine("using {0}", reference);
outfile.WriteLine();
outfile.WriteLine("namespace Oxide.Plugins");
outfile.WriteLine(StartNamespace);
outfile.Write(MainClass.ToString());
outfile.WriteLine(EndNamespace);
outfile.WriteLine();
foreach (var namespaceContent in NamespaceMergedContent)
{
outfile.WriteLine("namespace {0}", namespaceContent.Key);
outfile.WriteLine(StartNamespace);
outfile.Write(namespaceContent.Value.ToString());
outfile.WriteLine(EndNamespace);
outfile.WriteLine();
}
}
}
}
private void ReadFile(string filePath)
{
var file = new StreamReader(filePath);
string line;
List<string> fileContent = new List<string>();
string namespaceLine = "";
while ((line = file.ReadLine()) != null)
{
var isReferenceLine = line.StartsWith("using");
var strings = line.Split(' ');
if (isReferenceLine)
{
var referenceLine = "";
for (int i = 1; i < strings.Length; i++)
referenceLine += strings[i];
if (!References.Contains(referenceLine))
References.Add(referenceLine);
continue;
}
var isNamespaceLine = line.StartsWith("namespace");
if (isNamespaceLine)
{
namespaceLine = strings[1];
continue;
}
var isBrackAtStart = line.StartsWith(StartNamespace) || line.StartsWith(EndNamespace);
if (isBrackAtStart)
continue;
fileContent.Add(line);
}
if (namespaceLine.Contains("Oxide.Plugins"))
{
foreach (var fileLine in fileContent)
MainClass.AppendLine(fileLine);
}
else
{
if (!NamespaceMergedContent.ContainsKey(namespaceLine))
NamespaceMergedContent.Add(namespaceLine, new StringBuilder());
var sb = NamespaceMergedContent[namespaceLine];
foreach (var fileLine in fileContent)
sb.AppendLine(fileLine);
}
file.Close();
}
}
}