tasks/Clean.ModuleBuilder.build.ps1

param (
    # Base directory of all output (default to 'output')
    [Parameter()]
    [string]
    $OutputDirectory = (property OutputDirectory (Join-Path $BuildRoot 'output')),

    [Parameter()]
    [string]
    $RequiredModulesDirectory = (property RequiredModulesDirectory $(Join-Path $OutputDirectory 'RequiredModules'))
)

# Removes the BuildOutput\modules (errors if Pester is loaded)
task CleanAll Clean, CleanModule

# Synopsis: Deleting the content of the Build Output folder, except ./modules
task Clean {

    $OutputDirectory =  Get-SamplerAbsolutePath -Path $OutputDirectory -RelativeTo $BuildRoot
    $FolderToExclude = Split-Path -leaf $RequiredModulesDirectory

    Write-Build -Color Green "Removing $OutputDirectory\* excluding $FolderToExclude"
    Get-ChildItem -Path $OutputDirectory -Exclude $FolderToExclude | Remove-Item -Force -Recurse
}

# Synopsis: Removes the Modules from BuildOutput\Modules folder, might fail if there's an handle on one file.
task CleanModule {
    $OutputDirectory =  Get-SamplerAbsolutePath -Path $OutputDirectory -RelativeTo $BuildRoot

    Write-Build -Color Green "Removing $OutputDirectory\*"
    Get-ChildItem $OutputDirectory | Remove-Item -Force -Recurse -ErrorAction Stop
}