Public/Tasks/Invoke-DownloadAndUnpackTask.ps1

#Requires -Modules WebAdministration, SitecoreInstallFramework, SitecoreFundamentals, PKI

Set-StrictMode -Version Latest

Write-Verbose "Loading $($MyInvocation.MyCommand.Path)"

function Invoke-DownloadAndUnpackTask {
    [CmdletBinding(SupportsShouldProcess = $true)]
    Param(
        [Parameter(Mandatory = $true)]
        [string]$sourceUri,
        [Parameter(Mandatory = $true)]
        [string]$destinationPath
    )

    Set-PSDebug -Strict

    $downloadFileName = Split-Path $sourceUri -Leaf
    $downloadFileBase = [System.IO.Path]::GetFileNameWithoutExtension($downloadFileName)
    $downloadFullPath = Join-Path $env:TEMP $downloadFileName
    $downloadFullPathExists = Test-Path -Path $downloadFullPath
    $unpackedPath = Join-Path $destinationPath $downloadFileBase
    $unpackedPathExists = Test-Path -Path $unpackedPath -PathType Container
    $destinationPathExists = Test-Path -Path $destinationPath -PathType Container

    if ($PSCmdlet.ShouldProcess($sourceUri)) {

        if ($unpackedPathExists) {
            # let's check to see if there already is a directory with the same name as the downloaded file (- the extension)
            Write-Verbose "Unpack path $unpackedPath already exists."
        }
        else {
            if ($downloadFullPathExists) {
                Remove-Item $downloadFullPath -Force -Confirm:$false -ErrorAction SilentlyContinue
            }
            Invoke-EnsurePathTask -Exists $destinationPath
            #Start-BitsTransfer -Source $sourceUri -Destination $downloadFullPath
            Invoke-DownloadFileTask -SourceUri $sourceUri -DestinationPath $downloadFullPath
            # if ($destinationPathExists) {
            # Write-Verbose "$destinationPath already exists."
            # }
            # else {
            # Invoke-EnsurePathTask -Exists $destinationPath
            # Invoke-DownloadFileTask -SourceUri $sourceUri -DestinationPath $downloadFullPath
            # }

            Invoke-UnpackTask -Source $downloadFullPath -Destination $destinationPath

        }

        Remove-Item -Path $downloadFullPath -Force -Confirm:$false -ErrorAction SilentlyContinue
    }

}

Register-SitecoreInstallExtension -Command Invoke-DownloadAndUnpackTask -As DownloadAndUnpack -Type Task #-Force

Write-Verbose "Loaded $($MyInvocation.MyCommand.Path)"