Click here to Skip to main content
12,895,801 members (46,906 online)
Click here to Skip to main content
Articles » Languages » C# » Utilities » Downloads

Stats

61.7K views
3.3K downloads
110 bookmarked
Posted 2 Nov 2007

Template Based Code Generator

, 20 Nov 2007 CPOL
A template based, command-line oriented .NET code generator
Arebis.CodeGeneration.dll
Arebis.CodeGeneration.VisualStudio.dll
Arebis.CodeGenerator.dll
CGen.exe
Arebis.CodeGeneration.dll
Arebis.CodeGeneration.VisualStudio.dll
Arebis.CodeGenerator.dll
CGen.exe
CGen.exe
Arebis.CodeGeneration.dll
Arebis.CodeGeneration.VisualStudio.dll
Arebis.CodeGenerator.dll
Arebis.CodeGeneration
Properties
Arebis.CodeGeneration.VisualStudio
Properties
Arebis.CodeGenerator
Extensions
Parsing
Properties
Resources
Templated
backup_v1.zip
_ClassDiagram.cd
CGen
Properties
Resources
backup_v1.zip
_ClassDiagram.cd
ClassDiagram.cd
Arebis.CodeGenerator.csproj.user
_ClassDiagram.cd
backup_v1.zip
Syntax
bin
Arebis.CodeGeneration.dll
Arebis.CodeGeneration.VisualStudio.dll
Arebis.CodeGenerator.dll
CGen.exe
BuildClasses.cst
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 RealDolmen
Belgium Belgium
Rudi is a Software Architect at RealDolmen.

You may also be interested in...

Pro
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170424.1 | Last Updated 20 Nov 2007
Article Copyright 2007 by Rudi Breedenraedt
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid