install-hub.ps1


<#PSScriptInfo
 
.VERSION 1.0
 
.GUID 30bc180b-f0f0-4959-a7a3-077d1dc91e0e
 
.AUTHOR Mark Evans
 
.COMPANYNAME
 
.COPYRIGHT
 
.TAGS
 
.LICENSEURI
 
.PROJECTURI
 
.ICONURI
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
 
 
.PRIVATEDATA
 
#>


<#
 
.DESCRIPTION
 Install Hub
 
#>
 
Param()

function DownloadandInstall () {
    Write-Host "`nDownloading latest stable hub..." -ForegroundColor Yellow
    Remove-Item -Force $env:TEMP\hub-stable.zip -ErrorAction SilentlyContinue
    Invoke-WebRequest -Uri $dlurl -OutFile $env:TEMP\hub-stable.zip

    Write-Host "`nInstalling hub..." -ForegroundColor Yellow
    Expand-Archive -Path $env:TEMP\hub-stable.zip -DestinationPath (Split-Path(Split-Path $hubExePath -Parent) -Parent) -Force
    If (Test-Path -Path "C:\Program Files\Git\bin\hub.exe"){
        Remove-Item -Path "C:\Program Files\Git\bin\hub.exe" -Force
    }
    New-Item -ItemType SymbolicLink -Path "C:\Program Files\Git\bin\hub.exe" -Target $hubExePath
}

if (!($IsLinux -or $IsOSX))
{

    $hubExePath = Join-Path $env:ProgramFiles "Hub\bin\hub.exe"
# "C:\Program Files\Git\bin\hub.exe"

    #Added TLS negotiation Fork jmangan68
    [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12;

    foreach ($asset in (Invoke-RestMethod https://api.github.com/repos/github/hub/releases/latest).assets)
    {
        if ($asset.name -match 'hub-windows-amd64-\d*\.\d*\.\d*\.zip')
        {
            $dlurl = $asset.browser_download_url
            $newver = $asset.name
        }
    }

    try
    {
        $ProgressPreference = 'SilentlyContinue'

        if (!(Test-Path $hubExePath))
        {
            DownloadandInstall
        }
        else
        {
            $updateneeded = $false
            Write-Host "`nhub is already installed. Check if possible update..." -ForegroundColor Yellow
            (hub version)[1] -match "(\d*\.\d*\.\d*)" | Out-Null
            $installedversion = $matches[0].split('.')
            $newver -match "(\d*\.\d*\.\d*)" | Out-Null
            $newversion = $matches[0].split('.')

            if (($newversion[0] -gt $installedversion[0]) -or ($newversion[0] -eq $installedversion[0] -and $newversion[1] -gt $installedversion[1]) -or ($newversion[0] -eq $installedversion[0] -and $newversion[1] -eq $installedversion[1] -and $newversion[2] -gt $installedversion[2]))
            {
                $updateneeded = $true
            }

            if ($updateneeded)
            {

                Write-Host "`nUpdate available. Downloading latest stable git..." -ForegroundColor Yellow
                DownloadandInstall
            }
            else
            {
                Write-Host "`nNo update available. Already running latest version..." -ForegroundColor Yellow
            }

        }
        Write-Host "`nInstallation complete!`n`n" -ForegroundColor Green
    }
    finally
    {
        $ProgressPreference = 'Continue'
    }
}
else
{
    Write-Error "This script is currently only supported on the Windows operating system."
}