确定解决方案是否使用MSBuild和PSake进行编译

时间:2009-12-01 12:36:29

标签: powershell msbuild powershell-v2.0 psake

我已经整理了一个PSake(v2.0)构建脚本,并且该脚本将$psake.build_success属性设置为true,即使对MSBuild的调用失败也是如此。任何人都可以告诉我如何更改脚本,以便$psake.build_success属性在MSBuild调用失败时正确返回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 个答案:

答案 0 :(得分:3)

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

即,用

替换支票
if($lastexitcode -eq 0) {

免责声明:仅使用psake的播客级别体验:D

答案 1 :(得分: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)
}

答案 2 :(得分:1)

你可以使用psake Exec命令包装msbuild并抛出PowerShell错误。

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

答案 3 :(得分: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."
}

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