Skip to content

Commit

Permalink
Use new language features
Browse files Browse the repository at this point in the history
  • Loading branch information
ltrzesniewski committed Jan 8, 2024
1 parent 47542d1 commit 17ecd30
Show file tree
Hide file tree
Showing 55 changed files with 4,331 additions and 4,288 deletions.
23 changes: 11 additions & 12 deletions src/Abc.Zebus.MessageDsl.Build.Integration/IntegrationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,19 @@

using System;

namespace Abc.Zebus.MessageDsl.Build.Integration
namespace Abc.Zebus.MessageDsl.Build.Integration;

public class IntegrationTest
{
public class IntegrationTest
static IntegrationTest()
{
static IntegrationTest()
{
GC.KeepAlive(typeof(SomeMessage));
GC.KeepAlive(typeof(InnerNamespace.InnerMessage));
GC.KeepAlive(typeof(Abc.Zebus.CustomNamespace.HasCustomNamespace));
GC.KeepAlive(typeof(Abc.Zebus.CustomExplicitNamespace.HasCustomExplicitNamespace));
GC.KeepAlive(typeof(global::HasEmptyNamespace));
GC.KeepAlive(typeof(ExplicitItems.A.ExplicitlyDefinedMessage));
GC.KeepAlive(typeof(ExplicitItems.B.ExplicitlyDefinedMessage));
}
GC.KeepAlive(typeof(SomeMessage));
GC.KeepAlive(typeof(InnerNamespace.InnerMessage));
GC.KeepAlive(typeof(Abc.Zebus.CustomNamespace.HasCustomNamespace));
GC.KeepAlive(typeof(Abc.Zebus.CustomExplicitNamespace.HasCustomExplicitNamespace));
GC.KeepAlive(typeof(global::HasEmptyNamespace));
GC.KeepAlive(typeof(ExplicitItems.A.ExplicitlyDefinedMessage));
GC.KeepAlive(typeof(ExplicitItems.B.ExplicitlyDefinedMessage));
}
}

Expand Down
117 changes: 58 additions & 59 deletions src/Abc.Zebus.MessageDsl.Build/Build/GenerateZebusMessagesTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,85 +8,84 @@

#nullable enable

namespace Abc.Zebus.MessageDsl.Build
namespace Abc.Zebus.MessageDsl.Build;

[UsedImplicitly(ImplicitUseTargetFlags.WithMembers)]
public class GenerateZebusMessagesTask : Task
{
[UsedImplicitly(ImplicitUseTargetFlags.WithMembers)]
public class GenerateZebusMessagesTask : Task
{
private const string _logSubcategory = "Zebus.MessageDsl";
private const string _logSubcategory = "Zebus.MessageDsl";

[Required]
public ITaskItem[] InputFiles { get; set; } = default!;
[Required]
public ITaskItem[] InputFiles { get; set; } = default!;

public override bool Execute()
public override bool Execute()
{
foreach (var inputFile in InputFiles)
{
foreach (var inputFile in InputFiles)
try
{
try
{
TranslateFile(inputFile);
}
catch (Exception ex)
{
LogError(inputFile, $"Error translating file: {ex}");
}
TranslateFile(inputFile);
}
catch (Exception ex)
{
LogError(inputFile, $"Error translating file: {ex}");
}

return !Log.HasLoggedErrors;
}

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

if (!contracts.IsValid)
{
foreach (var error in contracts.Errors)
LogError(inputFile, error.Message, error.LineNumber, error.CharacterInLine);
private void TranslateFile(ITaskItem inputFile)
{
var fileContents = File.ReadAllText(inputFile.ItemSpec);
var defaultNamespace = inputFile.GetMetadata("CustomToolNamespace")?.Trim() ?? string.Empty;
var contracts = ParsedContracts.Parse(fileContents, defaultNamespace);

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

GenerateCSharpOutput(inputFile, contracts);
GenerateProtoOutput(inputFile, contracts);
return;
}

private void GenerateCSharpOutput(ITaskItem inputFile, ParsedContracts contracts)
{
var targetPath = GetValidTargetFilePath(inputFile);

var output = CSharpGenerator.Generate(contracts);
File.WriteAllText(targetPath, output);
GenerateCSharpOutput(inputFile, contracts);
GenerateProtoOutput(inputFile, contracts);
}

LogDebug($"{inputFile.ItemSpec}: Translated {contracts.Messages.Count} message{(contracts.Messages.Count > 1 ? "s" : "")}");
}
private void GenerateCSharpOutput(ITaskItem inputFile, ParsedContracts contracts)
{
var targetPath = GetValidTargetFilePath(inputFile);

private void GenerateProtoOutput(ITaskItem inputFile, ParsedContracts contracts)
{
if (!ProtoGenerator.HasProtoOutput(contracts))
return;
var output = CSharpGenerator.Generate(contracts);
File.WriteAllText(targetPath, output);

var targetPath = Path.ChangeExtension(GetValidTargetFilePath(inputFile), "proto") ?? throw new InvalidOperationException("Invalid target path");
LogDebug($"{inputFile.ItemSpec}: Translated {contracts.Messages.Count} message{(contracts.Messages.Count > 1 ? "s" : "")}");
}

var output = ProtoGenerator.Generate(contracts);
File.WriteAllText(targetPath, output);
private void GenerateProtoOutput(ITaskItem inputFile, ParsedContracts contracts)
{
if (!ProtoGenerator.HasProtoOutput(contracts))
return;

LogDebug($"{inputFile.ItemSpec}: Generated proto file");
}
var targetPath = Path.ChangeExtension(GetValidTargetFilePath(inputFile), "proto") ?? throw new InvalidOperationException("Invalid target path");

private static string GetValidTargetFilePath(ITaskItem inputFile)
{
var targetPath = inputFile.GetMetadata("GeneratorTargetPath") ?? throw new InvalidOperationException("No target path specified");
Directory.CreateDirectory(Path.GetDirectoryName(targetPath) ?? throw new InvalidOperationException("Invalid target directory"));
return targetPath;
}
var output = ProtoGenerator.Generate(contracts);
File.WriteAllText(targetPath, output);

private void LogDebug(string message)
=> Log.LogMessage(_logSubcategory, null, null, null, 0, 0, 0, 0, MessageImportance.Low, message, null);
LogDebug($"{inputFile.ItemSpec}: Generated proto file");
}

private void LogError(ITaskItem? inputFile, string message, int lineNumber = 0, int columnNumber = 0)
=> Log.LogError(_logSubcategory, null, null, inputFile?.ItemSpec, lineNumber, columnNumber, 0, 0, message, null);
private static string GetValidTargetFilePath(ITaskItem inputFile)
{
var targetPath = inputFile.GetMetadata("GeneratorTargetPath") ?? throw new InvalidOperationException("No target path specified");
Directory.CreateDirectory(Path.GetDirectoryName(targetPath) ?? throw new InvalidOperationException("Invalid target directory"));
return targetPath;
}

private void LogDebug(string message)
=> Log.LogMessage(_logSubcategory, null, null, null, 0, 0, 0, 0, MessageImportance.Low, message, null);

private void LogError(ITaskItem? inputFile, string message, int lineNumber = 0, int columnNumber = 0)
=> Log.LogError(_logSubcategory, null, null, inputFile?.ItemSpec, lineNumber, columnNumber, 0, 0, message, null);
}
23 changes: 11 additions & 12 deletions src/Abc.Zebus.MessageDsl.Generator.Integration/IntegrationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,19 @@

using System;

namespace Abc.Zebus.MessageDsl.Generator.Integration
namespace Abc.Zebus.MessageDsl.Generator.Integration;

public class IntegrationTest
{
public class IntegrationTest
static IntegrationTest()
{
static IntegrationTest()
{
GC.KeepAlive(typeof(SomeMessage));
GC.KeepAlive(typeof(InnerNamespace.InnerMessage));
GC.KeepAlive(typeof(Abc.Zebus.CustomNamespace.HasCustomNamespace));
GC.KeepAlive(typeof(Abc.Zebus.CustomExplicitNamespace.HasCustomExplicitNamespace));
GC.KeepAlive(typeof(global::HasEmptyNamespace));
GC.KeepAlive(typeof(ExplicitItems.A.ExplicitlyDefinedMessage));
GC.KeepAlive(typeof(ExplicitItems.B.ExplicitlyDefinedMessage));
}
GC.KeepAlive(typeof(SomeMessage));
GC.KeepAlive(typeof(InnerNamespace.InnerMessage));
GC.KeepAlive(typeof(Abc.Zebus.CustomNamespace.HasCustomNamespace));
GC.KeepAlive(typeof(Abc.Zebus.CustomExplicitNamespace.HasCustomExplicitNamespace));
GC.KeepAlive(typeof(global::HasEmptyNamespace));
GC.KeepAlive(typeof(ExplicitItems.A.ExplicitlyDefinedMessage));
GC.KeepAlive(typeof(ExplicitItems.B.ExplicitlyDefinedMessage));
}
}

Expand Down
Loading

0 comments on commit 17ecd30

Please sign in to comment.