PowerShell_Scripts/base/Start-GitHubDirDownload.ps1

62 lines
2.2 KiB
PowerShell
Raw Normal View History

[CmdletBinding()]param(
[Parameter(Mandatory)]
[string]$GitHubProject,
[string]$Directory,
[string]$Destination='.',
[switch]$Update,
[string]$BackupRelativePath="<auto>"
)
$local:testReq = $(try {
Invoke-WebRequest "https://github.com/$GitHubProject/tree/HEAD/$Directory" -UseBasicParsing -ErrorAction Stop
} catch {})
if( 200 -ne $testReq.StatusCode ) {
Write-Verbose $($testReq|Format-List|Out-String)
Write-Error "Request failed for $GitHubProject : $Directory"
return
}
$DestFull = Get-Item $(Join-Path $Destination '.')
Write-Verbose "DestFull: $($DestFull | Format-List | Out-String)"
$local:tmpFile = ""
New-TemporaryFile | Select-Object -ExpandProperty FullName -OutVariable tmpFile | Remove-Item -Force
$tmpPath = New-Item -ItemType Directory $tmpFile
Remove-Variable tmpFile
Push-Location $tmpPath
Write-Verbose "tmp $($tmpPath.FullName) created"
if( $BackupRelativePath -eq "<auto>" ) {
$BackupRelativePath = ".overitten.on.#Timestamp#"
}
$BackupRelativePath = $BackupRelativePath -replace "#Timestamp#",$(Get-Date -Format "yyyyMMdd-HHmmss")
Write-Verbose "Fetching $Directory..."
$null = & svn checkout "https://github.com/$GitHubProject/trunk/$Directory"
$local:dirName = Split-Path -Leaf $Directory
Remove-Item -Recurse (Join-Path $dirName '.svn') -Force
Get-ChildItem -File -Force $dirName -Recurse | ForEach-Object {
$local:item = $_.FullName.Replace($tmpPath.FullName,'') -replace '^/',''
$local:dst = Join-Path $DestFull.FullName $item
if( Test-Path $dst ) {
if( -not $Update ) {
Write-Verbose "$dst already exists, skipping..."
return
}
if( -not (Compare-Object (Get-Content $item) (Get-Content $dst) -CaseSensitive) ) {
Write-Verbose "$dst already updated, skipping..."
return
}
$local:bkPath = Join-Path $DestFull.FullName (Split-Path -Parent $item) $BackupRelativePath (Split-Path -leaf $item)
$null = New-Item -ItemType Directory -Force (Split-Path -Parent $bkPath) -ErrorAction Stop
Move-Item -Force $dst $bkPath
}
Write-Verbose "Copy-Item -Force $_ $dst -PassThru"
Copy-Item -Force $_ $dst -PassThru
}
Pop-Location
Remove-Item -Recurse $tmpPath -Force