3

我已经整理了一个 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
    }
}
4

4 回答 4

3

PowerShell 的本机$lastExitCode(即 WIn32 ExitCode)在上下文中是否有用?我猜想内置的只有在您调用与 psake 相关的 cmdlet 时才相关。

即,将支票替换为

if($lastexitcode -eq 0) {

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

于 2009-12-01T12:39:49.813 回答
3

问题似乎是对 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)
}
于 2009-12-01T16:30:47.107 回答
1

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

Exec {
     msbuild $solutionFile "/p:Configuration=$buildConfiguration;Platform=$buildPlatform;OutDir=$tempOutputDirectory"
}
于 2016-04-03T05:51:13.277 回答
0

$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."
}

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

于 2014-01-30T19:02:31.123 回答