install-hub.ps1


<#PSScriptInfo
 
.VERSION 1.1
 
.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 () {
    $GitExePath = Join-Path $env:ProgramFiles 'git\cmd\git.exe'
    $HubLinkFilePath = Join-Path (Split-Path $GitExePath -Parent) 'hub.exe'
    Write-Information "`nDownloading latest stable hub..."
    Remove-Item -Force $env:TEMP\hub-stable.zip -ErrorAction SilentlyContinue
    Invoke-WebRequest -Uri $dlurl -OutFile $env:TEMP\hub-stable.zip

    Write-Information "`nInstalling hub..."
    Expand-Archive -Path $env:TEMP\hub-stable.zip -DestinationPath (Split-Path(Split-Path $hubExePath -Parent) -Parent) -Force
    If (Test-Path -Path $HubLinkFilePath){
        Remove-Item -Path $HubLinkFilePath -Force
    }
    New-Item -ItemType SymbolicLink -Path $HubLinkFilePath -Target $hubExePath
}

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

    $hubExePath = Join-Path $env:ProgramFiles "Hub\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-Information "`nhub is already installed. Check if possible update..."
            (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-Information "`nUpdate available. Downloading latest stable git..."
                DownloadandInstall
            }
            else
            {
                Write-Information "`nNo update available. Already running latest version..."
            }

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