public void AddAssemblyDirectiveAsProjectReference(RgenFileBuilders rfb, IEnumerable<ParseTreeNode> directives) { var assemblies = _grammar.GetReferencedAssemblies(directives); var vsProject = (VSProject)_projectItem.Project.DteObject.Object; foreach (var asm in assemblies.Union(_requiredAssemblies)) { vsProject.References.Add(Convert.ToString(asm)); } }
public void Translate(RgenFileBuilders writers, ParseTreeNode rootNode) { _processed.Clear(); writers.ImportedNamespaces.AddRange(_requiredAssemblies); IncludeFileManager.ExpandIncludeFiles(Convert.ToString(_projectItem.DteObject.DTE.Version), rootNode, _grammar, _parser); TranslateTemplateDirectives(writers, rootNode); TranslateNodeRecursive(writers, rootNode); }
private void WriteCodeBehindImportsStatement(RgenFileBuilders sb, ParseTreeNode directiveNode) { if (directiveNode.Term != _grammar.Directive) { return; } var name = _grammar.GetDirectiveName(directiveNode); if (!T4Grammar.IsEqualText(Convert.ToString(name), "Import")) { return; } var namespc = _grammar.GetDirectiveAttributes(directiveNode)["Namespace"]; sb.ImportedNamespaces.Add(namespc); }
/// <summary> /// Some directive needs to be combined, so they need to be processed separately before other nodes /// </summary> /// <param name="rfb"></param> /// <param name="rootNode"></param> /// <remarks></remarks> private void TranslateTemplateDirectives(RgenFileBuilders rfb, ParseTreeNode rootNode) { try { var directives = (from c in rootNode.ChildNodes from gc in c.ChildNodes where gc.Term == _grammar.Directive select gc).ToArray(); //Combine the attributes of template and output directives var templateDirectiveNames = new[] { "template", "output" }; var templateDirectives = from d in directives where templateDirectiveNames.Contains(_grammar.GetDirectiveName(d), T4Grammar.Comparer) select d; var templateDirectiveAttributes = (from d in templateDirectives from c in d.ChildNodes where c.Term == _grammar.AttributeList select c).ToArray(); rfb.TemplateFile.Append(string.Format("<%@Template ClassName={0} ", rfb.ClassName.Quote())); foreach (var a in templateDirectiveAttributes) { TranslateNodeRecursive(rfb, a); } rfb.TemplateFile.Append("%>"); AddAssemblyDirectiveAsProjectReference(rfb, directives); } catch (Exception) { DebugExtensions.DebugHere(); } }
private void TranslateNodeRecursive(RgenFileBuilders sb, ParseTreeNode node) { if (SkipNode(node)) return; WriteCodeBehindImportsStatement(sb, node); var tag = node.Tag as TranslationTag; if (node.Term == _grammar.EmbeddedClassMember) { TranslateEmbeddedClassMember(sb, node); } else if (tag != null) //Process expanded tag //Tag may contain another ParseTreeNode from an Include directive (which is treated as embedded file by T4) TranslateNodeRecursive(sb, tag.ExpandedParseTreeNode); else if (node.ChildNodes.Any()) { //recursively translate node foreach (var n in node.ChildNodes) { TranslateNodeRecursive(sb, n); } } else { //No child, this is a terminal, translate node var translated = GetTranslatedText(node); if (translated != null) sb.TemplateFile.Append(translated); _processed.Add(node); } }
private void TranslateEmbeddedClassMember(RgenFileBuilders sb, ParseTreeNode node) { var text = _grammar.GetEmbeddedClassMemberText(node); sb.ClassMembers.Append(text); }