Click here to Skip to main content
11,640,788 members (60,215 online)
Click here to Skip to main content
Articles » Languages » C# » Utilities » Downloads
Add your own
alternative version

Template Based Code Generator

, 20 Nov 2007 CPOL 52.7K 2.8K 107
A template based, command-line oriented .NET code generator
arebiscgen10.zip
Arebis.CodeGeneration.dll
Arebis.CodeGeneration.VisualStudio.dll
Arebis.CodeGenerator.dll
CGen.exe
arebiscgen11.zip
Arebis.CodeGeneration.dll
Arebis.CodeGeneration.VisualStudio.dll
Arebis.CodeGenerator.dll
CGen.exe
arebiscgen12.zip
CGen.exe
Arebis.CodeGeneration.dll
Arebis.CodeGeneration.VisualStudio.dll
Arebis.CodeGenerator.dll
arebiscgensource10.zip
Arebis.CodeGeneration
Properties
Arebis.CodeGeneration.VisualStudio
Properties
Arebis.CodeGenerator
Extensions
Parsing
Properties
Resources
Templated
backup_v1.zip
_ClassDiagram.cd
CGen
Properties
Resources
arebiscgensource11.zip
backup_v1.zip
_ClassDiagram.cd
arebiscgensource12.zip
ClassDiagram.cd
Arebis.CodeGenerator.csproj.user
_ClassDiagram.cd
backup_v1.zip
Syntax
cgensample.zip
bin
Arebis.CodeGeneration.dll
Arebis.CodeGeneration.VisualStudio.dll
Arebis.CodeGenerator.dll
CGen.exe
BuildClasses.cst
BuildClasses.settings
Class.cst
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.Specialized;
using Arebis.CodeGenerator.Templated;
using System.CodeDom.Compiler;
using System.Reflection;
using System.IO;

namespace Arebis.CommandLineTools.CodeGenerator
{
	/// <summary>
	/// A Generator object creates a GenerationHost and executes the initial template on it.
	/// This implementation will host the GenerationHost in a separate AppDomain to allow unload of assemblies.
	/// </summary>
	[System.Diagnostics.DebuggerStepThrough]
	public class Generator
	{
		private NameValueCollection settings = new NameValueCollection();
		private object[] templateParameters = new object[0];

		public NameValueCollection Settings
		{
			get { return this.settings; }
			set { this.settings = value; }
		}

		public object[] TemplateParameters
		{
			get { return this.templateParameters; }
			set { this.templateParameters = value; }
		}

		public virtual void ExecuteTemplate()
		{
			// Execute the (initial) template by GenerationHost in separate AppDomain:
			AppDomain appDomain = null;
			GenerationHost genHost = null;
			try
			{
				AppDomainSetup info = new AppDomainSetup();
				info.ApplicationBase = new FileInfo(Assembly.GetEntryAssembly().Location).Directory.FullName;
				info.ShadowCopyFiles = "true";
				appDomain = AppDomain.CreateDomain("CodeGenSpace", AppDomain.CurrentDomain.Evidence, info);
				genHost = (GenerationHost)appDomain.CreateInstance(typeof(GenerationHost).Assembly.FullName, typeof(GenerationHost).FullName).Unwrap();
				genHost.Initialize(this.Settings);
				this.ExecuteTemplate(genHost);
			}
			finally
			{
				if (genHost != null) genHost.Dispose();
				if (appDomain != null) AppDomain.Unload(appDomain);
			}
		}

		public virtual void ExecuteTemplate(GenerationHost genHost)
		{
			this.ExecuteTemplate(genHost, this.Settings["template"], this.settings["output"], this.TemplateParameters);
		}

		public virtual void ExecuteTemplate(GenerationHost genHost, string templateFilename, string outputFilename, object[] templateParameters)
		{
			try
			{
				genHost.Initialize(this.Settings);
				genHost.CallTemplateToFile(templateFilename, outputFilename, templateParameters);
			}
			catch (CompilationFailedException ex)
			{
				Console.WriteLine(String.Format("Compilation failed for file: {0}", ex.Filename));
				foreach (CompilerError err in ex.Errors)
				{
					Console.WriteLine(String.Format("{0} {1}: {2}\r\n  \"{3}\", line #{4}",
						err.IsWarning ? "Warning" : "Error",
						err.ErrorNumber,
						err.ErrorText,
						err.FileName,
						err.Line));
				}
			}
			catch (RuntimeException ex)
			{
				Console.WriteLine("Runtime exception: ");
				Console.WriteLine(ex.InnerException);
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Rudi Breedenraedt
Architect Wolters Kluwer Belgium
Belgium Belgium
Rudi is a Software Architect at Wolters Kluwer Belgium.

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150731.1 | Last Updated 20 Nov 2007
Article Copyright 2007 by Rudi Breedenraedt
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid