一鍵生成dotnet5專案解決方案

輪迴發表於2021-04-19

> 作為一名從2002年.Net 1.0一路走來的老碼農,也持續跟進了dotnet core 1.0~3.1的變革,並不離不棄的玩起了dotnet 5。

每次接到新專案,都要從頭搭建專案解決方案,簡單的三層框架雖說不難,但搭一搭也要花不少時間,邏輯分層,專案之間的引用關係總要分一下的。為了提高效率,就把這些繁瑣的工作交給指令碼吧!

自動生成.net5專案解決方案的核心是dotnet命令,命令的詳細用法請參考官方文件:https://docs.microsoft.com/zh-cn/dotnet/core/tools/dotnet

 

dotnet 命令

命令函式
dotnet build 生成 .NET Core 應用程式。
dotnet build-server 與通過生成啟動的伺服器進行互動。
dotnet clean 清除生成輸出。
dotnet help 顯示命令更詳細的線上文件。
dotnet migrate 將有效的預覽版 2 專案遷移到 .NET Core SDK 1.0 專案。
dotnet msbuild 提供對 MSBuild 命令列的訪問許可權。
dotnet new 為給定的模板初始化 C# 或 F# 專案。
dotnet pack 建立程式碼的 NuGet 包。
dotnet publish 釋出 .NET 依賴於框架或獨立應用程式。
dotnet restore 還原給定應用程式的依賴項。
dotnet run 從源執行應用程式。
dotnet sln 用於新增、刪除和列出解決方案檔案中專案的選項。
dotnet store 將程式集儲存到執行時包儲存區。
dotnet test 使用測試執行程式執行測試。

請複製指令碼另存為文字檔案,改名如:DotNet5.Demo.bat,其中檔名就是解決方案的名字,執行批處理後,會自動建立如圖所以的.net5解決方案。大家可以根據自己的實際需求修改指令碼。

@echo 請修改批處理檔名後,如:Example.Core ,註釋掉goto end,再執行指令碼
@pause

set name=%~n0

::goto end

mkdir %name%.Model
cd %name%.Model
dotnet new classlib --framework net5.0
dotnet add package log4net
dotnet add package Newtonsoft.Json
dotnet add package Dapper
dotnet add package Oracle.EntityFrameworkCore
dotnet add package Oracle.ManagedDataAccess.Core
dotnet add package Microsoft.EntityFrameworkCore
dotnet add package Microsoft.EntityFrameworkCore.Relational
dotnet add package Microsoft.EntityFrameworkCore.Design
dotnet add package Microsoft.EntityFrameworkCore.Tools
cd ..

mkdir %name%.General
cd %name%.General
dotnet new classlib --framework net5.0
dotnet add package log4net
dotnet add package Newtonsoft.Json
cd ..

mkdir %name%.DAL
cd %name%.DAL
dotnet new classlib --framework net5.0
dotnet add package log4net
dotnet add package Newtonsoft.Json
dotnet add package Dapper
dotnet add package Oracle.EntityFrameworkCore
dotnet add package Oracle.ManagedDataAccess.Core
cd ..
dotnet add %name%.DAL/%name%.DAL.csproj reference %name%.Model/%name%.Model.csproj

mkdir %name%.BLL
cd %name%.BLL
dotnet new classlib --framework net5.0
dotnet add package log4net
dotnet add package Newtonsoft.Json
cd ..
dotnet add %name%.BLL/%name%.BLL.csproj reference %name%.Model/%name%.Model.csproj
dotnet add %name%.BLL/%name%.BLL.csproj reference %name%.General/%name%.General.csproj
dotnet add %name%.BLL/%name%.BLL.csproj reference %name%.DAL/%name%.DAL.csproj

mkdir %name%.Api
cd %name%.Api
dotnet new webapi
dotnet add package log4net
dotnet add package Newtonsoft.Json
dotnet add package Autofac
dotnet add package Autofac.Extensions.DependencyInjection
dotnet add package AutoMapper
dotnet add package Dapper
dotnet add package Microsoft.Extensions.Options
dotnet add package Microsoft.Extensions.Caching.Abstractions
dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design
dotnet add package Oracle.EntityFrameworkCore
dotnet add package Oracle.ManagedDataAccess.Core
dotnet add package Microsoft.EntityFrameworkCore
dotnet add package Microsoft.EntityFrameworkCore.Relational
dotnet add package Microsoft.EntityFrameworkCore.Design
dotnet add package Microsoft.EntityFrameworkCore.Tools
cd ..
dotnet add %name%.Api/%name%.Api.csproj reference %name%.Model/%name%.Model.csproj
dotnet add %name%.Api/%name%.Api.csproj reference %name%.General/%name%.General.csproj
dotnet add %name%.Api/%name%.Api.csproj reference %name%.BLL/%name%.BLL.csproj

goto :next

mkdir %name%.Web
cd %name%.Web
dotnet new mvc
dotnet add package log4net
dotnet add package Newtonsoft.Json
dotnet add package Autofac
dotnet add package Autofac.Extensions.DependencyInjection
dotnet add package AutoMapper
dotnet add package AutoMapper.Extensions.Microsoft.DependencyInjection
dotnet add package Dapper
dotnet add package Microsoft.Extensions.Options
dotnet add package Microsoft.Extensions.Caching.Abstractions
dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design
dotnet add package Oracle.EntityFrameworkCore
dotnet add package Oracle.ManagedDataAccess.Core
dotnet add package Microsoft.EntityFrameworkCore
dotnet add package Microsoft.EntityFrameworkCore.Relational
dotnet add package Microsoft.EntityFrameworkCore.Design
dotnet add package Microsoft.EntityFrameworkCore.Tools
cd ..
dotnet add %name%.Web/%name%.Web.csproj reference %name%.Model/%name%.Model.csproj
dotnet add %name%.Web/%name%.Web.csproj reference %name%.General/%name%.General.csproj
dotnet add %name%.Web/%name%.Web.csproj reference %name%.BLL/%name%.BLL.csproj

:next

mkdir %name%.Test
cd %name%.Test
dotnet new console
dotnet add package log4net
dotnet add package Newtonsoft.Json
dotnet add package Dapper
dotnet add package Oracle.EntityFrameworkCore
dotnet add package Oracle.ManagedDataAccess.Core
cd ..
dotnet add %name%.Test/%name%.Test.csproj reference %name%.Model/%name%.Model.csproj
dotnet add %name%.Test/%name%.Test.csproj reference %name%.General/%name%.General.csproj
dotnet add %name%.Test/%name%.Test.csproj reference %name%.BLL/%name%.BLL.csproj
dotnet add %name%.Test/%name%.Test.csproj reference %name%.DAL/%name%.DAL.csproj

mkdir %name%.Tools
cd %name%.Tools
dotnet new winforms
dotnet add package log4net
dotnet add package Newtonsoft.Json
dotnet add package Dapper
dotnet add package Oracle.EntityFrameworkCore
dotnet add package Oracle.ManagedDataAccess.Core
cd ..
dotnet add %name%.Tools/%name%.Tools.csproj reference %name%.Model/%name%.Model.csproj
dotnet add %name%.Tools/%name%.Tools.csproj reference %name%.General/%name%.General.csproj
dotnet add %name%.Tools/%name%.Tools.csproj reference %name%.BLL/%name%.BLL.csproj
dotnet add %name%.Tools/%name%.Tools.csproj reference %name%.DAL/%name%.DAL.csproj

mkdir %name%.Service
cd %name%.Service
dotnet new grpc
dotnet add package log4net
dotnet add package Newtonsoft.Json
dotnet add package Dapper
dotnet add package Oracle.EntityFrameworkCore
dotnet add package Oracle.ManagedDataAccess.Core
dotnet add package Grpc
dotnet add package Grpc.Tools
dotnet add package Google.Protobuf
cd ..
dotnet add %name%.Service/%name%.Service.csproj reference %name%.Model/%name%.Model.csproj
dotnet add %name%.Service/%name%.Service.csproj reference %name%.General/%name%.General.csproj
dotnet add %name%.Service/%name%.Service.csproj reference %name%.BLL/%name%.BLL.csproj
dotnet add %name%.Service/%name%.Service.csproj reference %name%.DAL/%name%.DAL.csproj

:sln

::建立解決方案,並把所有工程專案加入解決方案
dotnet new sln -n %name%

dotnet sln %name%.sln add %name%.Api/%name%.Api.csproj --solution-folder 01Web
dotnet sln %name%.sln add %name%.BLL/%name%.BLL.csproj --solution-folder 02Business
dotnet sln %name%.sln add %name%.DAL/%name%.DAL.csproj --solution-folder 02Business
dotnet sln %name%.sln add %name%.Model/%name%.Model.csproj --solution-folder 02Business
dotnet sln %name%.sln add %name%.General/%name%.General.csproj --solution-folder 03Common
dotnet sln %name%.sln add %name%.Service/%name%.Service.csproj --solution-folder 04Service
dotnet sln %name%.sln add %name%.Test/%name%.Test.csproj --solution-folder 05Test
dotnet sln %name%.sln add %name%.Tools/%name%.Tools.csproj --solution-folder 06Tools

::for /d %%i in (*) do ( 
::echo %%i
::dotnet sln %name%.sln add %%i/%%i.csproj
::)

::編譯解決方案
dotnet build

:end

@echo 構建完成,按任意鍵退出。
@pause

  

 

相關文章