2009-12-01 69 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" 
} 
相關問題