예제 #1
0
        static (Model.SchemaModel, CodeGen.CodeGenerator, string) LoadAndGenerate(string inputName)
        {
            var model   = Load(inputName);
            var codegen = new CodeGen.CodeGenerator(model, new CodeGen.GeneratorOptions());

            var code = model.FilesToGenerate.Select(f => codegen.Transform(f)).ToArray();

            Assert.AreEqual(
                Util.InlineAssemblyCompiler.CompileSummary.Success,
                Util.InlineAssemblyCompiler.TryCompileCapnp(
                    Microsoft.CodeAnalysis.NullableContextOptions.Disable,
                    code),
                "Compilation was not successful with no warnings");

            return(model, codegen, code[0]);
        /// <summary>
        /// Generates C# code from given input stream
        /// </summary>
        /// <param name="input">input stream containing the binary code generation request, which the frontend capnpc emits</param>
        /// <returns>generation result</returns>
        /// <exception cref="ArgumentNullException">if <paramref name="input"/> is null</exception>
        public static GenerationResult GenerateFromStream(Stream input)
        {
            if (input == null)
            {
                throw new ArgumentNullException(nameof(input));
            }

            try
            {
                var segments = Framing.ReadSegments(input);
                var dec      = DeserializerState.CreateRoot(segments);
                var reader   = Schema.CodeGeneratorRequest.Reader.Create(dec);
                var model    = Model.SchemaModel.Create(reader);
                var codeGen  = new CodeGen.CodeGenerator(model, new CodeGen.GeneratorOptions());
                return(new GenerationResult(codeGen.Generate()));
            }
            catch (Exception exception)
            {
                return(new GenerationResult(exception));
            }
        }
        static (Model.SchemaModel, CodeGen.CodeGenerator, string) LoadAndGenerate(params string[] inputNames)
        {
            Model.SchemaModel     firstModel   = null;
            CodeGen.CodeGenerator firstCodeGen = null;

            Model.SchemaModel LocalLoad(string name)
            {
                var model = Load(name);

                firstModel = firstModel ?? model;
                return(model);
            }

            CodeGen.CodeGenerator CreateCodeGen(Model.SchemaModel model)
            {
                var codegen = new CodeGen.CodeGenerator(model, new CodeGen.GeneratorOptions());

                firstCodeGen = firstCodeGen ?? codegen;
                return(codegen);
            }

            var codes = (
                from name in inputNames
                let model = LocalLoad(name)
                            let codegen = CreateCodeGen(model)
                                          from file in model.FilesToGenerate
                                          select codegen.Transform(file)).ToArray();

            Assert.AreEqual(
                Util.InlineAssemblyCompiler.CompileSummary.Success,
                Util.InlineAssemblyCompiler.TryCompileCapnp(
                    Microsoft.CodeAnalysis.NullableContextOptions.Disable,
                    codes),
                "Compilation was not successful with no warnings");

            return(firstModel, firstCodeGen, codes[0]);