Tooling/Types/Mocked/ActionTypes/CopyFiles.ps1

@{
    Clean = 
    {
        Param([ConfigAutomationContext] $context,[UIAction] $action)
        return $true
    };
    Action = 
    {
        Param([ConfigAutomationContext] $context,[UIAction] $action)
        $extracted = $action.Parameters().Extract(@("SourceFolder", "Contents", "TargetFolder"))
        
        $SourceFolder = $extracted.SourceFolder
        $TargetFolder = $extracted.TargetFolder
        $Contents     = $extracted.Contents
        $Contents = $Contents -split "`n" | Foreach {$_.Trim()}
        $include      = @($Contents | Where {$_[0] -ne "!"}                                          | Foreach {"'$_'"}) -join " "
        $excludeDir   = @($Contents | Where {$_ -match "^\!(.*[\/\\])\*$"}                             | Foreach {$_ -replace "^\!(.*[\/\\])\*$",'''$1'''}) -join " "
        $excludeFiles = @($Contents | Where {$_[0] -eq "!" -and (-not ($_ -match "^\!.*[\/\\]\*$"))} | Foreach {$_ -replace "^\!(.*)$)",'''$1'''}) -join " "
        $robocopy = "robocopy '$($SourceFolder)' '$($TargetFolder)' $include /xf $excludeFiles /xd $excludeDir /s"
        $context.Display($robocopy)
        Invoke-Expression $robocopy
        
        return $true
    };
    CanExecute = 
    {
        Param([ConfigAutomationContext] $context,[UIAction] $action)
        return $true
    };
    Validate = 
    {
        Param([ConfigAutomationContext] $context,[UIAction] $action)

        if(-not $action.Parameters().Validate(@("SourceFolder", "Contents", "TargetFolder"))){
            return $false
        }
        $extracted = $action.Parameters().Extract(@("SourceFolder", "Contents", "TargetFolder"))
        
        if(-not (Test-Path $($extracted.SourceFolder))){
            $context.Error("SourceFolder '{white}$($extracted.SourceFolder){gray}' does not exists")
            return $false
        }
        
        return $true
    };
    
}