Skip to content

Instantly share code, notes, and snippets.

@rschiefer
Last active October 17, 2022 08:21
Show Gist options
  • Save rschiefer/9c788c05ae2257aa11731d46fdf9b224 to your computer and use it in GitHub Desktop.
Save rschiefer/9c788c05ae2257aa11731d46fdf9b224 to your computer and use it in GitHub Desktop.
PowerShell script for pulling code from TFVC and preparing for Github.
Function PrepareSourceForGithub {
Param(
[string]$tfsUrl,
[string]$tfsCollection = 'defaultcollection',
[string]$folderPath,
[string]$newRepoName
)
$sourceZip = "source.zip"
$sourceZipUrl = "http://$tfsUrl/$tfsCollection/_apis/tfvc/items?path=$folderPath&api-version=1.0"
Write-Host "Please provide you TFS credentials:"
$creds = Get-Credential
Write-Host 'Downloading the source. This may take a few minutes ...'
Invoke-WebRequest $sourceZipUrl -Credential $creds -Headers @{'Accept'='application/zip'} -OutFile $sourceZip
$sourcePath = "D:\Github\$newRepoName"
UnzipFile $sourceZip $sourcePath $folderPath
Remove-Item $sourceZip -Force
Get-ChildItem -Path $sourcePath -Recurse -Directory -Filter 'Packages' | Get-ChildItem | Remove-Item -Force -Recurse
Get-ChildItem -Path $sourcePath -Recurse -Directory -Filter 'bin' | Remove-Item -Force -Recurse
Get-ChildItem -Path $sourcePath -Recurse -Directory -Filter 'obj' | Remove-Item -Force -Recurse
Get-ChildItem -Path $sourcePath -Recurse -File -Filter '*.vspscc' | Remove-Item -Force
Get-ChildItem -Path $sourcePath -Recurse -File -Filter '*.csproj' | ForEach-Object { RemoveSccNodesFromProjectFile($_.FullName)}
Get-ChildItem -Path $sourcePath -Recurse -File -Filter '*.sln' | ForEach-Object { RemoveSccFromSolutionFile($_.FullName)}
Invoke-WebRequest 'https://raw.githubusercontent.com/rschiefer/SeleniumInDotNetCore2P2/master/.gitignore' -OutFile "$sourcePath/.gitignore"
Invoke-WebRequest 'https://raw.githubusercontent.com/rschiefer/SeleniumInDotNetCore2P2/master/.gitattributes' -OutFile "$sourcePath/.gitattributes"
}
function RemoveSccNodesFromProjectFile ([string]$projectFile) {
[xml]$xml = Get-Content $projectFile
$xml.SelectNodes("//*[starts-with(name(), 'Scc')]") | ForEach-Object {$_.RemoveAll()}
$xml.Save($projectFile);
}
function RemoveSccFromSolutionFile ([string]$solutionFile) {
$solutionContents = [Io.File]::ReadAllText($solutionFile)
#Get-Content $solutionFile
$solutionContents = $solutionContents -replace 'GlobalSection\(TeamFoundationVersionControl\).*\s(.*Scc.*\s)*\sEndGlobalSection', ''
Set-Content $solutionFile $solutionContents
}
function UnzipFile ([string]$file, [string]$destination, [string]$subFolder)
{
$shell_app=new-object -com shell.application
$filePath = Get-Item $file
$zip_file = $shell_app.namespace("$($filePath.FullName)")
Get-Item $destination | Remove-Item -Recurse -Force
New-Item $destination -ItemType Directory
$destinationPath = Get-Item $destination
$destinationPath
$dest = $shell_app.namespace($destinationPath.FullName)
$dest.Copyhere($zip_file.items())
Get-ChildItem -Path "$($destinationPath.FullName)/$subFolder/*" | Copy-Item -Destination $destinationPath.FullName -Recurse
Get-Item -Path "$($destinationPath.FullName)/$" | Remove-Item -Force -Recurse
}
PrepareSourceForGithub 'tfsServer01p:8080/tfs' 'Acme' '$/Fx/Services/FeatureFlags' 'acme.platform.shared.featureflag'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment