gpt4 book ai didi

powershell - 使用 PowerShell 自定义 T4 脚手架

转载 作者:行者123 更新时间:2023-12-04 19:12:52 25 4
gpt4 key购买 nike

我想创建一个 定制脚手架 使用任意 电源 逻辑。可以渲染 T4 模板 (如果我愿意,可以有多个),结果是输出:

  • 作为项目中的新文件
  • 作为插入现有类的新代码块
  • 我的 PowerShell 逻辑可以使用 Visual Studio 的“代码模型”API 以其他任意方式操作文件和代码元素。

  • 如何使用 PowerShell 自定义 T4 脚手架?

    最佳答案

    来自 http://blog.stevensanderson.com/2011/04/07/mvcscaffolding-creating-custom-scaffolders/ :

    In Package Manager Console of Visual Studio execute the following command :

    Scaffold CustomScaffolder ClassName

    This adds a CodeTemplates folder to your project, containing files for the new scaffolder.

    As you can see, we’ve got two files:

    • A PowerShell script (.ps1), where we can put arbitrary logic to decide what templates get rendered and where the output goes. By default, it renders a T4 template and uses the output to create a new file called ExampleOutput in the root of your project.
    • A T4 template (.t4), i.e., the thing that the default .ps1 file renders. By default this generates a simple C#/VB class (depending on your project type). If you want to see this working, you can run the custom scaffolder right away:

      Scaffold ClassName

    This will generate a new class file, ExampleOutput.cs, in the root folder of your project. That’s really just to show you how it works. We don’t really want that, so don’t run the new scaffolder yet, or if you already have done, delete ExampleOutput.cs

    关于powershell - 使用 PowerShell 自定义 T4 脚手架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13454166/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com