Fixing an issue with output parameters that are of type IntPtr
[castle.git] / MonoRail / NewGenerator / Castle.NewGenerator.Core / VSProject.cs
blobfe3f51b5747767756cbb6d8b2006520124bdffbc
1 namespace Castle.NewGenerator.Core
3 using System;
4 using System.Collections.Generic;
5 using System.IO;
6 using System.Reflection;
7 using System.Xml;
9 public enum ProjectType
11 Web,
12 Test
15 public class VSProject
17 const string ns = "http://schemas.microsoft.com/developer/msbuild/2003";
19 private readonly Guid id;
20 private readonly string name;
21 private readonly string targetFileName;
22 private readonly XmlDocument document;
23 private XmlElement referencesGroup, contentGroup, compileGroup, projectReferencesGroup, foldersGroup;
24 private List<Assembly> assemblyList = new List<Assembly>();
26 public enum FileType
28 Undefined,
29 StaticContent,
30 SourceCode,
31 Folder,
32 None
35 private VSProject(Guid id, string name, string targetFileName, XmlDocument document)
37 this.id = id;
38 this.name = name;
39 this.targetFileName = targetFileName;
40 this.document = document;
43 public string Name
45 get { return name; }
48 public string FileName
50 get { return targetFileName; }
53 public Guid Id
55 get { return id; }
58 public IList<Assembly> AssemblyReferences
60 get { return assemblyList; }
63 public void AddAssemblyReference(string fileName, string hintPath)
65 Assembly assm = Assembly.Load(fileName);
67 assemblyList.Add(assm);
69 XmlElement refElem = (XmlElement) referencesGroup.AppendChild(document.CreateElement("Reference", ns));
70 refElem.SetAttribute("Include", assm.FullName);
72 refElem.AppendChild(document.CreateElement("SpecificVersion", ns)).AppendChild(document.CreateTextNode("False"));
73 refElem.AppendChild(document.CreateElement("HintPath", ns)).AppendChild(document.CreateTextNode(hintPath));
76 public void AddAssemblyReferenceShared(string assemblyName)
78 XmlElement refElem = (XmlElement)referencesGroup.AppendChild(document.CreateElement("Reference", ns));
79 refElem.SetAttribute("Include", assemblyName);
82 public void AddProjectReference(VSProject project)
84 XmlElement refElem = (XmlElement) projectReferencesGroup.AppendChild(document.CreateElement("ProjectReference", ns));
85 refElem.SetAttribute("Include", project.FileName);
87 refElem.AppendChild(document.CreateElement("Project", ns)).AppendChild(document.CreateTextNode(project.Id.ToString("P")));
88 refElem.AppendChild(document.CreateElement("Name", ns)).AppendChild(document.CreateTextNode(project.Name));
91 public void Save()
93 File.Delete(targetFileName);
95 document.Save(targetFileName);
98 public static VSProject Create(string name, string folder, ProjectType type, XmlDocument templateFile)
100 Guid id = Guid.NewGuid();
102 VSProject project = new VSProject(id, name, Path.Combine(folder, name + ".csproj"), templateFile);
104 ImportExistingFilesFromFolder(project, folder, templateFile);
106 SetUpProject(id, name, type, templateFile);
108 return project;
111 private static void SetUpProject(Guid id, string name, ProjectType type, XmlDocument doc)
113 XmlElement projectNode = doc.DocumentElement;
114 XmlElement pGroup = projectNode["PropertyGroup"];
115 XmlElement projectGuid = pGroup["ProjectGuid"];
116 XmlElement rootNamespace = pGroup["RootNamespace"];
117 XmlElement assemblyName = pGroup["AssemblyName"];
118 XmlElement typeGuids = pGroup["ProjectTypeGuids"];
120 projectGuid.FirstChild.Value = id.ToString("P");
121 rootNamespace.FirstChild.Value = name;
122 assemblyName.FirstChild.Value = name;
124 if (type == ProjectType.Web)
126 typeGuids.FirstChild.Value = "{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}";
128 XmlElement importElement = doc.CreateElement("Import", ns);
129 importElement.SetAttribute("Project", @"$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v8.0\WebApplications\Microsoft.WebApplication.targets");
130 doc.DocumentElement.AppendChild(importElement);
133 XmlElement propElement = (XmlElement) doc.DocumentElement.AppendChild(doc.CreateElement("ProjectExtensions", ns)).
134 AppendChild(doc.CreateElement("VisualStudio", ns)).
135 AppendChild(doc.CreateElement("FlavorProperties", ns));
137 propElement.SetAttribute("GUID", "{349c5851-65df-11da-9384-00065b846f21}");
139 XmlElement webProjectPropElem = (XmlElement) propElement.AppendChild(doc.CreateElement("WebProjectProperties", ns));
141 webProjectPropElem.AppendChild(doc.CreateElement("UseIIS", ns)).AppendChild(doc.CreateTextNode("False"));
142 webProjectPropElem.AppendChild(doc.CreateElement("AutoAssignPort", ns)).AppendChild(doc.CreateTextNode("True"));
143 webProjectPropElem.AppendChild(doc.CreateElement("DevelopmentServerVPath", ns)).AppendChild(doc.CreateTextNode("/"));
144 webProjectPropElem.AppendChild(doc.CreateElement("IISUrl", ns));
145 webProjectPropElem.AppendChild(doc.CreateElement("NTLMAuthentication", ns)).AppendChild(doc.CreateTextNode("False"));
147 // XmlDocumentFragment fragment = doc.CreateDocumentFragment();
148 // fragment.InnerXml = "\r\n" +
149 // " <ProjectExtensions>\r\n" +
150 // " <VisualStudio>\r\n" +
151 // " <FlavorProperties GUID=\"{349c5851-65df-11da-9384-00065b846f21}\">\r\n" +
152 // " <WebProjectProperties>\r\n" +
153 // " <UseIIS>False</UseIIS>\r\n" +
154 // " <AutoAssignPort>True</AutoAssignPort>\r\n" +
155 // " <DevelopmentServerVPath>/</DevelopmentServerVPath>\r\n" +
156 // " <IISUrl>\r\n" +
157 // " </IISUrl>\r\n" +
158 // " <NTLMAuthentication>False</NTLMAuthentication>\r\n" +
159 // " </WebProjectProperties>\r\n" +
160 // " </FlavorProperties>\r\n" +
161 // " </VisualStudio>\r\n" +
162 // " </ProjectExtensions>\r\n";
164 // doc.DocumentElement.AppendChild(fragment);
166 else
168 typeGuids.ParentNode.RemoveChild(typeGuids);
172 private static void ImportExistingFilesFromFolder(VSProject project, string folder, XmlDocument templateFile)
174 project.referencesGroup = templateFile.CreateElement("ItemGroup", ns);
175 project.contentGroup = templateFile.CreateElement("ItemGroup", ns);
176 project.compileGroup = templateFile.CreateElement("ItemGroup", ns);
177 project.projectReferencesGroup = templateFile.CreateElement("ItemGroup", ns);
178 project.foldersGroup = templateFile.CreateElement("ItemGroup", ns);
180 templateFile.DocumentElement.AppendChild(project.referencesGroup);
181 templateFile.DocumentElement.AppendChild(project.contentGroup);
182 templateFile.DocumentElement.AppendChild(project.compileGroup);
183 templateFile.DocumentElement.AppendChild(project.projectReferencesGroup);
184 templateFile.DocumentElement.AppendChild(project.foldersGroup);
186 List<string> entries = new List<string>();
187 entries.AddRange(Directory.GetDirectories(folder, "*", SearchOption.AllDirectories));
188 entries.AddRange(Directory.GetFiles(folder, "*", SearchOption.AllDirectories));
190 foreach(string entry in entries)
192 string nodeName;
193 XmlElement targetNode;
194 string relativeFilePath = entry.Substring(folder.Length + 1);
195 FileType fileType = ResolveFileType(entry);
197 switch(fileType)
199 case FileType.StaticContent:
200 nodeName = "Content";
201 targetNode = project.contentGroup;
202 break;
203 case FileType.SourceCode:
204 nodeName = "Compile";
205 targetNode = project.compileGroup;
206 break;
207 case FileType.Folder:
208 nodeName = "Folder";
209 targetNode = project.foldersGroup;
210 break;
211 case FileType.None:
212 nodeName = "None";
213 targetNode = project.contentGroup;
214 break;
215 default:
216 continue;
219 XmlElement item = templateFile.CreateElement(nodeName, ns);
220 item.SetAttribute("Include", relativeFilePath);
221 targetNode.AppendChild(item);
225 private static FileType ResolveFileType(string entry)
227 string ext = Path.GetExtension(entry);
229 if (ext == "")
231 if (new DirectoryInfo(entry).Exists)
233 return FileType.Folder;
237 switch(ext)
239 case ".aspx":
240 case ".config":
241 case ".asax":
242 case ".css":
243 case ".htm":
244 case ".html":
245 case ".js":
246 case ".txt":
247 return FileType.StaticContent;
248 case ".cs":
249 return FileType.SourceCode;
250 case ".vm":
251 case ".njs":
252 case ".brail":
253 return FileType.None;
254 default:
255 return FileType.Undefined;