Esempio n. 1
0
        // TODO: cleanup stuff
        public static Task RunCode(ITestOutputHelper?logger, string protobufFile, string csharpScript, string? @namespace = null)
        {
            // parse protobuf code
            @namespace ??= string.Empty;
            var protoDescriptor = ParserHelpers.ParseText(protobufFile, @namespace);

            if (protoDescriptor == null)
            {
                throw new InvalidOperationException("Unable to parse protobuf file.");
            }

            var scope = "global::" + @namespace;

            if (@namespace.Length > 0)
            {
                scope += ".";
            }

            var model = protoDescriptor.From(@namespace, scope);

            var stringBuilder = new StringBuilder();

            using var writer = new IndentedTextWriter(new StringWriter(stringBuilder), "\t");
            var generator = new ProtobufGenerator.Struct(writer, @namespace);

            generator.Generate(model);

            // write the roslyn scripting code
            var roslynScript = new StringBuilder();

            roslynScript.AppendLine();
            roslynScript.AppendLine("public void TestCode()");
            roslynScript.AppendLine("{");

            foreach (var line in csharpScript.Split('\n'))
            {
                roslynScript.AppendLine("\t" + line.Trim());
            }

            roslynScript.AppendLine("}");
            roslynScript.AppendLine();
            roslynScript.AppendLine("TestCode()");

            var generatedCode = stringBuilder.ToString();

            logger?.WriteLine("{0}", generatedCode);

            var roslynCode = roslynScript.ToString();

            logger?.WriteLine("{0}", roslynCode);

            return(CSharpLanguageService.Evaluate(logger, generatedCode, roslynCode, @namespace));
        }