-
Notifications
You must be signed in to change notification settings - Fork 0
/
Editor.cs
99 lines (81 loc) · 3.52 KB
/
Editor.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace SetCsprojVer
{
internal class VersionUpdate
{
public string VersionKey { get; private set; }
public string Version { get; private set; }
public VersionUpdate(string versionKey, string version)
{
VersionKey = versionKey;
Version = version;
}
public VersionUpdate(string incrementName, VersionData version)
{
VersionKey = incrementName;
Version = string.Format("{0}.{1}.{2}.{3}", version.Major, version.Minor, version.Build, version.Revision);
}
}
internal static class Editor
{
public static int Edit(IEnumerable<VersionUpdate> newVersions, string path, TextWriter output, bool dryRun)
{
var replacementVersions = newVersions.ToList();
var fileContents = File.ReadAllLines(path);
using (var writer = new StringWriter())
{
var file = Scanner.Scan(path, fileContents);
var index = 0;
foreach (var versionData in file.OrderBy(f => f.LineNumber))
{
while (index < versionData.LineNumber)
writer.WriteLine(fileContents[index++]);
var newVersionString = MakeNewVersion(replacementVersions, versionData);
var replacementLine = string.Format(@"[assembly: {0}(""{1}"")]", versionData.Description, newVersionString);
writer.WriteLine(replacementLine);
ReportUpdate(path, fileContents, index, replacementLine, output);
++index;
}
while (index < fileContents.Length)
writer.WriteLine(fileContents[index++]);
//Add missing values to the end.
foreach (var replacementVersion in replacementVersions)
{
var replacementLine = string.Format(@"[assembly: {0}(""{1}"")]",replacementVersion.VersionKey, replacementVersion.Version);
writer.WriteLine(replacementLine);
ReportAddition(path, replacementLine, output);
}
if (!dryRun)
File.WriteAllText(path, writer.ToString());
}
return 0;
}
private static void ReportAddition(string path, string replacementLine, TextWriter output)
{
if (output == null) return;
output.WriteLine(path);
output.WriteLine("Adding:");
output.WriteLine(" {0}", replacementLine);
}
private static void ReportUpdate(string path, string[] fileContents, int index, string replacementLine, TextWriter output)
{
if (output == null || replacementLine == fileContents[index]) return;
output.WriteLine(path);
output.WriteLine("Replacing:");
output.WriteLine(" {0}", fileContents[index]);
output.WriteLine("with");
output.WriteLine(" {0}", replacementLine);
}
private static string MakeNewVersion(List<VersionUpdate> newVersionRequests, VersionData currentVersion)
{
var match = newVersionRequests.FirstOrDefault(v => v.VersionKey == currentVersion.Description);
if (match == null)
return currentVersion.Original;
newVersionRequests.Remove(match);
return match.Version;
}
}
}