Mono.TextTemplating.TemplatingEngine.GenerateCompileUnit C# (CSharp) Method

GenerateCompileUnit() public static method

public static GenerateCompileUnit ( ITextTemplatingEngineHost host, ParsedTemplate pt, Mono.TextTemplating.TemplateSettings settings ) : CodeCompileUnit
host ITextTemplatingEngineHost
pt ParsedTemplate
settings Mono.TextTemplating.TemplateSettings
return System.CodeDom.CodeCompileUnit
			return false;
		}
		
		public static CodeCompileUnit GenerateCompileUnit (ITextTemplatingEngineHost host, ParsedTemplate pt,
		                                                   TemplateSettings settings)
		{
			//prep the compile unit
			CodeCompileUnit ccu = new CodeCompileUnit ();
			CodeNamespace namespac = new CodeNamespace (settings.Namespace);
			ccu.Namespaces.Add (namespac);
			
			HashSet<string> imports = new HashSet<string> ();
			imports.UnionWith (settings.Imports);
			imports.UnionWith (host.StandardImports);
			foreach (string ns in imports)
				namespac.Imports.Add (new CodeNamespaceImport (ns));
			
			//prep the type
			CodeTypeDeclaration type = new CodeTypeDeclaration (settings.Name);
			if (!String.IsNullOrEmpty (settings.Inherits))
				type.BaseTypes.Add (new CodeTypeReference (settings.Inherits));
			else
				type.BaseTypes.Add (new CodeTypeReference (typeof (TextTransformation)));
			namespac.Types.Add (type);
			
			//prep the transform method
			CodeMemberMethod transformMeth = new CodeMemberMethod ();
			transformMeth.Name = "TransformText";
			transformMeth.ReturnType = new CodeTypeReference (typeof (String));
			transformMeth.Attributes = MemberAttributes.Public | MemberAttributes.Override;
			
			//method references that will need to be used multiple times
			CodeMethodReferenceExpression writeMeth =
				new CodeMethodReferenceExpression (new CodeThisReferenceExpression (), "Write");
			CodeMethodReferenceExpression toStringMeth =
				new CodeMethodReferenceExpression (new CodeTypeReferenceExpression (typeof (ToStringHelper)), "ToStringWithCulture");
			
			//build the code from the segments
			foreach (TemplateSegment seg in pt.Content) {
				CodeStatement st = null;
				switch (seg.Type) {
				case SegmentType.Block:
					st = new CodeSnippetStatement (seg.Text);
					break;
				case SegmentType.Expression:
					st = new CodeExpressionStatement (
						new CodeMethodInvokeExpression (writeMeth,
							new CodeMethodInvokeExpression (toStringMeth, new CodeSnippetExpression (seg.Text))));
					break;
				case SegmentType.Content:
					st = new CodeExpressionStatement (new CodeMethodInvokeExpression (writeMeth, new CodePrimitiveExpression (seg.Text)));
					break;
				case SegmentType.Helper:
					CodeTypeMember mem = new CodeSnippetTypeMember (seg.Text);
					mem.LinePragma = new CodeLinePragma (host.TemplateFile, seg.StartLocation.Line);
					type.Members.Add (mem);
					break;
				default:
					throw new InvalidOperationException ();
				}
				if (st != null) {
					st.LinePragma = new CodeLinePragma (host.TemplateFile, seg.StartLocation.Line);
					transformMeth.Statements.Add (st);
				}
			}
			
			//complete the transform method
			transformMeth.Statements.Add (new CodeMethodReturnStatement (
				new CodeMethodInvokeExpression (
					new CodePropertyReferenceExpression (
						new CodeThisReferenceExpression (),
						"GenerationEnvironment"),
						"ToString")));
			type.Members.Add (transformMeth);
			
			//generate the Host property if needed
			if (settings.HostSpecific) {
				CodeMemberField hostField = new CodeMemberField (new CodeTypeReference (typeof (ITextTemplatingEngineHost)), "hostValue");
				hostField.Attributes = (hostField.Attributes & ~MemberAttributes.AccessMask) | MemberAttributes.Private;
				type.Members.Add (hostField);
				CodeMemberProperty hostProp = new CodeMemberProperty ();
				hostProp.Name = "Host";
				hostProp.Attributes = MemberAttributes.Public;
				hostProp.HasGet = hostProp.HasGet = true;
				hostProp.Type = hostField.Type;
				CodeFieldReferenceExpression hostFieldRef = new CodeFieldReferenceExpression (new CodeThisReferenceExpression (), "hostValue");
				hostProp.SetStatements.Add (new CodeAssignStatement (hostFieldRef, new CodePropertySetValueReferenceExpression ()));
				hostProp.GetStatements.Add (new CodeMethodReturnStatement (hostFieldRef));
				type.Members.Add (hostProp);
			}