public void should_generate_generic_constraints()
        {
            var contract = new ParsedContracts();

            contract.Messages.Add(new MessageDefinition
            {
                Name = "FooExecuted",
                GenericParameters  = { "TFoo", "TBar" },
                GenericConstraints =
                {
                    new GenericConstraint
                    {
                        GenericParameterName = "TFoo",
                        IsClass = true,
                        HasDefaultConstructor = true,
                        Types ={ "IDisposable"             }
                    },
                    new GenericConstraint
                    {
                        GenericParameterName  = "TBar",
                        IsStruct              = true,
                        HasDefaultConstructor = true,
                        Types ={ "IDisposable"             }
                    },
                }
            });
            var code = Generate(contract);

            code.ShouldContain("where TFoo : class, IDisposable, new()");
            code.ShouldContain("where TBar : struct, IDisposable");
        }
예제 #2
0
        public static string DumpParseTree(ParsedContracts contracts)
        {
            var writer = new IndentedTextWriter(new StringWriter());

            DumpParseTree(contracts.ParseTree, writer);
            return(writer.InnerWriter.ToString() ?? string.Empty);
        }
예제 #3
0
        protected string Generate(MessageDefinition message)
        {
            var contracts = new ParsedContracts();

            contracts.Messages.Add(message);
            return(Generate(contracts));
        }
예제 #4
0
        private static void ShouldContainError(ParsedContracts contracts, string expectedMessage)
        {
            var containsError = contracts.Errors.Any(err => err.Message.IndexOf(expectedMessage, StringComparison.OrdinalIgnoreCase) >= 0);

            if (!containsError)
            {
                Assert.Fail($"Expected error: {expectedMessage}");
            }
        }
        private void GenerateCSharpOutput(ITaskItem inputFile, ParsedContracts contracts)
        {
            var targetPath = GetValidTargetFilePath(inputFile);

            var output = CSharpGenerator.Generate(contracts);

            File.WriteAllText(targetPath, output);

            LogDebug($"{inputFile.ItemSpec}: Translated {contracts.Messages.Count} message{(contracts.Messages.Count > 1 ? "s" : "")}");
        }
예제 #6
0
        private static ParsedContracts Parse(string definitionText)
        {
            Console.WriteLine("PARSE: {0}", definitionText);
            var contracts = ParsedContracts.Parse(definitionText, "Some.Namespace");

            foreach (var error in contracts.Errors)
            {
                Console.WriteLine("ERROR: {0}", error);
            }

            return(contracts);
        }
        public void should_generate_generic_messages()
        {
            var contract = new ParsedContracts();

            contract.Messages.Add(new MessageDefinition
            {
                Name = "FooExecuted",
                GenericParameters = { "TFoo", "TBar" }
            });
            var code = Generate(contract);

            code.ShouldContain("class FooExecuted<TFoo, TBar> : IEvent");
        }
        private void GenerateProtoOutput(ITaskItem inputFile, ParsedContracts contracts)
        {
            if (!ProtoGenerator.HasProtoOutput(contracts))
            {
                return;
            }

            var targetPath = Path.ChangeExtension(GetValidTargetFilePath(inputFile), "proto") ?? throw new InvalidOperationException("Invalid target path");

            var output = ProtoGenerator.Generate(contracts);

            File.WriteAllText(targetPath, output);

            LogDebug($"{inputFile.ItemSpec}: Generated proto file");
        }
            public string GetValidatedId(ParsedContracts contracts)
            {
                var id = nameId?.Text ?? nameCtxKw?.GetText() ?? nameKw?.GetText();

                if (string.IsNullOrEmpty(id))
                {
                    return(string.Empty);
                }

                if (CSharpSyntax.IsCSharpKeyword(id) && escape == null)
                {
                    contracts.AddError(this, "'{0}' is a C# keyword and has to be escaped with '@'", id);
                }

                return(id);
            }
        public void should_generate_properties()
        {
            var contract = new ParsedContracts();

            contract.Messages.Add(new MessageDefinition
            {
                Name       = "FooExecuted",
                Parameters =
                {
                    new ParameterDefinition("System.Int32", "foo"),
                    new ParameterDefinition("string",       "bar")
                }
            });
            var code = Generate(contract);

            code.ShouldContain("public int Foo { get; private set; }");
        }
예제 #11
0
        protected string Generate(ParsedContracts contracts)
        {
            if (!contracts.ImportedNamespaces.Contains("System"))
            {
                contracts.Process();
            }

            var result = GenerateRaw(contracts);

            Console.WriteLine("----- START -----");
            Console.WriteLine(result);
            Console.WriteLine("-----  END  -----");

            contracts.Errors.ShouldBeEmpty();

            return(result);
        }
        private void TranslateFile(ITaskItem inputFile)
        {
            var fileContents = File.ReadAllText(inputFile.ItemSpec);
            var contracts    = ParsedContracts.Parse(fileContents, inputFile.GetMetadata("CustomToolNamespace") ?? string.Empty);

            if (!contracts.IsValid)
            {
                foreach (var error in contracts.Errors)
                {
                    LogError(inputFile, error.Message, error.LineNumber, error.CharacterInLine);
                }

                return;
            }

            GenerateCSharpOutput(inputFile, contracts);
            GenerateProtoOutput(inputFile, contracts);
        }
 public ContractsEnhancer(ParsedContracts contracts)
 {
     _contracts = contracts;
 }
예제 #14
0
 public AstProcessor(ParsedContracts contracts)
 {
     _contracts = contracts;
 }
예제 #15
0
 public AttributeInterpretor(ParsedContracts contracts)
 {
     _contracts = contracts;
 }
 protected override string GenerateRaw(ParsedContracts contracts) => CSharpGenerator.Generate(contracts);
 public AstCreationVisitor(ParsedContracts contracts)
 {
     _contracts            = contracts;
     _currentMemberOptions = new MemberOptions();
 }
예제 #18
0
 private CSharpGenerator(ParsedContracts contracts)
 {
     Contracts = contracts;
 }
예제 #19
0
 protected abstract string GenerateRaw(ParsedContracts contracts);
예제 #20
0
 public AstValidator(ParsedContracts contracts)
 {
     _contracts = contracts;
 }
예제 #21
0
 private ProtoGenerator(ParsedContracts contracts)
 {
     Contracts = contracts;
 }
예제 #22
0
 protected override string GenerateRaw(ParsedContracts contracts)
 {
     return(ProtoGenerator.Generate(contracts));
 }
예제 #23
0
 public static string Generate(ParsedContracts contracts)
 {
     using var generator = new ProtoGenerator(contracts);
     return(generator.Generate());
 }
예제 #24
0
 public static bool HasProtoOutput(ParsedContracts contracts)
 {
     return(contracts.Messages.Any(i => i.Options.Proto) ||
            contracts.Enums.Any(i => i.Options.Proto));
 }