private string GetTemplateBasedOnProject(TextTemplateArtifactLinkWrapper link, ProjectNode project)
        {
            TextTemplateTargetLanguage targetLanguage = ResolveTargetProjectLanguage(project);
            string template = link.GetTemplate(targetLanguage);

            if (string.IsNullOrEmpty(template))
            {
                template = link.GetTemplate(TextTemplateTargetLanguage.Any);
            }

            if (string.IsNullOrEmpty(template))
            {
                throw new InvalidOperationException(Properties.Resources.EmptyOrNonExistentTemplate);
            }

            return(template);
        }
        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);
        }
Example #3
0
		/// <summary>
		/// Returns a TextTemplateArtifactLinkWrapper.
		/// </summary>
		/// <param name="artifactLink"></param>
		/// <returns></returns>
		protected virtual TextTemplateArtifactLinkWrapper GetWrappedLink(IArtifactLink artifactLink)
		{
			TextTemplateArtifactLinkWrapper wrapper = new TextTemplateArtifactLinkWrapper(artifactLink);
			wrapper.ResourceResolver = new AssemblyResourceResolver();
			return wrapper;
		}