Ejemplo n.º 1
0
        public static string GenerateDocumentStorageCode(DocumentMapping[] mappings)
        {
            var writer = new SourceWriter();

            // TODO -- get rid of the magic strings
            var namespaces = new List <string> {
                "System", "Marten", "Marten.Schema", "Marten.Linq", "Marten.Util", "Npgsql", "Remotion.Linq"
            };

            namespaces.AddRange(mappings.Select(x => x.DocumentType.Namespace));

            namespaces.Distinct().OrderBy(x => x).Each(x => writer.WriteLine($"using {x};"));
            writer.BlankLine();

            writer.StartNamespace("Marten.GeneratedCode");

            mappings.Each(x =>
            {
                x.GenerateDocumentStorage(writer);
                writer.BlankLine();
                writer.BlankLine();
            });

            writer.FinishBlock();

            var code = writer.Code();

            return(code);
        }
Ejemplo n.º 2
0
        public void write_code()
        {
            #region sample_simple-usage-of-source-writer
            var writer = new SourceWriter();

            writer.Write(@"
BLOCK:public void SayHello()
Console.WriteLine('Hello');
END
".Replace("'", "\""));

            Console.WriteLine(writer.Code());
            #endregion

            _output.WriteLine(writer.Code());
        }
Ejemplo n.º 3
0
        public static string GenerateDocumentStorageCode(DocumentMapping[] mappings)
        {
            var writer = new SourceWriter();

            // TODO -- get rid of the magic strings
            var namespaces = new List<string> {"System", "Marten", "Marten.Schema", "Marten.Linq", "Marten.Util", "Npgsql", "Remotion.Linq"};
            namespaces.AddRange(mappings.Select(x => x.DocumentType.Namespace));

            namespaces.Distinct().OrderBy(x => x).Each(x => writer.WriteLine($"using {x};"));
            writer.BlankLine();

            writer.StartNamespace("Marten.GeneratedCode");

            mappings.Each(x =>
            {
                x.GenerateDocumentStorage(writer);
                writer.BlankLine();
                writer.BlankLine();
            });

            writer.FinishBlock();

            var code = writer.Code();
            return code;
        }
Ejemplo n.º 4
0
        public void write_code()
        {
            // SAMPLE: simple-usage-of-source-writer
            var writer = new SourceWriter();

            writer.Write(@"
BLOCK:public void SayHello()
Console.WriteLine('Hello');
END
".Replace("'", "\""));

            Console.WriteLine(writer.Code());
            // ENDSAMPLE

            _output.WriteLine(writer.Code());
        }
Ejemplo n.º 5
0
        public void write_using_by_type()
        {
            var writer = new SourceWriter();

            writer.UsingNamespace <ISourceWriter>();
            var lines = writer.Code().ReadLines().ToArray();

            lines[0].Should().Be($"using {typeof(ISourceWriter).Namespace};");
        }
Ejemplo n.º 6
0
        public void write_block()
        {
            var writer = new SourceWriter();

            writer.Write("BLOCK:public void Go()");

            var lines = writer.Code().ReadLines().ToArray();

            lines[0].ShouldBe("public void Go()");
            lines[1].ShouldBe("{");
        }
Ejemplo n.º 7
0
        public void indention_within_a_block()
        {
            var writer = new SourceWriter();

            writer.Write("BLOCK:public void Go()");
            writer.Write("var x = 0;");

            var lines = writer.Code().ReadLines().ToArray();

            lines[2].ShouldBe("    var x = 0;");
        }
Ejemplo n.º 8
0
        public void write_comment()
        {
            var writer = new SourceWriter();

            writer.Block("public void Go()");
            writer.Comment("Some Comment");

            var lines = writer.Code().ReadLines().ToArray();

            lines.Last().Should().Be("    // Some Comment");
        }
Ejemplo n.º 9
0
        public void multi_level_indention()
        {
            var writer = new SourceWriter();

            writer.Write("BLOCK:public void Go()");
            writer.Write("BLOCK:try");
            writer.Write("var x = 0;");

            var lines = writer.Code().ReadLines().ToArray();

            lines[4].ShouldBe("        var x = 0;");
        }
Ejemplo n.º 10
0
        public void tryit()
        {
            var mapping = new DocumentMapping(typeof(User));
            var writer = new SourceWriter();
            writer.StartNamespace("MyApplication");

            mapping.GenerateDocumentStorage(writer);

            writer.FinishBlock();

            Debug.WriteLine(writer.Code());
        }
Ejemplo n.º 11
0
        public void end_block()
        {
            var writer = new SourceWriter();

            writer.Write("BLOCK:public void Go()");
            writer.Write("var x = 0;");
            writer.Write("END");

            var lines = writer.Code().ReadLines().ToArray();

            lines[3].ShouldBe("}");
        }
Ejemplo n.º 12
0
        public void end_block()
        {
            var writer = new SourceWriter();

            writer.Block("public void Go()");
            writer.WriteLine("var x = 0;");
            writer.FinishBlock();

            var lines = writer.Code().ReadLines().ToArray();

            lines[3].Should().Be("}");
        }
Ejemplo n.º 13
0
        public void writes_the_closing_bracket()
        {
            var writer = new SourceWriter();
            var style  = IfStyle.If;

            style.Open(writer, "true");
            style.Close(writer);
            var lines = writer.Code().ReadLines().ToArray();

            lines[2].ShouldBe("}");
            writer.IndentionLevel.ShouldBe(0);
        }
Ejemplo n.º 14
0
        public override async Task <bool> Execute(GenerateInput input)
        {
            var settings = new CSharpGeneratorSettings
            {
                ClassStyle = CSharpClassStyle.Poco,
                Namespace  = input.NamespaceFlag,
            };

            var codeDirectory = input.OutputDirectory.ToFullPath();


            var system = new FileSystem();

            if (!system.DirectoryExists(codeDirectory))
            {
                Console.WriteLine("Creating directory " + codeDirectory);
                system.CreateDirectory(codeDirectory);
            }

            var files    = system.FindFiles(input.SchemaDirectory, FileSet.Shallow("*.json"));
            var messages = files.Select(x => new GeneratedMessage(x)).ToArray();

            var writer = new SourceWriter();

            writer.Namespace(input.NamespaceFlag);

            foreach (var message in messages)
            {
                var schema = await JsonSchema4.FromFileAsync(message.FilePath);


                var generator = new CSharpGenerator(schema, settings);
                var contents  = generator.GenerateFile(message.ClassName);

                var codeFile = codeDirectory.AppendPath(message.ClassName + ".cs");
                Console.WriteLine(codeFile);
                system.WriteStringToFile(codeFile, contents);

                message.WriteAnnotations(writer);
            }

            writer.FinishBlock();

            var annotationFile = codeDirectory.AppendPath("MessageAnnotations.cs");

            Console.WriteLine("Writing attribute annotations to " + annotationFile);
            system.WriteStringToFile(annotationFile, writer.Code());



            return(true);
        }
Ejemplo n.º 15
0
        public void tryit()
        {
            var mapping = new DocumentMapping(typeof(User));
            var writer  = new SourceWriter();

            writer.StartNamespace("MyApplication");

            mapping.GenerateDocumentStorage(writer);

            writer.FinishBlock();

            Debug.WriteLine(writer.Code());
        }
Ejemplo n.º 16
0
        public void open_and_close_as_elseif()
        {
            var writer = new SourceWriter();
            var style  = IfStyle.ElseIf;

            style.Open(writer, "true");
            var lines = writer.Code().ReadLines().ToArray();

            lines[0].ShouldBe("else if (true)");
            lines[1].ShouldBe("{");

            writer.IndentionLevel.ShouldBe(1);
        }
Ejemplo n.º 17
0
        protected string[] WriteMethod(string methodName, Action <MethodCall> configure = null)
        {
            var @call = new MethodCall(typeof(MethodTarget), methodName);

            @call.Target = Variable.For <MethodTarget>("target");
            configure?.Invoke(@call);

            var writer = new SourceWriter();

            @call.GenerateCode(theMethod, writer);

            return(writer.Code().ReadLines().ToArray());
        }
Ejemplo n.º 18
0
        protected string[] WriteMethod(Expression <Action <MethodTarget> > expression, Action <MethodCall> configure = null)
        {
            var @call = MethodCall.For(expression);

            @call.Target = Variable.For <MethodTarget>("target");
            configure?.Invoke(@call);

            var writer = new SourceWriter();

            @call.GenerateCode(theMethod, writer);

            return(writer.Code().ReadLines().ToArray());
        }
Ejemplo n.º 19
0
        public void generate_code_with_output_variables()
        {
            var @call = new MethodCall(typeof(MethodCallTarget), nameof(MethodCallTarget.ReturnAndOuts));

            @call.Arguments[0] = new Variable(typeof(string), "input");
            @call.IsLocal      = true;

            var writer = new SourceWriter();

            @call.GenerateCode(new GeneratedMethod("Go", typeof(void)), writer);

            writer.Code().Trim().ShouldBe("var result_of_ReturnAndOuts = ReturnAndOuts(input, out var string, out var int32);");
        }
Ejemplo n.º 20
0
        public void use_generic_argument_with_inner()
        {
            var @call = MethodCall.For <Loader>(x => x.Load <MyInnerClass>("foo"));

            @call.Target       = Variable.For <Loader>("loader");
            @call.Arguments[0] = Variable.For <string>("x");

            var writer = new SourceWriter();

            @call.GenerateCode(theMethod, writer);

            var code = writer.Code().ReadLines().ToArray();

            code[0].ShouldBe("var myInnerClass = loader.Load<LamarCompiler.Testing.Codegen.MethodCall_generate_code.MyInnerClass>(x);");
        }
Ejemplo n.º 21
0
        public void use_generic_argument()
        {
            var @call = MethodCall.For <Loader>(x => x.Load <string>("foo"));

            @call.Target       = Variable.For <Loader>("loader");
            @call.Arguments[0] = Variable.For <string>("x");

            var writer = new SourceWriter();

            @call.GenerateCode(theMethod, writer);

            var code = writer.Code().ReadLines().ToArray();

            code[0].ShouldBe("var result_of_Load = loader.Load<string>(x);");
        }
Ejemplo n.º 22
0
        public void write_comment_text_if_exists()
        {
            var @call = new MethodCall(typeof(MethodCallTarget), nameof(MethodCallTarget.ReturnAndOuts));

            @call.Arguments[0] = new Variable(typeof(string), "input");
            @call.IsLocal      = true;

            @call.CommentText = "Hey.";

            var writer = new SourceWriter();

            @call.GenerateCode(new GeneratedMethod("Go", typeof(void)), writer);

            writer.Code().Trim().ShouldStartWith("// Hey.");
        }
Ejemplo n.º 23
0
        public void write_else()
        {
            var writer = new SourceWriter();

            writer.Write(@"
BLOCK:public void Go()
var x = 0;
");

            writer.WriteElse();
            var lines = writer.Code().Trim().ReadLines().ToArray();


            lines[3].ShouldBe("    else");
            lines[4].ShouldBe("    {");
        }
Ejemplo n.º 24
0
        public void multi_end_blocks()
        {
            var writer = new SourceWriter();

            writer.Write("BLOCK:public void Go()");
            writer.Write("BLOCK:try");
            writer.Write("var x = 0;");
            writer.Write("END");
            writer.Write("END");

            var lines = writer.Code().ReadLines().ToArray();

            lines[5].ShouldBe("    }");

            // There's a line break between the blocks
            lines[7].ShouldBe("}");
        }
Ejemplo n.º 25
0
        public void write_several_lines()
        {
            var writer = new SourceWriter();

            writer.Write(@"
BLOCK:public void Go()
var x = 0;
END
");

            var lines = writer.Code().Trim().ReadLines().ToArray();

            lines[0].ShouldBe("public void Go()");
            lines[1].ShouldBe("{");
            lines[2].ShouldBe("    var x = 0;");
            lines[3].ShouldBe("}");
        }
Ejemplo n.º 26
0
        public string GenerateCode(GenerationRules generation)
        {
            beforeGeneratingCode();

            var classes    = chains.Select(x => x.ToClass(generation)).ToArray();
            var namespaces = classes
                             .SelectMany(x => x.Args())
                             .Select(x => x.ArgType.Namespace)
                             .Concat(new string[] { typeof(Task).Namespace })
                             .Distinct().ToList();

            var writer = new SourceWriter();

            foreach (var ns in namespaces.OrderBy(x => x))
            {
                writer.Write($"using {ns};");
            }

            writer.BlankLine();

            writer.Namespace(generation.ApplicationNamespace);

            foreach (var @class in classes)
            {
                writer.WriteLine($"// START: {@class.ClassName}");
                @class.Write(writer);
                writer.WriteLine($"// END: {@class.ClassName}");

                writer.WriteLine("");
                writer.WriteLine("");
            }

            writer.FinishBlock();


            var code = writer.Code();

            attachSourceCodeToChains(code);


            return(code);
        }
Ejemplo n.º 27
0
        private string[] GenerateMethodBody(Expression <Action <MethodTarget> > expression, Action <MethodCall> configure = null)
        {
            var @call = MethodCall.For(expression);

            @call.Target = Variable.For <MethodTarget>("target");
            configure?.Invoke(@call);

            var writer = new SourceWriter();

            theMethod.Frames.Clear();
            theMethod.Frames.Add(@call);
            theMethod.WriteMethod(writer);

            var allLines = writer.Code().ReadLines().ToArray();

            // Skip method declaration and {
            // Do not take first 2 lines, nor closing }
            return(allLines.Skip(2)
                   .Take(allLines.Length - 3)
                   .Select(l => l.Trim())
                   .ToArray());
        }
Ejemplo n.º 28
0
        public static string GenerateDocumentStorageCode(IDocumentMapping[] mappings)
        {
            var writer = new SourceWriter();

            // TODO -- get rid of the magic strings
            var namespaces = new List <string>
            {
                "System",
                "Marten",
                "Marten.Schema",
                "Marten.Services",
                "Marten.Linq",
                "Marten.Util",
                "Npgsql",
                "Remotion.Linq",
                typeof(NpgsqlDbType).Namespace,
                typeof(IEnumerable <>).Namespace,
                typeof(DbDataReader).Namespace,
                typeof(CancellationToken).Namespace,
                typeof(Task).Namespace
            };

            namespaces.AddRange(mappings.Select(x => x.DocumentType.Namespace));

            namespaces.Distinct().OrderBy(x => x).Each(x => writer.WriteLine($"using {x};"));
            writer.BlankLine();

            writer.StartNamespace("Marten.GeneratedCode");

            mappings.Each(x =>
            {
                GenerateDocumentStorage(x, writer);
                writer.BlankLine();
                writer.BlankLine();
            });

            writer.FinishBlock();
            return(writer.Code());
        }
Ejemplo n.º 29
0
        public string GenerateCode(IGenerationConfig generation)
        {
            beforeGeneratingCode();

            var writer = new SourceWriter();

            writer.UsingNamespace <Task>();
            writer.BlankLine();

            writer.Namespace(generation.ApplicationNamespace);

            foreach (var chain in chains)
            {
                var generationModel = chain.ToGenerationModel(generation);


                // TODO -- figure out how to get the source code for each handler
                writer.WriteLine($"// START: {chain.TypeName}");

                HandlerSourceWriter.Write(generationModel, writer);
                writer.WriteLine($"// END: {chain.TypeName}");

                writer.WriteLine("");
                writer.WriteLine("");
            }

            writer.FinishBlock();


            var code = writer.Code();

            attachSourceCodeToChains(code);


            return(code);
        }
Ejemplo n.º 30
0
        public static string GenerateDocumentStorageCode(DocumentMapping[] mappings)
        {
            var writer = new SourceWriter();

            // TODO -- get rid of the magic strings
            var namespaces = new List<string>
            {
                "System",
                "Marten",
                "Marten.Schema",
                "Marten.Services",
                "Marten.Linq",
                "Marten.Util",
                "Npgsql",
                "Remotion.Linq",
                typeof (NpgsqlDbType).Namespace,
                typeof (IEnumerable<>).Namespace,
                typeof(DbDataReader).Namespace,
                typeof(CancellationToken).Namespace,
                typeof(Task).Namespace
            };
            namespaces.AddRange(mappings.Select(x => x.DocumentType.Namespace));

            namespaces.Distinct().OrderBy(x => x).Each(x => writer.WriteLine($"using {x};"));
            writer.BlankLine();

            writer.StartNamespace("Marten.GeneratedCode");

            mappings.Each(x =>
            {
                GenerateDocumentStorage(x, writer);
                writer.BlankLine();
                writer.BlankLine();
            });

            writer.FinishBlock();
            return writer.Code();
        }