visual-studio 在构建时运行 Visual Studio 自定义工具
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/894806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Run a Visual Studio custom tool at build time
提问by mmr
I have a script that will convert a text file into a resource file, so that I can have multiple language support by having text files for different languages converted into different resources. Once I run the script to get the resx file, I then have to run a custom build tool (as described here:Link to Code Project) in order to create the Designer.csfiles to use the new files.
我有一个脚本可以将文本文件转换为资源文件,这样我就可以通过将不同语言的文本文件转换为不同的资源来获得多语言支持。运行脚本以获取 resx 文件后,我必须运行自定义构建工具(如此处所述:链接到代码项目)以创建Designer.cs文件以使用新文件。
In keeping with the philosophy that I should be able to build the entire project with a single button click, how can I remove the step where I have to explicitly call the custom build tool in order to make the codebehind files?
按照我应该能够通过单击一个按钮来构建整个项目的理念,我如何删除必须显式调用自定义构建工具以制作代码隐藏文件的步骤?
I've tried automatically deleting the Designer.csfiles as a pre-build step, since I would think that the custom build tool would automatically run if there were no Designer.cs files, but no dice.
我已经尝试Designer.cs在预构建步骤中自动删除文件,因为我认为如果没有 Designer.cs 文件但没有骰子,自定义构建工具会自动运行。
So, I want my build script in Visual Studio/msbuild to do: 1) convert text to resx (done) 2) move resx files to appropriate directory (done) 3) create designer.cs files using a custom build tool (not done)
因此,我希望我在 Visual Studio/msbuild 中的构建脚本执行以下操作:1)将文本转换为 resx(完成)2)将 resx 文件移动到适当的目录(完成)3)使用自定义构建工具创建 Designer.cs 文件(未完成) )
It's that last step...
这是最后一步...
采纳答案by Martin Peck
Unfortunately I don't think there's an easy way of doing this. Custom Build Tools only run from within VS.NET - they don't run when you build your project using MSBuild from the command line.
不幸的是,我认为没有一种简单的方法可以做到这一点。自定义构建工具仅在 VS.NET 中运行 - 当您从命令行使用 MSBuild 构建项目时,它们不会运行。
In general, if you have the choice of writing a build tool as a Customer Build Tool or an MSBuild Task then choose the MSBuild Task every time as these Tasks will run in VS.NET and from the command line.
通常,如果您可以选择将构建工具编写为客户构建工具或 MSBuild 任务,那么每次都选择 MSBuild 任务,因为这些任务将在 VS.NET 和命令行中运行。
The designer.cs files for resources are there to support your coding. They give you the strongly typed access into the resource file. So, as long as you create your culture invariant resources in VS.NET (and let it create the .designer.cs files) then adding additional language support later (additional .resx files) should work fine.
资源的designer.cs 文件用于支持您的编码。它们为您提供对资源文件的强类型访问。因此,只要您在 VS.NET 中创建文化不变资源(并让它创建 .designer.cs 文件),然后稍后添加额外的语言支持(额外的 .resx 文件)就可以正常工作。
If, however, your text files are your primary resource files, and you're adding new resource strings into these text files first, then you'll need to find another way of generating .cs files that allow you to code against those resources. If you HAD to, generating the .designer.cs file yourself (or somethign similar) wouldn't be that difficult. Using CodeDom or T4 you could create the helper class, using an existing .designer.cs file as a template.
但是,如果您的文本文件是您的主要资源文件,并且您首先将新的资源字符串添加到这些文本文件中,那么您需要找到另一种生成 .cs 文件的方法,以允许您针对这些资源进行编码。如果必须,自己生成 .designer.cs 文件(或类似的文件)不会那么困难。使用 CodeDom 或 T4,您可以创建助手类,使用现有的 .designer.cs 文件作为模板。
回答by hollo
There is a way to generate the *.Designer.cs for *.resx as part of the build process. There is a built-in MSBuild task GenerateResource, which basically a wrapper around the SDK tool resgen.exe. Hereyou can find an example how to use it.
作为构建过程的一部分,有一种方法可以为 *.resx 生成 *.Designer.cs。有一个内置的 MSBuild 任务GenerateResource,它基本上是 SDK 工具 resgen.exe 的包装器。在这里您可以找到如何使用它的示例。
Another thing you would find useful, if the generated *.Designer.cs version is not correct, basically, GenerateResource task is not calling the desired version of resgen.exe, setting property SdkToolsPathmight help you.
您会发现另一件事很有用,如果生成的 *.Designer.cs 版本不正确,基本上,GenerateResource 任务没有调用所需版本的 resgen.exe,设置属性SdkToolsPath可能对您有所帮助。
回答by gokult
Have you tried adding a Exec step in Before/AfterBuild step in your csproj? You have to manually edit the file for this, but that should solve your problem.
您是否尝试在 csproj 的 Before/AfterBuild 步骤中添加 Exec 步骤?您必须为此手动编辑文件,但这应该可以解决您的问题。
I'm not fully clear on if you want this done before or after the build. If you need it done sometime after Pass1/Pass2, you can hook into the targets directly. Try looking into Microsoft.Build.Common.Targets to get a feel for how to do this.
我不完全清楚您是否希望在构建之前或之后完成此操作。如果你需要在 Pass1/Pass2 之后的某个时间完成它,你可以直接钩入目标。尝试查看 Microsoft.Build.Common.Targets 以了解如何执行此操作。

