Exemple #1
0
        public static int Main(string[] args)
        {
            var options = Options.Parse(args);

            if (options == null)
            {
                return(-1);
            }

            ProtoCollection collection = new ProtoCollection();

            foreach (string protoPath in options.InputProto)
            {
                try
                {
                    Console.WriteLine("Parsing " + protoPath);

                    var proto = new ProtoCollection();
                    ProtoParser.Parse(protoPath, proto);
                    collection.Merge(proto);
                }
                catch (ProtoFormatException pfe)
                {
                    Console.WriteLine();
                    Console.WriteLine(pfe.SourcePath.Path + "(" + pfe.SourcePath.Line + "," + pfe.SourcePath.Column + "): error CS001: " + pfe.Message);
                    return(-1);
                }
            }

            Console.WriteLine(collection);

            //Interpret and reformat
            try
            {
                var pp = new ProtoPrepare(options);
                pp.Prepare(collection);
            }
            catch (ProtoFormatException pfe)
            {
                Console.WriteLine();
                Console.WriteLine(pfe.SourcePath.Path + "(" + pfe.SourcePath.Line + "," + pfe.SourcePath.Column + "): error CS001: " + pfe.Message);
                return(-1);
            }

            //Generate code
            ProtoCode.Save(collection, options);
            Console.WriteLine("Saved: " + options.OutputPath);
            return(0);
        }
Exemple #2
0
        public static void Build(Options options)
        {
            ProtoCollection collection = new ProtoCollection();

            foreach (string protoPath in options.InputProto)
            {
                try
                {
                    var proto = new ProtoCollection();
                    ProtoParser.Parse(protoPath, proto);
                    collection.Merge(proto);
                }
                catch (ProtoFormatException pfe)
                {
                    Console.WriteLine();
                    Console.WriteLine(pfe.SourcePath.Path + "(" + pfe.SourcePath.Line + "," + pfe.SourcePath.Column + "): error CS001: " + pfe.Message);
                    throw;
                }
            }

            Console.WriteLine(collection);

            //Interpret and reformat
            try
            {
                var pp = new ProtoPrepare(options);
                pp.Prepare(collection);
            }
            catch (ProtoFormatException pfe)
            {
                Console.WriteLine();
                Console.WriteLine(pfe.SourcePath.Path + "(" + pfe.SourcePath.Line + "," + pfe.SourcePath.Column + "): error CS001: " + pfe.Message);
                throw;
            }

            //Generate code
            ProtoCode.Save(collection, options);
            Console.WriteLine("Saved: " + options.OutputPath);
        }
Exemple #3
0
        public static void Build(Options options)
        {
            var parser     = new FileParser();
            var collection = parser.Import(options.InputProto);

            Console.WriteLine(collection);

            //Interpret and reformat
            try
            {
                var pp = new ProtoPrepare(options);
                pp.Prepare(collection);
            }
            catch (ProtoFormatException pfe)
            {
                Console.WriteLine();
                Console.WriteLine(pfe.SourcePath.Path + "(" + pfe.SourcePath.Line + "," + pfe.SourcePath.Column + "): error CS001: " + pfe.Message);
                throw;
            }

            //Generate code
            ProtoCode.Save(collection, options);
            Console.WriteLine("Saved: " + options.OutputPath);
        }
Exemple #4
0
        public static int Main(string[] args)
        {
            if (args.Length == 0)
            {
                Console.Error.WriteLine("Usage:\n\tCodeGenerator.exe [--preserve-names] [--use-tabs] path-to.proto [path-to-second.proto [...]] [output.cs]");
                return(-1);
            }

            ProtoCollection collection = new ProtoCollection();
            string          outputPath = null;

            int argIndex = 0;

            while (args.Length > argIndex && args [argIndex].StartsWith("--"))
            {
                switch (args[argIndex])
                {
                case "--preserve-names":
                    ProtoPrepare.ConvertToCamelCase = false;
                    break;

                case ProtoPrepare.FixNameclashArgument:
                    ProtoPrepare.FixNameclash = true;
                    break;

                case "--use-tabs":
                    CodeWriter.IndentPrefix = "\t";
                    break;

                default:
                    Console.Error.WriteLine("Unknown option: " + args[argIndex]);
                    return(-1);
                }
                argIndex++;
            }

            while (argIndex < args.Length)
            {
                string protoPath = Path.GetFullPath(args[argIndex]);
                string protoBase = Path.Combine(
                    Path.GetDirectoryName(protoPath),
                    Path.GetFileNameWithoutExtension(protoPath));
                argIndex += 1;

                //First .proto filename is used as output unless specified later
                if (outputPath == null)
                {
                    outputPath = protoBase + ".cs";
                }

                //Handle last argument as the output .cs path
                if (argIndex == args.Length && protoPath.EndsWith(".cs"))
                {
                    outputPath = protoPath;
                    break;
                }

                //Handle last argument as the output path
                //Filename is taken from first .proto argument
                if (argIndex == args.Length && protoPath.EndsWith(Path.DirectorySeparatorChar.ToString()))
                {
                    Directory.CreateDirectory(protoPath);

                    // Replace the original output directory with the custom one
                    outputPath = Path.Combine(protoPath, Path.GetFileName(outputPath));
                    break;
                }

                if (File.Exists(protoPath) == false)
                {
                    Console.Error.WriteLine("File not found: " + protoPath);
                    return(-1);
                }

                try
                {
                    var proto = new ProtoCollection();

                    //Parse .proto
                    Console.WriteLine("Parsing " + protoPath);
                    ProtoParser.Parse(protoPath, proto);

                    collection.Merge(proto);
                } catch (ProtoFormatException pfe)
                {
                    Console.WriteLine();
                    Console.WriteLine(pfe.SourcePath.Path + "(" + pfe.SourcePath.Line + "," + pfe.SourcePath.Column + "): error CS001: " + pfe.Message);
                    return(-1);
                }
            }

            Console.WriteLine(collection);

            //Interpret and reformat
            try
            {
                ProtoPrepare.Prepare(collection);
            } catch (ProtoFormatException pfe)
            {
                Console.WriteLine();
                Console.WriteLine(pfe.SourcePath.Path + "(" + pfe.SourcePath.Line + "," + pfe.SourcePath.Column + "): error CS001: " + pfe.Message);
                return(-1);
            }

            //Generate code
            ProtoCode.Save(collection, outputPath);
            Console.WriteLine("Saved: " + outputPath);
            return(0);
        }
Exemple #5
0
        public static void Build(Options options)
        {
            var parser     = new FileParser();
            var collection = parser.Import(options.InputProto, options.FixComments);

            Console.WriteLine(collection);

            //Interpret and reformat
            try
            {
                var pp = new ProtoPrepare(options);
                pp.Prepare(collection);
            }
            catch (ProtoFormatException pfe)
            {
                Console.WriteLine();
                Console.WriteLine(pfe.SourcePath.Path + "(" + pfe.SourcePath.Line + "," + pfe.SourcePath.Column + "): error CS001: " + pfe.Message);
                throw;
            }


            if (options.TranslateComments)
            {
                var translator = new GoogleTranslator();
                if (!string.IsNullOrWhiteSpace(collection.Comments))
                {
                    collection.Comments = translator.TranslateAsync(collection.Comments, options.FromLanguage, options.ToLanguage).GetAwaiter().GetResult().MergedTranslation;
                    //Console.WriteLine(collection.Comments);
                }

                foreach (var message in collection.Messages)
                {
                    message.Value.Comments = translator.TranslateAsync(message.Value.Comments, options.FromLanguage, options.ToLanguage).GetAwaiter().GetResult().MergedTranslation;
                    Console.WriteLine(message.Value.Comments);
                    foreach (var valueField in message.Value.Fields)
                    {
                        valueField.Value.Comments = translator.TranslateAsync(valueField.Value.Comments, options.FromLanguage, options.ToLanguage).GetAwaiter().GetResult().MergedTranslation;
                        Console.WriteLine(valueField.Value.Comments);
                    }
                }
            }



            //Generate code
            if (options.OneClassOneFile)
            {
                var classes = collection.Messages;
                var path    = options.OutputPath;
                foreach (var protoMessage in classes)
                {
                    collection.Messages = new Dictionary <string, ProtoMessage>()
                    {
                        { protoMessage.Key, protoMessage.Value }
                    };
                    options.OutputPath = Path.Combine(path, protoMessage.Value.CsType + ".cs");
                    ProtoCode.Save(collection, options);
                }
            }
            else
            {
                ProtoCode.Save(collection, options);
                Console.WriteLine("Saved: " + options.OutputPath);
            }
        }