public static void Merge(FileDescriptorSet files, string path, TextWriter stderr, params string[] args) { if (stderr == null) throw new ArgumentNullException("stderr"); if (files == null) throw new ArgumentNullException("files"); if (string.IsNullOrEmpty(path)) throw new ArgumentNullException("path"); bool deletePath = false; if(!IsValidBinary(path)) { // try to use protoc path = CompileDescriptor(path, stderr, args); deletePath = true; } try { using (FileStream stream = File.OpenRead(path)) { Serializer.Merge(stream, files); } } finally { if(deletePath) { File.Delete(path); } } }
public void TestProtoInput() { FileDescriptorSet files = new FileDescriptorSet(); Assert.AreEqual(0, files.file.Count); InputFileLoader.Merge(files, @"ProtoGen\descriptor.proto", Console.Error); Assert.AreEqual(1, files.file.Count); }
public void TestBinaryInput() { // compile .proto to .bin Process.Start("protoc", @"--descriptor_set_out=ProtoGen\descriptor.bin --include_imports ProtoGen\descriptor.proto").WaitForExit(); // process .bin FileDescriptorSet files = new FileDescriptorSet(); Assert.AreEqual(0, files.file.Count); InputFileLoader.Merge(files, @"ProtoGen\descriptor.bin", Console.Error); Assert.AreEqual(1, files.file.Count); }
static public void Gen(google.protobuf.FileDescriptorSet set) { currentSet = set; foreach (var proto in set.file) { foreach (var message in proto.message_type) { //message.nested_type GenMessageParse(message); } } currentSet = null; }
public void TestGarbageInput() { FileDescriptorSet files = new FileDescriptorSet(); try { InputFileLoader.Merge(files, @"ProtoGen\InputLoader.cs", Console.Error); Assert.Fail("Should have barfed"); } catch(ProtoParseException ex) { bool sw = ex.Message.StartsWith("An error occurred parsing InputLoader.cs"); if (!sw) Assert.Fail("Expected message not found: " + ex.Message); } catch { Assert.Fail("Expected ArgumentException"); } }
public FileDescriptorSet LoadTypes(Stream ms = null) { var files = new FileDescriptorSet(); var args = string.Join(" ", (_includePaths ?? new string[0]) .Concat(_addToIncludePath ? _paths.Select(Path.GetDirectoryName) : new string[0]) .Where(Directory.Exists) .Select(o => o.ToLowerInvariant()) .Distinct() .Select(o => "-I " + o)); using (var sr = ms != null ? new StreamWriter(ms) : StreamWriter.Null) { foreach (var path in _paths .Where(File.Exists) .Select(o => o.ToLowerInvariant()) .Distinct()) { InputFileLoader.Merge(files, path, sr, args); } } return files; }
private static string LoadFilesAsXml(CommandLineOptions options) { FileDescriptorSet set = new FileDescriptorSet(); foreach (string inPath in options.InPaths) { InputFileLoader.Merge(set, inPath, options.ErrorWriter, options.Arguments.ToArray()); } XmlSerializer xser = new XmlSerializer(typeof(FileDescriptorSet)); XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; settings.IndentChars = " "; settings.NewLineHandling = NewLineHandling.Entitize; StringBuilder sb = new StringBuilder(); using (XmlWriter writer = XmlWriter.Create(sb, settings)) { xser.Serialize(writer, set); } return sb.ToString(); }
public void TestMissingInput() { FileDescriptorSet files = new FileDescriptorSet(); InputFileLoader.Merge(files, @"ProtoGen\NoneSuch.bin", Console.Error); }
public void TestEmptyPath() { FileDescriptorSet files = new FileDescriptorSet(); InputFileLoader.Merge(files, "", Console.Error); }
public void TestNullPath() { FileDescriptorSet files = new FileDescriptorSet(); InputFileLoader.Merge(files, null, Console.Error); }