Die .Net-CLR als Makro-Baukasten

Komplette Listings

C#-Code für css.exe

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.CSharp;
using System.CodeDom.Compiler;
using System.Xml;

namespace css {
class Program {
static void Main(string[] args) {
if (args.Length == 0) {
Help();
} else {
CSharpCodeProvider c = new CSharpCodeProvider();
CompilerParameters options = new CompilerParameters();
options.GenerateExecutable = true;
options.GenerateInMemory = true;
options.CompilerOptions = GetReferencedAssemblies();
CompilerResults r = c.CompileAssemblyFromFile( options, args[0]);
if (r.Errors.Count != 0) {
DumpErrors(r.Errors);
} else {
r.CompiledAssembly.EntryPoint.Invoke(null, new object[] { args });
}
}
}

private static string GetReferencedAssemblies() {
StringBuilder result = new StringBuilder();

string configFile = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "\\css.xml";
XmlDocument document = new XmlDocument();
document.Load(configFile);
XmlNodeList list = document.GetElementsByTagName("assembly");
foreach (XmlNode node in list) {
XmlNode a = node.Attributes.GetNamedItem("path");
result.AppendFormat("/reference:{0} ", a.Value);
}
return result.ToString();
}

private static void DumpErrors(CompilerErrorCollection collection) {
foreach (CompilerError error in collection) {
Console.WriteLine( error.Line.ToString() + "," + error.Column.ToString() + ": " + error.ErrorText);
}
}

private static void Help() {
Console.WriteLine("css 1.0 - Von Thomas Wölfer - http://www.woelfer.com");
Console.WriteLine("Aufruf: css [Pfad]ScriptDatei.cs [Paramter fürs Skript]");
Console.WriteLine("Im Verzeichnis des Images kann eine XML-Datei mit zu referenzierenden Assemblies angelegt werden.");
}
}
}