Skip to content

Commit 63f18c4

Browse files
committed
重构(generator): 重命名FilePath为FileName
重命名RegionInjectAttribute特性中的FilePath属性为FileName,以更准确描述用途,同时更新代码生成器逻辑以支持新属性名。保留对旧属性名的向后兼容支持。更新README文档中的示例代码和描述。新增Program.cs中的代码注入示例。优化错误信息提示,调整文件匹配逻辑为基于文件名。
1 parent cb2ae33 commit 63f18c4

File tree

7 files changed

+118
-61
lines changed

7 files changed

+118
-61
lines changed

Directory.Build.props

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project>
22
<PropertyGroup>
3-
<Version>1.0.3</Version>
3+
<Version>1.0.4</Version>
44
<CheckForOverflowUnderflow>false</CheckForOverflowUnderflow>
55
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
66
<LangVersion>preview</LangVersion>
@@ -11,8 +11,6 @@
1111
<Authors>若汝棋茗</Authors>
1212
<BaseOutputPath>..\..\Build</BaseOutputPath>
1313
<NoWarn>IDE0290;IDE0090;IDE0305;IDE0028;IDE0300;IDE0130;IDE0057</NoWarn>
14-
<GenerateSatelliteAssemblies>true</GenerateSatelliteAssemblies>
15-
<GenerateSatelliteAssembliesCore>true</GenerateSatelliteAssembliesCore>
1614
<!--<Nullable>enable</Nullable>-->
1715
</PropertyGroup>
1816
</Project>

README.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# CodeInject - Code Region Source Generator
22

3+
English | [简体中文](README.zh-CN.md)
4+
35
[![Build and Publish](https://github.com/RRQM/CodeInject/actions/workflows/nuget-publish.yml/badge.svg)](https://github.com/RRQM/CodeInject/actions/workflows/nuget-publish.yml)
46
[![Release](https://github.com/RRQM/CodeInject/actions/workflows/release.yml/badge.svg)](https://github.com/RRQM/CodeInject/actions/workflows/release.yml)
57
[![NuGet Version](https://img.shields.io/nuget/v/CodeInject)](https://www.nuget.org/packages/CodeInject/)
@@ -63,7 +65,7 @@ public async Task<{ReturnType}> Create{EntityName}Async({ReturnType} entity)
6365
```csharp
6466
using CodeInject;
6567

66-
[RegionInject(FilePath = "Templates/ApiTemplate.cs", RegionName = "ApiMethods",
68+
[RegionInject(FileName = "Templates/ApiTemplate.cs", RegionName = "ApiMethods",
6769
Placeholders = new[] { "ReturnType", "User", "EntityName", "User" })]
6870
public partial class UserService
6971
{
@@ -104,11 +106,11 @@ partial class UserService
104106
### Multiple Injections
105107

106108
```csharp
107-
[RegionInject(FilePath = "Templates/CrudTemplate.cs", RegionName = "CreateMethods",
109+
[RegionInject(FileName = "Templates/CrudTemplate.cs", RegionName = "CreateMethods",
108110
Placeholders = new[] { "Entity", "Product" })]
109-
[RegionInject(FilePath = "Templates/CrudTemplate.cs", RegionName = "UpdateMethods",
111+
[RegionInject(FileName = "Templates/CrudTemplate.cs", RegionName = "UpdateMethods",
110112
Placeholders = new[] { "Entity", "Product" })]
111-
[RegionInject(FilePath = "Templates/ValidationTemplate.cs", RegionName = "Validators",
113+
[RegionInject(FileName = "Templates/ValidationTemplate.cs", RegionName = "Validators",
112114
Placeholders = new[] { "Type", "Product" })]
113115
public partial class ProductService
114116
{
@@ -118,7 +120,7 @@ public partial class ProductService
118120

119121
### Search All Files for Region
120122

121-
If you don't specify the `FilePath`, the generator will search all available files for the specified region:
123+
If you don't specify the `FileName`, the generator will search all available files for the specified region:
122124

123125
```csharp
124126
[RegionInject(RegionName = "CommonMethods")]
@@ -131,7 +133,7 @@ public partial class BaseService
131133
### Using Property Initializers
132134

133135
```csharp
134-
[RegionInject(FilePath = "Templates/ApiTemplate.cs", RegionName = "ApiMethods",
136+
[RegionInject(FileName = "Templates/ApiTemplate.cs", RegionName = "ApiMethods",
135137
Placeholders = new[] { "ReturnType", "Order", "EntityName", "Order" })]
136138
public partial class OrderService
137139
{
@@ -192,7 +194,7 @@ public async Task<ActionResult<{EntityType}>> Create{EntityName}({EntityType} {e
192194

193195
Usage:
194196
```csharp
195-
[RegionInject(FilePath = "Templates/ControllerTemplate.cs", RegionName = "CrudActions",
197+
[RegionInject(FileName = "Templates/ControllerTemplate.cs", RegionName = "CrudActions",
196198
Placeholders = new[] { "EntityType", "Product", "EntityName", "Product", "entityName", "product" })]
197199
public partial class ProductController : ControllerBase
198200
{
@@ -226,7 +228,7 @@ public async Task<{EntityType}> Create{EntityName}Async({EntityType} entity)
226228

227229
Usage:
228230
```csharp
229-
[RegionInject(FilePath = "Templates/RepositoryTemplate.cs", RegionName = "RepositoryMethods",
231+
[RegionInject(FileName = "Templates/RepositoryTemplate.cs", RegionName = "RepositoryMethods",
230232
Placeholders = new[] { "EntityType", "User", "EntityName", "User" })]
231233
public partial class UserRepository
232234
{

README.zh-CN.md

Lines changed: 63 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# CodeInject - 代码区域源生成器
22

3+
[English](README.md) | 简体中文
4+
5+
[![Build and Publish](https://github.com/RRQM/CodeInject/actions/workflows/nuget-publish.yml/badge.svg)](https://github.com/RRQM/CodeInject/actions/workflows/nuget-publish.yml)
6+
[![Release](https://github.com/RRQM/CodeInject/actions/workflows/release.yml/badge.svg)](https://github.com/RRQM/CodeInject/actions/workflows/release.yml)
7+
[![NuGet Version](https://img.shields.io/nuget/v/CodeInject)](https://www.nuget.org/packages/CodeInject/)
8+
[![NuGet Downloads](https://img.shields.io/nuget/dt/CodeInject)](https://www.nuget.org/packages/CodeInject/)
9+
310
一个强大的源生成器,可在编译时将模板文件中的代码区域注入到部分类中。
411

512
## ✨ 特性
@@ -56,11 +63,10 @@ public async Task<{ReturnType}> Create{EntityName}Async({ReturnType} entity)
5663
### 2. 应用特性
5764

5865
```csharp
59-
using CodeRegionSourceGenerator;
66+
using CodeInject;
6067

61-
[RegionInject("Templates/ApiTemplate.cs", "ApiMethods",
62-
"ReturnType", "User",
63-
"EntityName", "User")]
68+
[RegionInject(FileName = "Templates/ApiTemplate.cs", RegionName = "ApiMethods",
69+
Placeholders = new[] { "ReturnType", "User", "EntityName", "User" })]
6470
public partial class UserService
6571
{
6672
private readonly IRepository _repository;
@@ -100,19 +106,34 @@ partial class UserService
100106
### 多重注入
101107

102108
```csharp
103-
[RegionInject("Templates/CrudTemplate.cs", "CreateMethods", "Entity", "Product")]
104-
[RegionInject("Templates/CrudTemplate.cs", "UpdateMethods", "Entity", "Product")]
105-
[RegionInject("Templates/ValidationTemplate.cs", "Validators", "Type", "Product")]
109+
[RegionInject(FileName = "Templates/CrudTemplate.cs", RegionName = "CreateMethods",
110+
Placeholders = new[] { "Entity", "Product" })]
111+
[RegionInject(FileName = "Templates/CrudTemplate.cs", RegionName = "UpdateMethods",
112+
Placeholders = new[] { "Entity", "Product" })]
113+
[RegionInject(FileName = "Templates/ValidationTemplate.cs", RegionName = "Validators",
114+
Placeholders = new[] { "Type", "Product" })]
106115
public partial class ProductService
107116
{
108117
// 多个代码区域将被注入
109118
}
110119
```
111120

112-
### 使用 Placeholders 属性
121+
### 搜索所有文件中的区域
122+
123+
如果不指定 `FileName`,生成器将在所有可用文件中搜索指定的区域:
124+
125+
```csharp
126+
[RegionInject(RegionName = "CommonMethods")]
127+
public partial class BaseService
128+
{
129+
// 生成器将在所有文件中搜索"CommonMethods"区域
130+
}
131+
```
132+
133+
### 使用属性初始化器
113134

114135
```csharp
115-
[RegionInject("Templates/ApiTemplate.cs", "ApiMethods",
136+
[RegionInject(FileName = "Templates/ApiTemplate.cs", RegionName = "ApiMethods",
116137
Placeholders = new[] { "ReturnType", "Order", "EntityName", "Order" })]
117138
public partial class OrderService
118139
{
@@ -171,6 +192,16 @@ public async Task<ActionResult<{EntityType}>> Create{EntityName}({EntityType} {e
171192
#endregion
172193
```
173194

195+
使用方法:
196+
```csharp
197+
[RegionInject(FileName = "Templates/ControllerTemplate.cs", RegionName = "CrudActions",
198+
Placeholders = new[] { "EntityType", "Product", "EntityName", "Product", "entityName", "product" })]
199+
public partial class ProductController : ControllerBase
200+
{
201+
// 生成的 CRUD 操作将被注入到这里
202+
}
203+
```
204+
174205
### 2. 仓储模式模板
175206

176207
```csharp
@@ -195,6 +226,15 @@ public async Task<{EntityType}> Create{EntityName}Async({EntityType} entity)
195226
#endregion
196227
```
197228

229+
使用方法:
230+
```csharp
231+
[RegionInject(FileName = "Templates/RepositoryTemplate.cs", RegionName = "RepositoryMethods",
232+
Placeholders = new[] { "EntityType", "User", "EntityName", "User" })]
233+
public partial class UserRepository
234+
{
235+
// 生成的仓储方法将被注入到这里
236+
}
237+
198238
## 🔍 诊断信息
199239

200240
源生成器提供以下诊断信息:
@@ -205,10 +245,11 @@ public async Task<{EntityType}> Create{EntityName}Async({EntityType} entity)
205245

206246
## 💡 最佳实践
207247

208-
1. **组织模板文件**: 将模板文件放在专门的 `Templates` 文件夹中
248+
1. **组织模板**: 将模板文件保存在专门的 `Templates` 文件夹中
209249
2. **命名约定**: 使用描述性的区域名称,如 `CrudMethods`、`ValidationRules`
210-
3. **占位符命名**: 使用大写的占位符名称,如 `ENTITY_NAME``RETURN_TYPE`
250+
3. **占位符命名**: 使用一致的占位符名称,如 `EntityType`、`EntityName`
211251
4. **模块化**: 将相关功能分组到不同的区域中
252+
5. **基于属性的语法**: 使用新的基于属性的初始化方式以获得更好的可读性
212253

213254
## 📋 系统要求
214255

@@ -226,21 +267,21 @@ public async Task<{EntityType}> Create{EntityName}Async({EntityType} entity)
226267

227268
## 🆚 与其他方案对比
228269

229-
| 特性 | CodeInject | T4 模板 | 手动编写 |
230-
|------|------------|---------|----------|
231-
| 编译时生成 ||||
232-
| 增量编译 ||||
233-
| IDE 支持 || ⚠️ ||
234-
| 学习成本 ||||
235-
| 灵活性 ||||
270+
| 特性 | CodeInject | T4 模板 | 手动编写 |
271+
| ---------- | ---------- | ------- | -------- |
272+
| 编译时生成 | | | |
273+
| 增量编译 | | | |
274+
| IDE 支持 | | ⚠️ | |
275+
| 学习成本 | | | |
276+
| 灵活性 | | | |
236277

237278
## 📞 支持
238279

239-
如果遇到问题,请
280+
如果遇到问题:
240281

241-
1. 检查[常见问题](https://github.com/yourusername/CodeInject/wiki/FAQ)
242-
2. 搜索[已有问题](https://github.com/yourusername/CodeInject/issues)
243-
3. 创建[新问题](https://github.com/yourusername/CodeInject/issues/new)
282+
1. 检查 [FAQ](https://github.com/yourusername/CodeInject/wiki/FAQ)
283+
2. 搜索 [已有问题](https://github.com/yourusername/CodeInject/issues)
284+
3. 创建 [新问题](https://github.com/yourusername/CodeInject/issues/new)
244285
245286
---
246287

demo/CodeRegionExamplesConsoleApp/Program.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
namespace CodeRegionExamplesConsoleApp;
1717

18+
[RegionInject(RegionName = "ShowMyClass")]
1819
[RegionInject(RegionName = "Show")]
1920
[RegionInject(RegionName = "Show1", Placeholders = new[] { "Show123", "Show222" })]
2021
internal partial class ExampleProgram
@@ -23,5 +24,17 @@ static void Main(string[] args)
2324
{
2425
Show();
2526
Show1();
27+
ShowMyClass();
2628
}
2729
}
30+
31+
class MyClass
32+
{
33+
#region ShowMyClass
34+
public static void ShowMyClass()
35+
{
36+
37+
}
38+
#endregion
39+
40+
}

0 commit comments

Comments
 (0)