install-libsrt.ps1 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. # SRT library download and install for Windows.
  2. # Copyright (c) 2021, Thierry Lelegard
  3. # All rights reserved.
  4. <#
  5. .SYNOPSIS
  6. Download and install the libsrt library for Windows. This script is
  7. provided to automate the build of Windows applications using libsrt.
  8. .PARAMETER Destination
  9. Specify a local directory where the libsrt package will be downloaded.
  10. By default, use "tmp" subdirectory from this script.
  11. .PARAMETER ForceDownload
  12. Force a download even if the package is already downloaded.
  13. .PARAMETER GitHubActions
  14. When used in a GitHub Actions workflow, make sure that the LIBSRT
  15. environment variable is propagated to subsequent jobs.
  16. .PARAMETER NoInstall
  17. Do not install the package. By default, libsrt is installed.
  18. .PARAMETER NoPause
  19. Do not wait for the user to press <enter> at end of execution. By default,
  20. execute a "pause" instruction at the end of execution, which is useful
  21. when the script was run from Windows Explorer.
  22. #>
  23. [CmdletBinding(SupportsShouldProcess=$true)]
  24. param(
  25. [string]$Destination = "",
  26. [switch]$ForceDownload = $false,
  27. [switch]$GitHubActions = $false,
  28. [switch]$NoInstall = $false,
  29. [switch]$NoPause = $false
  30. )
  31. Write-Output "libsrt download and installation procedure"
  32. # Default directory for downloaded products.
  33. if (-not $Destination) {
  34. $Destination = "$PSScriptRoot\tmp"
  35. }
  36. # A function to exit this script.
  37. function Exit-Script([string]$Message = "")
  38. {
  39. $Code = 0
  40. if ($Message -ne "") {
  41. Write-Output "ERROR: $Message"
  42. $Code = 1
  43. }
  44. if (-not $NoPause) {
  45. pause
  46. }
  47. exit $Code
  48. }
  49. # Without this, Invoke-WebRequest is awfully slow.
  50. $ProgressPreference = 'SilentlyContinue'
  51. # Get the URL of the latest libsrt installer.
  52. $URL = (Invoke-RestMethod "https://api.github.com/repos/Haivision/srt/releases?per_page=20" |
  53. ForEach-Object { $_.assets } |
  54. ForEach-Object { $_.browser_download_url } |
  55. Select-String @("/libsrt-.*\.exe$", "/libsrt-.*-win-installer\.zip$") |
  56. Select-Object -First 1)
  57. if (-not $URL) {
  58. Exit-Script "Could not find a libsrt installer on GitHub"
  59. }
  60. if (-not ($URL -match "\.zip$") -and -not ($URL -match "\.exe$")) {
  61. Exit-Script "Unexpected URL, not .exe, not .zip: $URL"
  62. }
  63. # Installer name and path.
  64. $InstName = (Split-Path -Leaf $URL)
  65. $InstPath = "$Destination\$InstName"
  66. # Create the directory for downloaded products when necessary.
  67. [void](New-Item -Path $Destination -ItemType Directory -Force)
  68. # Download installer
  69. if (-not $ForceDownload -and (Test-Path $InstPath)) {
  70. Write-Output "$InstName already downloaded, use -ForceDownload to download again"
  71. }
  72. else {
  73. Write-Output "Downloading $URL ..."
  74. Invoke-WebRequest $URL.ToString() -UseBasicParsing -UserAgent Download -OutFile $InstPath
  75. if (-not (Test-Path $InstPath)) {
  76. Exit-Script "$URL download failed"
  77. }
  78. }
  79. # If installer is an archive, expect an exe with same name inside.
  80. if ($InstName -match "\.zip$") {
  81. # Expected installer name in archive.
  82. $ZipName = $InstName
  83. $ZipPath = $InstPath
  84. $InstName = $ZipName -replace '-win-installer.zip','.exe'
  85. $InstPath = "$Destination\$InstName"
  86. # Extract the installer.
  87. Remove-Item -Force $InstPath -ErrorAction SilentlyContinue
  88. Write-Output "Expanding $ZipName ..."
  89. Expand-Archive $ZipPath -DestinationPath $Destination
  90. if (-not (Test-Path $InstPath)) {
  91. Exit-Script "$InstName not found in $ZipName"
  92. }
  93. }
  94. # Install libsrt
  95. if (-not $NoInstall) {
  96. Write-Output "Installing $InstName"
  97. Start-Process -FilePath $InstPath -ArgumentList @("/S") -Wait
  98. }
  99. # Propagate LIBSRT in next jobs for GitHub Actions.
  100. if ($GitHubActions -and (-not -not $env:GITHUB_ENV) -and (Test-Path $env:GITHUB_ENV)) {
  101. $libsrt = [System.Environment]::GetEnvironmentVariable("LIBSRT","Machine")
  102. Write-Output "LIBSRT=$libsrt" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
  103. }
  104. Exit-Script