#params
$LocalWorkspace = "C:\Projekty\Fideuram\root"

$AppConfigTest = $LocalWorkspace + "\ContrattoSEI\GestoreTrimestrale.Test\app.config"
$AppConfigApp = $LocalWorkspace + "\ContrattoSEI\GestoreTrimestrale\app.config"
$Solution = $LocalWorkspace + "\ContrattoSEI\GestoreTrimestrale\GestoreTrimestrale.sln"
$TestDll = $LocalWorkspace + "\ContrattoSEI\GestoreTrimestrale.Test\bin\Debug\GestoreTrimestrale.Test.dll"
$ProjectExe = $LocalWorkspace + "\ContrattoSEI\GestoreTrimestrale\bin\Debug\GestoreTrimestrale.exe"

#setting up VS environment for msbuild
pushd 'C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC'
cmd /c "vcvarsall.bat x64&set" |
foreach {
  if ($_ -match "=") {
    $v = $_.split("="); set-item -force -path "ENV:\$($v[0])"  -value "$($v[1])"
  }
}
popd

#setting up plugin for tfs for Update-TfsWorkspace
Add-PSSnapin Microsoft.TeamFoundation.PowerShell

#updating sourcefrom tfs
Update-TfsWorkspace -Force -Recurse $LocalWorkspace 

$AppConfigTest = $LocalWorkspace + "\ContrattoSEI\GestoreTrimestrale.Test\app.config"
$AppConfigApp = "C:\ContrattoSEI\GestoreTrimestrale\appconfig.xml"
(Get-Content $AppConfigApp).replace('c:\', 'e:\') | Set-Content $AppConfigApp
#updating app.configs to use test server local paths
(Get-Content $AppConfigTest).replace('c:\', 'c:\') | Set-Content $AppConfigTest
(Get-Content $AppConfigApp).replace('c:\', 'c:\') | Set-Content $AppConfigApp

#build solution
msbuild $Solution

#run tests
vstest.console.exe $TestDll /Tests:FlushFilesWaitingForZIP
vstest.console.exe $TestDll /Tests:SetUpZipCases_DoNotUse
vstest.console.exe $TestDll /Tests:ProcessCanComplete
vstest.console.exe $TestDll /Tests:ZipIntegrity
vstest.console.exe $TestDll /Tests:NumberOfPackedPDFIsOK

#run application
Invoke-Expression $ProjectExe