functions/Compare-StagingConfig.ps1

[cmdletbinding]
function Get-ConfigFile(
[string] $Path = "."
) {
    $configname = "web.config"
    
    if (!(test-path (join-path $path $configname))) {
        $files = gci $Path -Filter "*.exe.config" | ? { $_.Name -notlike "*.vshost.exe.config" }
        if ($files -ne $null) {
            $configname = $files[0].Name
            write-verbose "Chosen config $configname for path $((get-item $path).FullName)"
        }
    }    
    if (!(test-path (Join-Path $path $configname))) {
        write-verbose "config $configname not found for path $((get-item $path).FullName)"
        return $null
    }
    return (get-item (Join-Path $path $configname))
}

function Get-RemoteFile($path, $session) {
    $content = icm -Session $Session { 
            param($Path, $enc) 
            if ($enc -ne $null) { 
                write-host "using encoding: $enc"
                # [Console]::OutputEncoding = [System.Text.Encoding]::GetEncoding($enc)
            }
            write-host "getting '$($path)'"            
            Get-Content ($path) -Encoding UTF8 | out-string 
            write-host "getting '$($path)' DONE"
    } `
      -ArgumentList $Path,$([Console]::OutputEncoding.bodyname)
    return $content
}

[cmdletbinding]
function Compare-StagingConfig(
[string] $Path = ".",
[string] $stagingdir = $null,
[System.Management.Automation.Runspaces.PSSession] $Session = $null
) {
    write-host "comparing staging config at '$path', session $session"
    $projectName = split-path -leaf $Path
    if ($projectName -match [regex]"(.*)-staging") {
        $projectName = $Matches[1]
    }

    if ([string]::IsNullOrEmpty($stagingDir)) {
        $stagingDir = "$Path\..\$projectName-staging"
    }

    if ($Session -ne $null) {

        if ($Path -eq ".") {
            $projectName = icm -Session $Session { param($Path) split-path -leaf $Path } -ArgumentList $Path
            $stagingDir = "$Path\..\$projectName-staging"
        }
        if ($path -eq $null) {
            throw "path cannot be null"
        }
        if ($stagingDir -eq $null) {
            throw "stagingdir cannot be null"
        }

        $configname = icm -Session $Session { 
            param($Path) 
            import-module require
            req beam.serverside -version 0.0.2
            (Get-ConfigFile $Path).Name 
        } -ArgumentList $Path
        if ($configname -eq $null) {
            throw "could not determine config file in path '$path'"
        }
        Write-Host "downloading file $(join-path $path $configname)"
        $currentCfg = Get-RemoteFile -path (join-path $path $configname) -session $session
        Write-Host "downloading file $(join-path $stagingDir $configname)"
        $stagingCfg = Get-RemoteFile -path (join-path $stagingDir $configname) -session $session

        $currentPath =  (join-path $env:TEMP "current.config")
        $stagingPath = (join-path $env:TEMP "staging.config")
        $currentCfg | Out-File $currentPath 
        $stagingCfg | Out-File $stagingPath 

        write-host "running kdiff"
        if ($promptpreference -ne "SilentlyContinue") {
            $exitCode = start-executable kdiff3 @($currentPath, $stagingPath)
        }
    }
    else {        
        $configname = Get-ConfigFile $Path
        if ($configname -ne $null) {
            if ($promptpreference -ne "SilentlyContinue") {
                $exitCode = start-executable kdiff3 @((join-path $path $configname.Name), (join-path $stagingDir $configname.Name))
            }
        }
        else {
            write-host "no config file found in $path"
        }
    }

    
    write-host "cmd exit code=$LASTEXITCODE. result=$exitCode"
}