-
Notifications
You must be signed in to change notification settings - Fork 0
/
GenerateViews.cs
71 lines (65 loc) · 2.57 KB
/
GenerateViews.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
using System.IO;
using System.Xml;
namespace Maussoft.Mvc.ViewGen
{
public class GenerateViews : Microsoft.Build.Utilities.Task
{
public override bool Execute()
{
string directory = Directory.GetCurrentDirectory();
string viewDirectory = Path.Combine(directory, "Views");
string rootNamespace = FindRootNamespaceInProjectFile(directory, "*.csproj");
string viewNamespace = null;
string sessionClass = null;
string language = "C#";
if (rootNamespace == null)
{
rootNamespace = FindRootNamespaceInProjectFile(directory, "*.vbproj");
language = "VB";
}
if (rootNamespace == null)
{
Log.LogMessage("Could not find project file (*.csproj or *.vbproj)");
return false;
}
viewNamespace = rootNamespace + '.' + "Views";
sessionClass = rootNamespace + '.' + "Session";
string[] files = Directory.GetFiles(viewDirectory, "*.aspx", SearchOption.AllDirectories);
Generator generator;
if (language == "C#")
{
generator = new CSharpGenerator(viewDirectory, viewNamespace, sessionClass);
}
else
{
generator = new VisualBasicGenerator(viewDirectory, viewNamespace, sessionClass, rootNamespace);
}
foreach (string filename in files)
{
Log.LogMessage('.' + filename.Substring(directory.Length));
generator.ConvertFile(filename);
}
return true;
}
private static string FindRootNamespaceInProjectFile(string directory, string glob)
{
string[] files = Directory.GetFiles(directory, glob, SearchOption.TopDirectoryOnly);
foreach (string fullPath in files)
{
if (File.Exists(fullPath))
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(fullPath);
XmlNamespaceManager nsManager = new XmlNamespaceManager(xmlDoc.NameTable);
nsManager.AddNamespace("p", xmlDoc.DocumentElement.NamespaceURI);
XmlNode node = xmlDoc.SelectSingleNode("/p:Project/p:PropertyGroup/p:RootNamespace", nsManager);
if (node != null)
{
return node.InnerText;
}
}
}
return null;
}
}
}