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"); }
public static string DumpParseTree(ParsedContracts contracts) { var writer = new IndentedTextWriter(new StringWriter()); DumpParseTree(contracts.ParseTree, writer); return(writer.InnerWriter.ToString() ?? string.Empty); }
protected string Generate(MessageDefinition message) { var contracts = new ParsedContracts(); contracts.Messages.Add(message); return(Generate(contracts)); }
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" : "")}"); }
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; }"); }
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; }
public AstProcessor(ParsedContracts contracts) { _contracts = contracts; }
public AttributeInterpretor(ParsedContracts contracts) { _contracts = contracts; }
protected override string GenerateRaw(ParsedContracts contracts) => CSharpGenerator.Generate(contracts);
public AstCreationVisitor(ParsedContracts contracts) { _contracts = contracts; _currentMemberOptions = new MemberOptions(); }
private CSharpGenerator(ParsedContracts contracts) { Contracts = contracts; }
protected abstract string GenerateRaw(ParsedContracts contracts);
public AstValidator(ParsedContracts contracts) { _contracts = contracts; }
private ProtoGenerator(ParsedContracts contracts) { Contracts = contracts; }
protected override string GenerateRaw(ParsedContracts contracts) { return(ProtoGenerator.Generate(contracts)); }
public static string Generate(ParsedContracts contracts) { using var generator = new ProtoGenerator(contracts); return(generator.Generate()); }
public static bool HasProtoOutput(ParsedContracts contracts) { return(contracts.Messages.Any(i => i.Options.Proto) || contracts.Enums.Any(i => i.Options.Proto)); }