Adding new features to the Compiler Extension API
A compiler extension is a .NET assembly containing a public type implementing the IPackage interface in the Dassie.Extensions namespace. All features of the API are supported through special interfaces in this namespace. To add a new feature to the extension API, follow the below steps. The code below uses the IBuildLogWriter interface as an example.
- Create a new public interface in the
Dassie.Extensionsnamespace:
using System.IO;
namespace Dassie.Extensions;
/// <summary>
/// Defines a mechanism to write build logs using <see cref="System.IO.TextWriter"/>.
/// </summary>
public interface IBuildLogWriter
{
/// <summary>
/// The name of the log writer.
/// </summary>
public string Name { get; }
/// <summary>
/// The error severities at which the log writer is active.
/// </summary>
public BuildLogSeverity Severity { get; }
/// <summary>
/// All text writers making up the build log writer.
/// </summary>
public TextWriter[] Writers { get; }
}
- Add a new virtual method to
IPackagein order not to break existing implementations:
/// <summary>
/// An array of build log writers.
/// </summary>
public virtual IBuildLogWriter[] BuildLogWriters() => [];
- Add a corresponding property to
ExtensionLoader:
public static IEnumerable<IBuildLogWriter> BuildLogWriters => InstalledExtensions.Select(a => a.BuildLogWriters()).SelectMany(a => a);
- Add a new section to the
dc package infocommand output:
if (package.BuildLogWriters().Length != 0)
{
sb.AppendLine();
WriteHeading("Build log writers");
foreach (IBuildLogWriter writer in package.BuildLogWriters())
sb.AppendLine($" {writer.Name}");
}
- Modify relevant code parts of the compiler to support the new API feature.
Following these steps ensures the new API feature is correctly integrated and recognized by the compiler.