コード例 #1
0
        public static int Main(string[] args)
        {
            // Hack to make sure everything's initialized
            DescriptorProtoFile.Descriptor.ToString();
            ExtensionRegistry extensionRegistry = ExtensionRegistry.CreateInstance();

            CSharpOptions.RegisterAllExtensions(extensionRegistry);

            CodeGeneratorRequest request;
            var response = new CodeGeneratorResponse.Builder();

            try
            {
                using (var input = Console.OpenStandardInput())
                {
                    request = CodeGeneratorRequest.ParseFrom(input, extensionRegistry);
                }
                Run(request, response);
            }
            catch (Exception e)
            {
                response.Error += e.ToString();
            }

            using (var output = Console.OpenStandardOutput())
            {
                response.Build().WriteTo(output);
                output.Flush();
            }
            return(0);
        }
コード例 #2
0
        public static int Main(string[] args)
        {
            // Hack to make sure everything's initialized
            DescriptorProtoFile.Descriptor.ToString();
            ExtensionRegistry extensionRegistry = ExtensionRegistry.CreateInstance();
            CSharpOptions.RegisterAllExtensions(extensionRegistry);

            CodeGeneratorRequest request;
            var response = new CodeGeneratorResponse.Builder();
            try
            {
                using (var input = Console.OpenStandardInput())
                {
                    request = CodeGeneratorRequest.ParseFrom(input, extensionRegistry);
                }
                Run(request, response);
            }
            catch (Exception e)
            {
                response.Error += e.ToString();
            }

            using (var output = Console.OpenStandardOutput())
            {
                response.Build().WriteTo(output);
                output.Flush();
            }
            return 0;
        }
コード例 #3
0
        internal static void Run(CodeGeneratorRequest request, CodeGeneratorResponse.Builder response)
        {
            var arguments = new List <string>();

            foreach (var arg in request.Parameter.Split(' '))
            {
                var timmedArg = (arg ?? "").Trim();
                if (!string.IsNullOrEmpty(timmedArg))
                {
                    arguments.Add(timmedArg);
                }
            }
            // Adding fake input file to make TryValidate happy.
            arguments.Add(System.Reflection.Assembly.GetExecutingAssembly().Location);

            GeneratorOptions options = new GeneratorOptions
            {
                Arguments = arguments
            };
            IList <string> validationFailures;

            if (!options.TryValidate(out validationFailures))
            {
                response.Error += new InvalidOptionsException(validationFailures).Message;
                return;
            }

            Generator generator = Generator.CreateGenerator(options);

            generator.Generate(request, response);
        }
コード例 #4
0
        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);
            }
        }
コード例 #5
0
        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;
            }
        }
コード例 #6
0
        /// <summary>
        /// Generates code for a particular file. All dependencies must
        /// already have been resolved.
        /// </summary>
        private void Generate(FileDescriptor descriptor, bool duplicates, CodeGeneratorResponse.Builder response)
        {
            var code = new StringBuilder();
            var ucg  = new UmbrellaClassGenerator(descriptor);

            using (StringWriter textWriter = new StringWriter(code))
            {
                TextGenerator writer = new TextGenerator(textWriter, options.LineBreak);
                ucg.Generate(writer);
            }
            response.AddFile(new CodeGeneratorResponse.Types.File.Builder
            {
                Name    = GetOutputFile(descriptor, duplicates),
                Content = code.ToString(),
            }.Build());
        }
コード例 #7
0
        public void Generate(CodeGeneratorRequest request, CodeGeneratorResponse.Builder response)
        {
            IList <FileDescriptor> descriptors = ConvertDescriptors(options.FileOptions, request.ProtoFileList);

            // 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);
            }

            //ROK - Changed to dictionary from HashSet to allow 2.0 compile
            var filesToGenerate = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);

            foreach (var item in request.FileToGenerateList)
            {
                filesToGenerate[item] = null;
            }
            foreach (FileDescriptor descriptor in descriptors)
            {
                // Optionally exclude descriptors in google.protobuf
                if (descriptor.CSharpOptions.IgnoreGoogleProtobuf && descriptor.Package == "google.protobuf")
                {
                    continue;
                }
                if (filesToGenerate.ContainsKey(descriptor.Name))
                {
                    Generate(descriptor, duplicates, response);
                }
            }
        }