Skip to content

Commit 72200d0

Browse files
authored
build: migrate to .NET Core Local Tools (#4)
* chore: add T4 local tool * chore: remove dotnet-cli-tool & update Mono.T4 * style: optimize t4 file * docs: remove install dotnet-cli-tool process
1 parent 6d4327e commit 72200d0

File tree

4 files changed

+54
-66
lines changed

4 files changed

+54
-66
lines changed

.config/dotnet-tools.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"version": 1,
3+
"isRoot": true,
4+
"tools": {
5+
"dotnet-t4": {
6+
"version": "2.2.1",
7+
"commands": [
8+
"t4"
9+
]
10+
}
11+
}
12+
}

README.md

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,36 @@ This sample uses [Mono.TextTemplating](https://github.com/mono/t4) which is a T4
2626

2727
## Processes
2828

29-
### 1. Create Project
29+
### 1. Install .NET local tool
3030

3131
```console
32-
> dotnet new console
32+
# You should create manifest file before install local tool.
33+
> dotnet new tool-manifest
34+
# Install T4 tool in local
35+
> dotnet tool install dotnet-t4
3336
```
3437

35-
### 2. Add `LangVersion` to .csproj
38+
```json
39+
{
40+
"version": 1,
41+
"isRoot": true,
42+
"tools": {
43+
"dotnet-t4": {
44+
"version": "2.2.1",
45+
"commands": [
46+
"t4"
47+
]
48+
}
49+
}
50+
}
51+
```
3652

37-
- Set `LangVersion` to *8.0*.
53+
**Note: You should call `dotnet tool restore` command after cloned repo.**
3854

39-
```diff
40-
<PropertyGroup>
41-
+ <LangVersion>8.0</LangVersion>
42-
<OutputType>Exe</OutputType>
43-
<TargetFramework>netcoreapp3.0</TargetFramework>
44-
</PropertyGroup>
55+
### 2. Create Project
56+
57+
```console
58+
> dotnet new console -n T4Sample -o ./src
4559
```
4660

4761
### 3. Create Table and Column Entities
@@ -54,30 +68,18 @@ This sample uses [Mono.TextTemplating](https://github.com/mono/t4) which is a T4
5468
The program code generated by T4 file depends on this package.
5569

5670
```console
57-
> dotnet add package Mono.TextTemplating --version 2.0.5
58-
```
59-
60-
### 5. Install T4 Cli Tool
61-
62-
Add `DotNetCliToolReference` in csproj to use [dotnet-t4-project-tool](https://www.nuget.org/packages/dotnet-t4-project-tool/).
63-
64-
```diff
65-
<ItemGroup>
66-
+ <DotNetCliToolReference Include="dotnet-t4-project-tool" Version="2.0.5" />
67-
<PackageReference Include="Mono.TextTemplating" Version="2.0.5" />
68-
</ItemGroup>
71+
> dotnet add package Mono.TextTemplating --version 2.2.1
6972
```
7073

71-
### 6. Add Task to Prebuild and Cleanup
74+
### 5. Add Task to Prebuild and Cleanup
7275

7376
#### Define Target Files
7477

7578
Define `TextTemplate` and `Genarated` using Glob.
7679

7780
```diff
7881
<ItemGroup>
79-
<DotNetCliToolReference Include="dotnet-t4-project-tool" Version="2.0.5" />
80-
<PackageReference Include="Mono.TextTemplating" Version="2.0.5" />
82+
<PackageReference Include="Mono.TextTemplating" Version="2.2.1" />
8183
+ <TextTemplate Include="**\*.tt" />
8284
+ <Generated Include="**\*.Generated.cs" />
8385
</ItemGroup>
@@ -113,7 +115,7 @@ Define Task to delete `*.Generated.cs`.
113115
+ </Target>
114116
```
115117

116-
### 7. Create T4 Interface and define implements
118+
### 6. Create T4 Interface and define implements
117119

118120
`TransFormText()`, Called by Program to use T4 Classes are defined in *\*.Generated.cs*.
119121
But \*.Generated.cs is generated just before the build.
@@ -133,14 +135,14 @@ namespace T4Sample
133135
}
134136
```
135137

136-
### 8. Create T4 Template File & Class
138+
### 7. Create T4 Template File & Class
137139

138140
Note that T4 class should be defined with `partial` class.
139141

140142
- [TableEntityTemplate.tt](./src/TableEntityTemplate.tt)
141143
- [TableEntityTemplate.cs](./src/TableEntityTemplate.cs)
142144

143-
### 9. Call T4 Class in Program.cs
145+
### 8. Call T4 Class in Program.cs
144146

145147
Note that variable type should be an interface. **Don't** use `var` or `TableEntityTemplate`.
146148

src/T4Sample.csproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9-
<DotNetCliToolReference Include="dotnet-t4-project-tool" Version="2.0.5" />
10-
<PackageReference Include="Mono.TextTemplating" Version="2.0.5" />
9+
<PackageReference Include="Mono.TextTemplating" Version="2.2.1" />
1110
<TextTemplate Include="**\*.tt" />
1211
<Generated Include="**\*.Generated.cs" />
1312
</ItemGroup>

src/TableEntityTemplate.tt

Lines changed: 11 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,44 @@
11
<#@ template language="C#" #>
22
<#@ assembly name="System.Core" #>
33
<#@ import namespace="System.Linq" #>
4-
<#@ import namespace="System.Collections.Generic" #>
4+
<#@ output extension=".cs" #>
55
using System;
66
using System.Collections.Generic;
77

88
namespace <#= NameSpace #>
99
{
10-
<#
11-
if (!string.IsNullOrEmpty(Table.Description))
12-
{
13-
#>
10+
<# if (!string.IsNullOrEmpty(Table.Description)) { #>
1411
/// <summery>
1512
/// <#= Table.Description #>
1613
/// </summery>
17-
<#
18-
}
19-
#>
14+
<# } #>
2015
public class <#= Table.Name.ToPascalCase() #>
2116
{
22-
<#
23-
foreach (var x in Table.Columns)
24-
{
25-
#>
26-
<#
27-
if (!string.IsNullOrEmpty(x.Description))
28-
{
29-
#>
17+
<# foreach (var x in Table.Columns) { #>
18+
<# if (!string.IsNullOrEmpty(x.Description)) { #>
3019
/// <summery>
3120
/// <#= x.Description #>
3221
/// </summery>
33-
<#
34-
}
35-
#>
22+
<# } // End if #>
3623
public <#= GetColumnType(x) #> <#= x.Name.ToPascalCase() #> { get; <#= x.IsPrimary ? "" : "set; " #>}
3724

38-
<#
39-
}
40-
#>
25+
<# } // End foreach #>
4126
public <#= Table.Name.ToPascalCase() #>(
4227
<#= string.Join(",\n ", Table.Columns.Where(x => x.IsPrimary).Select(d => $"{GetColumnType(d)} {d.Name.ToCamelCase()}")) #>
4328
)
4429
{
45-
<#
46-
foreach (var x in Table.Columns.Where(x => x.IsPrimary))
47-
{
48-
#>
30+
<# foreach (var x in Table.Columns.Where(x => x.IsPrimary)) { #>
4931
<#= x.Name.ToPascalCase() #> = <#= x.Name.ToCamelCase() #>;
50-
<#
51-
}
52-
#>
32+
<# } #>
5333
}
5434

5535
public <#= Table.Name.ToPascalCase() #>(
5636
<#= string.Join(",\n ", Table.Columns.Select(d => $"{GetColumnType(d)} {d.Name.ToCamelCase()}")) #>
5737
)
5838
{
59-
<#
60-
foreach (var x in Table.Columns)
61-
{
62-
#>
39+
<# foreach (var x in Table.Columns) { #>
6340
<#= x.Name.ToPascalCase() #> = <#= x.Name.ToCamelCase() #>;
64-
<#
65-
}
66-
#>
41+
<# } #>
6742
}
6843
}
6944
}

0 commit comments

Comments
 (0)