-
Notifications
You must be signed in to change notification settings - Fork 0
/
CodeFileModifier.cs
57 lines (48 loc) · 1.37 KB
/
CodeFileModifier.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
using System.Collections.Generic;
using System.IO;
using System.Linq;
public class CodeFileModifier
{
private readonly string filename;
private readonly List<string> lines;
public CodeFileModifier(string filename)
{
this.filename = filename;
lines = File.ReadAllLines(filename).ToList();
}
public void AddUsing(string usingNamespace)
{
lines.Insert(0, "using " + usingNamespace + ";");
}
public void Insert(int lineNumber, int indentLevel, string line)
{
var indent = "";
for (var i = 0; i < indentLevel; i++) indent += " ";
lines.Insert(lineNumber, indent + line);
}
public void ReplaceLine(string original, params string[] updated)
{
var source = lines.ToArray();
for (var i = 0; i < source.Length; i++)
{
var line = source[i];
if (line.Contains(original))
{
var start = line.IndexOf(original);
var padding = line.Substring(0, start);
lines.RemoveAt(i);
var j = 0;
foreach (var update in updated)
{
lines.Insert(i + j, padding + update);
j++;
}
return;
}
}
}
public void Modify()
{
File.WriteAllLines(filename, lines);
}
}