2009-12-01 76 views
3

我已经放在一起构建PSake(v2.0)脚本,并且脚本将$psake.build_success属性设置为true,即使认为对MSBuild的调用失败。任何人都可以告诉我如何改变脚本,以便在MSBuild调用失败时$psake.build_success属性将正确返回false确定是否使用MSBuild和PSake编译解决方案

我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 
    } 
} 
+3

当前psake有一个挂钩'{'只会回显内容而不执行它们的错误。所以,转到'任务BuildSolution {',你应该有更好的结果。 – 2010-07-22 16:52:21

+0

布雷特,感谢您的信息,非常感谢。如果你将它作为下面的答案,我会接受它作为正式答案。 – MagicAndi 2010-07-22 20:48:30

回答

3

是PowerShell的本地$lastExitCode(即,WIN32的ExitCode)的背景下,有什么用处?我猜测只有当你调用一个与psake相关的cmdlet时,内置的才是相关的。

即与

if($lastexitcode -eq 0) { 

免责声明替换检查:用psake只播客级经验:d

+1

谢谢鲁本,因为实际调用MSBuild实际上是成功的,但它启动的构建操作失败,这是行不通的。对于我将来要使用的有用代码片段,请+1。 – MagicAndi 2009-12-01 16:29:22

+1

我很确定msbuild应该设置'lastExitCode' - 你想要去多少层次?通常,任何msbuild执行失败都应该冒泡(即任何失败的子构建都会返回一个非零的退出代码,并触发parrent失败等等,这里讨论了这个概念: - http://code.google.com/p/psake /问题/细节?ID = 9 – 2009-12-02 08:33:44

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) 
} 
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处理等待进程

1

有psake Exec命令,你可以包装msbuild并引发powershell错误。

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