forked from JulianBirch/Solution-Transform
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolutionFile.cs
More file actions
122 lines (109 loc) · 3.79 KB
/
SolutionFile.cs
File metadata and controls
122 lines (109 loc) · 3.79 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
122
// Copyright 2004-2009 Castle Project - http://www.castleproject.org/
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using System.Text;
namespace SolutionTransform
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Xml;
using SolutionTransform.ProjectFile;
public class SolutionFile {
private readonly string path;
internal List<string> lines = new List<string>();
internal List<SolutionProject> projects;
public SolutionFile(string path, string content) {
this.path = path;
lines.AddRange(content.AsLines());
// .SkipWhile(l => l.TrimEnd().Length == 0)
// The whole skipwhile thing is to deal with the blank lines at the start of the Castle solution file
projects = GetProjects().ToList();
}
public string FullPath
{
get { return path; }
}
IEnumerable<SolutionProject> GetProjects() {
int index = 0;
foreach (var line in lines)
{
if (line.StartsWith("Project"))
{
yield return new SolutionProject(line, index, this);
}
index++;
}
}
public void Remove(SolutionProject solutionProject) {
lines.RemoveRange(solutionProject.lineIndex, 2); // Take out the end project as well
foreach (var project in projects.Where(p => p.lineIndex > solutionProject.lineIndex)) {
project.lineIndex -= 2;
}
var regex = new Regex(solutionProject.Id.ToString(), RegexOptions.IgnoreCase);
lines.RemoveAll(regex.IsMatch);
projects.Remove(solutionProject);
}
public void ProcessProjects(IRename rename, ITransform transform)
{
var nameTransform = new NameTransform(rename);
var fullTransform = new CompositeTransform(transform, nameTransform);
string basePath = Path.GetDirectoryName(path);
foreach (var project in projects.Where(p => !p.IsFolder))
{
var projectPath = Path.Combine(basePath, project.Path);
var document = new XmlDocument();
document.Load(projectPath);
fullTransform.ApplyTransform(projectPath, document);
project.Name = rename.RenameSolutionProjectName(project.Name);
project.Path = rename.RenameCsproj(project.Path);
document.Save(rename.RenameCsproj(projectPath));
// Note that projectPath and project.Path have different values....
}
Save(rename.RenameSln(path));
}
public void Transform(IRename rename, IProjectFilter filter, ITransform transform)
{
FilterProjects(filter);
ProcessProjects(rename, transform);
}
public void Transform(IRename rename, IProjectFilter filter, params ITransform[] transforms) {
Transform(rename, filter, new CompositeTransform(transforms));
}
private void Save(string destination)
{
// NB Solution files will not load unless you save them as Unicode. In particular, UTF8 doesn't work.
using (var writer = new StreamWriter(destination, false, Encoding.Unicode))
{
writer.WriteLine(); // Guarantee a blank line
foreach (var line in lines.SkipWhile(string.IsNullOrEmpty)) // Skip any others
{
writer.WriteLine(line);
}
writer.Flush();
}
}
public void FilterProjects(IProjectFilter filter)
{
foreach (var project in projects.ToList())
{
if (!filter.ShouldIncludeProject(project))
{
Remove(project);
}
}
}
}
}