public ProcessTemplate ( TextReader input, |
||
input | TextReader | |
output | ||
Результат | bool |
public void ProcessT4File(string inputFile, string outputFile) { var templateGenerator = new TemplateGenerator(); templateGenerator.ProcessTemplate(inputFile, outputFile); foreach (CompilerError item in templateGenerator.Errors) { errors.Add(item.ErrorText + ": " + item.FileName + " at line: " + item.Line); } }
public static int Main (string[] args) { if (args.Length == 0) { ShowHelp (true); } TemplateGenerator generator = new TemplateGenerator (); string outputFile = null, inputFile = null; optionSet = new OptionSet () { { "o=|out=", "The name of the output {file}", s => outputFile = s }, { "r=", "Assemblies to reference", s => generator.Refs.Add (s) }, { "u=", "Namespaces to import <{0:namespace}>", s => generator.Imports.Add (s) }, { "I=", "Paths to search for included files", s => generator.IncludePaths.Add (s) }, { "P=", "Paths to search for referenced assemblies", s => generator.ReferencePaths.Add (s) }, { "dp=", "Directive processor name!class!assembly", s => generator.DirectiveProcessors.Add (s) }, { "a=", "Key value pairs for directive processors", (s, p) => generator.ProcessorValues[s] = p }, { "h|?|help", "Show help", s => ShowHelp (false) } }; List<string> remainingArgs = optionSet.Parse (args); if (String.IsNullOrEmpty (outputFile)) { Console.WriteLine ("No output file specified."); return -1; } if (remainingArgs.Count != 1) { Console.WriteLine ("No input file specified."); return -1; } inputFile = remainingArgs [0]; if (!File.Exists (inputFile)) { Console.WriteLine ("Input file '{0}' does not exist."); return -1; } System.Console.Write("Processing '{0}'... ", inputFile); generator.ProcessTemplate (inputFile, outputFile); if (generator.Errors.HasErrors) { System.Console.WriteLine("failed."); } else { System.Console.WriteLine("completed successfully."); } foreach (System.CodeDom.Compiler.CompilerError err in generator.Errors) Console.WriteLine ("{0}({1},{2}): {3} {4}", err.FileName, err.Line, err.Column, err.IsWarning? "WARNING" : "ERROR", err.ErrorText); return generator.Errors.HasErrors? -1 : 0; }
private static void Stub(FileSystemInfo source, FileSystemInfo destination) { if (source.FullName.EndsWith(".spark")) { // At this time the destination is already copied and processed by // 'simple' text substitution, so destination should be processed. var settings = new SparkSettings(); settings.AddNamespace("System"); var engine = new SparkViewEngine(settings) {DefaultPageBaseType = typeof(Template).FullName}; var folder = new InMemoryViewFolder {{source.Name, File.ReadAllText(destination.FullName)}}; engine.ViewFolder = folder; var descriptor = new SparkViewDescriptor(); descriptor.AddTemplate(source.Name); var view = (Template) engine.CreateInstance(descriptor); var builder = new StringBuilder(); using (var output = new StringWriter(builder)) { view.RenderView(output); File.WriteAllText(destination.FullName, output.ToString()); } engine.ReleaseInstance(view); } else if (source.FullName.EndsWith(".tt")) { var generator = new TemplateGenerator(); if (!generator.ProcessTemplate(source.FullName, destination.FullName)) { var exception = new TemplateException(source.FullName); foreach (CompilerError error in generator.Errors) { exception.AddError(error.ErrorText); } throw exception; } } }
private static int MainInternal (string[] args) { if (args.Length == 0) { ShowHelp (true); } var generator = new TemplateGenerator (); string outputFile = null, inputFile = null; var directives = new List<string> (); var parameters = new List<string> (); // var session = new Microsoft.VisualStudio.TextTemplating.TextTemplatingSession (); string preprocess = null; optionSet = new OptionSet () { { "o=|out=", "The name of the output {file}", s => outputFile = s }, { "r=", "Assemblies to reference", s => generator.Refs.Add (s) }, { "u=", "Namespaces to import <{0:namespace}>", s => generator.Imports.Add (s) }, { "I=", "Paths to search for included files", s => generator.IncludePaths.Add (s) }, { "P=", "Paths to search for referenced assemblies", s => generator.ReferencePaths.Add (s) }, { "dp=", "Directive processor (name!class!assembly)", s => directives.Add (s) }, { "a=", "Parameters ([processorName]![directiveName]!name!value)", s => parameters.Add (s) }, { "h|?|help", "Show help", s => ShowHelp (false) }, // { "k=,", "Session {key},{value} pairs", (s, t) => session.Add (s, t) }, { "c=", "Preprocess the template into {0:class}", (s) => preprocess = s }, }; var remainingArgs = optionSet.Parse (args); if (remainingArgs.Count != 1) { Console.Error.WriteLine ("No input file specified."); return -1; } inputFile = remainingArgs [0]; if (!File.Exists (inputFile)) { Console.Error.WriteLine ("Input file '{0}' does not exist."); return -1; } if (string.IsNullOrEmpty (outputFile)) { outputFile = inputFile; if (Path.HasExtension (outputFile)) { var dir = Path.GetDirectoryName (outputFile); var fn = Path.GetFileNameWithoutExtension (outputFile); outputFile = Path.Combine (dir, fn + ".txt"); } else { outputFile = outputFile + ".txt"; } } //FIXME: implement quoting and escaping for values foreach (var par in parameters) { var split = par.Split ('!'); if (split.Length < 2) { Console.Error.WriteLine ("Parameter does not have enough values: {0}", par); return -1; } if (split.Length > 2) { Console.Error.WriteLine ("Parameter has too many values: {0}", par); return -1; } string name = split[split.Length-2]; string val = split[split.Length-1]; if (string.IsNullOrEmpty (name)) { Console.Error.WriteLine ("Parameter has no name: {0}", par); return -1; } generator.AddParameter (split.Length > 3? split[0] : null, split.Length > 2? split[split.Length-3] : null, name, val); } foreach (var dir in directives) { var split = dir.Split ('!'); if (split.Length != 3) { Console.Error.WriteLine ("Directive does not have correct number of values: {0}", dir); return -1; } foreach (var s in split) { if (string.IsNullOrEmpty (s)) { Console.Error.WriteLine ("Directive has missing value: {0}", dir); return -1; } } generator.AddDirectiveProcessor (split[0], split[1], split[2]); } if (preprocess == null) { generator.ProcessTemplate (inputFile, outputFile); if (generator.Errors.HasErrors) { Console.WriteLine ("Processing '{0}' failed.", inputFile); } } else { string className = preprocess; string classNamespace = null; int s = preprocess.LastIndexOf ('.'); if (s > 0) { classNamespace = preprocess.Substring (0, s); className = preprocess.Substring (s + 1); } string language; string[] references; generator.PreprocessTemplate (inputFile, className, classNamespace, outputFile, System.Text.Encoding.UTF8, out language, out references); if (generator.Errors.HasErrors) { Console.Write ("Preprocessing '{0}' into class '{1}.{2}' failed.", inputFile, classNamespace, className); } } foreach (System.CodeDom.Compiler.CompilerError err in generator.Errors) Console.Error.WriteLine ("{0}({1},{2}): {3} {4}", err.FileName, err.Line, err.Column, err.IsWarning? "WARNING" : "ERROR", err.ErrorText); return generator.Errors.HasErrors? -1 : 0; }
static int MainInternal(string[] args) { if (args.Length == 0) { ShowHelp(true); } var generator = new TemplateGenerator(); string outputFile = null, inputFile = null; var directives = new List <string> (); var parameters = new List <string> (); string preprocess = null; optionSet = new OptionSet() { { "o=|out=", "The name of the output {file}", s => outputFile = s }, { "r=", "Assemblies to reference", s => generator.Refs.Add(s) }, { "u=", "Namespaces to import <{0:namespace}>", s => generator.Imports.Add(s) }, { "I=", "Paths to search for included files", s => generator.IncludePaths.Add(s) }, { "P=", "Paths to search for referenced assemblies", s => generator.ReferencePaths.Add(s) }, { "dp=", "Directive processor (name!class!assembly)", s => directives.Add(s) }, { "a=", "Parameters (name=value) or ([processorName!][directiveName!]name!value)", s => parameters.Add(s) }, #if NET5 { "roslyn", "Use Roslyn", s => generator.UseInProcessCompiler() }, #endif { "h|?|help", "Show help", s => ShowHelp(false) }, // { "k=,", "Session {key},{value} pairs", (s, t) => session.Add (s, t) }, { "c=", "Preprocess the template into {0:class}", (s) => preprocess = s }, }; var remainingArgs = optionSet.Parse(args); if (remainingArgs.Count != 1) { Console.Error.WriteLine("No input file specified."); return(-1); } inputFile = remainingArgs [0]; if (!File.Exists(inputFile)) { Console.Error.WriteLine("Input file '{0}' does not exist.", inputFile); return(-1); } if (string.IsNullOrEmpty(outputFile)) { outputFile = inputFile; if (Path.HasExtension(outputFile)) { var dir = Path.GetDirectoryName(outputFile); var fn = Path.GetFileNameWithoutExtension(outputFile); outputFile = Path.Combine(dir, fn + ".txt"); } else { outputFile = outputFile + ".txt"; } } foreach (var par in parameters) { if (!generator.TryAddParameter(par)) { Console.Error.WriteLine("Parameter has incorrect format: {0}", par); return(-1); } } foreach (var dir in directives) { var split = dir.Split('!'); if (split.Length != 3) { Console.Error.WriteLine("Directive must have 3 values: {0}", dir); return(-1); } for (int i = 0; i < 3; i++) { string s = split [i]; if (string.IsNullOrEmpty(s)) { string kind = i == 0? "name" : (i == 1 ? "class" : "assembly"); Console.Error.WriteLine("Directive has missing {0} value: {1}", kind, dir); return(-1); } } generator.AddDirectiveProcessor(split[0], split[1], split[2]); } if (preprocess == null) { generator.ProcessTemplate(inputFile, outputFile); if (generator.Errors.HasErrors) { Console.WriteLine("Processing '{0}' failed.", inputFile); } } else { string className = preprocess; string classNamespace = null; int s = preprocess.LastIndexOf('.'); if (s > 0) { classNamespace = preprocess.Substring(0, s); className = preprocess.Substring(s + 1); } generator.PreprocessTemplate(inputFile, className, classNamespace, outputFile, new System.Text.UTF8Encoding(encoderShouldEmitUTF8Identifier: false), out string language, out string[] references); if (generator.Errors.HasErrors) { Console.Write("Preprocessing '{0}' into class '{1}.{2}' failed.", inputFile, classNamespace, className); } } foreach (TemplateError err in generator.Errors) { Console.Error.WriteLine("{0}({1},{2}): {3} {4}", err.Location.FileName, err.Location.Line, err.Location.Column, err.IsWarning ? "WARNING" : "ERROR", err.Message); } return(generator.Errors.HasErrors? -1 : 0); }
public static int Main(string[] args) { if (args.Length == 0) { ShowHelp(true); } var generator = new TemplateGenerator(); string outputFile = null, inputFile = null; var directives = new List <string> (); var parameters = new List <string> (); var session = new Microsoft.VisualStudio.TextTemplating.TextTemplatingSession(); string preprocess = null; optionSet = new OptionSet() { { "o=|out=", "The name of the output {file}", s => outputFile = s }, { "r=", "Assemblies to reference", s => generator.Refs.Add(s) }, { "u=", "Namespaces to import <{0:namespace}>", s => generator.Imports.Add(s) }, { "I=", "Paths to search for included files", s => generator.IncludePaths.Add(s) }, { "P=", "Paths to search for referenced assemblies", s => generator.ReferencePaths.Add(s) }, { "dp=", "Directive processor (name!class!assembly)", s => directives.Add(s) }, { "a=", "Parameters ([processorName]![directiveName]!name!value)", s => parameters.Add(s) }, { "h|?|help", "Show help", s => ShowHelp(false) }, // { "k=,", "Session {key},{value} pairs", (s, t) => session.Add (s, t) }, { "c=", "Preprocess the template into {0:class}", (s) => preprocess = s }, }; var remainingArgs = optionSet.Parse(args); if (string.IsNullOrEmpty(outputFile)) { Console.Error.WriteLine("No output file specified."); return(-1); } if (remainingArgs.Count != 1) { Console.Error.WriteLine("No input file specified."); return(-1); } inputFile = remainingArgs [0]; if (!File.Exists(inputFile)) { Console.Error.WriteLine("Input file '{0}' does not exist."); return(-1); } //FIXME: implement quoting and escaping for values foreach (var par in parameters) { var split = par.Split('!'); if (split.Length < 2) { Console.Error.WriteLine("Parameter does not have enough values: {0}", par); return(-1); } if (split.Length > 2) { Console.Error.WriteLine("Parameter has too many values: {0}", par); return(-1); } string name = split[split.Length - 2]; string val = split[split.Length - 1]; if (string.IsNullOrEmpty(name)) { Console.Error.WriteLine("Parameter has no name: {0}", par); return(-1); } generator.AddParameter(split.Length > 3? split[0] : null, split.Length > 2? split[split.Length - 3] : null, name, val); } foreach (var dir in directives) { var split = dir.Split('!'); if (split.Length != 3) { Console.Error.WriteLine("Directive does not have correct number of values: {0}", dir); return(-1); } foreach (var s in split) { if (string.IsNullOrEmpty(s)) { Console.Error.WriteLine("Directive has missing value: {0}", dir); return(-1); } } generator.AddDirectiveProcessor(split[0], split[1], split[2]); } if (preprocess == null) { Console.Write("Processing '{0}'... ", inputFile); generator.ProcessTemplate(inputFile, outputFile); if (generator.Errors.HasErrors) { Console.WriteLine("failed."); } else { Console.WriteLine("completed successfully."); } } else { string className = preprocess; string classNamespace = null; int s = preprocess.LastIndexOf('.'); if (s > 0) { classNamespace = preprocess.Substring(0, s); className = preprocess.Substring(s + 1); } Console.Write("Preprocessing '{0}' into class '{1}.{2}'... ", inputFile, classNamespace, className); string language; string[] references; generator.PreprocessTemplate(inputFile, className, classNamespace, outputFile, System.Text.Encoding.UTF8, out language, out references); if (generator.Errors.HasErrors) { Console.WriteLine("failed."); } else { Console.WriteLine("completed successfully:"); Console.WriteLine(" Language: {0}", language); if (references != null && references.Length > 0) { Console.WriteLine(" References:"); foreach (string r in references) { Console.WriteLine(" {0}", r); } } } } foreach (System.CodeDom.Compiler.CompilerError err in generator.Errors) { Console.Error.WriteLine("{0}({1},{2}): {3} {4}", err.FileName, err.Line, err.Column, err.IsWarning? "WARNING" : "ERROR", err.ErrorText); } return(generator.Errors.HasErrors? -1 : 0); }
public static int Main(string[] args) { if (args.Length == 0) { ShowHelp(true); } TemplateGenerator generator = new TemplateGenerator(); string outputFile = null, inputFile = null; optionSet = new OptionSet() { { "o=|out=", "The name of the output {file}", s => outputFile = s }, { "r=", "Assemblies to reference", s => generator.Refs.Add(s) }, { "u=", "Namespaces to import <{0:namespace}>", s => generator.Imports.Add(s) }, { "I=", "Paths to search for included files", s => generator.IncludePaths.Add(s) }, { "P=", "Paths to search for referenced assemblies", s => generator.ReferencePaths.Add(s) }, { "dp=", "Directive processor name!class!assembly", s => generator.DirectiveProcessors.Add(s) }, { "a=", "Key value pairs for directive processors", (s, p) => generator.ProcessorValues[s] = p }, { "h|?|help", "Show help", s => ShowHelp(false) } }; List <string> remainingArgs = optionSet.Parse(args); if (String.IsNullOrEmpty(outputFile)) { Console.WriteLine("No output file specified."); return(-1); } if (remainingArgs.Count != 1) { Console.WriteLine("No input file specified."); return(-1); } inputFile = remainingArgs [0]; if (!File.Exists(inputFile)) { Console.WriteLine("Input file '{0}' does not exist."); return(-1); } System.Console.Write("Processing '{0}'... ", inputFile); generator.ProcessTemplate(inputFile, outputFile); if (generator.Errors.HasErrors) { System.Console.WriteLine("failed."); } else { System.Console.WriteLine("completed successfully."); } foreach (System.CodeDom.Compiler.CompilerError err in generator.Errors) { Console.WriteLine("{0}({1},{2}): {3} {4}", err.FileName, err.Line, err.Column, err.IsWarning? "WARNING" : "ERROR", err.ErrorText); } return(generator.Errors.HasErrors? -1 : 0); }
public static int Main (string[] args) { if (args.Length == 0) { ShowHelp (true); } var generator = new TemplateGenerator (); string outputFile = null, inputFile = null; var directives = new List<string> (); var parameters = new List<string> (); optionSet = new OptionSet () { { "o=|out=", "The name of the output {file}", s => outputFile = s }, { "r=", "Assemblies to reference", s => generator.Refs.Add (s) }, { "u=", "Namespaces to import <{0:namespace}>", s => generator.Imports.Add (s) }, { "I=", "Paths to search for included files", s => generator.IncludePaths.Add (s) }, { "P=", "Paths to search for referenced assemblies", s => generator.ReferencePaths.Add (s) }, { "dp=", "Directive processor (name!class!assembly)", s => directives.Add (s) }, { "a=", "Parameters ([processorName]![directiveName]!name!value)", s => parameters.Add (s) }, { "h|?|help", "Show help", s => ShowHelp (false) } }; var remainingArgs = optionSet.Parse (args); if (string.IsNullOrEmpty (outputFile)) { Console.Error.WriteLine ("No output file specified."); return -1; } if (remainingArgs.Count != 1) { Console.Error.WriteLine ("No input file specified."); return -1; } inputFile = remainingArgs [0]; if (!File.Exists (inputFile)) { Console.Error.WriteLine ("Input file '{0}' does not exist."); return -1; } //FIXME: implement quoting and escaping for values foreach (var par in parameters) { var split = par.Split ('!'); if (split.Length < 2) { Console.Error.WriteLine ("Parameter does not have enough values: {0}", par); return -1; } if (split.Length > 2) { Console.Error.WriteLine ("Parameter has too many values: {0}", par); return -1; } string name = split[split.Length-2]; string val = split[split.Length-1]; if (string.IsNullOrEmpty (name)) { Console.Error.WriteLine ("Parameter has no name: {0}", par); return -1; } generator.AddParameter (split.Length > 3? split[0] : null, split.Length > 2? split[split.Length-3] : null, name, val); } foreach (var dir in directives) { var split = dir.Split ('!'); if (split.Length != 3) { Console.Error.WriteLine ("Directive does not have correct number of values: {0}", dir); return -1; } foreach (var s in split) { if (string.IsNullOrEmpty (s)) { Console.Error.WriteLine ("Directive has missing value: {0}", dir); return -1; } } generator.AddDirectiveProcessor (split[0], split[1], split[2]); } Console.Write("Processing '{0}'... ", inputFile); generator.ProcessTemplate (inputFile, outputFile); if (generator.Errors.HasErrors) { Console.WriteLine("failed."); } else { Console.WriteLine("completed successfully."); } foreach (System.CodeDom.Compiler.CompilerError err in generator.Errors) Console.Error.WriteLine ("{0}({1},{2}): {3} {4}", err.FileName, err.Line, err.Column, err.IsWarning? "WARNING" : "ERROR", err.ErrorText); return generator.Errors.HasErrors? -1 : 0; }
static void generateCSharpStatesEnum (string namespaceName, string enumName, string fileName, StringBuilder statesStringBuilder) { //Create template generator TemplateGenerator generator = new TemplateGenerator (); generator.Session.Add ("Namespace", namespaceName); //generator.Session.Add ("ClassName", controller.name.Replace (" ", "") + "Class"); generator.Session.Add ("EnumName", enumName); generator.Session.Add ("Values", statesStringBuilder.ToString ()); string generated; generated = Path.Combine (Application.dataPath, "Scripts"); //if Scripts doesn't exist if (!Directory.Exists (generated)) Directory.CreateDirectory (generated); //string[] filesFound=Directory.GetFiles (Path.Combine (generated, fileName)); //if (filesFound.Length == 0) { generated = namespaceToDirectory (generated, namespaceName); generated = Path.GetFullPath (Path.Combine (generated, fileName)); //} else { // generated=filesFound[0]; //} try { ///!!! OSX : error !Error running gmcs : Cannot find the specified file.! happen here cos of unity bug /// Download and install MRE(Mono Runtime Envi) /// http://www.mono-project.com/download/ /// saving doesn't work still cos of unknown fileName = Path.Combine (Path.Combine (Application.dataPath, "Editor"), "StatesEnumTemplate.tpl"); UnityEngine.Debug.Log ("Generating " + generated + " from template:" + fileName); generator.ProcessTemplate (fileName, generated); } catch (Exception e) { UnityEngine.Debug.LogError (e.Message); } AssetDatabase.Refresh (); }