How can I t4 scaffold from powershell.exe?

心不动则不痛 提交于 2019-12-30 11:54:06

问题


NuGet's PM console is great and all but you have to be in Visual Studio to use it. I have asked this question before without any ansewr and I find it hard to believe there is no documentation on the topic.

I have two users: User A sell shoes, User B sells cars. Each have different property needs. If I allow them to write there needed properties/datatypes to their respective Product.cs entity, how can I use PowerShell.exe to:

Scaffold the controller, views, dbcontext and repositories?

I know I can do a MSBuild afterwards, but how is it there seems to be no way to use PowerShell from say a bat file to do the scaffolding outside VS 2010?

Any insight would be appreciated.


回答1:


I finally figured this out after days of no luck. First and foremost, do not use any Microsoft betas, incl Windows 8 Developer Edition, PowerShell 3.0 and VS 2011. Once you have PowerShell 2.0 up and running:

1. in the PS/v1.0 folder add a powershell.exe.config file:

Follow this link for the script:http://connect.microsoft.com/PowerShell/feedback/details/525435/net-4-0-assemblies-and-powershell-v2

restart PowerShell 2.0 and it will now support 4.0 framework.

2. Set the execution policy to require only remote scripts to be sign:

Command line: Set-ExecutionPolicy RemoteSigned

3. Import the required Custom t4 scaffolders (so you can use T4 Scaffold):

Command Line: import-module C:\Users\Admin\Documents\"Visual Studio 2010"\Projects\MvcApplication1\packages\T4Scaffolding.1.0.5\tools\T4Scaffolding.NuGetServices.dll

Command Line: import-module C:\Users\Admin\Documents\"Visual Studio 2010"\Projects\MvcApplication1\packages\T4Scaffolding.1.0.5\tools\T4Scaffolding.dll

4. Now invoke your powershell file:

Command Line:C:\Users\Admin\Documents\"Visual Studio 2010"\Projects\MvcApplication1\MvcApplication1\CodeTemplates\Scaffolders\StevceScaffolders.AjaxGrid\StevceScaffolders.AjaxGrid.ps1

(be sure to quote any folder that has spaces, eg. "Visual Studio 2010"

I certainly think it would have been better if MS provided some insight on this in their documentation.




回答2:


In trying to run it from powershell, you're in hardly-tread territory but its conceivable.

I think the T4 templates are rendered by an external tool. I can't see any reason why you couldn't invoke that EXE from powershell, passing parameters on the command line of the t4 template to be invoked. The resultant file (or files, see the Entity Framework T4 examples for how to write multiple files) would be output by the t4 conversion tool upon completion.

Your various different file types would be handled by different t4 templates, a Controller.t4, a Views.t4, Dbcontext.t4 and respositories.t4.

Hope that helps



来源:https://stackoverflow.com/questions/9049244/how-can-i-t4-scaffold-from-powershell-exe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!