public void HostReturnsWarningsFromLogCall() { Engine engine = new Engine(); Store store = new Store(typeof(MockDomainModel)); MockDomainModel domainModel = GetModel(store); const string LogMessage = "Message1"; const string LogTitle = "Title1"; using (Transaction t = store.TransactionManager.BeginTransaction()) { ExtensibleMockModelElement serviceModel = new ExtensibleMockModelElement(store.DefaultPartition, string.Empty); TextTemplateHost host = new TextTemplateHost(domainModel, serviceModel, null); string transformResult = engine.ProcessTemplate( GetStandardTemplateHeader() + @" <# LogWarning(""" + LogMessage + @""",""" + LogTitle + @"""); #>", host); Assert.AreEqual <int>(1, host.CompilerErrors.Count); Assert.IsTrue(host.CompilerErrors[0].IsWarning); Assert.IsTrue(host.CompilerErrors[0].ErrorNumber.Contains(LogMessage), "Could not find expected error in compiler errors."); Assert.IsTrue(host.CompilerErrors[0].ErrorText.Contains(LogTitle), "Could not find expected error in compiler errors."); t.Rollback(); } }
private TemplateResult RunTemplateReturningErrors(ModelElement rootElement, ICodeGenerationService codeGenerationService) { TextTemplateHost host = new TextTemplateHost(DomainModel, rootElement, rootElement, codeGenerationService, ContractType); host.ResourceResolver = new AssemblyResourceResolver(); Engine engine = new Engine(); string result = engine.ProcessTemplate(Template, host); string[] errors = GetErrors(host); return(new TemplateResult(host.GenerateOutput ? result : string.Empty, errors)); }
public void NewInstanceWithDefaultValues() { TextTemplateHost host = new TextTemplateHost(null, null, null); Assert.AreEqual(0, host.ProjectReferences.Count); Assert.IsNull(host.CurrentElement); Assert.IsNull(host.CurrentExtender); Assert.IsNull(host.Model); Assert.IsNull(host.RootElement); Assert.AreEqual(14, host.StandardAssemblyReferences.Count); Assert.AreEqual(3, host.StandardImports.Count); }
public void NewInstanceWithDefaultValues() { TextTemplateHost host = new TextTemplateHost(null, null, null); Assert.AreEqual(0, host.ProjectReferences.Count); Assert.IsNull(host.CurrentElement); Assert.IsNull(host.CurrentExtender); Assert.IsNull(host.Model); Assert.IsNull(host.RootElement); Assert.AreEqual(14, host.StandardAssemblyReferences.Count); Assert.AreEqual(3, host.StandardImports.Count); }
private static string[] GetErrors(TextTemplateHost host) { string[] errors = new string[0]; if (host.CompilerErrors.Count > 0) { errors = new string[host.CompilerErrors.Count]; for (int i = 0; i < host.CompilerErrors.Count; i++) { errors[i] = host.CompilerErrors[i].ErrorText; } } return(errors); }
private string IsValid(bool expectedValue) { Engine engine = new Engine(); Store store = new Store(typeof(MockDomainModel)); MockDomainModel domainModel = GetModel(store); using (Transaction t = store.TransactionManager.BeginTransaction()) { ExtensibleMockModelElement serviceModel = new ExtensibleMockModelElement(store.DefaultPartition, string.Empty); MockCodeGenerationService cgs = new MockCodeGenerationService(expectedValue); TextTemplateHost host = new TextTemplateHost(domainModel, serviceModel, serviceModel, cgs); string transformResult = engine.ProcessTemplate( GetStandardTemplateHeader().Replace("/n", "") + @"<#= this.IsValid(CurrentElement.InvalidArtifactLink).ToString()#>", host); t.Rollback(); return(transformResult.Trim()); } }
public void CanGetCSharpTypeOutput() { Engine engine = new Engine(); Store store = new Store(typeof(MockDomainModel)); MockDomainModel domainModel = GetModel(store); using (Transaction t = store.TransactionManager.BeginTransaction()) { ExtensibleMockModelElement serviceModel = new ExtensibleMockModelElement(store.DefaultPartition, string.Empty); TextTemplateHost host = new TextTemplateHost(domainModel, serviceModel, null); string transformResult = engine.ProcessTemplate( GetStandardTemplateHeader() + @" <#= Utility.GetCSharpTypeOutput(""System.String"") #>" , host); Assert.IsTrue(transformResult.Contains("string")); t.Rollback(); } }
public void InvokeHostWithModel() { Engine engine = new Engine(); Store store = new Store(typeof(MockDomainModel)); MockDomainModel domainModel = GetModel(store); using (Transaction t = store.TransactionManager.BeginTransaction()) { ExtensibleMockModelElement serviceModel = new ExtensibleMockModelElement(store.DefaultPartition,string.Empty); TextTemplateHost host = new TextTemplateHost(domainModel, serviceModel,null); string templateContent = GetStandardTemplateHeader() + @" <#= this.Model.DomainModelInfo.Id #>"; string transformResult = engine.ProcessTemplate( templateContent, host); Assert.AreEqual(domainModel.DomainModelInfo.Id, new Guid(transformResult)); t.Rollback(); } }
public void InvokeHostWithModel() { Engine engine = new Engine(); Store store = new Store(typeof(MockDomainModel)); MockDomainModel domainModel = GetModel(store); using (Transaction t = store.TransactionManager.BeginTransaction()) { ExtensibleMockModelElement serviceModel = new ExtensibleMockModelElement(store.DefaultPartition, string.Empty); TextTemplateHost host = new TextTemplateHost(domainModel, serviceModel, null); string templateContent = GetStandardTemplateHeader() + @" <#= this.Model.DomainModelInfo.Id #>" ; string transformResult = engine.ProcessTemplate( templateContent, host); Assert.AreEqual(domainModel.DomainModelInfo.Id, new Guid(transformResult)); t.Rollback(); } }
public void HostReturnsEmptyContentOnCancelOutput() { Engine engine = new Engine(); Store store = new Store(typeof(MockDomainModel)); MockDomainModel domainModel = GetModel(store); using (Transaction t = store.TransactionManager.BeginTransaction()) { ExtensibleMockModelElement serviceModel = new ExtensibleMockModelElement(store.DefaultPartition, string.Empty); TextTemplateHost host = new TextTemplateHost(domainModel, serviceModel, null); string templateContent = GetStandardTemplateHeader() + @"<# CancelOutput(); #>"; string transformResult = engine.ProcessTemplate(templateContent, host); Assert.AreEqual <int>(0, host.CompilerErrors.Count); Assert.IsFalse(host.GenerateOutput); t.Rollback(); } }
public CodeGenerationResults Generate(IArtifactLink link) { CodeGenerationResults result = new CodeGenerationResults(); if (link is IModelReference) { this.serviceProvider = Utility.GetData <IServiceProvider>(link); ProjectNode project = Utility.GetData <ProjectNode>(link); ModelElement modelElement = ((IModelReference)link).ModelElement; TextTemplateArtifactLinkWrapper textTemplateArtifactLink = new TextTemplateArtifactLinkWrapper(link); textTemplateArtifactLink.ResourceResolver = ResourceResolver; string template = GetTemplateBasedOnProject(textTemplateArtifactLink, project); if (modelElement != null && !string.IsNullOrEmpty(template)) { Engine engine = new Engine(); DomainModel model = (DomainModel)modelElement.Store.GetDomainModel(modelElement.GetDomainClass().DomainModel.Id); TextTemplateHost host = new TextTemplateHost(model, modelElement, modelElement, GetService <ICodeGenerationService>()); host.ResourceResolver = textTemplateArtifactLink.ResourceResolver; string content = engine.ProcessTemplate(template, host); if (host.GenerateOutput) { this.projectReferences = new List <Guid>(host.ProjectReferences); this.assemblyReferences = GetAssemblyReferences(link); if (host.CompilerErrors.Count > 0) { foreach (CompilerError error in host.CompilerErrors) { LogError(error); } } // Will create a file with the 'ErrorGeneratingOutput' text in the generated file. result.Add(link.ItemPath, content); } } } return(result); }
public void HostReturnsErrorsInCollection() { Engine engine = new Engine(); Store store = new Store(typeof(MockDomainModel)); MockDomainModel domainModel = GetModel(store); using (Transaction t = store.TransactionManager.BeginTransaction()) { ExtensibleMockModelElement serviceModel = new ExtensibleMockModelElement(store.DefaultPartition, string.Empty); TextTemplateHost host = new TextTemplateHost(domainModel, serviceModel, null); string transformResult = engine.ProcessTemplate( GetStandardTemplateHeader() + @" <# throw new global::System.Exception(""TestException""); #>" , host); Assert.AreEqual <int>(2, host.CompilerErrors.Count); Assert.IsTrue(host.CompilerErrors[1].ErrorText.Contains("TestException"), "Could not find expected exception in compiler errors."); t.Rollback(); } }
public void CanAddProjectReference() { Engine engine = new Engine(); Store store = new Store(typeof(MockDomainModel)); MockDomainModel domainModel = GetModel(store); using (Transaction t = store.TransactionManager.BeginTransaction()) { ExtensibleMockModelElement serviceModel = new ExtensibleMockModelElement(store.DefaultPartition, "test"); serviceModel.ObjectExtender = new MockObjectExtender(); TextTemplateHost host = new TextTemplateHost(domainModel, serviceModel, serviceModel); host.StandardAssemblyReferences.Add(typeof(Microsoft.Practices.Modeling.ExtensionProvider.Extension.ObjectExtender <ExtensibleMockModelElement>).Assembly.FullName); string transformResult = engine.ProcessTemplate( GetStandardTemplateHeader() + @" <# AddProjectReference(CurrentExtender.ArtifactLink); #>" , host); Assert.AreEqual(1, host.ProjectReferences.Count); t.Rollback(); } }
public void CanAddProjectReference() { Engine engine = new Engine(); Store store = new Store(typeof(MockDomainModel)); MockDomainModel domainModel = GetModel(store); using (Transaction t = store.TransactionManager.BeginTransaction()) { ExtensibleMockModelElement serviceModel = new ExtensibleMockModelElement(store.DefaultPartition, "test"); serviceModel.ObjectExtender = new MockObjectExtender(); TextTemplateHost host = new TextTemplateHost(domainModel, serviceModel, serviceModel); host.StandardAssemblyReferences.Add(typeof(Microsoft.Practices.Modeling.ExtensionProvider.Extension.ObjectExtender<ExtensibleMockModelElement>).Assembly.FullName); string transformResult = engine.ProcessTemplate( GetStandardTemplateHeader() + @" <# AddProjectReference(CurrentExtender.ArtifactLink); #>", host); Assert.AreEqual(1, host.ProjectReferences.Count); t.Rollback(); } }
private string IsValid(bool expectedValue) { Engine engine = new Engine(); Store store = new Store(typeof(MockDomainModel)); MockDomainModel domainModel = GetModel(store); using (Transaction t = store.TransactionManager.BeginTransaction()) { ExtensibleMockModelElement serviceModel = new ExtensibleMockModelElement(store.DefaultPartition, string.Empty); MockCodeGenerationService cgs = new MockCodeGenerationService(expectedValue); TextTemplateHost host = new TextTemplateHost(domainModel, serviceModel, serviceModel, cgs); string transformResult = engine.ProcessTemplate( GetStandardTemplateHeader().Replace("/n","") + @"<#= this.IsValid(CurrentElement.InvalidArtifactLink).ToString()#>", host); t.Rollback(); return transformResult.Trim(); } }
public void HostReturnsEmptyContentOnCancelOutput() { Engine engine = new Engine(); Store store = new Store(typeof(MockDomainModel)); MockDomainModel domainModel = GetModel(store); using (Transaction t = store.TransactionManager.BeginTransaction()) { ExtensibleMockModelElement serviceModel = new ExtensibleMockModelElement(store.DefaultPartition, string.Empty); TextTemplateHost host = new TextTemplateHost(domainModel, serviceModel, null); string templateContent = GetStandardTemplateHeader() + @"<# CancelOutput(); #>"; string transformResult = engine.ProcessTemplate(templateContent, host); Assert.AreEqual<int>(0, host.CompilerErrors.Count); Assert.IsFalse(host.GenerateOutput); t.Rollback(); } }
public void HostReturnsWarningsFromLogCall() { Engine engine = new Engine(); Store store = new Store(typeof(MockDomainModel)); MockDomainModel domainModel = GetModel(store); const string LogMessage = "Message1"; const string LogTitle = "Title1"; using (Transaction t = store.TransactionManager.BeginTransaction()) { ExtensibleMockModelElement serviceModel = new ExtensibleMockModelElement(store.DefaultPartition, string.Empty); TextTemplateHost host = new TextTemplateHost(domainModel, serviceModel, null); string transformResult = engine.ProcessTemplate( GetStandardTemplateHeader() + @" <# LogWarning(""" + LogMessage + @""",""" + LogTitle + @"""); #>", host); Assert.AreEqual<int>(1, host.CompilerErrors.Count); Assert.IsTrue(host.CompilerErrors[0].IsWarning); Assert.IsTrue(host.CompilerErrors[0].ErrorNumber.Contains(LogMessage), "Could not find expected error in compiler errors."); Assert.IsTrue(host.CompilerErrors[0].ErrorText.Contains(LogTitle), "Could not find expected error in compiler errors."); t.Rollback(); } }
private TemplateResult RunTemplateReturningErrors(ModelElement rootElement, ICodeGenerationService codeGenerationService) { TextTemplateHost host = new TextTemplateHost(DomainModel, rootElement, rootElement, codeGenerationService, ContractType); host.ResourceResolver = new AssemblyResourceResolver(); Engine engine = new Engine(); string result = engine.ProcessTemplate(Template, host); string[] errors = GetErrors(host); return new TemplateResult(host.GenerateOutput ? result : string.Empty, errors); }
public void CanGetCSharpTypeOutput() { Engine engine = new Engine(); Store store = new Store(typeof(MockDomainModel)); MockDomainModel domainModel = GetModel(store); using (Transaction t = store.TransactionManager.BeginTransaction()) { ExtensibleMockModelElement serviceModel = new ExtensibleMockModelElement(store.DefaultPartition, string.Empty); TextTemplateHost host = new TextTemplateHost(domainModel, serviceModel, null); string transformResult = engine.ProcessTemplate( GetStandardTemplateHeader() + @" <#= Utility.GetCSharpTypeOutput(""System.String"") #>", host); Assert.IsTrue(transformResult.Contains("string")); t.Rollback(); } }
public MainWindow() { InitializeComponent(); ViewModel viewModel = null; if ((App.Current as App).ViewModel == null) { viewModel = new ViewModel(); viewModel.ControllerDefinition = new ControllerDefinition(viewModel); viewModel.RepositoryDefinition = new RepositoryDefinition(viewModel); viewModel.ServiceDefinition = new ServiceDefinition(viewModel); viewModel.ViewDefinition = new ViewDefinition(viewModel); viewModel.ViewModelDefinition = new ViewModelDefinition(viewModel); } else viewModel = (App.Current as App).ViewModel; Engine engine = new Engine(); TextTemplateHost host; viewModel.Ler = new ActionCommand( a => { if(Directory.Exists(a as string)) Directory.GetFiles(a as string, "*.hbm.xml").ToList().ForEach(c => { XDocument doc = XDocument.Load(c); viewModel.Entidades.Add(new EntityViewModel { FilePath = c, Nome = doc.Descendants().Where(it=>it.Name.LocalName == "class").FirstOrDefault().Attribute("name").Value }); }); }, a => true ); viewModel.Gerar = new ActionCommand( c => { var entities = new List<EntityModel>(); string filecontent = ""; string template = ""; viewModel.Entidades.Where(it => it.Ativo).ToList().ForEach(it => { XDocument doc = XDocument.Load(it.FilePath); var entity = new EntityModel() { ClassName = it.Nome.Contains(",") ? it.Nome.Split(',')[0].Contains(".") ? it.Nome.Split(',')[0].Substring(it.Nome.Split(',')[0].LastIndexOf(".")+1) : it.Nome.Split(',')[0] : it.Nome, Properties = doc.Descendants() .Where(d => d.Name.LocalName == "property" || d.Name.LocalName == "many-to-one" || d.Name.LocalName =="set") .Select(d => new PropertyModel() { Nome = d.Attribute("name").Value, Required = d.Attribute("not-null") == null ? false : d.Attribute("not-null").Value == "true", DataType = d.Attribute("type") ==null ? d.Attribute("name").Value : d.Attribute("type").Value, ColumnName = d.Attribute("column") == null ? null : d.Attribute("column").Value.Replace("`", ""), Length = d.Attribute("length") == null ? null : (int?)Int32.Parse(d.Attribute("length").Value.Replace("`", "")), DisplayName = d.Attribute("name").Value }).ToList() }; entities.Add(entity); }); foreach (LayerDefinition item in viewModel.Layers.Where(it=>it.Ativo )) { foreach (var typedef in item.TypesDefinitions) { host = new TextTemplateHost() { TemplateFile = typedef.TemplatePath }; template = File.ReadAllText(typedef.TemplatePath); foreach (EntityModel entity in entities) { host.Model = entity; host.TypeDefinition = typedef; filecontent = engine.ProcessTemplate(template, host); if (!Directory.Exists(typedef.OutputDir + "\\" + (typedef.DirectoryPerEntity ? "\\" + entity.ClassName + "\\" : ""))) Directory.CreateDirectory(typedef.OutputDir + "\\" + (typedef.DirectoryPerEntity ? "\\" + entity.ClassName + "\\" : "")); if(host.Errors.HasErrors) File.WriteAllText(typedef.OutputDir + "\\" + (typedef.DirectoryPerEntity ? "\\" + entity.ClassName + "\\" : "") + typedef.Prefix + entity.ClassName + typedef.Sufix + ".cs", host.Errors.ToString()); else File.WriteAllText(typedef.OutputDir + "\\" + (typedef.DirectoryPerEntity ? "\\" + entity.ClassName + "\\" : "") + typedef.Prefix + entity.ClassName + typedef.Sufix + ".cs", filecontent); } } } }, c => true); DataContext = viewModel; }
private static string[] GetErrors(TextTemplateHost host) { string[] errors = new string[0] ; if (host.CompilerErrors.Count > 0) { errors = new string[host.CompilerErrors.Count]; for (int i = 0; i < host.CompilerErrors.Count; i++) { errors[i] = host.CompilerErrors[i].ErrorText; } } return errors; }
public MainWindow() { InitializeComponent(); ViewModel viewModel = null; if ((App.Current as App).ViewModel == null) { viewModel = new ViewModel(); viewModel.ControllerDefinition = new ControllerDefinition(viewModel); viewModel.RepositoryDefinition = new RepositoryDefinition(viewModel); viewModel.ServiceDefinition = new ServiceDefinition(viewModel); viewModel.ViewDefinition = new ViewDefinition(viewModel); viewModel.ViewModelDefinition = new ViewModelDefinition(viewModel); } else { viewModel = (App.Current as App).ViewModel; } Engine engine = new Engine(); TextTemplateHost host; viewModel.Ler = new ActionCommand( a => { if (Directory.Exists(a as string)) { Directory.GetFiles(a as string, "*.hbm.xml").ToList().ForEach(c => { XDocument doc = XDocument.Load(c); viewModel.Entidades.Add(new EntityViewModel { FilePath = c, Nome = doc.Descendants().Where(it => it.Name.LocalName == "class").FirstOrDefault().Attribute("name").Value }); }); } }, a => true ); viewModel.Gerar = new ActionCommand( c => { var entities = new List <EntityModel>(); string filecontent = ""; string template = ""; viewModel.Entidades.Where(it => it.Ativo).ToList().ForEach(it => { XDocument doc = XDocument.Load(it.FilePath); var entity = new EntityModel() { ClassName = it.Nome.Contains(",") ? it.Nome.Split(',')[0].Contains(".") ? it.Nome.Split(',')[0].Substring(it.Nome.Split(',')[0].LastIndexOf(".") + 1) : it.Nome.Split(',')[0] : it.Nome, Properties = doc.Descendants() .Where(d => d.Name.LocalName == "property" || d.Name.LocalName == "many-to-one" || d.Name.LocalName == "set") .Select(d => new PropertyModel() { Nome = d.Attribute("name").Value, Required = d.Attribute("not-null") == null ? false : d.Attribute("not-null").Value == "true", DataType = d.Attribute("type") == null ? d.Attribute("name").Value : d.Attribute("type").Value, ColumnName = d.Attribute("column") == null ? null : d.Attribute("column").Value.Replace("`", ""), Length = d.Attribute("length") == null ? null : (int?)Int32.Parse(d.Attribute("length").Value.Replace("`", "")), DisplayName = d.Attribute("name").Value }).ToList() }; entities.Add(entity); }); foreach (LayerDefinition item in viewModel.Layers.Where(it => it.Ativo)) { foreach (var typedef in item.TypesDefinitions) { host = new TextTemplateHost() { TemplateFile = typedef.TemplatePath }; template = File.ReadAllText(typedef.TemplatePath); foreach (EntityModel entity in entities) { host.Model = entity; host.TypeDefinition = typedef; filecontent = engine.ProcessTemplate(template, host); if (!Directory.Exists(typedef.OutputDir + "\\" + (typedef.DirectoryPerEntity ? "\\" + entity.ClassName + "\\" : ""))) { Directory.CreateDirectory(typedef.OutputDir + "\\" + (typedef.DirectoryPerEntity ? "\\" + entity.ClassName + "\\" : "")); } if (host.Errors.HasErrors) { File.WriteAllText(typedef.OutputDir + "\\" + (typedef.DirectoryPerEntity ? "\\" + entity.ClassName + "\\" : "") + typedef.Prefix + entity.ClassName + typedef.Sufix + ".cs", host.Errors.ToString()); } else { File.WriteAllText(typedef.OutputDir + "\\" + (typedef.DirectoryPerEntity ? "\\" + entity.ClassName + "\\" : "") + typedef.Prefix + entity.ClassName + typedef.Sufix + ".cs", filecontent); } } } } }, c => true); DataContext = viewModel; }
public override void Initialize(ITextTemplatingEngineHost host) { templateEngineHost = host as TextTemplateHost; base.Initialize(host); }
public void HostReturnsErrorsInCollection() { Engine engine = new Engine(); Store store = new Store(typeof(MockDomainModel)); MockDomainModel domainModel = GetModel(store); using (Transaction t = store.TransactionManager.BeginTransaction()) { ExtensibleMockModelElement serviceModel = new ExtensibleMockModelElement(store.DefaultPartition, string.Empty); TextTemplateHost host = new TextTemplateHost(domainModel, serviceModel, null); string transformResult = engine.ProcessTemplate( GetStandardTemplateHeader() + @" <# throw new global::System.Exception(""TestException""); #>", host); Assert.AreEqual<int>(2, host.CompilerErrors.Count); Assert.IsTrue(host.CompilerErrors[1].ErrorText.Contains("TestException"),"Could not find expected exception in compiler errors."); t.Rollback(); } }