Exemplo n.º 1
0
        public void Generate()
        {
            List <FileDescriptorSet> descriptorProtos = new List <FileDescriptorSet>();

            foreach (string inputFile in options.InputFiles)
            {
                ExtensionRegistry extensionRegistry = ExtensionRegistry.CreateInstance();
                CSharpOptions.RegisterAllExtensions(extensionRegistry);
                using (Stream inputStream = File.OpenRead(inputFile))
                {
                    descriptorProtos.Add(FileDescriptorSet.ParseFrom(inputStream, extensionRegistry));
                }
            }

            IList <FileDescriptor> descriptors = ConvertDescriptors(options.FileOptions, descriptorProtos.ToArray());

            // Combine with options from command line
            foreach (FileDescriptor descriptor in descriptors)
            {
                descriptor.ConfigureWithDefaultOptions(options.FileOptions);
            }

            foreach (FileDescriptor descriptor in descriptors)
            {
                // Optionally exclude descriptors in google.protobuf
                if (descriptor.CSharpOptions.IgnoreGoogleProtobuf && descriptor.Package == "google.protobuf")
                {
                    continue;
                }
                Generate(descriptor);
            }
        }
Exemplo n.º 2
0
        public MessageRegistry(string descriptionFile)
        {
            FileDescriptorSet descriptorSet =
                FileDescriptorSet.ParseFrom(new FileStream(descriptionFile, FileMode.Open));

            foreach (FileDescriptorProto fdp in descriptorSet.FileList)
            {
                FileDescriptor fd = null;
                try
                {
                    fd = FileDescriptor.BuildFrom(fdp, new FileDescriptor[] { });
                }
                catch (DescriptorValidationException e)
                {
                    // TODO Auto-generated catch block
                    logger.Error(e);
                }

                foreach (MessageDescriptor descriptor in fd.MessageTypes)
                {
                    String className = fdp.Options.JavaPackage + "."
                                       + fdp.Options.JavaOuterClassname + "."
                                       + descriptor.Name;

                    Type type = ReflectionHelper.FindTypeFullSearch(className);

                    if (type != null)
                    {
                        mapping.Add(descriptor.FullName, type);
                    }
                }
            }
        }
        internal static int Main(string[] args)
        {
            try
            {
                // Hack to make sure everything's initialized
                DescriptorProtoFile.Descriptor.ToString();
                GeneratorOptions options = new GeneratorOptions {
                    Arguments = args
                };

                IList <string> validationFailures;
                if (!options.TryValidate(out validationFailures))
                {
                    // We've already got the message-building logic in the exception...
                    InvalidOptionsException exception = new InvalidOptionsException(validationFailures);
                    Console.WriteLine(exception.Message);
                    return(1);
                }

                var request = new CodeGeneratorRequest.Builder();
                foreach (string inputFile in options.InputFiles)
                {
                    ExtensionRegistry extensionRegistry = ExtensionRegistry.CreateInstance();
                    CSharpOptions.RegisterAllExtensions(extensionRegistry);
                    using (Stream inputStream = File.OpenRead(inputFile))
                    {
                        var fileSet = FileDescriptorSet.ParseFrom(inputStream, extensionRegistry);
                        foreach (var fileProto in fileSet.FileList)
                        {
                            request.AddFileToGenerate(fileProto.Name);
                            request.AddProtoFile(fileProto);
                        }
                    }
                }

                Generator generator = Generator.CreateGenerator(options);
                var       response  = new CodeGeneratorResponse.Builder();
                generator.Generate(request.Build(), response);
                if (response.HasError)
                {
                    throw new Exception(response.Error);
                }
                foreach (var file in response.FileList)
                {
                    File.WriteAllText(file.Name, file.Content);
                }
                return(0);
            }
            catch (Exception e)
            {
                Console.Error.WriteLine("Error: {0}", e.Message);
                Console.Error.WriteLine();
                Console.Error.WriteLine("Detailed exception information: {0}", e);
                return(1);
            }
        }
    public static void ParsePB(string FilePath, string outputFilePath)
    {
        var pbFile = FileDescriptorSet.ParseFrom(File.ReadAllBytes(FilePath));

        var protoList = pbFile.FileList;

        //指代特定proto
        foreach (var proto in protoList)
        {
            ParseProto(proto, outputFilePath);
        }
    }
Exemplo n.º 5
0
        public void Generate()
        {
            List <FileDescriptorSet> descriptorProtos = new List <FileDescriptorSet>();

            foreach (string inputFile in options.InputFiles)
            {
                ExtensionRegistry extensionRegistry = ExtensionRegistry.CreateInstance();
                CSharpOptions.RegisterAllExtensions(extensionRegistry);
                using (Stream inputStream = File.OpenRead(inputFile))
                {
                    descriptorProtos.Add(FileDescriptorSet.ParseFrom(inputStream, extensionRegistry));
                }
            }

            IList <FileDescriptor> descriptors = ConvertDescriptors(options.FileOptions, descriptorProtos.ToArray());

            // Combine with options from command line
            foreach (FileDescriptor descriptor in descriptors)
            {
                descriptor.ConfigureWithDefaultOptions(options.FileOptions);
            }

            bool duplicates = false;
            Dictionary <string, bool> names = new Dictionary <string, bool>(StringComparer.OrdinalIgnoreCase);

            foreach (FileDescriptor descriptor in descriptors)
            {
                string file = GetOutputFile(descriptor, false);
                if (names.ContainsKey(file))
                {
                    duplicates = true;
                    break;
                }
                names.Add(file, true);
            }

            foreach (FileDescriptor descriptor in descriptors)
            {
                // Optionally exclude descriptors in google.protobuf
                if (descriptor.CSharpOptions.IgnoreGoogleProtobuf && descriptor.Package == "google.protobuf")
                {
                    continue;
                }
                Generate(descriptor, duplicates);
            }
        }
Exemplo n.º 6
0
        public void Generate()
        {
            foreach (string inputFile in options.InputFiles)
            {
                FileDescriptorSet descriptorProtos;
                ExtensionRegistry extensionRegistry = ExtensionRegistry.CreateInstance();
                extensionRegistry.Add(CSharpOptions.CSharpFileOptions);
                extensionRegistry.Add(CSharpOptions.CSharpFieldOptions);
                using (Stream inputStream = File.OpenRead(inputFile)) {
                    descriptorProtos = FileDescriptorSet.ParseFrom(inputStream, extensionRegistry);
                }
                IList <FileDescriptor> descriptors = ConvertDescriptors(descriptorProtos);

                foreach (FileDescriptor descriptor in descriptors)
                {
                    Generate(descriptor);
                }
            }
        }