Asteroid.CodeBuilder 1.4.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Asteroid.CodeBuilder --version 1.4.0
                    
NuGet\Install-Package Asteroid.CodeBuilder -Version 1.4.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="Asteroid.CodeBuilder" Version="1.4.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Asteroid.CodeBuilder" Version="1.4.0" />
                    
Directory.Packages.props
<PackageReference Include="Asteroid.CodeBuilder" />
                    
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 Asteroid.CodeBuilder --version 1.4.0
                    
#r "nuget: Asteroid.CodeBuilder, 1.4.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 Asteroid.CodeBuilder@1.4.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=Asteroid.CodeBuilder&version=1.4.0
                    
Install as a Cake Addin
#tool nuget:?package=Asteroid.CodeBuilder&version=1.4.0
                    
Install as a Cake Tool

Asteroid.CodeBuilder

Asteroid.CodeBuilder 通过结构化 fluent 节点 API 构建 C# 源码,面向 Roslyn 生成器和可选 编辑器工具。

包以 netstandard2.0 兼容编译器宿主,以 net10.0 支持当前外部工具和编辑器 addon。 它不依赖 Asteroid 运行时模块或 Godot。

游戏运行时项目不直接引用此包。分析器和生成器包可以将 netstandard2.0 程序集作为编译器 宿主依赖携带。

上手路径

  1. FileBuilder.New(hintName) 起文件,builder.Root.Using(ns) / FileNamespace(ns) 声明头部;
  2. Decl.* / builder.Root.Class(...) 声明类型与成员,CodeNodeExtensions(AsPublic、 WithGet、Implement、Inherit 等)配置修饰与成员;
  3. 方法体走 St.* 语句节点(St.Line/St.If/St.For/St.While/St.Block),表达式走 Exp.* 节点(Exp.Invoke/Exp.Assign/Exp.New/Exp.Ternary/运算符族等);
  4. builder.ToCode(cancellationToken) 出文本。

可执行示例见仓库 examples/CodeBuilder/(dotnet run --example <name>,含生成产物校验): FullPropertyExample 属性全形态、GeneratorPathExample 生成器推荐路径。

表达式字符串纪律

表达式字符串始终显式构造:标识符使用 Exp.Id,字符串字面量使用 Exp.String,只有 有意插入原始源码时才使用 Exp.Raw。string 永不隐式转换为 ExpNode。

  • 字面量转义归发射器:字符串/字符字面量的内容转义由 CodeBuilder 负责(普通模式转义反斜杠/引号/控制字符;逐字模式双引号翻倍;原始字符串自动垫高定界符过引号连击)——含 \ | ; " 换行 等毒字符的内容直接传 Exp.String 即可,无需调用方预处理。插值串的花括号视为有意插值孔,不转义。
  • Exp.Raw 只留给无节点形态的叶子(default!、关键字、已解析的类型名 token); 含运算符/调用/构造/强转的语法串必须节点化。

语义陷阱速查

  • Property 构造即带 get+set:只配 WithGet 会留下空 set——get-only 用 GetterProperty 或显式 Setter.Enabled = false;
  • Exp.Line 自带行尾分号:Raw 片段自带 ; 会叠加成 ;;;
  • Exp.Raw 多行内容只缩进首行:整段方法体必须走 St.* 节点树;
  • Exp.New 恒带参数括号:定长数组用 Exp.NewArrayByType(Exp.SizedArrayType(...));
  • 特性名渲染剥 Attribute 后缀(XxxAttribute → [Xxx]);
  • 指名实参形态随宿主切换:方法 name:,特性 name =;
  • 基类走 Inherit(Implement 是接口族),单接口同行需 InheritImplementGroup.MultiLine = false;
  • 类型名必须结构化(Exp.Namespace(ns).Qa(...) / TypeNameRecord),字符串路径不参与 NameScopes 简化。

职责

CodeBuilder 是用于结构化构造 C# 源码的构建期基础设施。它保持多目标框架,以便 Roslyn 分析器和生成器加载 netstandard2.0 程序集;net10.0 目标供外部工具和可选编辑器 addon 使用。

边界

  • 属于开发环境仓库,不属于分发给游戏项目的 Framework 仓库;
  • 不引用运行时 Framework 模块或 Godot;
  • 作为 NuGet 包供源生成器、外部工具和可选编辑器 addon 使用;
  • Framework 运行时模块和游戏运行时项目不得引用它;
  • 生成器包可以携带 netstandard2.0 程序集作为编译器宿主依赖;
  • 开发期间本地分析器和源生成器使用项目引用,不等待包发布;
  • 标识符和字符串字面量必须使用专用结构化工厂,原始源码必须显式调用 Exp.Raw,不得隐式转换;
  • 公共 API 文档由仓库级文档和 warnings-as-errors 门禁强制校验。

旧 CodeBuilder 仓库仅作为实现参考。迁移后的实现是后续渐进式重设计基线,不承担兼容旧实现的要求。

机制

三类角色

  • 节点树(ICodeNode/CodeNodeBase 及其派生):只声明"要什么结构",不管缩进与换行;
  • 构建上下文(CodeBuildContext/CodeNodeBuildContext):持有输出缓冲、缩进栈、行状态与 NameScopes,节点经 OnBuild(context) 把文本写进上下文;
  • 工厂(Exp.*/St.*/Decl.* 静态入口 + CodeNodeExtensions 扩展):创建与挂接节点的唯一正门。

构建生命周期

  1. ShouldBuild(可整体折断:空组、Enabled=false 的访问器、null 占位);
  2. OnBuildStart:输出作用域行(如 if (...))与起始括号,进入缩进;
  3. OnBuild:输出自身内容;子节点逐个经 ShouldBuildChild 决定是否发射;
  4. OnBuildEnd:退出缩进,输出收尾括号/初始化器。

"可选发射"一律走 ShouldBuild/ShouldBuildChild,不要在调用方做字符串拼接。

缩进与行

  • 换行与缩进归 writer:节点声明结构,writer 负责深度与行状态;
  • St.Line(表达式语句)自带行尾分号(LineEnder)——Exp.Raw 片段自带 ; 会叠加成 ;;;
  • Exp.Raw 的多行内容只缩进首行:整段方法体禁止塞进单条 St.Line(Exp.Raw(...)), 必须用 St.If/St.For/St.While/St.Block 节点树表达。

NameScopes(类型名简化)

  • file.Using(ns) 把命名空间登记进 context.NameScopes;结构化类型名 (Exp.Namespace(ns).Qa(name) 与 TypeNameRecord.ToCode())构建时命中登记即省略前缀;
  • 字符串路径不参与简化:Exp.Qa(null, "A.B.C")、(TypeName)字符串、"global::X.Y" 都是不透明 token——去掉 global:: 不等于走了结构化路径;
  • 提取期拿 Roslyn 符号时,用 CompilerTools.Shared 的 TypeNameRecord.From(symbol) 兑换、 NameScope 解析短名并按需收集 using。

using 区正形(1.4.0 起)

  • 调用方按任意序 file.Using(...),发射时统一正形——与仓库 using 形态闸(AST1026)及 样式整理趟同判据:常规(与 global)命名空间按根段分桶,System 桶最前、桶头按根名 OrdinalIgnoreCase 升序、桶内按全名升序、桶间一空行;using static 与别名各自成节居后, 分别按目标名/别名升序;
  • 排序是发射器职责:程序化收集 using 的生成器无需自行排序,也不会把乱序发射成 落盘 .cs 后被形态闸咬住的形态;
  • EnableImplicitUsings = true(默认)时隐式集(System 等)已预登记 NameScope—— 显式 Using 同名命名空间会被去重省略,观察真实 using 行需关掉该选项。

形态语义(默认值清单)

  • Property 构造即带 get+set 两个访问器:只配 WithGet 会留下空 set——get-only 用 GetterProperty(表达式体)或显式 Setter.Enabled = false;
  • InheritImplementNodeGroup.MultiLine 默认 true(冒号后换行缩进):单接口同行需显式置 false; 基类走 Inherit,Implement 是接口族专用;
  • Exp.New 恒带参数括号:定长数组创建用 Exp.NewArrayByType(Exp.SizedArrayType(type, [len]));
  • AttributeNode 渲染时剥类型名的 Attribute 后缀(XxxAttribute → [Xxx]);
  • ArgumentNode 指名实参形态随宿主自动切换:方法调用 name: value、特性 name = value;
  • 二元/一元运算符节点族见 Exp_Op_* 文件(文件头注释列明各自包含的族);
  • Exp.Invoke 有 IEnumerable<ArgumentNode?>/IEnumerable<ExpNode?> 双重载:实参传集合表达式 ([...])可能 CS0121 歧义,传显式 List<> 或先取 var 即解。

发射纪律

  • Exp.Raw 仅限无节点形态的叶子(default!、关键字、已解析的类型名 token); 含运算符/调用/构造/强转的串一律节点化——串语法会把 C# 文法手写进字符串, 坏形态要到消费端编译才暴露;
  • 语句走 St.*,表达式走 Exp.*,类型引用走结构化节点。

消费侧注意

  • 分析器工程经 NuGet 包消费 CodeBuilder:库源码改动需重发包并升分析器包引用, 否则生成器产物仍按旧版行为发射;
  • 生成器改动后必须落盘实看产物 (dotnet build <proj> -p:EmitCompilerGeneratedFiles=true),"编译过 + Contains 断言" 抓不住结构级异常。
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.
  • net10.0

    • No dependencies.

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
3.1.0 47 11/18/2026
2.0.0 112 10/26/2026
1.9.0 117 10/17/2026
1.8.0 124 10/13/2026
1.7.0 112 10/6/2026
1.6.0 119 9/27/2026
1.5.0 113 9/25/2026
1.4.0 107 9/25/2026
1.3.0 111 9/25/2026
1.2.1 127 9/12/2026
1.2.0 131 9/10/2026
1.1.0 134 9/8/2026
1.0.7 136 9/8/2026
1.0.6 123 8/30/2026
1.0.5 138 8/21/2026
1.0.4 131 8/13/2026
1.0.3 127 8/13/2026
1.0.2 145 5/16/2026
1.0.1 152 5/15/2026
1.0.0 136 5/15/2026