在我们的psake脚本中,在编译之前,我们正在运行签出程序集文件。 成功完成编译任务后,将复制程序集,然后签入任务将提交程序集。
这种方法的问题是如果停止编译步骤,文件将被保存结帐。
如果编译任务失败,那么我想运行另一个任务来撤消签出文件。
task CheckOutFiles -description "Checkout the files" `
-precondition { $OutputFiles -ne $null } `
-action {
foreach( $file in $OutputFiles ) {
exec { Checkout-File -FilePath $file }
}
}
task Compile `
-depends Clean `
-description "Compile the code" `
-requiredVariables solutionFile, buildConfiguration `
{
Write-Host "Building solution $solutionFile" | Out-Null
Assert ( Test-Path $solutionFile ) "SolutionFile $SolutionFile is not found "
Exec { msbuild $SolutionFile "/t:build" "/p:Configuration=$buildConfiguration" }
}
task UndoCheckOutFiles -description "UndoCheckout the files" `
-precondition { $OutputFiles -ne $null } `
-action {
foreach( $file in $OutputFiles ) {
exec { UndoCheckout-File -FilePath $file }
}
}
在编译任务中,有没有办法,如果失败,我可以运行UndoCheckoutFiles任务?
答案 0 :(得分:1)
以下两个解决方案的关键是使用try/catch/finally来处理MSBuild异常。
如果您想将UndoCheckOutFiles
作为单独的任务运行,则可以运行报告外部(Compile
)失败的嵌套构建,但内部(UndoCheckOutFiles
)成功:
...
task Compile `
-depends Clean `
-description "Compile the code" `
-requiredVariables solutionFile, buildConfiguration `
{
Write-Host "Building solution $solutionFile" | Out-Null
Assert ( Test-Path $solutionFile ) "SolutionFile $SolutionFile is not found"
Try
{
Exec { msbuild $SolutionFile "/t:build" "/p:Configuration=$buildConfiguration" }
}
Catch
{
Write-Error $_;
}
Finally
{
Invoke-psake -taskList UndoCheckoutFiles
}
}
task UndoCheckOutFiles -description "UndoCheckout the files" `
-precondition { $OutputFiles -ne $null } `
-action {
foreach( $file in $OutputFiles ) {
exec { UndoCheckout-File -FilePath $file }
}
要获得更清晰的构建输出,只需将UndoCheckOutFiles
操作移到Compile
任务中:
...
task Compile `
-depends Clean `
-description "Compile the code" `
-requiredVariables solutionFile, buildConfiguration `
{
Write-Host "Building solution $solutionFile" | Out-Null
Assert ( Test-Path $solutionFile ) "SolutionFile $SolutionFile is not found"
Try
{
Exec { msbuild $SolutionFile "/t:build" "/p:Configuration=$buildConfiguration" }
}
Catch
{
Write-Error $_;
}
Finally
{
foreach( $file in $OutputFiles ) {
exec { UndoCheckout-File -FilePath $file }
}
}
}