如何从命令行为Visual Studio创建简单的构建脚本? [英] How to create a Simple Build Script for Visual Studio from the Command Line?

查看:269
本文介绍了如何从命令行为Visual Studio创建简单的构建脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在多个目录中都有很多Visual Studio Project解决方案(都带有扩展名.sln),我想编写一个简单的批处理脚本,该脚本将自动构建批处理文件中列出的所有解决方案。

I have a lot of Visual Studio Project Solutions in multiple directories (all with the extension .sln) and I want to write a simple batch script that will automatically build all solutions listed in the batch file.

我可以通过启动 Visual Studio命令提示符(这只是一个带有以下内容的命令行实例)来手动构建解决方案命令执行

I am able to manually build a solution by starting up the Visual Studio Command Prompt (which is just a command-line instance with the following command executed

%comspec% / k C:\Program Files\Microsoft Visual Studio 10.0\VC\vcvarsall。 bat x86

之后,我通过调用以下内容来构建项目:

After which I then build the project by calling:

devenv到solutionfile\projectSolution1.sln的路径 / build调试

这将构建项目(假设该项目没有错误),然后我冲洗并为要构建的每个项目重复。

This will build the project (assuming the project does not have errors) and I rinse and repeat for each project I want built.

但是在名为 build.bat 的批处理文件中:

However when I have the following in a batch file called build.bat:

"%comspec%" /k "C:\Program Files\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" x86  
echo "Starting Build for all Projects with proposed changes" 
echo . 
devenv "path\to\solutionFile\projectSolution2.sln" /build Debug
devenv "another\path\to\solutionFile\projectSolution3.sln" /build Debug
devenv "yet\another\path\to\solutionFile\projectSolution4.sln" /build Debug
echo "All builds completed."
pause

批处理脚本仅执行第一行,并等到我键入 exit 后再执行其他命令。根据对批处理文件和所有StackOverflow问题所做的研究,我对此的理解是 cmd 实际上调用了另一个执行 vcvarsall的实例。 .bat 来设置构建环境。

The batch script only executes the first line, and waits until I type in exit before executing the others. My understanding of this based on the research I have done on batch files and all the StackOverflow questions is that cmd actually invokes another instance of itself that executes vcvarsall.bat to set up the build environment.

这不适用于键入 exit 杀死具有 devenv 设置后,其后的命令将不能作为 devenv 执行(因为导出的路径将不再存在)不被识别为命令

This will not work as typing exit kills that instance with devenv set up and the commands after that cannot execute as devenv is not a recognized command (since the exported path will not exist anymore)

简而言之,如何实现(将其余命令传递给定义了 devenv 的cmd实例)放在一个批处理文件中?我知道这不是调用构建的可靠方法(并且有很多工具可以执行此操作),但是我只是希望有一个批处理脚本来自动执行单独调用这些项目的手动工作。

In short, how can this be achieved (passing in the rest of the commands to the cmd instance with devenv defined) in a single batch file? I understand this isn't a robust way (and there are a lot of tools that do this) of invoking builds but I'm just hoping to have one batch script to automate the manual work of individually calling these projects.

推荐答案

找到解决方案,正如Jimmy所述,需要删除环境变量%comspec% ,因为这是CMD.exe的快捷方式。

Found the solution, as noted by Jimmy, one needs to remove the environment variable %comspec% as that is a shortcut to CMD.exe.

但是,仅删除%comspec / k 会导致CMD实例打开,然后在第二。我以前也尝试过 call 函数,该函数与%comspec%

However, just removing "%comspec" /k will cause a CMD instance to open and then exit after a second. I also previously tried the call function which created a separate CMD instance when used with %comspec%

解决方案是在第一行前添加通话,并删除%comspec

The solution is to add call in front of the first line, and remove %comspec

这是使事情按预期运行的最终批处理文件。

Here is the final batch file that got things working as intended.

@echo OFF 
call "C:\Program Files\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" x86
echo "Starting Build for all Projects with proposed changes"
echo .  
devenv "path\to\solutionFile\projectSolution2.sln" /build Debug 
devenv "another\path\to\solutionFile\projectSolution3.sln" /build Debug 
devenv "yet\another\path\to\solutionFile\projectSolution4.sln" /build Debug 
echo . 
echo "All builds completed." 
pause

请注意 @echo OFF 告诉批处理脚本不要在终端中回显命令(例如该调用命令)(但是仍然会显示错误和警告)

Note that @echo OFF tells the batch script to not echo out the commands (such as that call command) into the terminal (errors and warnings will still be shown, however)

这篇关于如何从命令行为Visual Studio创建简单的构建脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆