I\'m using VSTS as a build server, and while building I want to copy the bin folder contents to the root of the target, and also custom files from another folder to this tar
For those who would like to have a PowerShell script to use in your build server, here is a working (at least, on my build server ;)) sample:
param
(
[string] $buildConfiguration = "Debug",
[string] $outputFolder = $PSScriptRoot + "\[BuildOutput]\"
)
Write-Output "Copying all build output to folder '$outputFolder'..."
$includeWildcards = @("*.dll","*.exe","*.pdb","*.sql")
$excludeWildcards = @("*.vshost.*")
# create target folder if not existing, or, delete all files if existing
if(-not (Test-Path -LiteralPath $outputFolder)) {
New-Item -ItemType Directory -Force -Path $outputFolder | Out-Null
# exit if target folder (still) does not exist
if(-not (Test-Path -LiteralPath $outputFolder)) {
Write-Error "Output folder '$outputFolder' could not be created."
Exit 1
}
} else {
Get-ChildItem -LiteralPath $outputFolder -Include * -Recurse -File | foreach {
$_.Delete()
}
Get-ChildItem -LiteralPath $outputFolder -Include * -Recurse -Directory | foreach {
$_.Delete()
}
}
# find all output files (only when in their own project directory)
$files = @(Get-ChildItem ".\" -Include $includeWildcards -Recurse -File |
Where-Object {(
$_.DirectoryName -inotmatch '\\obj\\' -and
$_.DirectoryName -inotmatch '\\*Test*\\' -and
$_.DirectoryName -ilike "*\" + $_.BaseName + "\*" -and
$_.DirectoryName -ilike "*\" + $buildConfiguration
)}
)
# copy output files (overwrite if destination already exists)
foreach ($file in $files) {
Write-Output ("Copying: " + $file.FullName)
Copy-Item $file.FullName $outputFolder -Force
# copy all dependencies from folder (also in subfolders) to output folder as well (if not existing already)
$dependencies = Get-ChildItem $file.DirectoryName -Include $includeWildcards -Exclude $excludeWildcards -Recurse -File
foreach ($dependency in $dependencies) {
$dependencyRelativePathAndFilename = $dependency.FullName.Replace($file.DirectoryName, "")
$destinationFileName = Join-Path -Path $outputFolder -ChildPath $dependencyRelativePathAndFilename
if (-not(Test-Path -LiteralPath $destinationFileName)) {
Write-Output ("Copying: " + $dependencyRelativePathAndFilename + " => " + $destinationFileName)
# create sub directory if not exists
$destinationDirectory = Split-Path $destinationFileName -Parent
if (-not(Test-Path -LiteralPath $destinationDirectory)) {
New-Item -Type Directory $destinationDirectory
}
Copy-Item $dependency.FullName $destinationDirectory
} else {
Write-Debug ("Ignoring (existing destination): " + $dependency.FullName)
}
}
}
Here is the script being used in a PowerShell build step: