2

In our psake script , prior to compile we are running check-out assembly files . when compilation task is successfully done the assemblies will be copied then Check-in task will commit the assemblies.

Problem with this approach is if compilation step is stopped the files will be kept checkout.

If the compile task failed then i would like to run another task to undo checkout the files.

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 }
    }
} 

In compile task ,is there any way that if failed i can run the UndoCheckoutFiles task?

Samselvaprabu
  • 16,830
  • 32
  • 144
  • 230

1 Answers1

1

Key to both of the solutions below is to handle MSBuild exceptions with try/catch/finally.

If you want to keep UndoCheckOutFiles as a separate task, it's possible to run a nested build that reports outer (Compile) failure, but inner (UndoCheckOutFiles) success:

...
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 }
}

For cleaner build output, just move the UndoCheckOutFiles actions into the Compile task:

...
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 }
        }
    }
}
Bryan
  • 17,112
  • 7
  • 57
  • 80