Пример #1
1
        static void GenProtocol(string infile, string outdir)
        {
            try
            {
                string text = System.IO.File.ReadAllText(infile);
                Protocol protocol = Protocol.Parse(text);

                CodeGen codegen = new CodeGen();
                codegen.AddProtocol(protocol);

                codegen.GenerateCode();
                codegen.WriteTypes(outdir);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception occurred. " + ex.Message);
            }
        }
Пример #2
0
        static int GenProtocol(string infile, string outdir,
                               IEnumerable <KeyValuePair <string, string> > namespaceMapping)
        {
            try
            {
                string   text     = System.IO.File.ReadAllText(infile);
                Protocol protocol = Protocol.Parse(text);

                CodeGen codegen = new CodeGen();
                codegen.AddProtocol(protocol);

                foreach (var entry in namespaceMapping)
                {
                    codegen.NamespaceMapping[entry.Key] = entry.Value;
                }

                codegen.GenerateCode();
                codegen.WriteTypes(outdir);
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine("Exception occurred. " + ex.Message);
                return(1);
            }

            return(0);
        }
Пример #3
0
        static void GenProtocol(string infile, string outdir)
        {
            try
            {
                string text     = System.IO.File.ReadAllText(infile);
                var    protocol = Protocol.Parse(text);

                var codegen = new CodeGen();
                codegen.AddProtocol(protocol);

                codegen.GenerateCode();
                codegen.WriteTypes(outdir);
            }
            catch (Exception ex)
            {
                throw new Exception(string.Concat("Exception occurred. ", ex.Message));
            }
        }
Пример #4
0
        static void GenProtocol(string infile, string outdir)
        {
            try
            {
                string   text     = System.IO.File.ReadAllText(infile);
                Protocol protocol = Protocol.Parse(text);

                CodeGen codegen = new CodeGen();
                codegen.AddProtocol(protocol);

                codegen.GenerateCode();
                codegen.WriteTypes(outdir);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception occurred. " + ex.Message);
            }
        }
Пример #5
0
        public static void TestSpecific(string str, object[] result)
        {
            Protocol protocol = Protocol.Parse(str);
            var codegen = new CodeGen();
            codegen.AddProtocol(protocol);
            var compileUnit = codegen.GenerateCode();

            // add a constructor to the main class using the passed assignment statements
            CodeTypeDeclaration ctd = compileUnit.Namespaces[0].Types[(int)result[0]];
            CodeConstructor constructor = new CodeConstructor();
            constructor.Attributes = MemberAttributes.Public;
            CodeSnippetExpression snippet = new CodeSnippetExpression((string)result[2]);
            constructor.Statements.Add(snippet);
            ctd.Members.Add(constructor);

            // add a function to the main class to populate the data
            // This has been moved from constructor, as it was causing some tests to pass that shouldn't when referencing a blank object on READ.

            CodeMemberMethod method = new CodeMemberMethod();
            method.Attributes = MemberAttributes.Public;
            method.Name = "Populate";
            CodeSnippetExpression snippet2 = new CodeSnippetExpression((string)result[3]);
            method.Statements.Add(snippet2);
            ctd.Members.Add(method);

            // compile
            var comparam = new CompilerParameters(new string[] { "mscorlib.dll" });
            comparam.ReferencedAssemblies.Add("System.dll");
            comparam.ReferencedAssemblies.Add("Avro.dll");
            comparam.GenerateInMemory = true;
            var ccp = new Microsoft.CSharp.CSharpCodeProvider();
            var units = new CodeCompileUnit[] { compileUnit };
            var compres = ccp.CompileAssemblyFromDom(comparam, units);
            if (compres == null || compres.Errors.Count > 0)
            {
                for (int i = 0; i < compres.Errors.Count; i++)
                    Console.WriteLine(compres.Errors[i]);
            }
            if (null != compres)
                Assert.IsTrue(compres.Errors.Count == 0);

            // create record
            ISpecificRecord rec = compres.CompiledAssembly.CreateInstance((string)result[1]) as ISpecificRecord;

            // Call populate to put some data in it.
            Type recType = rec.GetType(); ;
            MethodInfo methodInfo = recType.GetMethod("Populate");
            methodInfo.Invoke(rec, null);

            var x1 = compres.CompiledAssembly.FullName;

            Assert.IsFalse(rec == null);

            // serialize
            var stream = new MemoryStream();
            var binEncoder = new BinaryEncoder(stream);
            var writer = new SpecificDefaultWriter(rec.Schema);
            writer.Write(rec.Schema, rec, binEncoder);

            // deserialize
            stream.Position = 0;
            var decoder = new BinaryDecoder(stream);
            var reader = new SpecificDefaultReader(rec.Schema, rec.Schema);
            var rec2 = (ISpecificRecord)reader.Read(null, rec.Schema, rec.Schema, decoder);
            Assert.IsFalse(rec2 == null);
        }
Пример #6
0
 private void SaveOutProtocals()
 {
     CodeGen cg = new CodeGen();
     foreach (Protocol loaded in Protos.Values)
     {
         cg.AddProtocol(loaded);
         foreach (var s in loaded.Types)
         {
             cg.AddSchema(s);
         }
     }
     foreach (var s in Loaded.Values)
     {
         cg.AddSchema(s);
     }
     cg.GenerateCode();
     string at = @"..\sources\modules\RoboKindAvroQPIDModule\";
     Directory.CreateDirectory(at);
     cg.WriteTypes(at);
 }
Пример #7
0
        public static void TestSpecific(string str, object[] result)
        {
            Protocol protocol = Protocol.Parse(str);
            var codegen = new CodeGen();
            codegen.AddProtocol(protocol);
            var compileUnit = codegen.GenerateCode();

            // add a constructor to the main class using the passed assignment statements
            CodeTypeDeclaration ctd = compileUnit.Namespaces[0].Types[(int)result[0]];
            CodeConstructor constructor = new CodeConstructor();
            constructor.Attributes = MemberAttributes.Public;
            CodeSnippetExpression snippet = new CodeSnippetExpression((string)result[2]);
            constructor.Statements.Add(snippet);
            ctd.Members.Add(constructor);

            // compile
            var comparam = new CompilerParameters(new string[] { "mscorlib.dll" });
            comparam.ReferencedAssemblies.Add("System.dll");
            comparam.ReferencedAssemblies.Add("System.Core.dll");
            comparam.ReferencedAssemblies.Add(Type.GetType("Mono.Runtime") != null ? "Mono.CSharp.dll" : "Microsoft.CSharp.dll");
            comparam.ReferencedAssemblies.Add("Avro.dll");
            comparam.GenerateInMemory = true;
            var ccp = new Microsoft.CSharp.CSharpCodeProvider();
            var units = new CodeCompileUnit[] { compileUnit };
            var compres = ccp.CompileAssemblyFromDom(comparam, units);
            if (compres == null || compres.Errors.Count > 0)
            {
                for (int i = 0; i < compres.Errors.Count; i++)
                    Console.WriteLine(compres.Errors[i]);
            }
            if (null != compres)
                Assert.IsTrue(compres.Errors.Count == 0);

            // create record
            ISpecificRecord rec = compres.CompiledAssembly.CreateInstance((string)result[1]) as ISpecificRecord;
            Assert.IsFalse(rec == null);

            // serialize
            var stream = new MemoryStream();
            var binEncoder = new BinaryEncoder(stream);
            var writer = new SpecificDefaultWriter(rec.Schema);
            writer.Write(rec.Schema, rec, binEncoder);

            // deserialize
            stream.Position = 0;
            var decoder = new BinaryDecoder(stream);
            var reader = new SpecificDefaultReader(rec.Schema, rec.Schema);
            var rec2 = (ISpecificRecord)reader.Read(null, rec.Schema, rec.Schema, decoder);
            Assert.IsFalse(rec2 == null);
        }
Пример #8
0
        static int GenerateCode(string projectName, Options options)
        {
            var namespaceMapping = options.Namespace.Select(r => r.Split('.')).ToDictionary(k => k[0], v => v[1]);

            var codeGen    = new CodeGen(namespaceMapping);
            var codeWriter = new CodeWriter();

            var protocolHashes = new Dictionary <string, string>();
            var typeHashes     = new Dictionary <string, string>();

            if (options.Schema)
            {
                Log("Parsing Schema File(s) ...", options.Quiet);
                foreach (var schemaFile in GetFiles(options.Path, ".avsc"))
                {
                    Log($"    '{schemaFile.FullName}' ...", options.Quiet);
                    using (var reader = new StreamReader(schemaFile.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite), Encoding.UTF8))
                    {
                        var text   = reader.ReadToEnd();
                        var schema = AvroParser.ReadSchema(text, out var schemas);
                        codeGen.AddSchemas(schemas);
                        foreach (var s in schemas)
                        {
                            var hash = string.Join("", HASH_FUNC.ComputeHash(Encoding.UTF8.GetBytes(s.ToAvroCanonical())).Select(r => r.ToString("X2")));
                            if (typeHashes.TryGetValue(s.Name, out var existingHash))
                            {
                                typeHashes.Add(s.Name, hash);
                            }
                            else
                            if (hash != existingHash)
                            {
                                Log("           Hash mismatch:", options.Quiet);
                            }
                            Log($"        T: '{s.FullName}'", options.Quiet);
                        }
                    }
                }
            }

            if (options.Protocol)
            {
                Log("Parsing Schema File(s) ...", options.Quiet);
                foreach (var protocolFile in GetFiles(options.Path, ".avpr"))
                {
                    Log($"    '{protocolFile.FullName}' ...", options.Quiet);
                    using (var reader = new StreamReader(protocolFile.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite), Encoding.UTF8))
                    {
                        var text     = reader.ReadToEnd();
                        var protocol = AvroParser.ReadProtocol(text, out var schemas);
                        codeGen.AddProtocol(protocol);
                        Log($"        P: '{protocol.FullName}'", options.Quiet);
                        foreach (var s in schemas)
                        {
                            Log($"        T: '{s.FullName}'", options.Quiet);
                        }
                    }
                }
            }

            codeWriter.WriteProject(options.OutDir, codeGen, projectName);
            return(0);
        }