Skip to content

Instantly share code, notes, and snippets.

@Tsugami
Created June 17, 2022 06:39
Show Gist options
  • Save Tsugami/2de3d13bfd9f9ffd6ea5eb26efad41cd to your computer and use it in GitHub Desktop.
Save Tsugami/2de3d13bfd9f9ffd6ea5eb26efad41cd to your computer and use it in GitHub Desktop.
Powershell script to download and unzip a github repository
function Download-File {
param (
[string]$Url,
[string]$File
)
Write-Host "Downloading $Url to $File"
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Invoke-WebRequest -Uri $url -OutFile $File
}
function Remove-Directory {
param (
[string]$Path
)
if ([System.IO.Directory]::Exists($Path)) {
Write-Host "Deleting $Path"
[System.IO.Directory]::Delete($Path, $true)
}
}
function Unzip-File {
param(
[string]$File,
[string]$Destination = (Get-Location).Path
)
$FilePath = Resolve-Path $File
$DestinationPath = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($Destination)
If ($PSVersionTable.PSVersion.Major -ge 3) {
[System.IO.Compression.ZipFile]::ExtractToDirectory("$FilePath", "$DestinationPath")
}
else {
throw "Unzip-File is not supported on PowerShell versions less than 3.0"
}
}
function Move-DirectoryContent {
param (
[string]$Source,
[string]$Destination
)
$SourcePath = Resolve-Path $Source
$DestinationPath = Resolve-Path $Destination
Write-Output "Moving content of $SourcePath to $DestinationPath"
Get-ChildItem -Path $SourcePath -Recurse | Move-Item -Destination $DestinationPath
}
$UserName = "tsugami"
$RepoName = "dotfiles"
$Branch = "master"
$DotfileTmpDir = $env:TEMP
$SourceFile = Join-Path $DotfileTmpDir "dotfiles.zip"
$DestPath = Join-Path $env:userprofile ".dotfiles"
Download-File "https://github.com/$UserName/$RepoName/archive/$Branch.zip" $SourceFile
Remove-Directory $DestPath
Unzip-File $SourceFile $DestPath
$ZipOutputDir = Join-Path $DestPath "$RepoName-$Branch"
Move-DirectoryContent $ZipOutputDir $DestPath
Remove-Directory $ZipOutputDir
@Tsugami
Copy link
Author

Tsugami commented Jun 17, 2022

this script was based on this file

the problem with this script is that it doesn't download git history together, because the repository is not downloaded as HTTP or SSH

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment