From ff50538db85cca027fa1578bf2f08f4c57f75e61 Mon Sep 17 00:00:00 2001 From: Julien Moura Date: Fri, 6 Dec 2024 12:49:12 +0100 Subject: [PATCH] addt(docs): add script todownload latest QDT version with PowerShell --- docs/guides/howto_download_latest_qdt_exe.md | 14 ++++ docs/index.md | 1 + scripts/qdt_dowloader.ps1 | 71 ++++++++++++++++++++ 3 files changed, 86 insertions(+) create mode 100644 docs/guides/howto_download_latest_qdt_exe.md create mode 100644 scripts/qdt_dowloader.ps1 diff --git a/docs/guides/howto_download_latest_qdt_exe.md b/docs/guides/howto_download_latest_qdt_exe.md new file mode 100644 index 00000000..45348b02 --- /dev/null +++ b/docs/guides/howto_download_latest_qdt_exe.md @@ -0,0 +1,14 @@ +# How to dwonload the latest QDT's version + +QDT comes with an upgrade command that check if a new version has been released in comparison with the used on, read the changelog and download the newest version: see [command-line usage](../usage/cli.md#upgrade-\(auto-update,-update\)). + +Sometimes a system script fits better to usage, use-case or IT policy. We give below an example in PowerShell for Windows. + +:::{info} +This script is a sample et might be not adapted to your environment or your IT policy. If you intend to use it in production, take time to review it before. If you improve or fix it, please share it. +::: + +```{eval-rst} +.. literalinclude:: ../../scripts/qdt_dowloader.ps1 + :language: powershell +``` diff --git a/docs/index.md b/docs/index.md index 0a43da81..6c07e231 100644 --- a/docs/index.md +++ b/docs/index.md @@ -138,6 +138,7 @@ caption: Guides glob: maxdepth: 1 --- +guides/howto_download_latest_qdt_exe.md guides/howto_validate_profiles_scenarios guides/howto_behind_proxy guides/howto_use_custom_ssl_certs diff --git a/scripts/qdt_dowloader.ps1 b/scripts/qdt_dowloader.ps1 new file mode 100644 index 00000000..01fa4bcf --- /dev/null +++ b/scripts/qdt_dowloader.ps1 @@ -0,0 +1,71 @@ +<# +.Synopsis + Download the latest version of QDT executable from GitHub Releases. +.DESCRIPTION + This script will: + 1. retrieve latest version of QDT from GitHub Releases API + 2. identify the asset to download (*.exe) + 3. download it as ~/qdt.exe + 4. launch it with --help args to check it runs well +.LICENSE + SPDX-License-Identifier: Apache-2.0 +#> + +# -- VARIABLES + +# source repository +$repository = "qgis-deployment/qgis-deployment-toolbelt-cli" + +# GitHub API URL for the latest release +$apiUrl = "https://api.github.com/repos/$repository/releases/latest" + +# API request headers +$apiHeaders = @{"User-Agent" = "QDT upgrader from $env:computername" } + +# destination path +$destinationFile = "$env:USERPROFILE/qdt.exe" + +# -- MAIN +try { + # Retrieve the latest release data + Write-Host "Retrieving latest QDT release from $apiUrl..." + $releaseData = Invoke-RestMethod -Uri $apiUrl -Headers $apiHeaders + + # Extract the latest tag and the asset download URL + $latestTag = $releaseData.tag_name + $asset = $releaseData.assets | Where-Object { $_.name -like "*.exe" } + + if (-not $asset) { + Write-Error "No executable asset found in the latest release: &apiUrl" + exit 1 + } + + Write-Host "Downloading QDT version $latestTag from $downloadUrl to $destinationFile" + $downloadUrl = $asset.browser_download_url + + # Download the asset + $webClient = New-Object System.Net.WebClient + $webClient.Proxy = [System.Net.WebRequest]::DefaultWebProxy + $webClient.Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials + $webClient.DownloadFile($downloadUrl, $destinationFile) + + # log it + Write-Host "Download successful! File saved as $destinationFile" -ForegroundColor Green +} +catch { + # If there's an error, output details + if ($null -ne $_.Exception.Response) { + $statusCode = $_.Exception.Response.StatusCode + $statusDescription = $_.Exception.Response.StatusDescription + Write-Error "API request failed with status code ${statusCode}: ${statusDescription}" + } + else { + Write-Error "An error occurred: $_" + } +} + +# Run the downloaded executable with --help +Write-Host "Running qdt.exe with --help..." +#Start-Process -FilePath $destinationFile -ArgumentList "--help" -NoNewWindow -Wait +#Start-Process -FilePath $destinationFile -ArgumentList "--help" -Wait -WindowStyle Hidden +Start-Process -FilePath $destinationFile -ArgumentList "--help" -NoNewWindow -Wait