CreateGenerator() public static method

Returns a generator configured with the specified options.
public static CreateGenerator ( GeneratorOptions options ) : Generator
options GeneratorOptions
return Generator
Beispiel #1
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);
                }

                Generator generator = Generator.CreateGenerator(options);
                generator.Generate();
                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);
            }
        }
Beispiel #2
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);
        }
        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);
            }
        }