1 namespace Castle
.NewGenerator
.Core
4 using System
.Collections
.Generic
;
6 using System
.Reflection
;
9 public enum ProjectType
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
>();
35 private VSProject(Guid id
, string name
, string targetFileName
, XmlDocument document
)
39 this.targetFileName
= targetFileName
;
40 this.document
= document
;
48 public string FileName
50 get { return targetFileName; }
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
));
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
);
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" +
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);
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
)
193 XmlElement targetNode
;
194 string relativeFilePath
= entry
.Substring(folder
.Length
+ 1);
195 FileType fileType
= ResolveFileType(entry
);
199 case FileType
.StaticContent
:
200 nodeName
= "Content";
201 targetNode
= project
.contentGroup
;
203 case FileType
.SourceCode
:
204 nodeName
= "Compile";
205 targetNode
= project
.compileGroup
;
207 case FileType
.Folder
:
209 targetNode
= project
.foldersGroup
;
213 targetNode
= project
.contentGroup
;
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
);
231 if (new DirectoryInfo(entry
).Exists
)
233 return FileType
.Folder
;
247 return FileType
.StaticContent
;
249 return FileType
.SourceCode
;
253 return FileType
.None
;
255 return FileType
.Undefined
;