2

在我们的 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 任务吗?

4

1 回答 1

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 }
        }
    }
}
于 2016-12-28T21:21:03.480 回答