|
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.