SmartCode功能強大的代碼生成器
SmartCode = IDataSource -> IBuildTask -> IOutput => Build Everything
Introduction
SmartCode.Generator (代碼生成器)
Demo
Getting Started
- Install from .NET Core Global Tool
dotnet tool install --global SmartCode.CLI
- 編輯構(gòu)建配置文件(默認:SmartCode.yml)
- 命令行執(zhí)行SmartCode命令
- SmartCode
- 等待提示輸入配置文件路徑(可選:默認程序根目錄下SmartCode.yml文件)
- 回車執(zhí)行命令
- 等待任務(wù)執(zhí)行結(jié)束
- 查看輸出目錄結(jié)果
- 運行API項目,進行Swagger調(diào)試
構(gòu)建配置文件
Module: SmartSql.Starter
Author: Ahoo Wang
DataSource:
Name: DbTable
Parameters:
DbName: SmartSqlTestDB
DbProvider: SqlServer
ConnectionString: Data Source=.;Initial Catalog=SmartSqlTestDB;Integrated Security=True
Language: CSharp
TemplateEngine:
Name: Razor
Root: CSharp
Output:
Type: File
Path: 'E:\SmartSql-Starter'
Parameters:
SmartSqlVersion: '4.0.46'
SmartSqlSchemaVersion: '4.0.42'
BuildDir: 'E:\SmartSql-Starter\build'
DockerImage: 'smartsql.starter'
NamingConverter:
Table:
Tokenizer:
Type: Default
Parameters:
IgnorePrefix: 'T_'
Delimiter: '_'
Converter:
Type: Pascal
Parameters: { }
View:
Tokenizer:
Type: Default
Parameters:
IgnorePrefix: 'V_'
Delimiter: '_'
Converter:
Type: Pascal
Column:
Tokenizer:
Type: Default
Parameters:
Delimiter: '_'
Converter:
Type: Pascal
# 構(gòu)建任務(wù)
Build:
# ClearDir:
# Type: Clear
# Parameters:
# Dirs: '.'
MakeBuildDir:
Type: Process
Parameters:
FileName: powershell
Args: mkdir '{{Project.Parameters.BuildDir}}'
Copy:
Type: Process
Parameters:
FileName: powershell
Args: cp '{{Project.ConfigPath}}' '{{Project.Parameters.BuildDir}}'
Scaffolding:
Type: MultiTemplate
Output:
Path: '.'
Parameters:
Templates: [{Key: 'Sln.cshtml',Output: {Name: '{{Project.Module}}',Extension: '.sln'}},
{Key: 'Sln-Directory.Build.cshtml',Output: {Name: 'Directory.Build',Extension: '.props'}},
{Key: 'Sln-Version.cshtml',Output: {Path: 'build',Name: 'version',Extension: '.props'}},
{Key: 'Sln-Dockerfile.cshtml',Output: {Name: 'Dockerfile',Extension: ''}},
{Key: 'Sln-DockerIgnore.cshtml',Output: {Name: '.dockerignore',Extension: ''}},
{Key: 'Sln-GitIgnore.cshtml',Output: {Name: '.gitignore',Extension: ''}},
{Key: "Proj-Entity.cshtml",Output: {Path: 'src/{{Project.Module}}.Entity',Name: '{{Project.Module}}.Entity',Extension: '.csproj'}},
{Key: "Proj-Repository.cshtml",Output: {Path: 'src/{{Project.Module}}.Repository',Name: '{{Project.Module}}.Repository',Extension: '.csproj'}},
{Key: "Proj-Service.cshtml",Output: {Path: 'src/{{Project.Module}}.Service',Name: '{{Project.Module}}.Service',Extension: '.csproj'}},
{Key: "Proj-API.cshtml",Output: {Path: 'src/{{Project.Module}}.API',Name: '{{Project.Module}}.API',Extension: '.csproj'}},
{Key: "API/LaunchSettings.cshtml",Output: {Path: 'src/{{Project.Module}}.API/Properties',Name: 'launchSettings',Extension: '.json'}},
{Key: "API/AppSettings.cshtml",Output: {Path: 'src/{{Project.Module}}.API',Name: 'appsettings',Extension: '.json'}},
{Key: "API/AppSettings-Development.cshtml",Output: {Path: 'src/{{Project.Module}}.API',Name: 'appsettings.Development',Extension: '.json'}},
{Key: "API/Program.cshtml",Output: {Path: 'src/{{Project.Module}}.API',Name: 'Program',Extension: '.cs'}},
{Key: "API/Startup.cshtml",Output: {Path: 'src/{{Project.Module}}.API',Name: 'Startup',Extension: '.cs'}},
{Key: "API/APIException.cshtml",Output: {Path: 'src/{{Project.Module}}.API/Exceptions',Name: 'APIException',Extension: '.cs'}},
{Key: "API/GlobalExceptionFilter.cshtml",Output: {Path: 'src/{{Project.Module}}.API/Filters',Name: 'GlobalExceptionFilter',Extension: '.cs'}},
{Key: "API/GlobalValidateModelFilter.cshtml",Output: {Path: 'src/{{Project.Module}}.API/Filters',Name: 'GlobalValidateModelFilter',Extension: '.cs'}},
{Key: "API/QueryRequest.cshtml",Output: {Path: 'src/{{Project.Module}}.API/Messages',Name: 'QueryRequest',Extension: '.cs'}},
{Key: "API/QueryByPageRequest.cshtml",Output: {Path: 'src/{{Project.Module}}.API/Messages',Name: 'QueryByPageRequest',Extension: '.cs'}},
{Key: "API/ResponseMessage.cshtml",Output: {Path: 'src/{{Project.Module}}.API/Messages',Name: 'ResponseMessage',Extension: '.cs'}},
{Key: "API/QueryResponse.cshtml",Output: {Path: 'src/{{Project.Module}}.API/Messages',Name: 'QueryResponse',Extension: '.cs'}},
{Key: "API/QueryByPageResponse.cshtml",Output: {Path: 'src/{{Project.Module}}.API/Messages',Name: 'QueryByPageResponse',Extension: '.cs'}},
{Key: "API/ResponseMessage.cshtml",Output: {Path: 'src/{{Project.Module}}.API/Messages',Name: 'ResponseMessage',Extension: '.cs'}},
{Key: "SqlMapConfig.cshtml",Output: {Path: 'src/{{Project.Module}}.Repository',Name: 'SmartSqlMapConfig',Extension: '.xml'}},
{Key: "SqlMapConfig.cshtml",Output: {Path: 'src/{{Project.Module}}.Repository',Name: 'SmartSqlMapConfig.Development',Extension: '.xml'}}]
Entity:
Type: Table
Module: Entity
TemplateEngine:
Path: Entity.cshtml
Output:
Path: 'src/{{Project.Module}}.{{Build.Module}}'
Name: '{{Items.CurrentTable.ConvertedName}}'
Extension: '.cs'
Repository:
Type: Table
Module: Repository
TemplateEngine:
Path: Repository.cshtml
IgnoreNoPKTable: true
IgnoreView: true
Output:
Path: 'src/{{Project.Module}}.{{Build.Module}}'
Name: 'I{{Items.CurrentTable.ConvertedName}}Repository'
Extension: .cs
Service:
Type: Table
Module: Service
TemplateEngine:
Path: Service.cshtml
IgnoreNoPKTable: true
IgnoreView: true
Output:
Path: 'src/{{Project.Module}}.{{Build.Module}}'
Name: '{{Items.CurrentTable.ConvertedName}}Service'
Extension: .cs
APIController:
Type: Table
Module: API
TemplateEngine:
Path: API/APIController.cshtml
IgnoreNoPKTable: true
IgnoreView: true
Output:
Path: 'src/{{Project.Module}}.{{Build.Module}}/Controllers'
Name: '{{Items.CurrentTable.ConvertedName}}Controller'
Extension: .cs
SqlMap:
Type: Table
TemplateEngine:
Path: SqlMap.cshtml
Output:
Path: 'src/{{Project.Module}}.Repository/Maps'
Name: '{{Items.CurrentTable.ConvertedName}}'
Extension: .xml
IgnoreNoPKTable: true
IgnoreView: true
# Please install dotnet-format first!
# dotnet tool install -g dotnet-format
CodeFormat:
Type: Process
Parameters:
FileName: powershell
WorkingDirectory: '{{Project.Output.Path}}'
Args: dotnet-format
ReStore:
Type: Process
Parameters:
FileName: powershell
WorkingDirectory: '{{Project.Output.Path}}'
Args: dotnet restore
# BuildDocker:
# Type: Process
# Parameters:
# FileName: powershell
# WorkingDirectory: '{{Project.Output.Path}}'
# Args: docker build -t {{Project.Parameters.DockerImage}}:v1.0.0 .
# RunDocker:
# Type: Process
# Parameters:
# FileName: powershell
# WorkingDirectory: '{{Project.Output.Path}}'
# Args: docker run --name {{Project.Parameters.DockerImage}} --rm -d -p 8008:80 {{Project.Parameters.DockerImage}}:v1.0.0 .
# Publish:
# Type: Process
# Parameters:
# FileName: powershell
# WorkingDirectory: '{{Project.Output.Path}}'
# Args: dotnet publish -c Release -o '{{Project.Output.Path}}\publish'
# Run:
# Type: Process
# Parameters:
# FileName: powershell
# WorkingDirectory: '{{Project.Output.Path}}\publish'
# CreateNoWindow: false
# RedirectStandardOutput: false
# RedirectStandardError: false
# WaitForExit: false
# WriteLines: ['dotnet {{Project.Module}}.API.dll']
# RunChrome:
# Type: Process
# Parameters:
# FileName: C:\Program Files (x86)\Google\Chrome\Application\chrome.exe
# CreateNoWindow: false
# Args: http://localhost:8008/swagger
構(gòu)建文件參數(shù)概覽
| 參數(shù)名 | 說明 |
|---|---|
| Module | 根模塊名 |
| Author | 作者 |
| DataSource | 數(shù)據(jù)源 |
| Language | 語言:CSharp/Java/.... |
| TemplateEngine | 模板引擎:目前內(nèi)置:Razor/Handlebars |
| Output | 輸出 |
| Build | 任務(wù)構(gòu)建s |
DataSource 數(shù)據(jù)源,Name:DbTable
屬性 Name:DbTable,使用 DbTableSource 插件作為數(shù)據(jù)源
DbTableSource.Parameters 接受以下三個參數(shù):
| 參數(shù)名 | 說明 |
|---|---|
| DbName | 數(shù)據(jù)庫名稱 |
| DbProvider | 數(shù)據(jù)驅(qū)動提供者:MySql,MariaDB,PostgreSql,SqlServer,Oracle,SQLite |
| ConnectionString | 連接字符串 |
Output 輸出
| 參數(shù)名 | 說明 |
|---|---|
| Type | File |
| Path | 輸出目錄 |
| Name | 文件名 |
| Extension | 擴展名 |
| Mode | 輸出模式,默認:Incre。Incre:增量創(chuàng)建,如果文件存在則忽略 。Full:全量創(chuàng)建,如果存在則重新創(chuàng)建 |
Build 任務(wù)構(gòu)建
| 參數(shù)名 | 說明 |
|---|---|
| Type | 構(gòu)建類型,Clear:用于清理目錄s/文件s, Project:用于構(gòu)建單文件,如:解決方案文件/項目文件, MultiTemplate:多模板構(gòu)建多個文件, Table: 用于構(gòu)建以數(shù)據(jù)表為基礎(chǔ)的文件,如:Entity,Repository文件, Process: 啟動新進程 |
| Module | 構(gòu)建模塊名 |
| TemplateEngine | 模板引擎,可選,默認使用根模塊引擎 |
| Template | 模板文件 |
| Output | 輸出 |
| IgnoreNoPKTable | 忽略無主鍵表 |
| IgnoreView | 忽略視圖 |
| IncludeTables | 包括表名s |
| IgnoreTables | 忽略表名s |
| NamingConverter | 命名轉(zhuǎn)換器 |
| Parameters | 自定義構(gòu)建參數(shù) |
NamingConverter 命名轉(zhuǎn)換
| 屬性 | 說明 |
|---|---|
| 類型 | Table/View/Column |
| Tokenizer | 分詞器 |
| Converter | 轉(zhuǎn)換器:Camel/Pascal/None |
NamingConverter.Tokenizer 分詞器
| 屬性 | 說明 |
|---|---|
| Type | Default |
| Parameters.IgnorePrefix | 忽略前綴字符 |
| Parameters.Delimiter | 分隔符 |
| Parameters.UppercaseSplit | 使用大寫分隔,默認:true |
評論
圖片
表情
