【问题标题】:Determine If Solution Compiles using MSBuild and PSake确定解决方案是否使用 MSBuild 和 PSake 编译
【发布时间】:2009-12-01 12:36:29
【问题描述】:

我已经编写了一个 PSake (v2.0) 构建脚本,该脚本将 $psake.build_success 属性设置为 true,即使对 MSBuild 的调用失败。谁能告诉我如何更改脚本,以便在 MSBuild 调用失败时$psake.build_success 属性将正确返回false

我的 PSake 构建脚本如下:

properties {
    $solutionFile = 'SOLUTION_FILE'
    $buildSuccessfulMessage = 'Solution Successfully Built!'
    $buildFailureMessage = 'Solution Failed to Build!'
    $cleanMessage = 'Executed Clean!'
}

task default -depends BuildSolution 

task BuildSolution
{
    msbuild $solutionFile /t:Clean,Build
    if ($psake.build_success) 
    {
        $buildSuccessfulMessage
    } 
    else 
    {
        $buildFailureMessage
    }
}

【问题讨论】:

  • 目前 psake 有一个错误,即挂起 { 只会回显内容而不执行它们。所以,改成task BuildSolution {,应该会有更好的结果。
  • 布雷特,感谢您提供的信息,非常感谢。如果您将其作为答案留在下面,我会接受它作为官方答案。

标签: powershell msbuild powershell-2.0 psake


【解决方案1】:

PowerShell 的原生 $lastExitCode(即 WIn32 ExitCode)在上下文中是否有用?我猜只有当你调用 psake 相关的 cmdlet 时,内置的才相关。

即,将检查替换为

if($lastexitcode -eq 0) {

免责声明:只有 psake 的播客级别体验:D

【讨论】:

  • 感谢 Ruben,因为对 MSBuild 的实际调用实际上是成功的,但是它发起的构建操作失败了,这是行不通的。 +1 一个有用的代码 sn-p 我将在未来使用。
  • 我很确定 msbuild 应该设置 lastExitCode - 你想深入多少层?通常,任何 msbuild 执行失败都应该冒泡(即任何失败的子构建都会返回非零退出代码并触发父级失败等等。这讨论了这个概念:-code.google.com/p/psake/issues/detail?id=9
【解决方案2】:

问题似乎是对 MSBuild 操作的调用实际上成功完成,而它启动的构建操作失败。我能够解决这个问题的方法是将 MSBuild 调用的输出通过管道传输到文本文件,然后解析文件中的字符串“Build Failed”。如果它包含字符串,显然构建失败。

我的 PSake 构建脚本如下:

properties {
    $solutionFile = 'SOLUTION_FILE'
    $buildSuccessfulMessage = 'Solution Successfully Built!'
    $buildFailureMessage = 'Solution Failed to Build!'
    $cleanMessage = 'Executed Clean!'
}

task default -depends Build 

task Build -depends Clean {
    msbuild $solutionFile /t:Build /p:Configuration=Release >"MSBuildOutput.txt"
}

task Clean {
    msbuild $solutionFile /t:Clean 
}

在我的调用脚本中:

function Check-BuildSuccess()
{
    return (! (Find-StringInTextFile  -filePath .\MSBuildOutput.txt -searchTerm "Build Failed"))
}

function Is-StringInTextFile
(
    [string]$filePath = $(Throw "File Path Required!"),
    [string]$searchTerm = $(Throw "Search Term Required!")
)
{
    $fileContent = Get-Content $filePath    
    return ($fileContent -match $searchTerm)
}

【讨论】:

    【解决方案3】:

    您可以使用 psake Exec 命令包装 msbuild 并引发 powershell 错误。

    Exec {
         msbuild $solutionFile "/p:Configuration=$buildConfiguration;Platform=$buildPlatform;OutDir=$tempOutputDirectory"
    }
    

    【讨论】:

      【解决方案4】:

      $LastExitCode 或 $_ 都不适合我。然而,这确实:

      $buildArgs = "MySolution.sln", "/t:Build", "/p:Configuration=Debug"
      $procExitCode = 0
      $process = Start-Process -FilePath "msbuild" -ArgumentList $buildArgs -NoNewWindow -PassThru
      Wait-Process -InputObject $process
      $procExitCode = $process.ExitCode
      
      #aha! msbuild sets the process exit code but powershell doesn't notice
      if ($procExitCode -ne 0)
      {
          throw "msbuild failed with exit code $procExitCode."
      }
      

      附:如果您在生产中使用它,我建议将 -timeout 处理添加到 Wait-Process

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-05
        • 2012-09-21
        • 2019-02-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多