【问题标题】:Making pre- and post-build event scripts pretty?使构建前和构建后的事件脚本漂亮吗?
【发布时间】:2011-09-10 04:48:47
【问题描述】:

我的 Visual Studio 2008 项目有一些相当大的构建前和构建后事件脚本(实际上它主要是构建后事件脚本)。它们工作正常,因为它们运行正常,当我 exit 0 构建成功时,当我 exit 1 构建失败并出现错误时。然而,这个错误是巨大的,并且是这样的:

The command "if Release == Debug goto Foo
if Release == Release goto Bar
exit 0

:Foo
mkdir "abc"
copy "$(TargetDir)file.dll" "abc"
[...]
" exited with code 1.

你明白了。整个脚本总是作为错误描述的一部分被转储出去。在构建过程中,整个脚本也会在“输出”窗口中转储。那么,为什么我会在网上看到各种关于在这些脚本中使用 echo 的参考资料?例如,这里是一个特定站点上的示例的一部分:

:BuildEventFailed
echo POSTBUILDSTEP for $(ProjectName) FAILED
exit 1
:BuildEventOK
echo POSTBUILDSTEP for $(ProjectName) COMPLETED OK

有没有办法让 Visual Studio 抑制除 echoed 之外的所有脚本输出(因此使用 echo 仅输出您想要的内容是有意义的),或者这些示例只是被误导了,它们没有意识到整个脚本总是被丢弃?

【问题讨论】:

    标签: c# visual-studio visual-studio-2008 scripting build


    【解决方案1】:

    @Jez 你的回答很好,但我会改进批处理文件如下: 通过使用 %~1 可以更轻松地从 args 中删除引号:

    @echo off
    if "%~1"=="" (set ErrorMessage=Parameter 1 missing. It has to be the Config name. & GOTO :EndError)
    if "%~2"=="" (set ErrorMessage=Parameter 2 missing. It has to be the Project name. & GOTO :EndError)
    if "%~3"=="" (set ErrorMessage=Parameter 3 missing. It has to be the Targetdir. & GOTO :EndError)
    if "%~4"=="" (set ErrorMessage=Parameter 4 missing. It has to be the Projectdir. & GOTO :EndError)
    
    cd /D "%~dp0"  &:: setting current directory to there where the batchfile is.
    
    :: With ~ the quotes are removed
    set _configName=%~1 
    set _projectName=%~2
    set _targetDir=%~3
    set _projectDir=%~4
    set _outDir=%~5
    
    rd Plugins\%_projectName% /S /Q
    if errorlevel 1 (set ErrorMessage=remove directory failed!!. & GOTO :EndError)
    md Plugins\%_projectName%
    if errorlevel 1 (set ErrorMessage=make directory failed!!. & GOTO :EndError)
    xcopy %_targetDir%* Plugins\%_projectName% /E /Q
    if errorlevel 1 (set ErrorMessage=Copy failed!!. & GOTO :EndError)
    
    exit 0
    
    :EndError
    echo --! Error in %~nx0 !-- 
    echo %ErrorMessage%
    pause
    exit 1
    

    【讨论】:

      【解决方案2】:

      好的 - 问题的核心似乎是 Visual Studio C++ 和 C# 构建引擎完全不同。

      C++ 构建引擎执行项目的构建前或构建后事件“命令行”中指定的批处理代码而不将实际代码转储到输出窗口;它只是转储代码echoed 的内容,以及代码执行的命令发出的内容(例如copy 命令的“复制的2 个文件”等)。在网上看到的大概是针对 Visual Studio 的 C++ 构建引擎的,因为如果您将 C# 构建前或构建后的批处理代码放在 C# 项目的“事件”中,则无需真正与 C# 构建引擎回显任何内容命令行'框。

      这是因为我使用的 C# 构建引擎确实将该框中的所有代码转储到“输出”窗口。更重要的是,如果代码失败,它会在错误消息中包含该框中的整个代码块,该错误消息将出现在“错误列表”中 - C++ 构建引擎不会(稍后会详细介绍)。

      因此,我发现的最佳解决方案是尽量减少您在 C# 项目的构建前或构建后事件命令行框中放入的代码量。你放在那里的所有东西都会在执行时被转储到输出窗口。最小化代码的最好方法是让它只执行一个批处理文件,并将必要的参数传递给批处理文件。批处理文件的内容不会转储到“输出”窗口,但是(就像 C++ 构建引擎的“命令行”中的代码一样)echo 的输出和批处理文件代码执行的命令的输出将是;这是控制 C# 预构建或构建后脚本输出的好方法。因此,C++ 构建引擎处理“命令行”框中指定的代码的方式与 C# 引擎处理批处理文件中的代码的方式相同;它不会转储代码本身,只会转储代码的输出。

      因此,基本上,如果您在 Visual Studio 中编译 C++ 项目,您只需将所有批处理脚本放在“命令行”框中,它就不会全部转储到“输出”窗口。但是,如果编译 C# 项目,我建议将您的批处理脚本放在单独的 .bat 文件中,然后使用构建前或构建后事件命令行框中的适当参数调用该文件。我最终得到了我的 C# 构建后事件命令行框,如下所示:

      ..\..\BuildScripts\PostBuild.bat $(ConfigurationName) $(ProjectName) "$(TargetDir)" "$(ProjectDir)"
      

      ...和我的 PostBuild.bat 文件如下所示:

      @REM Store args...
      set _configName=%1%
      set _projectName=%2%
      
      @REM Remove quotes from remaining args...
      set _targetDir=###%3%###
      set _targetDir=%_targetDir:"###=%
      set _targetDir=%_targetDir:###"=%
      set _targetDir=%_targetDir:###=%
      
      set _projectDir=###%4%###
      set _projectDir=%_projectDir:"###=%
      set _projectDir=%_projectDir:###"=%
      set _projectDir=%_projectDir:###=%
      
      if %_configName% == Release goto StartProcessing
      echo No post-build processing required.
      exit 0
      
      @REM Start post-build processing
      :StartProcessing
      echo Starting post-build processing.
      
      @REM use input args to do batch script work, such as:
      copy "%_targetDir%*.dll" "%_projectDir%..\..\..\..\..\CommonAssemblies"
      if errorlevel 1 goto CopyFailure
      
      @REM etc.
      
      goto PostBuildSuccess
      
      @REM Failure labels
      :CopyFailure
      echo Post-build processing for %_projectName% FAILED: Failed to copy file(s) to common assemblies directory!
      exit 1
      
      @REM Post-build success
      :PostBuildSuccess
      echo Post-build processing for %_projectName% completed OK.
      exit 0
      

      这样可以更简洁地控制输出,只有在这个批处理脚本中echoed 的东西才会输出到输出窗口。

      最后,如上所述,当构建后处理失败时(即批处理代码以@以外的代码退出),C# 和 C++ 构建引擎也会在“错误列表”中出现的错误消息中输出不同的内容987654328@)。 C++ 构建引擎似乎总是说:

      Error result 1 returned from 'C:\Windows\system32\cmd.exe'.
      

      但是,C# 构建引擎将在错误消息中包含构建前或构建后事件命令行框的全部内容(以失败者为准),例如:

      The command "..\..\BuildScripts\PostBuild.bat Release MyProject "C:\Development\MyProject\bin\" "C:\Development\MyProject\"" exited with code 1.
      

      对于单个错误消息仍然有点拗口,但比将我们移动到批处理文件中的所有代码都包含在错误消息中更易于管理!

      即便如此,如果能够自定义出现在错误列表中的此错误消息以显示我定义的内容,那就太好了。我怀疑这是否可能,但如果有人知道这样做的方法,请不要犹豫,将其作为评论发布!

      【讨论】:

        【解决方案3】:

        如果你把你的脚本放在一个批处理文件中,VS 会对此保持沉默。当您需要访问 $(projectName) 等时,您必须将它们作为参数传递给批处理文件。我查找了可能会影响输出的设置,但没有找到。

        已使用此批处理文件 (d:\test.bat) 进行测试; @echo off 甚至不需要:

        @echo off
        echo "I'm Gonna Fail"
        exit 1
        

        Pre-Build Event->Command Line 设置为d:\test.bat,将Description 留空(描述是显示的内容,而不是执行预构建事件... . 在 VS2008 中,我在 C++ 项目的构建输出窗口中得到了这个:

        Performing Pre-Build Event...
        "I'm Gonna Fail"
        Project : error PRJ0002 : Error result 1 returned from 'C:\Windows\system32\cmd.exe'.
        

        对于 C# 项目,将显示此内容(MSBuild verbosity 设置为 正常):

        Target PreBuildEvent:
          d:\test.bat
          "I'm gonna fail"
          Microsoft.Common.targets(895,9) error MSB3073: The command "d:\test.bat" exited with code 1.
        

        【讨论】:

        • @stijn 很好奇,我的输出中没有看到Performing Pre-Build Event...。您使用的是什么版本的 Visual Studio?另外,您可以粘贴“预构建事件命令行”窗口内容的全部内容吗?
        • 更新了答案。你能准确地发布你在命令行中使用的内容吗?我尝试了几件事,但无法显示整个命令行
        • @stijn Ahhh... 你的是 C++ 版本,而我正在构建一个 C# 项目,这就解释了为什么我看不到 Performing Pre-Build Event 输出。我确实看到了第二个。我将尝试运行一个 .bat 文件,看看会发生什么。我不太明白您所说的“我无法让它显示整个命令行”是什么意思。你的命令行是d:\test.bat;它在您的输出中显示整个命令行,不是(对于 C# 构建)吗?
        • 好吧,我尝试了if $(Configuration) == Release call d:\test.bat 之类的东西,但它仍然没有显示整行。
        • @stijn 你确定你正在构建一个 C# 项目吗?它肯定在我的输出中给了我整个 shebang(虽然不是 .bat 文件的内容):Target PostBuildEvent: ..\..\BuildScripts\CampusPostBuild.bat Release C:\dev\[my other args here...]
        猜你喜欢
        • 2011-09-05
        • 2019-12-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-24
        相关资源
        最近更新 更多