protected void Generate(string name) { PatternBufferSchemaParser loader = new PatternBufferSchemaParser(); string pbFile = @"..\..\Generation\" + name + @".pb"; string schemaSource = File.ReadAllText(pbFile); PatternBufferSchema schema = loader.Parse(schemaSource); Console.WriteLine(schema.Name); PatternBufferCompiler compiler = new PatternBufferCompiler("Test." + name); Dictionary <string, string> files = compiler.Compile(schema); Console.WriteLine(files.Count + " files generated"); string directory = @"..\..\..\PatternBufferGeneratedTest\" + name + @"\Generated"; if (Directory.Exists(directory)) { Directory.Delete(directory, true); } Directory.CreateDirectory(directory); foreach (string fileName in files.Keys) { Console.WriteLine("writing to " + fileName); File.WriteAllText(directory + @"\" + fileName, files[fileName]); } }
/** * Parses a schema string into a PatternBufferSchema. */ protected PatternBufferSchema Parse(string schema) { PatternBufferSchema s = null; try { PatternBufferSchemaParser loader = new PatternBufferSchemaParser(@"..\..\..\..\..\PatternBuffer.cgt"); s = loader.Parse(schema); } catch (Exception x) { //Console.WriteLine(x.StackTrace); throw x; } return(s); }
/** * The main method that performs cues up the compiler args, gathers files, and feeds it all * to the actual compiler object. */ static void Main(string[] args) { // For timing purposes. It's like totally fast, though, don't worry. Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); // Gather the command line args PatternBufferCompilerCommandLine commandLine = new PatternBufferCompilerCommandLine(args); if (commandLine.DoPrintUsage || commandLine.HasMissingRequiredCommandLineOptions) { Environment.Exit(0); } // Load the specified file(s) IList <string> schemaFiles = GatherSchemaFiles(commandLine); if (schemaFiles.Count == 0) { Console.WriteLine("No input file(s) found."); Environment.Exit(1); } // Verify the output path exists string outputDirectory = commandLine.OutputDirectory; if (commandLine.OutputDirectory == null) { outputDirectory = "./"; } else if (Directory.Exists(commandLine.OutputDirectory)) { outputDirectory = commandLine.OutputDirectory; } else { Console.WriteLine("Specified output directory does not exist."); Environment.Exit(1); } // Append a directory separator if it's missing if (!(outputDirectory.EndsWith("" + Path.AltDirectorySeparatorChar) || outputDirectory.EndsWith("" + Path.DirectorySeparatorChar))) { outputDirectory += Path.AltDirectorySeparatorChar; } // Concatenate all the schema files string schemaText = ""; // A little flag to ignore multiple "name" directives. bool nameFound = false; foreach (string file in schemaFiles) { Console.WriteLine(file); string[] allLines = File.ReadAllLines(file); foreach (string line in allLines) { string temp = line.Trim(); // Remember the schema name if you haven't seen one yet. if (temp.StartsWith("name")) { if (!nameFound) { schemaText += line; schemaText += "\r\n"; nameFound = true; } } // Otherwise, not a name line... append and move on. else { schemaText += line; schemaText += "\r\n"; } } } // Load the schema PatternBufferSchemaParser loader = new PatternBufferSchemaParser(); PatternBufferSchema schema = loader.Parse(schemaText); // Create a compiler with the proper namespace string @namespace = commandLine.CSharpNamespace != null ? commandLine.CSharpNamespace : schema.Name; PatternBufferCompiler compiler = new PatternBufferCompiler(@namespace); // Set the custom boilerplate, if any. if (commandLine.BoilerplateFilePath != null) { compiler.Boilerplate = File.ReadAllText(commandLine.BoilerplateFilePath); } // Set the type safety flag. compiler.MakeThreadsafe = commandLine.MakeThreadsafe; // Compile Dictionary <string, string> files = compiler.Compile(schema); // Save all the files. foreach (string fileName in files.Keys) { Console.WriteLine("writing to " + outputDirectory + fileName); if (fileName.Contains("/")) { string subdirectory = outputDirectory + fileName; subdirectory = subdirectory.Substring(0, subdirectory.LastIndexOf("/")); if (!Directory.Exists(subdirectory)) { Console.WriteLine("creating " + subdirectory); Directory.CreateDirectory(subdirectory); } } File.WriteAllText(outputDirectory + fileName, files[fileName]); } stopwatch.Stop(); Console.WriteLine("\nCompiled " + files.Count + " files in " + stopwatch.ElapsedMilliseconds + " ms."); }