private static void TestBuildCode(string fileName, string contentTypeName) { var xml = ""; var expectedOutput = ""; using (var inputReader = File.OpenText(@"..\..\TestFiles\" + fileName + ".xml")) { xml = inputReader.ReadToEnd(); } using (var goldReader = File.OpenText(@"..\..\TestFiles\" + fileName + ".cs")) { expectedOutput = goldReader.ReadToEnd(); } var configuration = new CodeGeneratorConfiguration { TypeMappings = new Dictionary<string, string> { {"1413afcb-d19a-4173-8e9a-68288d2a73b8", "Int32"} }, DefaultTypeMapping = "String", }; var typeConfig = new ContentTypeConfiguration(configuration) { ContentTypeName = contentTypeName, BaseClass = "DocumentTypeBase", Namespace = "Umbraco.CodeGen.Models" }; var sb = new StringBuilder(); var writer = new StringWriter(sb); var generator = new ContentTypeCodeGenerator(typeConfig, XDocument.Parse(xml), new CSharpCodeProvider()); generator.BuildCode(writer); writer.Flush(); Console.WriteLine(sb.ToString()); Assert.AreEqual(expectedOutput, sb.ToString()); }
private void OnDocumentTypeSaved(XmlDocFileEventArgs e) { if (!e.Path.Contains("DocumentType") && !e.Path.Contains("MediaType")) return; var typeConfig = e.Path.Contains("DocumentType") ? configuration.DocumentTypes : configuration.MediaTypes; if (!typeConfig.GenerateClasses) return; var codeDomProvider = new CSharpCodeProvider(); var doc = XDocument.Load(e.Path); var classGenerator = new ContentTypeCodeGenerator(typeConfig, doc, codeDomProvider); var modelPath = paths[typeConfig.ContentTypeName]; if (!Directory.Exists(modelPath)) Directory.CreateDirectory(modelPath); var path = Path.Combine(modelPath, doc.XPathSelectElement("//Info/Alias").Value.PascalCase() + ".cs"); if (configuration.OverwriteReadOnly && File.Exists(path)) File.SetAttributes(path, File.GetAttributes(path) & ~FileAttributes.ReadOnly); using (var stream = File.CreateText(path)) { classGenerator.BuildCode(stream); } }