RhoMicro.CodeAnalysis.Lyra 1.2.0

Prefix Reserved
dotnet add package RhoMicro.CodeAnalysis.Lyra --version 1.2.0
                    
NuGet\Install-Package RhoMicro.CodeAnalysis.Lyra -Version 1.2.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="RhoMicro.CodeAnalysis.Lyra" Version="1.2.0">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="RhoMicro.CodeAnalysis.Lyra" Version="1.2.0" />
                    
Directory.Packages.props
<PackageReference Include="RhoMicro.CodeAnalysis.Lyra">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add RhoMicro.CodeAnalysis.Lyra --version 1.2.0
                    
#r "nuget: RhoMicro.CodeAnalysis.Lyra, 1.2.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package RhoMicro.CodeAnalysis.Lyra@1.2.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=RhoMicro.CodeAnalysis.Lyra&version=1.2.0
                    
Install as a Cake Addin
#tool nuget:?package=RhoMicro.CodeAnalysis.Lyra&version=1.2.0
                    
Install as a Cake Tool

Lyra

This is a source generator generating library classes for writing beautiful C# source code; e.g. in source generators.

Licensing

This project is licensed under the MPL-2.0 license.

Features

  • indentation and newline aware string building
  • reusable component system
  • advanced interpolated strings for efficient source building

Installation

.NET CLI:

dotnet add package RhoMicro.CodeAnalysis.Lyra --version 1.1.0

PackageReference:

<PackageReference Include="RhoMicro.CodeAnalysis.Lyra" Version="1.1.0">
    <PrivateAssets>all</PrivateAssets>
    <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>

How To Use

Simple Example

Instantiate a new CSharpSourceBuilder:

using var sb = new CSharpSourceBuilder();

Append text:

sb.Append("namespace ").AppendLine("Foo").AppendLine('{');

Indent text:

sb.Indent();

Use builtin components:

sb.Append(
        ComponentFactory.Type(
                "enum",
                "Bar",
                ComponentFactory.Create((b, _) => b.AppendLine("Baz = 0"))));

Detent text:

sb.Detent();

Generate output:

sb.AppendLine('}');
Console.WriteLine(sb);

The generated output will look like this:

// <auto-generated>
// This file was generated using the global::RhoMicro.CodeAnalysis.Lyra.CSharpSourceBuilder.
// </auto-generated>
namespace Foo
{
    enum Bar
    {
        Baz = 0
    }
}

The CSharpSourceBuilder can be configured using the CSharpSourceBuilderOptions passed to its constructor:

using var sb1 = new CSharpSourceBuilder(
        new CSharpSourceBuilderOptions()
        {
                Prelude = (b,_)=>b.AppendLine("// <auto-generated/>"),
                DefaultIndentation = "  ".AsMemory()
        });

The output changes to:

// <auto-generated/>
namespace Foo
{
  enum Bar
  {
    Baz = 0
  }
}

Interpolated Strings

Interpolated strings are compiled to Append invocations, with indentation being automatically detected:

var placeholder = "\nbar\nbaz";
sb.Clear().AppendLine(
         $"""
             foo{placeholder}
         """);
Console.WriteLine(sb);

The output changes as follows:

// <auto-generated/>
    foo
    bar
    baz

Note how the initial indentation in front of foo is carried along.

The detection of these indentations can be configured using the CSharpSourceBuilderOptions.InitialInterpolationIndentationDetector property.

Components

Some useful components are provided out of the box:

var body = ComponentFactory.Create(
        myModel,
        static (myModel, b, ct) =>
        {
            ct.ThrowIfCancellationRequested();
        
            b.Append(myModel.Value);
        });

var type = ComponentFactory.Type(
        "partial struct",
        myModel.Name,
        body,
        baseTypeList:
        [
            TypeName<IComparable>()
        ]);

var @namespace = ComponentFactory.Namespace(
        myModel.Namespace,
        type);

builder.AppendLine(@namespace);

The output will look similar to this:

namespace Namespace
{
    partial struct Name : global::System.IComparable
    {
        // myModel.Value
    }
}

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.2.0 203 11/22/2025
1.1.0 283 10/30/2025
1.0.1 196 10/29/2025
1.0.0 176 10/29/2025