Adito.psm1

Function Install-AditoDesigner {
    Param (
        [String] $Version = '2024.1.3'
    )
    $PathTemp = [System.IO.Path]::GetTempPath()
    $VersionYear = ($Version -Split '\.')[0]
    $PathDestination = [Environment]::GetFolderPath('ProgramFiles') + '\ADITO ' + $Version
    $UriBase = "https://releases.adito.cloud/${VersionYear}/${Version}"
    $Files = @(
        @{
            Uri     = "${Uribase}/ADITO_${Version}_windows-x64.exe"
            OutFile = "$PathTemp\ADITO.exe"
        },
        @{
            Uri     = "${Uribase}/ADITODesigner_${Version}.zip"
            OutFile = "$PathTemp\ADITO.zip"
        }
    )
    $Jobs = @()
    Foreach ($File in $Files) {
        $Jobs += Start-ThreadJob -Name $File.OutFile -InputObject $File -ScriptBlock {
            Try {
                $Params = $Using:File
                Invoke-WebRequest @Params
            }
            Catch {
                If ($Input.Uri -like '*.exe') {
                    Write-Error -Message "The version was not found." -Category InvalidArgument
                }
            }
        }
    }
    Wait-Job -Job $Jobs | Out-Null
    Foreach ($Job in $Jobs) {
        Receive-Job -Job $Job
    }
    If (Test-Path -Path "$PathTemp\ADITO.exe") {
        Start-Process -Wait -FilePath "$PathTemp\ADITO.exe" -ArgumentList "-q -dir `"$PathDestination`""
        Start-Process -Wait Powershell -Verb RunAs -ArgumentList "-Command `"Expand-Archive -Force -Path '$PathTemp\ADITO.zip' -DestinationPath '$PathDestination'`""
        $Shell = New-Object -ComObject WScript.Shell
        $Shortcut = $Shell.CreateShortcut([Environment]::GetFolderPath('StartMenu') + "\Programs\ADITO Designer $Version.lnk")
        $Shortcut.TargetPath = "$PathDestination\bin\ADITOdesigner64.exe"
        $Shortcut.Save()
        Remove-Item -Path "$PathTemp/ADITO.*"
        Remove-Item -Force -Recurse -Path ([Environment]::GetFolderPath('StartMenu') + '\Programs\ADITO 2023')
    }
}