-
Notifications
You must be signed in to change notification settings - Fork 0
/
Munger.cs
100 lines (86 loc) · 3.82 KB
/
Munger.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
namespace ProjectMunger
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
public static class Munger
{
public static void RenameProject(string slnPath, string projectPath, string newName)
{
var oldName = Path.GetFileName(projectPath);
var slnData = File.ReadAllText(slnPath);
var newData = slnData.Replace(oldName, newName);
File.WriteAllText(slnPath, newData);
var dest = Path.Combine(Path.GetDirectoryName(projectPath), newName);
Git.Mv(projectPath, dest);
foreach (var fi in Directory.GetFiles(dest, "*", SearchOption.AllDirectories))
{
var actualFile = Path.Combine(Path.GetDirectoryName(fi), fi.Replace(Path.GetFileName(projectPath), newName));
if (!string.Equals(actualFile, fi, StringComparison.OrdinalIgnoreCase))
{
Git.Mv(fi, actualFile);
}
var data = File.ReadAllText(actualFile);
newData = data.Replace(Path.GetFileName(projectPath), newName);
if (!string.Equals(data, newData))
{
File.WriteAllText(actualFile, newData);
}
}
}
public static void FixupDocumentationPath(string path)
{
foreach (var fi in Directory.GetFiles(path, "*.csproj", SearchOption.AllDirectories))
{
var data = File.ReadAllText(fi);
var changed = Regex.Replace(data, @"\<DocumentationFile\>.+\<\/DocumentationFile\>", @"<DocumentationFile>$(MSBuildProjectDirectory)\$(OutputPath)$(MSBuildProjectName).xml</DocumentationFile>");
if (!string.Equals(data, changed, System.StringComparison.OrdinalIgnoreCase))
{
File.WriteAllText(fi, changed);
}
}
}
public static void CloneProject(string source, IList<string> destinations)
{
var projectName = Path.GetFileName(source);
foreach (var dir in Directory.GetDirectories(source, "*", SearchOption.AllDirectories))
{
foreach (var newProjectName in destinations)
{
var newDest = dir.Replace(projectName, newProjectName);
Directory.CreateDirectory(newDest);
}
}
foreach (var file in Directory.GetFiles(source, "*", SearchOption.AllDirectories))
{
var ext = Path.GetExtension(file);
if (ext != ".cs" && ext != ".csproj" && ext != ".config" && ext != ".settings")
{
continue;
}
var data = File.ReadAllText(file);
foreach (var newProjectName in destinations)
{
var newDest = file.Replace(projectName, newProjectName);
var newData = data.Replace(projectName, newProjectName);
if (file.EndsWith(".csproj", StringComparison.OrdinalIgnoreCase))
{
newData = Regex.Replace(newData, @"\<ProjectGuid\>.+\<\/ProjectGuid\>", $@"<ProjectGuid>{Guid.NewGuid().ToString("B").ToUpper()}</ProjectGuid>");
}
File.WriteAllText(newDest, newData);
}
}
}
public static void CloneFile(string source, string destination, List<KeyValuePair<string, string>> change)
{
var data = File.ReadAllText(source);
foreach (var item in change)
{
data = data.Replace(item.Key, item.Value);
}
File.WriteAllText(destination, data);
Git.Add(destination);
}
}
}