Example #1
0
        static void Main(string[] args)
        {
            try
            {
                ConsoleCmdLine c         = new ConsoleCmdLine();
                CmdLineString  srcDir    = new CmdLineString("src", true, "源文件目录");
                CmdLineString  csharpDir = new CmdLineString("csharp", false, "生成C#文件的目录");
                CmdLineString  cppDir    = new CmdLineString("cpp", false, "生成C++文件的目录");
                CmdLineString  nameSpace = new CmdLineString("ns", false, "命名空间");
                c.RegisterParameter(srcDir);
                c.RegisterParameter(csharpDir);
                c.RegisterParameter(cppDir);
                c.RegisterParameter(nameSpace);
                c.Parse(args);
                ProtoResult result = Builder.BuildProto(srcDir);
                if (csharpDir.Exists)
                {
                    Builder.GenCSharpFile(result, CodeGenHelper.InputDirHandle(csharpDir), nameSpace);
                }
                if (cppDir.Exists)
                {
                    Builder.GenCppFile(result, CodeGenHelper.InputDirHandle(cppDir), nameSpace);
                }
            }
            catch (System.Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.DarkRed;
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
                Console.ResetColor();

                Console.WriteLine("Please press any key to continue ... ");
                Console.ReadKey();
            }
        }
Example #2
0
        public static ProtoResult BuildProto(string srcDir)
        {
            ProtoResult result = new ProtoResult();
            var         files  = Directory.GetFiles(srcDir, "*.sdp", SearchOption.AllDirectories);

            foreach (var file in files)
            {
                ProtoParser.ParseFile(file, result);
            }
            ProtoCheck.Check(result);
            return(result);
        }
Example #3
0
        public static void GenCSharpFile(ProtoResult result, string outDir, string nameSpace)
        {
            if (!Directory.Exists(outDir))
            {
                Directory.CreateDirectory(outDir);
            }

            foreach (var entity in result.Enums)
            {
                string saveFile = Path.Combine(outDir, ProtoToCSharp.TypeToFileName(entity.Name.Value, nameSpace));
                string code     = ProtoToCSharp.EunmToFile(entity, nameSpace);
                CodeGenHelper.WriteFile(saveFile, code);
            }

            foreach (var entity in result.Structs)
            {
                string saveFile = Path.Combine(outDir, ProtoToCSharp.TypeToFileName(entity.Name.Value, nameSpace));
                string code     = ProtoToCSharp.StructToFile(entity, nameSpace);
                CodeGenHelper.WriteFile(saveFile, code);
            }
        }