Dav*_*ier 4 msbuild command-line code-generation visual-studio-2010
我正在开发一个项目,该项目使用代码生成来使用基于文本的描述中的命令行工具生成C#类.我们将开始使用这些javascript描述.
目前生成这些类然后签入,但是,我希望能够自动生成代码,以便将任何更改传播到两个版本.
手动运行的步骤是:
servicegen.exe -i:MyService.txt -o:MyService.cs
Run Code Online (Sandbox Code Playgroud)
当我构建时,我希望MSBuild/VS首先生成CS文件然后编译它.可以通过修改csproj,或许使用MSBuild任务来实现这一点Exec,DependentUpon&AutoGen?
通常我会建议将预构建命令放在预构建事件中,但由于您的命令行工具将创建编译所需的C#类,因此应该在.csproj文件中的BeforeBuild目标中完成.原因是因为MSBuild在调用BeforeBuild的时间和整个过程中调用PreBuildEvent的时间之间查找需要编译的文件(您可以在MSBuild使用的Microsoft.Common.targets文件中看到此流程) .
从BeforeBuild目标中调用Exec任务以生成文件:
<Target Name="BeforeBuild">
<Exec Command="servicegen.exe -i:MyService.txt -o:MyService.cs" />
</Target>
Run Code Online (Sandbox Code Playgroud)
有关为Exec任务指定不同选项的更多详细信息,请参阅Exec任务 MSDN文档.
Antlr 有一个流程示例,可用于将生成的代码添加到项目中。这样做的优点是可以显示嵌套在源文件下生成的文件,尽管添加起来比较复杂。
您需要添加一个包含要生成的文件的项目组,例如:
<ItemGroup>
<ServiceDescription Include="MyService.txt"/>
</ItemGroup>
Run Code Online (Sandbox Code Playgroud)
然后将要生成的cs文件添加到包含其余源代码的ItemGroup中。
<ItemGroup>
...
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
...etc..
<Compile Include="MyService.txt.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>MyService.txt</DependentUpon> <!--note: this should be the file name of the source file, not the path-->
</Compile>
</ItemGroup>
Run Code Online (Sandbox Code Playgroud)
然后最后添加构建目标来执行代码生成(使用 % 为 ItemGroup 中的每个项目执行命令)。可以将其放入单独的文件中,以便可以将其包含在许多项目中。
<Target Name="GenerateService">
<Exec Command="servicegen.exe -i:%(ServiceDescription.Identity) -o:%(ServiceDescription.Identity).cs" />
</Target>
<PropertyGroup>
<BuildDependsOn>GenerateService;$(BuildDependsOn)</BuildDependsOn>
</PropertyGroup>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1469 次 |
| 最近记录: |