/// <summary> /// Execute the variable /// </summary> /// <param name="w">writer</param> /// <param name="indentValue">space size</param> /// <param name="currentLine">in-progress line add</param> /// <param name="config">configuration</param> /// <param name="dir">directory</param> public void Execute(TextWriter w, ref int indentValue, ref string currentLine, Configuration config, string dir) { if (this.Indent) { ++indentValue; } if (this.Include) { string fileName = config.Execute(this.Value); FileInfo fi = new FileInfo(Path.Combine(dir, fileName)); if (fi.Exists) { using (FileStream fs = fi.Open(FileMode.Open, FileAccess.Read, FileShare.Read)) { PrinterObject po = PrinterObject.Load(fs); po.CurrentDirectory = dir; foreach (PrinterVariable pv in this.Values) { po.AddVariable(pv.Name, pv); } po.ImportConfiguration(config); po.Execute(w, ref indentValue, ref currentLine, po.Configuration); fs.Close(); } } } else { if (!String.IsNullOrEmpty(this.Value)) { string val = config.Execute(this.Value); PrinterObject.IndentSource(w, indentValue, ref currentLine, val); } } if (this.Indent) { --indentValue; } }
static void Main(string[] args) { try { if (args.Length == 0) { throw new ArgumentException("USAGE : printer file.prt"); } PrinterObject po = null; FileInfo fi = new FileInfo(args[0]); if (fi.Exists) { po = PrinterObject.Load(args[0]); } else { po = PrinterObject.Create(new PrinterVersion(Path.GetDirectoryName(args[0]), Path.GetFileName(args[0]))); po.AddData(@"using System; using System.IO; using System.Runtime.Serialization.Formatters.Binary; namespace "); po.AddData("[Printer]"); po.AddData(@" { class "); po.AddData("[Program]"); po.AddData(@" { static void Main(string[] args) { //"); PrinterVariable pv = new PrinterVariable(); pv.Name = "condition"; pv.Include = true; pv.Indent = true; pv.AddVariable("expression", "true"); pv.AddVariable("then", "code"); pv.AddVariable("else", "code"); pv.Value = "C/if.prt"; po.AddVariable("condition", pv); po.UseVariable("condition"); po.AddData(@" } } } "); } Console.WriteLine(po.Execute()); Console.WriteLine("code:"); Console.WriteLine(po.ToString()); PrinterObject.Save(po, args[0]); } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { Console.WriteLine("Touch your keyboard"); Console.ReadKey(); } }