functions/Copy-FromStaging.ps1


function Copy-FromStaging
{
[cmdletbinding()]
param (
[string] $Path = ".",
$stagingdir = $null,
[Alias("NoConfig", "IgnoreConfig")][switch][bool] $ExcludeConfig = $false,
$Exclude = @("log/", "App_Data/"),
[switch][bool]$ClearDest = $false
) 
    $verbose = ($PSCmdlet.MyInvocation.BoundParameters["Verbose"].IsPresent -eq $true)
    if ([string]::IsNullOrEmpty($Path)) {
    }

    $projectName = split-path -leaf $Path
    if ($stagingDir -eq $null) {
        $stagingDir = "$Path\..\$projectName-staging"
    }

    if ($ExcludeConfig) {
        $f = Get-ConfigFile $stagingDir
        if ($f -ne $null) {
            $exclude += $f.Name
        }
    }

    if (test-path "$stagingDir\web.config") {
        Write-Host "Copy-FromStaging: exclude list=($Exclude). copying $stagingDir -> $path"
        write-host "copying web.config from '$stagingDir\web.config'"
        # start with web.config to force iis app pool recycle
        # Force remove web.config file from prod dir
        if (Test-Path "$path\web.config") {
            $trials = 1,2,3,4,5
            foreach ($try in $trials) {
                Start-Sleep -s 1 -Verbose:$verbose
                Write-Host "This is $try try to remove web.config"
                Remove-Item "$path\web.config" -Force -Verbose:$verbose
                if (-not(Test-Path "$path\web.config")) {
                    write-host "File '$path\web.config' was removed"
                    break
                }
            }
        }
        copy-item "$stagingDir\web.config" $path -Exclude $exclude -Force -Verbose:$verbose
        # give IIS some time
        start-sleep -Seconds 1
    } else {
        write-host "web.config at '$stagingDir\web.config' not found"
    }
    Copy-ItemFiltered -SourceDir $stagingDir -TargetDir $path -Excludes $exclude -Force -Verbose:$verbose -ClearDest:$ClearDest

    #copy-item $stagingDir\* $path -Force -Recurse -Exclude $exclude -Verbose:$verbose -ErrorAction:Continue
}